Merge #936
936: Add split() method to BufferedUarte in embassy-nrf r=ZoeyR a=ZoeyR I haven't completed testing this yet. I'm creating this PR early so that I can get corrected if I went way off course. This PR adds a `split()` method to `BufferedUarte` as discussed on matrix. Co-authored-by: Zoey Riordan <zoey@dos.cafe>
This commit is contained in:
commit
838f3065ea
@ -13,6 +13,7 @@
|
|||||||
//!
|
//!
|
||||||
//! Please also see [crate::uarte] to understand when [BufferedUarte] should be used.
|
//! Please also see [crate::uarte] to understand when [BufferedUarte] should be used.
|
||||||
|
|
||||||
|
use core::cell::RefCell;
|
||||||
use core::cmp::min;
|
use core::cmp::min;
|
||||||
use core::future::Future;
|
use core::future::Future;
|
||||||
use core::sync::atomic::{compiler_fence, Ordering};
|
use core::sync::atomic::{compiler_fence, Ordering};
|
||||||
@ -71,7 +72,7 @@ struct StateInner<'d, U: UarteInstance, T: TimerInstance> {
|
|||||||
|
|
||||||
/// Interface to a UARTE instance
|
/// Interface to a UARTE instance
|
||||||
pub struct BufferedUarte<'d, U: UarteInstance, T: TimerInstance> {
|
pub struct BufferedUarte<'d, U: UarteInstance, T: TimerInstance> {
|
||||||
inner: PeripheralMutex<'d, StateInner<'d, U, T>>,
|
inner: RefCell<PeripheralMutex<'d, StateInner<'d, U, T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {}
|
impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {}
|
||||||
@ -169,7 +170,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
|
|||||||
ppi_ch2.enable();
|
ppi_ch2.enable();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
inner: PeripheralMutex::new(irq, &mut state.0, move || StateInner {
|
inner: RefCell::new(PeripheralMutex::new(irq, &mut state.0, move || StateInner {
|
||||||
_peri: peri,
|
_peri: peri,
|
||||||
timer,
|
timer,
|
||||||
_ppi_ch1: ppi_ch1,
|
_ppi_ch1: ppi_ch1,
|
||||||
@ -182,13 +183,13 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
|
|||||||
tx: RingBuffer::new(tx_buffer),
|
tx: RingBuffer::new(tx_buffer),
|
||||||
tx_state: TxState::Idle,
|
tx_state: TxState::Idle,
|
||||||
tx_waker: WakerRegistration::new(),
|
tx_waker: WakerRegistration::new(),
|
||||||
}),
|
})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adjust the baud rate to the provided value.
|
/// Adjust the baud rate to the provided value.
|
||||||
pub fn set_baudrate(&mut self, baudrate: Baudrate) {
|
pub fn set_baudrate(&mut self, baudrate: Baudrate) {
|
||||||
self.inner.with(|state| {
|
self.inner.borrow_mut().with(|state| {
|
||||||
let r = U::regs();
|
let r = U::regs();
|
||||||
|
|
||||||
let timeout = 0x8000_0000 / (baudrate as u32 / 40);
|
let timeout = 0x8000_0000 / (baudrate as u32 / 40);
|
||||||
@ -198,21 +199,16 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
|
|||||||
r.baudrate.write(|w| w.baudrate().variant(baudrate));
|
r.baudrate.write(|w| w.baudrate().variant(baudrate));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::Io for BufferedUarte<'d, U, T> {
|
pub fn split<'u>(&'u mut self) -> (BufferedUarteRx<'u, 'd, U, T>, BufferedUarteTx<'u, 'd, U, T>) {
|
||||||
type Error = core::convert::Infallible;
|
(BufferedUarteRx { inner: self }, BufferedUarteTx { inner: self })
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarte<'d, U, T> {
|
async fn inner_read<'a>(&'a self, buf: &'a mut [u8]) -> Result<usize, core::convert::Infallible> {
|
||||||
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
|
|
||||||
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
||||||
poll_fn(move |cx| {
|
poll_fn(move |cx| {
|
||||||
let mut do_pend = false;
|
let mut do_pend = false;
|
||||||
let res = self.inner.with(|state| {
|
let mut inner = self.inner.borrow_mut();
|
||||||
|
let res = inner.with(|state| {
|
||||||
compiler_fence(Ordering::SeqCst);
|
compiler_fence(Ordering::SeqCst);
|
||||||
trace!("poll_read");
|
trace!("poll_read");
|
||||||
|
|
||||||
@ -232,62 +228,18 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for Buffe
|
|||||||
Poll::Pending
|
Poll::Pending
|
||||||
});
|
});
|
||||||
if do_pend {
|
if do_pend {
|
||||||
self.inner.pend();
|
inner.pend();
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarte<'d, U, T> {
|
async fn inner_write<'a>(&'a self, buf: &'a [u8]) -> Result<usize, core::convert::Infallible> {
|
||||||
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| {
|
poll_fn(move |cx| {
|
||||||
self.inner.with(|state| {
|
let mut inner = self.inner.borrow_mut();
|
||||||
compiler_fence(Ordering::SeqCst);
|
let res = inner.with(|state| {
|
||||||
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 for BufferedUarte<'d, U, T> {
|
|
||||||
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
|
|
||||||
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
||||||
poll_fn(move |cx| {
|
|
||||||
let res = self.inner.with(|state| {
|
|
||||||
trace!("poll_write: {:?}", buf.len());
|
trace!("poll_write: {:?}", buf.len());
|
||||||
|
|
||||||
let tx_buf = state.tx.push_buf();
|
let tx_buf = state.tx.push_buf();
|
||||||
@ -308,19 +260,16 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for Buff
|
|||||||
Poll::Ready(Ok(n))
|
Poll::Ready(Ok(n))
|
||||||
});
|
});
|
||||||
|
|
||||||
self.inner.pend();
|
inner.pend();
|
||||||
|
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
|
async fn inner_flush<'a>(&'a self) -> Result<(), core::convert::Infallible> {
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
|
|
||||||
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
|
||||||
poll_fn(move |cx| {
|
poll_fn(move |cx| {
|
||||||
self.inner.with(|state| {
|
self.inner.borrow_mut().with(|state| {
|
||||||
trace!("poll_flush");
|
trace!("poll_flush");
|
||||||
|
|
||||||
if !state.tx.is_empty() {
|
if !state.tx.is_empty() {
|
||||||
@ -332,6 +281,147 @@ impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for Buff
|
|||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn inner_fill_buf<'a>(&'a self) -> Result<&'a [u8], core::convert::Infallible> {
|
||||||
|
poll_fn(move |cx| {
|
||||||
|
self.inner.borrow_mut().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], core::convert::Infallible>>::Pending
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inner_consume(&self, amt: usize) {
|
||||||
|
let mut inner = self.inner.borrow_mut();
|
||||||
|
let signal = inner.with(|state| {
|
||||||
|
let full = state.rx.is_full();
|
||||||
|
state.rx.pop(amt);
|
||||||
|
full
|
||||||
|
});
|
||||||
|
if signal {
|
||||||
|
inner.pend();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BufferedUarteTx<'u, 'd, U: UarteInstance, T: TimerInstance> {
|
||||||
|
inner: &'u BufferedUarte<'d, U, T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BufferedUarteRx<'u, 'd, U: UarteInstance, T: TimerInstance> {
|
||||||
|
inner: &'u BufferedUarte<'d, U, T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::Io for BufferedUarte<'d, U, T> {
|
||||||
|
type Error = core::convert::Infallible;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'u, 'd, U: UarteInstance, T: TimerInstance> embedded_io::Io for BufferedUarteRx<'u, 'd, U, T> {
|
||||||
|
type Error = core::convert::Infallible;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'u, 'd, U: UarteInstance, T: TimerInstance> embedded_io::Io for BufferedUarteTx<'u, 'd, U, T> {
|
||||||
|
type Error = core::convert::Infallible;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarte<'d, U, T> {
|
||||||
|
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
||||||
|
self.inner_read(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Read for BufferedUarteRx<'u, 'd, U, T> {
|
||||||
|
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
||||||
|
self.inner.inner_read(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
self.inner_fill_buf()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn consume(&mut self, amt: usize) {
|
||||||
|
self.inner_consume(amt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::BufRead for BufferedUarteRx<'u, '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> {
|
||||||
|
self.inner.inner_fill_buf()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn consume(&mut self, amt: usize) {
|
||||||
|
self.inner.inner_consume(amt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'d, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarte<'d, U, T> {
|
||||||
|
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
||||||
|
self.inner_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
||||||
|
self.inner_flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'u, 'd: 'u, U: UarteInstance, T: TimerInstance> embedded_io::asynch::Write for BufferedUarteTx<'u, 'd, U, T> {
|
||||||
|
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
||||||
|
self.inner.inner_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
|
||||||
|
where
|
||||||
|
Self: 'a;
|
||||||
|
|
||||||
|
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
||||||
|
self.inner.inner_flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user