Skip to content

Commit

Permalink
Merge pull request #727 from Sympatron/main
Browse files Browse the repository at this point in the history
Implement embedded-io Read + Write for UartPeripheral
  • Loading branch information
jannic authored Dec 23, 2023
2 parents 7c9117c + ec24ff2 commit 9c4eea7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rp2040-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ embedded-hal = { version = "0.2.5", features = ["unproven"] }
eh1_0_alpha = { package = "embedded-hal", version = "=1.0.0-rc.3", optional = true }
eh_nb_1_0_alpha = { package = "embedded-hal-nb", version = "=1.0.0-rc.3", optional = true }
embedded-dma = "0.2.0"
embedded-io = "0.6.1"
fugit = "0.3.6"
itertools = { version = "0.10.1", default-features = false }
nb = "1.0"
Expand Down Expand Up @@ -79,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
Expand Down
33 changes: 30 additions & 3 deletions rp2040-hal/src/uart/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -344,7 +344,7 @@ fn set_format<'w>(
w
}

impl<D: UartDevice, P: ValidUartPinout<D>> Read<u8> for UartPeripheral<Enabled, D, P> {
impl<D: UartDevice, P: ValidUartPinout<D>> eh0::Read<u8> for UartPeripheral<Enabled, D, P> {
type Error = ReadErrorType;

fn read(&mut self) -> nb::Result<u8, Self::Error> {
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> eh1nb::Read<u8> for UartPeripheral<En
}
}

impl<D: UartDevice, P: ValidUartPinout<D>> Write<u8> for UartPeripheral<Enabled, D, P> {
impl<D: UartDevice, P: ValidUartPinout<D>> eh0::Write<u8> for UartPeripheral<Enabled, D, P> {
type Error = Infallible;

fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
Expand Down Expand Up @@ -416,8 +416,35 @@ impl<D: UartDevice, P: ValidUartPinout<D>> eh1nb::Write<u8> for UartPeripheral<E

impl<D: UartDevice, P: ValidUartPinout<D>> fmt::Write for UartPeripheral<Enabled, D, P> {
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)
}
}

impl embedded_io::Error for ReadErrorType {
fn kind(&self) -> embedded_io::ErrorKind {
embedded_io::ErrorKind::Other
}
}
impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType
for UartPeripheral<Enabled, D, P>
{
type Error = ReadErrorType;
}
impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Read for UartPeripheral<Enabled, D, P> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
nb::block!(self.read_raw(buf)).map_err(|e| e.err_type)
}
}
impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Write for UartPeripheral<Enabled, D, P> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
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(())
}
}

0 comments on commit 9c4eea7

Please sign in to comment.