From 5aa59e97372e2efed4e35625b754364b1b4839a3 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Wed, 4 Jan 2023 12:57:19 +0100 Subject: [PATCH 1/4] 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", From 4c4b47f78a57e5bf3d05b319759351a005e69206 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Fri, 6 Jan 2023 14:24:29 +0100 Subject: [PATCH 2/4] feat(stm32): Add embedded-io traits for UartRx and UartTx --- embassy-stm32/src/usart/mod.rs | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 233b56ba..1c13d9ec 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -948,6 +948,45 @@ mod eio { self.blocking_flush() } } + + impl Io for UartRx<'_, T, RxDma> + where + T: BasicInstance, + { + type Error = Error; + } + + impl Read for UartRx<'_, T, RxDma> + where + T: BasicInstance, + RxDma: super::RxDma, + { + async fn read(&mut self, buf: &mut [u8]) -> Result { + self.read_until_idle(buf).await + } + } + + impl Io for UartTx<'_, T, TxDma> + where + T: BasicInstance, + { + type Error = Error; + } + + impl Write for UartTx<'_, T, TxDma> + 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( From 816b214403b7ce524cbb6ce85732df70f5b8f19e Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Sat, 14 Jan 2023 07:12:43 +0100 Subject: [PATCH 3/4] Only implement Write --- embassy-stm32/src/usart/mod.rs | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 1c13d9ec..1f50e5dc 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -912,7 +912,7 @@ mod eh1 { #[cfg(all(feature = "unstable-traits", feature = "nightly"))] mod eio { - use embedded_io::asynch::{Read, Write}; + use embedded_io::asynch::Read; use embedded_io::Io; use super::*; @@ -924,16 +924,6 @@ mod eio { 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, @@ -949,23 +939,6 @@ mod eio { } } - impl Io for UartRx<'_, T, RxDma> - where - T: BasicInstance, - { - type Error = Error; - } - - impl Read for UartRx<'_, T, RxDma> - where - T: BasicInstance, - RxDma: super::RxDma, - { - async fn read(&mut self, buf: &mut [u8]) -> Result { - self.read_until_idle(buf).await - } - } - impl Io for UartTx<'_, T, TxDma> where T: BasicInstance, From 16590732f8708f4f55cbb9bc126bf5218b5e3c58 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Sat, 14 Jan 2023 07:13:29 +0100 Subject: [PATCH 4/4] Update mod.rs --- embassy-stm32/src/usart/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 1f50e5dc..d71aa61a 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -912,7 +912,7 @@ mod eh1 { #[cfg(all(feature = "unstable-traits", feature = "nightly"))] mod eio { - use embedded_io::asynch::Read; + use embedded_io::asynch::Write; use embedded_io::Io; use super::*;