From 07166ed3ba055820fbcfce463415f4816a34eda8 Mon Sep 17 00:00:00 2001 From: antbern <40672068+antbern@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:52:29 +0200 Subject: [PATCH] implement `embedded_io` `ReadReady` and `WriteReady` traits for uart --- rp2040-hal/src/uart/peripheral.rs | 17 +++++++++++++++++ rp2040-hal/src/uart/reader.rs | 6 ++++++ rp2040-hal/src/uart/writer.rs | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index 20b5f555a..0ef341f1a 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -490,6 +490,15 @@ impl> embedded_io::Read for UartPeripheral< } } } + +impl> embedded_io::ReadReady + for UartPeripheral +{ + fn read_ready(&mut self) -> Result { + Ok(self.uart_is_readable()) + } +} + impl> embedded_io::Write for UartPeripheral { fn write(&mut self, buf: &[u8]) -> Result { self.write_full_blocking(buf); @@ -500,3 +509,11 @@ impl> embedded_io::Write for UartPeripheral Ok(()) } } + +impl> embedded_io::WriteReady + for UartPeripheral +{ + fn write_ready(&mut self) -> Result { + Ok(self.uart_is_writable()) + } +} diff --git a/rp2040-hal/src/uart/reader.rs b/rp2040-hal/src/uart/reader.rs index 11829f90d..6be1e2fac 100644 --- a/rp2040-hal/src/uart/reader.rs +++ b/rp2040-hal/src/uart/reader.rs @@ -244,6 +244,12 @@ impl> embedded_io::Read for Reader { } } +impl> embedded_io::ReadReady for Reader { + fn read_ready(&mut self) -> Result { + Ok(is_readable(&self.device)) + } +} + impl> Read02 for Reader { type Error = ReadErrorType; diff --git a/rp2040-hal/src/uart/writer.rs b/rp2040-hal/src/uart/writer.rs index 747e1d620..5be6dd89b 100644 --- a/rp2040-hal/src/uart/writer.rs +++ b/rp2040-hal/src/uart/writer.rs @@ -223,6 +223,12 @@ impl> embedded_io::Write for Writer { } } +impl> embedded_io::WriteReady for Writer { + fn write_ready(&mut self) -> Result { + Ok(uart_is_writable(&self.device)) + } +} + impl> Write02 for Writer { type Error = Infallible;