From 5aa59e97372e2efed4e35625b754364b1b4839a3 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Wed, 4 Jan 2023 12:57:19 +0100 Subject: [PATCH] feat(stm32): Let uart implement embedded-io Read/Write --- embassy-stm32/src/usart/mod.rs | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 6f8b6a9e..233b56ba 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -910,6 +910,46 @@ mod eh1 { } } +#[cfg(all(feature = "unstable-traits", feature = "nightly"))] +mod eio { + use embedded_io::asynch::{Read, Write}; + use embedded_io::Io; + + use super::*; + + impl Io for Uart<'_, T, TxDma, RxDma> + where + T: BasicInstance, + { + type Error = Error; + } + + impl Read for Uart<'_, T, TxDma, RxDma> + where + T: BasicInstance, + RxDma: super::RxDma, + { + async fn read(&mut self, buf: &mut [u8]) -> Result { + self.read_until_idle(buf).await + } + } + + impl Write for Uart<'_, T, TxDma, RxDma> + where + T: BasicInstance, + TxDma: super::TxDma, + { + async fn write(&mut self, buf: &[u8]) -> Result { + self.write(buf).await?; + Ok(buf.len()) + } + + async fn flush(&mut self) -> Result<(), Self::Error> { + self.blocking_flush() + } + } +} + #[cfg(all( feature = "unstable-traits", feature = "nightly",