From eea9ecff6541a9632bdcf51d484b9f209028ec61 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Tue, 4 Feb 2025 22:15:59 +0100 Subject: [PATCH] Fix implementations of embedded_io::Write for UartPeripheral (#895) Fixes #840 --- rp2040-hal/src/uart/peripheral.rs | 5 +++-- rp2040-hal/src/uart/writer.rs | 1 + rp235x-hal/src/uart/peripheral.rs | 5 +++-- rp235x-hal/src/uart/writer.rs | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index 6435229db..ab856f221 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -504,8 +504,9 @@ impl> embedded_io::ReadReady impl> embedded_io::Write for UartPeripheral { fn write(&mut self, buf: &[u8]) -> Result { - self.write_full_blocking(buf); - Ok(buf.len()) + // Blocks if and only if no bytes can be written. + let remaining = nb::block!(super::writer::write_raw(&self.device, buf)).unwrap(); // Infallible + Ok(buf.len() - remaining.len()) } fn flush(&mut self) -> Result<(), Self::Error> { nb::block!(super::writer::transmit_flushed(&self.device)).unwrap(); // Infallible diff --git a/rp2040-hal/src/uart/writer.rs b/rp2040-hal/src/uart/writer.rs index 47bc5a0aa..51fa1ecab 100644 --- a/rp2040-hal/src/uart/writer.rs +++ b/rp2040-hal/src/uart/writer.rs @@ -214,6 +214,7 @@ impl> embedded_io::ErrorType for Writer> embedded_io::Write for Writer { fn write(&mut self, buf: &[u8]) -> Result { + // Blocks if and only if no bytes can be written. let remaining = nb::block!(write_raw(&self.device, buf)).unwrap(); // Infallible Ok(buf.len() - remaining.len()) } diff --git a/rp235x-hal/src/uart/peripheral.rs b/rp235x-hal/src/uart/peripheral.rs index 95421d3b8..064875987 100644 --- a/rp235x-hal/src/uart/peripheral.rs +++ b/rp235x-hal/src/uart/peripheral.rs @@ -504,8 +504,9 @@ impl> embedded_io::ReadReady impl> embedded_io::Write for UartPeripheral { fn write(&mut self, buf: &[u8]) -> Result { - self.write_full_blocking(buf); - Ok(buf.len()) + // Blocks if and only if no bytes can be written. + let remaining = nb::block!(super::writer::write_raw(&self.device, buf)).unwrap(); // Infallible + Ok(buf.len() - remaining.len()) } fn flush(&mut self) -> Result<(), Self::Error> { nb::block!(super::writer::transmit_flushed(&self.device)).unwrap(); // Infallible diff --git a/rp235x-hal/src/uart/writer.rs b/rp235x-hal/src/uart/writer.rs index ac9c4779c..2f49c2648 100644 --- a/rp235x-hal/src/uart/writer.rs +++ b/rp235x-hal/src/uart/writer.rs @@ -215,6 +215,7 @@ impl> embedded_io::ErrorType for Writer> embedded_io::Write for Writer { fn write(&mut self, buf: &[u8]) -> Result { + // Blocks if and only if no bytes can be written. let remaining = nb::block!(write_raw(&self.device, buf)).unwrap(); // Infallible Ok(buf.len() - remaining.len()) }