Skip to content

Commit

Permalink
Avoid discarding already read bytes on error
Browse files Browse the repository at this point in the history
Closes: #782
  • Loading branch information
jannic committed May 28, 2024
1 parent d152844 commit 4c50e94
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
23 changes: 22 additions & 1 deletion rp2040-hal/src/uart/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct UartPeripheral<S: State, D: UartDevice, P: ValidUartPinout<D>> {
device: D,
_state: S,
pins: P,
read_error: Option<ReadErrorType>,
}

impl<S: State, D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<S, D, P> {
Expand All @@ -29,6 +30,7 @@ impl<S: State, D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<S, D, P> {
device: self.device,
pins: self.pins,
_state: state,
read_error: None,
}
}

Expand All @@ -48,6 +50,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Disabled, D, P> {
device,
_state: Disabled,
pins,
read_error: None,
}
}

Expand Down Expand Up @@ -88,6 +91,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Disabled, D, P> {
device,
pins,
_state: Enabled,
read_error: None,
})
}
}
Expand Down Expand Up @@ -245,6 +249,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Enabled, D, P> {
device: reader.device,
_state: Enabled,
pins: reader.pins,
read_error: reader.read_error,
}
}
}
Expand All @@ -255,6 +260,7 @@ impl<P: ValidUartPinout<UART0>> UartPeripheral<Enabled, UART0, P> {
let reader = Reader {
device: self.device,
pins: self.pins,
read_error: self.read_error,
};
// Safety: reader and writer will never write to the same address
let device_copy = unsafe { Peripherals::steal().UART0 };
Expand All @@ -273,6 +279,7 @@ impl<P: ValidUartPinout<UART1>> UartPeripheral<Enabled, UART1, P> {
let reader = Reader {
device: self.device,
pins: self.pins,
read_error: self.read_error,
};
// Safety: reader and writer will never write to the same address
let device_copy = unsafe { Peripherals::steal().UART1 };
Expand Down Expand Up @@ -464,7 +471,21 @@ impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType
}
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)
// If the last read stored an error, report it now
if let Some(err) = self.read_error.take() {
return Err(err);
}
match nb::block!(self.read_raw(buf)) {
Ok(bytes_read) => Ok(bytes_read),
Err(err) if !err.discarded.is_empty() => {
// If an error was reported but some bytes were already read,
// return the data now and store the error for the next
// invocation.
self.read_error = Some(err.err_type);
Ok(err.discarded.len())
}
Err(err) => Err(err.err_type),
}
}
}
impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Write for UartPeripheral<Enabled, D, P> {
Expand Down
17 changes: 16 additions & 1 deletion rp2040-hal/src/uart/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub(crate) fn read_full_blocking<D: UartDevice>(
pub struct Reader<D: UartDevice, P: ValidUartPinout<D>> {
pub(super) device: D,
pub(super) pins: P,
pub(super) read_error: Option<ReadErrorType>,
}

impl<D: UartDevice, P: ValidUartPinout<D>> Reader<D, P> {
Expand Down Expand Up @@ -224,7 +225,21 @@ impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType for Reader<D,

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Read for Reader<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)
// If the last read stored an error, report it now
if let Some(err) = self.read_error.take() {
return Err(err);
}
match nb::block!(self.read_raw(buf)) {
Ok(bytes_read) => Ok(bytes_read),
Err(err) if !err.discarded.is_empty() => {
// If an error was reported but some bytes were already read,
// return the data now and store the error for the next
// invocation.
self.read_error = Some(err.err_type);
Ok(err.discarded.len())
}
Err(err) => Err(err.err_type),
}
}
}

Expand Down

0 comments on commit 4c50e94

Please sign in to comment.