From 4032fc06556312eab27488f05efe1803ade47b45 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Wed, 26 Jan 2022 22:39:06 +0100 Subject: Support unstable-trait feature for stm32 --- embassy-stm32/src/adc/f1.rs | 2 +- embassy-stm32/src/adc/v3.rs | 2 +- embassy-stm32/src/exti.rs | 119 +++++++++++++----------- embassy-stm32/src/gpio.rs | 2 +- embassy-stm32/src/i2c/mod.rs | 1 + embassy-stm32/src/i2c/v1.rs | 6 +- embassy-stm32/src/i2c/v2.rs | 144 ++++++++++++++++++---------- embassy-stm32/src/spi/mod.rs | 206 +++++++++++++++++++++++++++++------------ embassy-stm32/src/usart/mod.rs | 142 +++++++++++++++++----------- 9 files changed, 403 insertions(+), 221 deletions(-) (limited to 'embassy-stm32/src') diff --git a/embassy-stm32/src/adc/f1.rs b/embassy-stm32/src/adc/f1.rs index 3ed1701fa..6031883ec 100644 --- a/embassy-stm32/src/adc/f1.rs +++ b/embassy-stm32/src/adc/f1.rs @@ -4,7 +4,7 @@ use crate::time::Hertz; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::unborrow; -use embedded_hal::blocking::delay::DelayUs; +use embedded_hal_02::blocking::delay::DelayUs; pub const VDDA_CALIB_MV: u32 = 3300; pub const ADC_MAX: u32 = (1 << 12) - 1; diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index ddf06deae..6f36daa23 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -2,7 +2,7 @@ use crate::adc::{AdcPin, Instance}; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::unborrow; -use embedded_hal::blocking::delay::DelayUs; +use embedded_hal_02::blocking::delay::DelayUs; pub const VDDA_CALIB_MV: u32 = 3000; diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index af401796c..7d974c2dc 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -1,15 +1,10 @@ -use core::convert::Infallible; use core::future::Future; use core::marker::PhantomData; use core::pin::Pin; use core::task::{Context, Poll}; -use embassy::traits::gpio::{ - WaitForAnyEdge, WaitForFallingEdge, WaitForHigh, WaitForLow, WaitForRisingEdge, -}; use embassy::util::Unborrow; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::unsafe_impl_unborrow; -use embedded_hal::digital::v2::InputPin; use crate::gpio::{AnyPin, Input, Pin as GpioPin}; use crate::interrupt; @@ -134,70 +129,88 @@ impl<'d, T: GpioPin> ExtiInput<'d, T> { } } -impl<'d, T: GpioPin> InputPin for ExtiInput<'d, T> { - type Error = Infallible; +mod eh02 { + use super::*; + use core::convert::Infallible; - fn is_high(&self) -> Result { - Ok(self.is_high()) - } + impl<'d, T: GpioPin> embedded_hal_02::digital::v2::InputPin for ExtiInput<'d, T> { + type Error = Infallible; + + fn is_high(&self) -> Result { + Ok(self.is_high()) + } - fn is_low(&self) -> Result { - Ok(self.is_low()) + fn is_low(&self) -> Result { + Ok(self.is_low()) + } } } -impl<'d, T: GpioPin> WaitForHigh for ExtiInput<'d, T> { - type Future<'a> - where - Self: 'a, - = impl Future + 'a; +#[cfg(feature = "unstable-traits")] +mod eh1 { + use super::*; + use core::convert::Infallible; + use futures::FutureExt; - fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { - self.wait_for_high() + impl<'d, T: GpioPin> embedded_hal_1::digital::ErrorType for ExtiInput<'d, T> { + type Error = Infallible; } -} -impl<'d, T: GpioPin> WaitForLow for ExtiInput<'d, T> { - type Future<'a> - where - Self: 'a, - = impl Future + 'a; + impl<'d, T: GpioPin> embedded_hal_1::digital::blocking::InputPin for ExtiInput<'d, T> { + fn is_high(&self) -> Result { + Ok(self.is_high()) + } - fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { - self.wait_for_low() + fn is_low(&self) -> Result { + Ok(self.is_low()) + } } -} -impl<'d, T: GpioPin> WaitForRisingEdge for ExtiInput<'d, T> { - type Future<'a> - where - Self: 'a, - = impl Future + 'a; + impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for ExtiInput<'d, T> { + type WaitForHighFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a> { - self.wait_for_rising_edge() - } -} + fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHighFuture<'a> { + self.wait_for_high().map(Ok) + } -impl<'d, T: GpioPin> WaitForFallingEdge for ExtiInput<'d, T> { - type Future<'a> - where - Self: 'a, - = impl Future + 'a; + type WaitForLowFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a> { - self.wait_for_falling_edge() - } -} + fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLowFuture<'a> { + self.wait_for_low().map(Ok) + } + + type WaitForRisingEdgeFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a> { + self.wait_for_rising_edge().map(Ok) + } + + type WaitForFallingEdgeFuture<'a> + where + Self: 'a, + = impl Future> + 'a; -impl<'d, T: GpioPin> WaitForAnyEdge for ExtiInput<'d, T> { - type Future<'a> - where - Self: 'a, - = impl Future + 'a; + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a> { + self.wait_for_falling_edge().map(Ok) + } - fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { - self.wait_for_any_edge() + type WaitForAnyEdgeFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a> { + self.wait_for_any_edge().map(Ok) + } } } diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 57b9ba6b7..d4606716c 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -3,7 +3,7 @@ use core::convert::Infallible; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::{unborrow, unsafe_impl_unborrow}; -use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}; +use embedded_hal_02::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}; use crate::pac; use crate::pac::gpio::{self, vals}; diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index 8eee13825..2dcb9b720 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -8,6 +8,7 @@ mod _version; use crate::{dma, peripherals}; pub use _version::*; +#[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { Bus, diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 6b2c8a35c..aa5268987 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs @@ -270,7 +270,7 @@ impl<'d, T: Instance> I2c<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> { type Error = Error; fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { @@ -278,7 +278,7 @@ impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> { type Error = Error; fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { @@ -286,7 +286,7 @@ impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { +impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> { type Error = Error; fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 5b0e5fce2..9c05187a5 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -1,11 +1,9 @@ use core::cmp; -use core::future::Future; use core::marker::PhantomData; use core::task::Poll; use atomic_polyfill::{AtomicUsize, Ordering}; use embassy::interrupt::InterruptExt; -use embassy::traits::i2c::I2c as I2cTrait; use embassy::util::Unborrow; use embassy::waitqueue::AtomicWaker; use embassy_hal_common::drop::OnDrop; @@ -735,32 +733,36 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { } } -impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { - type Error = Error; +mod eh02 { + use super::*; - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - self.blocking_read(address, buffer) + impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> { + type Error = Error; + + fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + self.blocking_read(address, buffer) + } } -} -impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { - type Error = Error; + impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> { + type Error = Error; - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - self.blocking_write(address, bytes) + fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + self.blocking_write(address, bytes) + } } -} -impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { - type Error = Error; + impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> { + type Error = Error; - fn write_read( - &mut self, - address: u8, - bytes: &[u8], - buffer: &mut [u8], - ) -> Result<(), Self::Error> { - self.blocking_write_read(address, bytes, buffer) + fn write_read( + &mut self, + address: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Self::Error> { + self.blocking_write_read(address, bytes, buffer) + } } } @@ -906,38 +908,80 @@ impl Timings { } } -impl<'d, T: Instance, TXDMA: super::TxDma, RXDMA: super::RxDma> I2cTrait - for I2c<'d, T, TXDMA, RXDMA> -{ - type Error = super::Error; - - type WriteFuture<'a> - where - Self: 'a, - = impl Future> + 'a; - type ReadFuture<'a> - where - Self: 'a, - = impl Future> + 'a; - type WriteReadFuture<'a> - where - Self: 'a, - = impl Future> + 'a; - - fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read(address, buffer) +#[cfg(feature = "unstable-traits")] +mod eh1 { + use super::super::{RxDma, TxDma}; + use super::*; + use core::future::Future; + + impl embedded_hal_1::i2c::Error for Error { + fn kind(&self) -> embedded_hal_1::i2c::ErrorKind { + match *self { + Self::Bus => embedded_hal_1::i2c::ErrorKind::Bus, + Self::Arbitration => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss, + Self::Nack => embedded_hal_1::i2c::ErrorKind::NoAcknowledge( + embedded_hal_1::i2c::NoAcknowledgeSource::Unknown, + ), + Self::Timeout => embedded_hal_1::i2c::ErrorKind::Other, + Self::Crc => embedded_hal_1::i2c::ErrorKind::Other, + Self::Overrun => embedded_hal_1::i2c::ErrorKind::Overrun, + Self::ZeroLengthTransfer => embedded_hal_1::i2c::ErrorKind::Other, + } + } } - fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { - self.write(address, bytes) + impl<'d, T: Instance, TXDMA: TxDma, RXDMA: RxDma> embedded_hal_1::i2c::ErrorType + for I2c<'d, T, TXDMA, RXDMA> + { + type Error = Error; } - fn write_read<'a>( - &'a mut self, - address: u8, - bytes: &'a [u8], - buffer: &'a mut [u8], - ) -> Self::WriteReadFuture<'a> { - self.write_read(address, bytes, buffer) + impl<'d, T: Instance, TXDMA: TxDma, RXDMA: RxDma> embedded_hal_async::i2c::I2c + for I2c<'d, T, TXDMA, RXDMA> + { + type ReadFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { + self.read(address, buffer) + } + + type WriteFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { + self.write(address, bytes) + } + + type WriteReadFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + fn write_read<'a>( + &'a mut self, + address: u8, + bytes: &'a [u8], + buffer: &'a mut [u8], + ) -> Self::WriteReadFuture<'a> { + self.write_read(address, bytes, buffer) + } + + type TransactionFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn transaction<'a>( + &'a mut self, + address: u8, + operations: &mut [embedded_hal_async::i2c::Operation<'a>], + ) -> Self::TransactionFuture<'a> { + let _ = address; + let _ = operations; + async move { todo!() } + } } } diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index f1ea8592d..4cf45f6f9 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -1,11 +1,9 @@ #![macro_use] -use core::future::Future; use core::marker::PhantomData; use core::ptr; use embassy::util::Unborrow; use embassy_hal_common::unborrow; -use embassy_traits::spi as traits; use self::sealed::WordSize; use crate::dma; @@ -17,7 +15,7 @@ use crate::peripherals; use crate::rcc::RccPeripheral; use crate::time::Hertz; -pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; +pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; #[cfg_attr(spi_v1, path = "v1.rs")] #[cfg_attr(spi_f1, path = "v1.rs")] @@ -549,76 +547,168 @@ fn transfer_word(regs: Regs, tx_word: W) -> Result { return Ok(rx_word); } -// Note: It is not possible to impl these traits generically in embedded-hal 0.2 due to a conflict with -// some marker traits. For details, see https://github.com/rust-embedded/embedded-hal/pull/289 -macro_rules! impl_blocking { - ($w:ident) => { - impl<'d, T: Instance> embedded_hal::blocking::spi::Write<$w> for Spi<'d, T, NoDma, NoDma> { - type Error = Error; +mod eh02 { + use super::*; - fn write(&mut self, words: &[$w]) -> Result<(), Self::Error> { - self.blocking_write(words) + // Note: It is not possible to impl these traits generically in embedded-hal 0.2 due to a conflict with + // some marker traits. For details, see https://github.com/rust-embedded/embedded-hal/pull/289 + macro_rules! impl_blocking { + ($w:ident) => { + impl<'d, T: Instance> embedded_hal_02::blocking::spi::Write<$w> + for Spi<'d, T, NoDma, NoDma> + { + type Error = Error; + + fn write(&mut self, words: &[$w]) -> Result<(), Self::Error> { + self.blocking_write(words) + } } - } - impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<$w> - for Spi<'d, T, NoDma, NoDma> - { - type Error = Error; + impl<'d, T: Instance> embedded_hal_02::blocking::spi::Transfer<$w> + for Spi<'d, T, NoDma, NoDma> + { + type Error = Error; - fn transfer<'w>(&mut self, words: &'w mut [$w]) -> Result<&'w [$w], Self::Error> { - self.blocking_transfer_in_place(words)?; - Ok(words) + fn transfer<'w>(&mut self, words: &'w mut [$w]) -> Result<&'w [$w], Self::Error> { + self.blocking_transfer_in_place(words)?; + Ok(words) + } } - } - }; + }; + } + + impl_blocking!(u8); + impl_blocking!(u16); } -impl_blocking!(u8); -impl_blocking!(u16); +#[cfg(feature = "unstable-traits")] +mod eh1 { + use super::*; + use core::future::Future; -impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { - type Error = Error; -} + impl<'d, T: Instance, Tx, Rx> embedded_hal_1::spi::ErrorType for Spi<'d, T, Tx, Rx> { + type Error = Error; + } -impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { - type WriteFuture<'a> - where - Self: 'a, - = impl Future> + 'a; + impl embedded_hal_1::spi::Error for Error { + fn kind(&self) -> embedded_hal_1::spi::ErrorKind { + match *self { + Self::Framing => embedded_hal_1::spi::ErrorKind::FrameFormat, + Self::Crc => embedded_hal_1::spi::ErrorKind::Other, + Self::ModeFault => embedded_hal_1::spi::ErrorKind::ModeFault, + Self::Overrun => embedded_hal_1::spi::ErrorKind::Overrun, + } + } + } + + impl<'d, T: Instance, Tx: TxDmaChannel, Rx> embedded_hal_async::spi::Write + for Spi<'d, T, Tx, Rx> + { + type WriteFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - self.write(data) + fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { + self.write(data) + } + + type WriteTransactionFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn write_transaction<'a>( + &'a mut self, + words: &'a [&'a [u8]], + ) -> Self::WriteTransactionFuture<'a> { + async move { + for buf in words { + self.write(buf).await? + } + Ok(()) + } + } } -} -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read - for Spi<'d, T, Tx, Rx> -{ - type ReadFuture<'a> - where - Self: 'a, - = impl Future> + 'a; + impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> + embedded_hal_async::spi::Read for Spi<'d, T, Tx, Rx> + { + type ReadFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read(data) + fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { + self.read(data) + } + + type ReadTransactionFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn read_transaction<'a>( + &'a mut self, + words: &'a mut [&'a mut [u8]], + ) -> Self::ReadTransactionFuture<'a> { + async move { + for buf in words { + self.read(buf).await? + } + Ok(()) + } + } } -} -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex - for Spi<'d, T, Tx, Rx> -{ - type WriteReadFuture<'a> - where - Self: 'a, - = impl Future> + 'a; - - fn read_write<'a>( - &'a mut self, - read: &'a mut [u8], - write: &'a [u8], - ) -> Self::WriteReadFuture<'a> { - self.transfer(read, write) + impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> + embedded_hal_async::spi::ReadWrite for Spi<'d, T, Tx, Rx> + { + type TransferFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn transfer<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::TransferFuture<'a> { + self.transfer(rx, tx) + } + + type TransferInPlaceFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn transfer_in_place<'a>( + &'a mut self, + words: &'a mut [u8], + ) -> Self::TransferInPlaceFuture<'a> { + // TODO: Implement async version + let result = self.blocking_transfer_in_place(words); + async move { result } + } + + type TransactionFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn transaction<'a>( + &'a mut self, + operations: &'a mut [embedded_hal_async::spi::Operation<'a, u8>], + ) -> Self::TransactionFuture<'a> { + use embedded_hal_1::spi::blocking::Operation; + async move { + for o in operations { + match o { + Operation::Read(b) => self.read(b).await?, + Operation::Write(b) => self.write(b).await?, + Operation::Transfer(r, w) => self.transfer(r, w).await?, + Operation::TransferInPlace(b) => self.transfer_in_place(b).await?, + } + } + Ok(()) + } + } } } diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 5309c6296..a391379c8 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -1,11 +1,9 @@ #![macro_use] -use core::future::Future; use core::marker::PhantomData; use embassy::interrupt::Interrupt; use embassy::util::Unborrow; use embassy_hal_common::unborrow; -use futures::TryFutureExt; use crate::dma::NoDma; use crate::gpio::sealed::AFType::{OutputOpenDrain, OutputPushPull}; @@ -211,72 +209,108 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { } } -impl<'d, T: Instance, TxDma, RxDma> embedded_hal::serial::Read for Uart<'d, T, TxDma, RxDma> { - type Error = Error; - fn read(&mut self) -> Result> { - let r = self.inner.regs(); - unsafe { - let sr = sr(r).read(); - if sr.pe() { - rdr(r).read_volatile(); - Err(nb::Error::Other(Error::Parity)) - } else if sr.fe() { - rdr(r).read_volatile(); - Err(nb::Error::Other(Error::Framing)) - } else if sr.ne() { - rdr(r).read_volatile(); - Err(nb::Error::Other(Error::Noise)) - } else if sr.ore() { - rdr(r).read_volatile(); - Err(nb::Error::Other(Error::Overrun)) - } else if sr.rxne() { - Ok(rdr(r).read_volatile()) - } else { - Err(nb::Error::WouldBlock) +mod eh02 { + use super::*; + + impl<'d, T: Instance, TxDma, RxDma> embedded_hal_02::serial::Read + for Uart<'d, T, TxDma, RxDma> + { + type Error = Error; + fn read(&mut self) -> Result> { + let r = self.inner.regs(); + unsafe { + let sr = sr(r).read(); + if sr.pe() { + rdr(r).read_volatile(); + Err(nb::Error::Other(Error::Parity)) + } else if sr.fe() { + rdr(r).read_volatile(); + Err(nb::Error::Other(Error::Framing)) + } else if sr.ne() { + rdr(r).read_volatile(); + Err(nb::Error::Other(Error::Noise)) + } else if sr.ore() { + rdr(r).read_volatile(); + Err(nb::Error::Other(Error::Overrun)) + } else if sr.rxne() { + Ok(rdr(r).read_volatile()) + } else { + Err(nb::Error::WouldBlock) + } } } } + + impl<'d, T: Instance, TxDma, RxDma> embedded_hal_02::blocking::serial::Write + for Uart<'d, T, TxDma, RxDma> + { + type Error = Error; + fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { + self.blocking_write(buffer) + } + fn bflush(&mut self) -> Result<(), Self::Error> { + self.blocking_flush() + } + } } -impl<'d, T: Instance, TxDma, RxDma> embedded_hal::blocking::serial::Write - for Uart<'d, T, TxDma, RxDma> -{ - type Error = Error; - fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - self.blocking_write(buffer) +#[cfg(feature = "unstable-traits")] +mod eh1 { + use super::*; + use core::future::Future; + + impl embedded_hal_1::serial::Error for Error { + fn kind(&self) -> embedded_hal_1::serial::ErrorKind { + match *self { + Self::Framing => embedded_hal_1::serial::ErrorKind::FrameFormat, + Self::Noise => embedded_hal_1::serial::ErrorKind::Noise, + Self::Overrun => embedded_hal_1::serial::ErrorKind::Overrun, + Self::Parity => embedded_hal_1::serial::ErrorKind::Parity, + } + } } - fn bflush(&mut self) -> Result<(), Self::Error> { - self.blocking_flush() + + impl<'d, T: Instance, TxDma, RxDma> embedded_hal_1::serial::ErrorType + for Uart<'d, T, TxDma, RxDma> + { + type Error = Error; } -} -impl<'d, T: Instance, TxDma, RxDma> embassy_traits::uart::Write for Uart<'d, T, TxDma, RxDma> -where - TxDma: crate::usart::TxDma, -{ - type WriteFuture<'a> + impl<'d, T: Instance, TxDma, RxDma> embedded_hal_async::serial::Write for Uart<'d, T, TxDma, RxDma> where - Self: 'a, - = impl Future> + 'a; + TxDma: crate::usart::TxDma, + { + type WriteFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { - self.write(buf) - .map_err(|_| embassy_traits::uart::Error::Other) + fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { + self.write(buf) + } + + type FlushFuture<'a> + where + Self: 'a, + = impl Future> + 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + async move { Ok(()) } + } } -} -impl<'d, T: Instance, TxDma, RxDma> embassy_traits::uart::Read for Uart<'d, T, TxDma, RxDma> -where - RxDma: crate::usart::RxDma, -{ - type ReadFuture<'a> + impl<'d, T: Instance, TxDma, RxDma> embedded_hal_async::serial::Read for Uart<'d, T, TxDma, RxDma> where - Self: 'a, - = impl Future> + 'a; + RxDma: crate::usart::RxDma, + { + type ReadFuture<'a> + where + Self: 'a, + = impl Future> + 'a; - fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read(buf) - .map_err(|_| embassy_traits::uart::Error::Other) + fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { + self.read(buf) + } } } -- cgit