From 49847ff4320daaae16a7e46c43bfb70f9ea3e3d6 Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 14 Sep 2023 10:09:09 +0200 Subject: [PATCH] Implement blocking embedded-io::Write for Uart & UartTx --- embassy-stm32/src/usart/buffered.rs | 6 --- embassy-stm32/src/usart/mod.rs | 70 +++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index 989c8820..b88eebc7 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs @@ -410,12 +410,6 @@ impl<'d, T: BasicInstance> Drop for BufferedUartTx<'d, T> { } } -impl embedded_io_async::Error for Error { - fn kind(&self) -> embedded_io_async::ErrorKind { - embedded_io_async::ErrorKind::Other - } -} - impl<'d, T: BasicInstance> embedded_io_async::ErrorType for BufferedUart<'d, T> { type Error = Error; } diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 2d744322..c6d6cc59 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -1037,20 +1037,61 @@ mod eh1 { } } -#[cfg(all(feature = "unstable-traits", feature = "nightly"))] -mod eio { - use embedded_io_async::{ErrorType, Write}; +impl embedded_io::Error for Error { + fn kind(&self) -> embedded_io::ErrorKind { + embedded_io::ErrorKind::Other + } +} - use super::*; +impl embedded_io::ErrorType for Uart<'_, T, TxDma, RxDma> +where + T: BasicInstance, +{ + type Error = Error; +} - impl ErrorType for Uart<'_, T, TxDma, RxDma> - where - T: BasicInstance, - { - type Error = Error; +impl embedded_io::ErrorType for UartTx<'_, T, TxDma> +where + T: BasicInstance, +{ + type Error = Error; +} + +impl embedded_io::Write for Uart<'_, T, TxDma, RxDma> +where + T: BasicInstance, + TxDma: crate::usart::TxDma, +{ + fn write(&mut self, buf: &[u8]) -> Result { + self.blocking_write(buf)?; + Ok(buf.len()) } - impl Write for Uart<'_, T, TxDma, RxDma> + fn flush(&mut self) -> Result<(), Self::Error> { + self.blocking_flush() + } +} + +impl embedded_io::Write for UartTx<'_, T, TxDma> +where + T: BasicInstance, + TxDma: crate::usart::TxDma, +{ + fn write(&mut self, buf: &[u8]) -> Result { + self.blocking_write(buf)?; + Ok(buf.len()) + } + + fn flush(&mut self) -> Result<(), Self::Error> { + self.blocking_flush() + } +} + +#[cfg(all(feature = "unstable-traits", feature = "nightly"))] +mod eio { + use super::*; + + impl embedded_io_async::Write for Uart<'_, T, TxDma, RxDma> where T: BasicInstance, TxDma: super::TxDma, @@ -1065,14 +1106,7 @@ mod eio { } } - impl ErrorType for UartTx<'_, T, TxDma> - where - T: BasicInstance, - { - type Error = Error; - } - - impl Write for UartTx<'_, T, TxDma> + impl embedded_io_async::Write for UartTx<'_, T, TxDma> where T: BasicInstance, TxDma: super::TxDma,