From 9a018ac359faee6c4c452e91a9939fb8cc8b4053 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Fri, 19 Nov 2021 22:34:00 +0000 Subject: [PATCH] Update uart implementation of embedded-hal 1.x to 1.0.0-alpha.6 --- rp2040-hal/src/uart.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/uart.rs b/rp2040-hal/src/uart.rs index a842321..b6724c3 100644 --- a/rp2040-hal/src/uart.rs +++ b/rp2040-hal/src/uart.rs @@ -82,6 +82,18 @@ pub enum ReadErrorType { Framing, } +#[cfg(feature = "eh1_0_alpha")] +impl eh1_0_alpha::serial::Error for ReadErrorType { + fn kind(&self) -> eh1_0_alpha::serial::ErrorKind { + match self { + ReadErrorType::Overrun => eh1_0_alpha::serial::ErrorKind::Overrun, + ReadErrorType::Break => eh1_0_alpha::serial::ErrorKind::Other, + ReadErrorType::Parity => eh1_0_alpha::serial::ErrorKind::Parity, + ReadErrorType::Framing => eh1_0_alpha::serial::ErrorKind::FrameFormat, + } + } +} + /// State of the UART Peripheral. pub trait State {} @@ -516,6 +528,27 @@ impl eh1::Read for UartPeripheral { } } +/// Same as core::convert::Infallible, but implementing spi::Error +/// +/// For eh 1.0.0-alpha.6, Infallible doesn't implement spi::Error, +/// so use a locally defined type instead. +#[cfg(feature = "eh1_0_alpha")] +pub enum SerialInfallible {} + +#[cfg(feature = "eh1_0_alpha")] +impl core::fmt::Debug for SerialInfallible { + fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match *self {} + } +} + +#[cfg(feature = "eh1_0_alpha")] +impl eh1_0_alpha::serial::Error for SerialInfallible { + fn kind(&self) -> eh1_0_alpha::serial::ErrorKind { + match *self {} + } +} + impl Write for UartPeripheral { type Error = Infallible; @@ -534,7 +567,7 @@ impl Write for UartPeripheral { #[cfg(feature = "eh1_0_alpha")] impl eh1::Write for UartPeripheral { - type Error = Infallible; + type Error = SerialInfallible; fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { if self.write_raw(&[word]).is_err() { @@ -545,7 +578,10 @@ impl eh1::Write for UartPeripheral { } fn flush(&mut self) -> nb::Result<(), Self::Error> { - self.transmit_flushed() + self.transmit_flushed().map_err(|e| match e { + WouldBlock => WouldBlock, + Other(v) => match v {}, + }) } }