783: Reimplement BufRead for BufferedUart r=Dirbaio a=chemicstry

The `AsyncBufRead` implementation for `BufferedUart` was removed in https://github.com/embassy-rs/embassy/pull/752, this PR reimplements it from `embedded-io`. This allows reading `BufferedUart` without copying slices.

Co-authored-by: chemicstry <chemicstry@gmail.com>
This commit is contained in:
bors[bot]
2022-05-27 16:03:19 +00:00
committed by GitHub
5 changed files with 121 additions and 15 deletions

View File

@ -6,7 +6,7 @@ use defmt::*;
use embassy::executor::Spawner;
use embassy_nrf::buffered_uarte::State;
use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals};
use embedded_io::asynch::{Read, Write};
use embedded_io::asynch::{BufRead, Write};
use futures::pin_mut;
use defmt_rtt as _; // global logger
@ -46,23 +46,13 @@ async fn main(_spawner: Spawner, p: Peripherals) {
unwrap!(u.write_all(b"Hello!\r\n").await);
info!("wrote hello in uart!");
// Simple demo, reading 8-char chunks and echoing them back reversed.
loop {
info!("reading...");
let mut buf = [0u8; 8];
unwrap!(u.read_exact(&mut buf).await);
let buf = unwrap!(u.fill_buf().await);
info!("read done, got {}", buf);
// Reverse buf
for i in 0..4 {
buf.swap(i, 7 - i);
}
info!("writing...");
unwrap!(u.write_all(&buf).await);
info!("write done");
// Wait until the bytes are actually finished being transmitted
unwrap!(u.flush().await);
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
let n = buf.len();
u.consume(n);
}
}

View File

@ -16,6 +16,7 @@ defmt-rtt = "0.3"
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
embedded-hal = "0.2.6"
embedded-io = "0.3.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false }

View File

@ -0,0 +1,36 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use defmt_rtt as _; // global logger
use embassy::executor::Spawner;
use embassy_stm32::dma::NoDma;
use embassy_stm32::usart::{BufferedUart, Config, State, Uart};
use embassy_stm32::{interrupt, Peripherals};
use embedded_io::asynch::BufRead;
use panic_probe as _;
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World!");
let config = Config::default();
let usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config);
let mut state = State::new();
let irq = interrupt::take!(USART3);
let mut tx_buf = [0u8; 32];
let mut rx_buf = [0u8; 32];
let mut buf_usart =
unsafe { BufferedUart::new(&mut state, usart, irq, &mut tx_buf, &mut rx_buf) };
loop {
let buf = buf_usart.fill_buf().await.unwrap();
info!("Received: {}", buf);
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
let n = buf.len();
buf_usart.consume(n);
}
}