feat: Make read_bytes return how many bytes it read from the fifo and fix docs/tests
This commit is contained in:
parent
133d5eb368
commit
91dd9ac6a0
@ -79,8 +79,8 @@
|
|||||||
//! let (mut rx, mut tx) = uart1.split();
|
//! let (mut rx, mut tx) = uart1.split();
|
||||||
//!
|
//!
|
||||||
//! // Each component can be used individually to interact with the UART:
|
//! // Each component can be used individually to interact with the UART:
|
||||||
//! tx.write_bytes(&[42u8]).expect("write error!");
|
//! tx.write_bytes(&[42u8]);
|
||||||
//! let byte = rx.read_byte().expect("read error!");
|
//! let byte = rx.read_byte();
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
@ -198,9 +198,8 @@
|
|||||||
//! let serial = serial.as_mut().unwrap();
|
//! let serial = serial.as_mut().unwrap();
|
||||||
//!
|
//!
|
||||||
//! let mut cnt = 0;
|
//! let mut cnt = 0;
|
||||||
//! while let Some(_c) = serial.read_byte() {
|
//! let mut buff = [0u8; 64];
|
||||||
//! cnt += 1;
|
//! let cnt = serial.read_bytes(&mut buff);
|
||||||
//! }
|
|
||||||
//! writeln!(serial, "Read {} bytes", cnt).ok();
|
//! writeln!(serial, "Read {} bytes", cnt).ok();
|
||||||
//!
|
//!
|
||||||
//! let pending_interrupts = serial.interrupts();
|
//! 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 {
|
pub fn read_bytes(&mut self, buf: &mut [u8]) -> usize {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
while count < buf.len() {
|
while count < buf.len() && self.rx_fifo_count() > 0 {
|
||||||
buf[count] = self.read_byte();
|
buf[count] = self.read_byte();
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,9 +24,6 @@ mod tests {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.with_rx(rx);
|
.with_rx(rx);
|
||||||
|
|
||||||
// start reception
|
|
||||||
_ = rx.read_byte(); // this will just return WouldBlock
|
|
||||||
|
|
||||||
unsafe { tx.set_output_high(false, esp_hal::Internal::conjure()) };
|
unsafe { tx.set_output_high(false, esp_hal::Internal::conjure()) };
|
||||||
|
|
||||||
// set up TX and send a byte
|
// set up TX and send a byte
|
||||||
@ -34,13 +31,9 @@ mod tests {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.with_tx(tx);
|
.with_tx(tx);
|
||||||
|
|
||||||
tx.flush().unwrap();
|
tx.flush();
|
||||||
tx.write_bytes(&[0x42]).unwrap();
|
tx.write_bytes(&[0x42]).unwrap();
|
||||||
let read = loop {
|
let read = rx.read_byte();
|
||||||
if let Some(byte) = rx.read_byte() {
|
|
||||||
break byte;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(read, 0x42);
|
assert_eq!(read, 0x42);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,13 +41,9 @@ mod tests {
|
|||||||
fn test_send_receive(mut ctx: Context) {
|
fn test_send_receive(mut ctx: Context) {
|
||||||
let byte = [0x42];
|
let byte = [0x42];
|
||||||
|
|
||||||
ctx.tx.flush().unwrap();
|
ctx.tx.flush();
|
||||||
ctx.tx.write_bytes(&byte).unwrap();
|
ctx.tx.write_bytes(&byte).unwrap();
|
||||||
let read = loop {
|
let read = ctx.rx.read_byte();
|
||||||
if let Some(byte) = ctx.rx.read_byte() {
|
|
||||||
break byte;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(read, 0x42);
|
assert_eq!(read, 0x42);
|
||||||
}
|
}
|
||||||
@ -57,7 +53,7 @@ mod tests {
|
|||||||
let bytes = [0x42, 0x43, 0x44];
|
let bytes = [0x42, 0x43, 0x44];
|
||||||
let mut buf = [0u8; 3];
|
let mut buf = [0u8; 3];
|
||||||
|
|
||||||
ctx.tx.flush().unwrap();
|
ctx.tx.flush();
|
||||||
ctx.tx.write_bytes(&bytes).unwrap();
|
ctx.tx.write_bytes(&bytes).unwrap();
|
||||||
|
|
||||||
let mut bytes_read = 0;
|
let mut bytes_read = 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user