Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read_bytes method to uart #1784

Merged
merged 2 commits into from
Jul 11, 2024
Merged
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
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `#[ram(persistent)]` option to replace the unsound `uninitialized` option (#1677)
- uart: Make `rx_timeout` optional in Config struct (#1759)
- Add interrupt related functions to `PeriodicTimer`/`OneShotTimer`, added `ErasedTimer` (#1753)
- Added blocking `read_bytes` method to `Uart` and `UartRx` (#1784)

### Fixed

Expand Down
31 changes: 30 additions & 1 deletion esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,30 @@ where
self
}

/// Fill a buffer with received bytes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a note that it will block until the buffer is full?

pub fn read_bytes(&mut self, mut buf: &mut [u8]) -> Result<(), Error> {
if buf.is_empty() {
return Ok(());
}
let cap = buf.len();
let mut total = 0;
loop {
while T::get_rx_fifo_count() == 0 {
// Block until we received at least one byte
}
let read = self.drain_fifo(buf);
total += read;
// drain_fifo only drains bytes that will fit in buf,
// so we will always have an exact total
if total == cap {
break;
}
// update the buffer position based on the bytes read
buf = &mut buf[read..];
}
Ok(())
}

/// Read a byte from the UART
pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
// On the ESP32-S2 we need to use PeriBus2 to read the FIFO:
Expand Down Expand Up @@ -832,6 +856,11 @@ where
self.tx.write_bytes(data)
}

/// Fill a buffer with received bytes
pub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error> {
self.rx.read_bytes(buf)
}

/// Configures the AT-CMD detection settings
#[allow(clippy::useless_conversion)]
pub fn set_at_cmd(&mut self, config: config::AtCmdConfig) {
Expand Down Expand Up @@ -1681,7 +1710,7 @@ where
M: Mode,
{
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if buf.len() == 0 {
if buf.is_empty() {
return Ok(0);
}

Expand Down
14 changes: 14 additions & 0 deletions hil-test/tests/uart_tx_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ mod tests {

assert_eq!(read, Ok(0x42));
}

#[test]
#[timeout(3)]
fn test_send_receive_bytes(mut ctx: Context) {
let bytes = [0x42, 0x43, 0x44];
let mut buf = [0u8; 3];

ctx.tx.flush_tx().unwrap();
ctx.tx.write_bytes(&bytes).unwrap();

ctx.rx.read_bytes(&mut buf).unwrap();

assert_eq!(buf, bytes);
}
}