Add read_bytes method to uart (#1784)
* Add read_bytes method to uart * Changelog
This commit is contained in:
parent
a2883ac318
commit
6c3ccb6043
@ -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)
|
- `#[ram(persistent)]` option to replace the unsound `uninitialized` option (#1677)
|
||||||
- uart: Make `rx_timeout` optional in Config struct (#1759)
|
- uart: Make `rx_timeout` optional in Config struct (#1759)
|
||||||
- Add interrupt related functions to `PeriodicTimer`/`OneShotTimer`, added `ErasedTimer` (#1753)
|
- Add interrupt related functions to `PeriodicTimer`/`OneShotTimer`, added `ErasedTimer` (#1753)
|
||||||
|
- Added blocking `read_bytes` method to `Uart` and `UartRx` (#1784)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
@ -519,6 +519,30 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fill a buffer with received bytes
|
||||||
|
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
|
/// Read a byte from the UART
|
||||||
pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
|
pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
|
||||||
// On the ESP32-S2 we need to use PeriBus2 to read the FIFO:
|
// On the ESP32-S2 we need to use PeriBus2 to read the FIFO:
|
||||||
@ -832,6 +856,11 @@ where
|
|||||||
self.tx.write_bytes(data)
|
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
|
/// Configures the AT-CMD detection settings
|
||||||
#[allow(clippy::useless_conversion)]
|
#[allow(clippy::useless_conversion)]
|
||||||
pub fn set_at_cmd(&mut self, config: config::AtCmdConfig) {
|
pub fn set_at_cmd(&mut self, config: config::AtCmdConfig) {
|
||||||
@ -1681,7 +1710,7 @@ where
|
|||||||
M: Mode,
|
M: Mode,
|
||||||
{
|
{
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
||||||
if buf.len() == 0 {
|
if buf.is_empty() {
|
||||||
return Ok(0);
|
return Ok(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -67,4 +67,18 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(read, Ok(0x42));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user