Reimplement BufRead for BufferedUart

This commit is contained in:
chemicstry 2022-05-26 14:02:55 +03:00
parent 36a1f20364
commit 1d951a54be
3 changed files with 76 additions and 1 deletions

View File

@ -191,6 +191,43 @@ impl<'d, T: Instance> embedded_io::asynch::Read for BufferedUart<'d, T> {
}
}
impl<'d, T: Instance> embedded_io::asynch::BufRead for BufferedUart<'d, T> {
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>>
where
Self: 'a;
fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
poll_fn(move |cx| {
self.inner.with(|state| {
compiler_fence(Ordering::SeqCst);
// We have data ready in buffer? Return it.
let buf = state.rx.pop_buf();
if !buf.is_empty() {
let buf: &[u8] = buf;
// Safety: buffer lives as long as uart
let buf: &[u8] = unsafe { core::mem::transmute(buf) };
return Poll::Ready(Ok(buf));
}
state.rx_waker.register(cx.waker());
Poll::<Result<&[u8], Self::Error>>::Pending
})
})
}
fn consume(&mut self, amt: usize) {
let signal = self.inner.with(|state| {
let full = state.rx.is_full();
state.rx.pop(amt);
full
});
if signal {
self.inner.pend();
}
}
}
impl<'d, T: Instance> embedded_io::asynch::Write for BufferedUart<'d, T> {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
where

View File

@ -9,13 +9,13 @@ resolver = "2"
[dependencies]
embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] }
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] }
defmt = "0.3"
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,38 @@
#![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 n = {
let buf = buf_usart.fill_buf().await.unwrap();
info!("Received: {}", buf);
buf.len()
};
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
buf_usart.consume(n);
}
}