mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-29 10:12:35 +00:00
Implement BufRead for nrf BufferedUarte
This commit is contained in:
parent
667abe6d1d
commit
c3b899c470
@ -236,6 +236,48 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for Buffe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead
|
||||||
|
for BufferedUarte<'d, U, 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);
|
||||||
|
trace!("fill_buf");
|
||||||
|
|
||||||
|
// We have data ready in buffer? Return it.
|
||||||
|
let buf = state.rx.pop_buf();
|
||||||
|
if !buf.is_empty() {
|
||||||
|
trace!(" got {:?} {:?}", buf.as_ptr() as u32, buf.len());
|
||||||
|
let buf: &[u8] = buf;
|
||||||
|
// Safety: buffer lives as long as uart
|
||||||
|
let buf: &[u8] = unsafe { core::mem::transmute(buf) };
|
||||||
|
return Poll::Ready(Ok(buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
trace!(" empty");
|
||||||
|
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, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write
|
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write
|
||||||
for BufferedUarte<'d, U, T>
|
for BufferedUarte<'d, U, T>
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@ use defmt::*;
|
|||||||
use embassy::executor::Spawner;
|
use embassy::executor::Spawner;
|
||||||
use embassy_nrf::buffered_uarte::State;
|
use embassy_nrf::buffered_uarte::State;
|
||||||
use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals};
|
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 futures::pin_mut;
|
||||||
|
|
||||||
use defmt_rtt as _; // global logger
|
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);
|
unwrap!(u.write_all(b"Hello!\r\n").await);
|
||||||
info!("wrote hello in uart!");
|
info!("wrote hello in uart!");
|
||||||
|
|
||||||
// Simple demo, reading 8-char chunks and echoing them back reversed.
|
|
||||||
loop {
|
loop {
|
||||||
info!("reading...");
|
info!("reading...");
|
||||||
let mut buf = [0u8; 8];
|
let buf = unwrap!(u.fill_buf().await);
|
||||||
unwrap!(u.read_exact(&mut buf).await);
|
|
||||||
info!("read done, got {}", buf);
|
info!("read done, got {}", buf);
|
||||||
|
|
||||||
// Reverse buf
|
// Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
|
||||||
for i in 0..4 {
|
let n = buf.len();
|
||||||
buf.swap(i, 7 - i);
|
u.consume(n);
|
||||||
}
|
|
||||||
|
|
||||||
info!("writing...");
|
|
||||||
unwrap!(u.write_all(&buf).await);
|
|
||||||
info!("write done");
|
|
||||||
|
|
||||||
// Wait until the bytes are actually finished being transmitted
|
|
||||||
unwrap!(u.flush().await);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user