From 3fef96222043359d5320b44715b09bde1b85cd6b Mon Sep 17 00:00:00 2001 From: Sympatron GmbH <35803463+Sympatron@users.noreply.github.com> Date: Wed, 13 Dec 2023 22:15:40 +0100 Subject: [PATCH 1/2] Implement embedded-io Read + Write for UartPeripheral --- rp2040-hal/Cargo.toml | 1 + rp2040-hal/src/uart/peripheral.rs | 37 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/rp2040-hal/Cargo.toml b/rp2040-hal/Cargo.toml index 293120799..38f20062b 100644 --- a/rp2040-hal/Cargo.toml +++ b/rp2040-hal/Cargo.toml @@ -21,6 +21,7 @@ embedded-hal = { version = "0.2.5", features = ["unproven"] } eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true } eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.1", optional = true } embedded-dma = "0.2.0" +embedded-io = { version = "0.6.1", optional = true } fugit = "0.3.6" itertools = { version = "0.10.1", default-features = false } nb = "1.0" diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index 8fa05e5a5..a2f78d963 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -4,7 +4,7 @@ //! UartPeripheral object that can both read and write. use core::{convert::Infallible, fmt}; -use embedded_hal::serial::{Read, Write}; +use embedded_hal::serial as eh0; use fugit::HertzU32; use nb::Error::{Other, WouldBlock}; @@ -344,7 +344,7 @@ fn set_format<'w>( w } -impl> Read for UartPeripheral { +impl> eh0::Read for UartPeripheral { type Error = ReadErrorType; fn read(&mut self) -> nb::Result { @@ -380,7 +380,7 @@ impl> eh1nb::Read for UartPeripheral> Write for UartPeripheral { +impl> eh0::Write for UartPeripheral { type Error = Infallible; fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { @@ -416,8 +416,39 @@ impl> eh1nb::Write for UartPeripheral> fmt::Write for UartPeripheral { fn write_str(&mut self, s: &str) -> fmt::Result { + use eh0::Write; s.bytes() .try_for_each(|c| nb::block!(self.write(c))) .map_err(|_| fmt::Error) } } + +#[cfg(feature = "embedded-io")] +impl embedded_io::Error for ReadErrorType { + fn kind(&self) -> embedded_io::ErrorKind { + embedded_io::ErrorKind::Other + } +} +#[cfg(feature = "embedded-io")] +impl> embedded_io::ErrorType + for UartPeripheral +{ + type Error = ReadErrorType; +} +#[cfg(feature = "embedded-io")] +impl> embedded_io::Read for UartPeripheral { + fn read(&mut self, buf: &mut [u8]) -> Result { + nb::block!(self.read_raw(buf)).map_err(|e| e.err_type) + } +} +#[cfg(feature = "embedded-io")] +impl> embedded_io::Write for UartPeripheral { + fn write(&mut self, buf: &[u8]) -> Result { + self.write_full_blocking(buf); + Ok(buf.len()) + } + fn flush(&mut self) -> Result<(), Self::Error> { + nb::block!(super::writer::transmit_flushed(&self.device)).unwrap(); // Infallible + Ok(()) + } +} From 117a64366e6397dbfd1cbd5f6b0af3be0002d0ef Mon Sep 17 00:00:00 2001 From: Sympatron GmbH <35803463+Sympatron@users.noreply.github.com> Date: Thu, 14 Dec 2023 10:14:12 +0100 Subject: [PATCH 2/2] Make embedded-io dependency non-conditional --- rp2040-hal/Cargo.toml | 8 ++++---- rp2040-hal/src/uart/peripheral.rs | 4 ---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/rp2040-hal/Cargo.toml b/rp2040-hal/Cargo.toml index 38f20062b..8eb988de8 100644 --- a/rp2040-hal/Cargo.toml +++ b/rp2040-hal/Cargo.toml @@ -18,10 +18,10 @@ targets = ["thumbv6m-none-eabi"] [dependencies] cortex-m = "0.7.2" embedded-hal = { version = "0.2.5", features = ["unproven"] } -eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true } -eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.1", optional = true } +eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.1", optional = true } +eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.1", optional = true } embedded-dma = "0.2.0" -embedded-io = { version = "0.6.1", optional = true } +embedded-io = "0.6.1" fugit = "0.3.6" itertools = { version = "0.10.1", default-features = false } nb = "1.0" @@ -80,7 +80,7 @@ rp2040-e5 = [] critical-section-impl = ["critical-section/restore-state-u8"] # Support alpha release of embedded-hal -eh1_0_alpha = [ "dep:eh1_0_alpha", "dep:eh_nb_1_0_alpha" ] +eh1_0_alpha = ["dep:eh1_0_alpha", "dep:eh_nb_1_0_alpha"] [[example]] # irq example uses cortex-m-rt::interrupt, need rt feature for that diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index a2f78d963..61193347a 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -423,25 +423,21 @@ impl> fmt::Write for UartPeripheral embedded_io::ErrorKind { embedded_io::ErrorKind::Other } } -#[cfg(feature = "embedded-io")] impl> embedded_io::ErrorType for UartPeripheral { type Error = ReadErrorType; } -#[cfg(feature = "embedded-io")] impl> embedded_io::Read for UartPeripheral { fn read(&mut self, buf: &mut [u8]) -> Result { nb::block!(self.read_raw(buf)).map_err(|e| e.err_type) } } -#[cfg(feature = "embedded-io")] impl> embedded_io::Write for UartPeripheral { fn write(&mut self, buf: &[u8]) -> Result { self.write_full_blocking(buf);