feat: Make read_bytes return how many bytes it read from the fifo and fix docs/tests

This commit is contained in:
Sergio Gasquez 2025-01-08 12:07:56 +01:00
parent 133d5eb368
commit 91dd9ac6a0
3 changed files with 12 additions and 23 deletions

View File

@ -79,8 +79,8 @@
//! let (mut rx, mut tx) = uart1.split();
//!
//! // Each component can be used individually to interact with the UART:
//! tx.write_bytes(&[42u8]).expect("write error!");
//! let byte = rx.read_byte().expect("read error!");
//! tx.write_bytes(&[42u8]);
//! let byte = rx.read_byte();
//! # }
//! ```
//!
@ -198,9 +198,8 @@
//! let serial = serial.as_mut().unwrap();
//!
//! let mut cnt = 0;
//! while let Some(_c) = serial.read_byte() {
//! cnt += 1;
//! }
//! let mut buff = [0u8; 64];
//! let cnt = serial.read_bytes(&mut buff);
//! writeln!(serial, "Read {} bytes", cnt).ok();
//!
//! let pending_interrupts = serial.interrupts();
@ -843,10 +842,11 @@ where
}
}
/// Read as many bytes from the UART as the provided buffer can hold.
/// Read all available bytes from the RX FIFO into the provided buffer and
/// returns the number of read bytes.
pub fn read_bytes(&mut self, buf: &mut [u8]) -> usize {
let mut count = 0;
while count < buf.len() {
while count < buf.len() && self.rx_fifo_count() > 0 {
buf[count] = self.read_byte();
count += 1;
}

View File

@ -24,9 +24,6 @@ mod tests {
.unwrap()
.with_rx(rx);
// start reception
_ = rx.read_byte(); // this will just return WouldBlock
unsafe { tx.set_output_high(false, esp_hal::Internal::conjure()) };
// set up TX and send a byte
@ -34,13 +31,9 @@ mod tests {
.unwrap()
.with_tx(tx);
tx.flush().unwrap();
tx.flush();
tx.write_bytes(&[0x42]).unwrap();
let read = loop {
if let Some(byte) = rx.read_byte() {
break byte;
}
};
let read = rx.read_byte();
assert_eq!(read, 0x42);
}

View File

@ -41,13 +41,9 @@ mod tests {
fn test_send_receive(mut ctx: Context) {
let byte = [0x42];
ctx.tx.flush().unwrap();
ctx.tx.flush();
ctx.tx.write_bytes(&byte).unwrap();
let read = loop {
if let Some(byte) = ctx.rx.read_byte() {
break byte;
}
};
let read = ctx.rx.read_byte();
assert_eq!(read, 0x42);
}
@ -57,7 +53,7 @@ mod tests {
let bytes = [0x42, 0x43, 0x44];
let mut buf = [0u8; 3];
ctx.tx.flush().unwrap();
ctx.tx.flush();
ctx.tx.write_bytes(&bytes).unwrap();
let mut bytes_read = 0;