Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- `timer.rs` refactoring

### Added

- `embedded_io::Read` trait implemented for `serial`

## [v0.11.0] - 2025-09-09

### Breaking changes
Expand Down
31 changes: 30 additions & 1 deletion src/serial/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ mod nb {

mod io {
use super::super::{Error, Instance, Rx, Serial, Tx};
use embedded_io::Write;
use embedded_io::{Read, Write};

impl embedded_io::Error for Error {
// TODO: fix error conversion
Expand All @@ -122,6 +122,19 @@ mod io {
type Error = Error;
}

impl<USART: Instance> Read for Rx<USART> {
fn read(&mut self, bytes: &mut [u8]) -> Result<usize, Self::Error> {
for (i, byte) in bytes.iter_mut().enumerate() {
match self.read() {
Ok(b) => *byte = b,
Err(nb::Error::WouldBlock) => return Ok(i),
Err(nb::Error::Other(e)) => return Err(e),
}
}
Ok(bytes.len())
}
}

impl<USART: Instance> Write for Tx<USART> {
fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
let mut i = 0;
Expand All @@ -147,6 +160,22 @@ mod io {
}
}

impl<USART: Instance, Otype, PULL> Read for Serial<USART, Otype, PULL>
where
Rx<USART>: Read<Error = Error>,
{
fn read(&mut self, bytes: &mut [u8]) -> Result<usize, Self::Error> {
for (i, byte) in bytes.iter_mut().enumerate() {
match self.rx.read() {
Ok(b) => *byte = b,
Err(nb::Error::WouldBlock) => return Ok(i),
Err(nb::Error::Other(e)) => return Err(e),
}
}
Ok(bytes.len())
}
}

impl<USART: Instance, Otype, PULL> Write for Serial<USART, Otype, PULL>
where
Tx<USART>: Write<Error = Error>,
Expand Down
Loading