2022-09-26 05:32:45 +02:00
|
|
|
use core::future::{poll_fn, Future};
|
2022-09-21 06:00:35 +02:00
|
|
|
use core::task::{Poll, Waker};
|
2022-08-26 09:05:12 +02:00
|
|
|
|
|
|
|
use atomic_polyfill::{compiler_fence, Ordering};
|
|
|
|
use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage};
|
|
|
|
use embassy_hal_common::ring_buffer::RingBuffer;
|
|
|
|
use embassy_sync::waitqueue::WakerRegistration;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
pub struct State<'d, T: Instance>(StateStorage<FullStateInner<'d, T>>);
|
2022-08-26 09:05:12 +02:00
|
|
|
impl<'d, T: Instance> State<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
pub const fn new() -> Self {
|
2022-08-26 09:05:12 +02:00
|
|
|
Self(StateStorage::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
pub struct RxState<'d, T: Instance>(StateStorage<RxStateInner<'d, T>>);
|
|
|
|
impl<'d, T: Instance> RxState<'d, T> {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self(StateStorage::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxState<'d, T: Instance>(StateStorage<TxStateInner<'d, T>>);
|
|
|
|
impl<'d, T: Instance> TxState<'d, T> {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self(StateStorage::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RxStateInner<'d, T: Instance> {
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
|
|
|
|
waker: WakerRegistration,
|
|
|
|
buf: RingBuffer<'d>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TxStateInner<'d, T: Instance> {
|
2022-08-26 09:05:12 +02:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
waker: WakerRegistration,
|
|
|
|
buf: RingBuffer<'d>,
|
|
|
|
}
|
2022-08-26 09:05:12 +02:00
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
struct FullStateInner<'d, T: Instance> {
|
|
|
|
rx: RxStateInner<'d, T>,
|
|
|
|
tx: TxStateInner<'d, T>,
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
unsafe impl<'d, T: Instance> Send for RxStateInner<'d, T> {}
|
|
|
|
unsafe impl<'d, T: Instance> Sync for RxStateInner<'d, T> {}
|
|
|
|
|
|
|
|
unsafe impl<'d, T: Instance> Send for TxStateInner<'d, T> {}
|
|
|
|
unsafe impl<'d, T: Instance> Sync for TxStateInner<'d, T> {}
|
|
|
|
|
|
|
|
unsafe impl<'d, T: Instance> Send for FullStateInner<'d, T> {}
|
|
|
|
unsafe impl<'d, T: Instance> Sync for FullStateInner<'d, T> {}
|
2022-08-26 09:05:12 +02:00
|
|
|
|
|
|
|
pub struct BufferedUart<'d, T: Instance> {
|
2022-09-09 10:36:27 +02:00
|
|
|
inner: PeripheralMutex<'d, FullStateInner<'d, T>>,
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
pub struct BufferedUartRx<'d, T: Instance> {
|
2022-09-09 10:36:27 +02:00
|
|
|
inner: PeripheralMutex<'d, RxStateInner<'d, T>>,
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
pub struct BufferedUartTx<'d, T: Instance> {
|
2022-09-09 10:36:27 +02:00
|
|
|
inner: PeripheralMutex<'d, TxStateInner<'d, T>>,
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> Unpin for BufferedUart<'d, T> {}
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> Unpin for BufferedUartRx<'d, T> {}
|
|
|
|
impl<'d, T: Instance> Unpin for BufferedUartTx<'d, T> {}
|
2022-08-26 09:05:12 +02:00
|
|
|
|
|
|
|
impl<'d, T: Instance> BufferedUart<'d, T> {
|
|
|
|
pub fn new<M: Mode>(
|
|
|
|
state: &'d mut State<'d, T>,
|
|
|
|
_uart: Uart<'d, T, M>,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
) -> BufferedUart<'d, T> {
|
|
|
|
into_ref!(irq);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
|
|
|
r.uartimsc().modify(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
2022-09-21 06:00:35 +02:00
|
|
|
w.set_txim(true);
|
2022-08-26 09:05:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
2022-09-09 10:36:27 +02:00
|
|
|
inner: PeripheralMutex::new(irq, &mut state.0, move || FullStateInner {
|
|
|
|
tx: TxStateInner {
|
|
|
|
phantom: PhantomData,
|
|
|
|
waker: WakerRegistration::new(),
|
|
|
|
buf: RingBuffer::new(tx_buffer),
|
|
|
|
},
|
|
|
|
rx: RxStateInner {
|
|
|
|
phantom: PhantomData,
|
|
|
|
waker: WakerRegistration::new(),
|
|
|
|
buf: RingBuffer::new(rx_buffer),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> BufferedUartRx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
pub fn new<M: Mode>(
|
|
|
|
state: &'d mut RxState<'d, T>,
|
|
|
|
_uart: UartRx<'d, T, M>,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
rx_buffer: &'d mut [u8],
|
2022-09-27 05:51:31 +02:00
|
|
|
) -> BufferedUartRx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
into_ref!(irq);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
|
|
|
r.uartimsc().modify(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inner: PeripheralMutex::new(irq, &mut state.0, move || RxStateInner {
|
|
|
|
phantom: PhantomData,
|
|
|
|
|
|
|
|
buf: RingBuffer::new(rx_buffer),
|
|
|
|
waker: WakerRegistration::new(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> BufferedUartTx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
pub fn new<M: Mode>(
|
|
|
|
state: &'d mut TxState<'d, T>,
|
|
|
|
_uart: UartTx<'d, T, M>,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
2022-09-27 05:51:31 +02:00
|
|
|
) -> BufferedUartTx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
into_ref!(irq);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
|
|
|
r.uartimsc().modify(|w| {
|
2022-09-21 06:00:35 +02:00
|
|
|
w.set_txim(true);
|
2022-09-09 10:36:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inner: PeripheralMutex::new(irq, &mut state.0, move || TxStateInner {
|
2022-08-26 09:05:12 +02:00
|
|
|
phantom: PhantomData,
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
buf: RingBuffer::new(tx_buffer),
|
|
|
|
waker: WakerRegistration::new(),
|
2022-08-26 09:05:12 +02:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
impl<'d, T: Instance> PeripheralState for FullStateInner<'d, T>
|
2022-08-26 09:05:12 +02:00
|
|
|
where
|
|
|
|
Self: 'd,
|
|
|
|
{
|
2022-09-09 10:36:27 +02:00
|
|
|
type Interrupt = T::Interrupt;
|
|
|
|
fn on_interrupt(&mut self) {
|
|
|
|
self.rx.on_interrupt();
|
|
|
|
self.tx.on_interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:00:35 +02:00
|
|
|
impl<'d, T: Instance> RxStateInner<'d, T>
|
|
|
|
where
|
|
|
|
Self: 'd,
|
|
|
|
{
|
|
|
|
fn read(&mut self, buf: &mut [u8], waker: &Waker) -> (Poll<Result<usize, Error>>, bool) {
|
|
|
|
// We have data ready in buffer? Return it.
|
|
|
|
let mut do_pend = false;
|
|
|
|
let data = self.buf.pop_buf();
|
|
|
|
if !data.is_empty() {
|
|
|
|
let len = data.len().min(buf.len());
|
|
|
|
buf[..len].copy_from_slice(&data[..len]);
|
|
|
|
|
|
|
|
if self.buf.is_full() {
|
|
|
|
do_pend = true;
|
|
|
|
}
|
|
|
|
self.buf.pop(len);
|
|
|
|
|
|
|
|
return (Poll::Ready(Ok(len)), do_pend);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.waker.register(waker);
|
|
|
|
(Poll::Pending, do_pend)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_buf<'a>(&mut self, waker: &Waker) -> Poll<Result<&'a [u8], Error>> {
|
|
|
|
// We have data ready in buffer? Return it.
|
|
|
|
let buf = self.buf.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));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.waker.register(waker);
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) -> bool {
|
|
|
|
let full = self.buf.is_full();
|
|
|
|
self.buf.pop(amt);
|
|
|
|
full
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
impl<'d, T: Instance> PeripheralState for RxStateInner<'d, T>
|
|
|
|
where
|
|
|
|
Self: 'd,
|
|
|
|
{
|
|
|
|
type Interrupt = T::Interrupt;
|
|
|
|
fn on_interrupt(&mut self) {
|
2022-08-26 09:05:12 +02:00
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
2022-09-27 07:45:10 +02:00
|
|
|
let ris = r.uartris().read();
|
2022-08-26 09:05:12 +02:00
|
|
|
// Clear interrupt flags
|
2022-09-09 10:36:27 +02:00
|
|
|
r.uarticr().modify(|w| {
|
2022-08-26 09:05:12 +02:00
|
|
|
w.set_rxic(true);
|
|
|
|
w.set_rtic(true);
|
|
|
|
});
|
|
|
|
|
2022-09-27 07:45:10 +02:00
|
|
|
if ris.peris() {
|
|
|
|
warn!("Parity error");
|
|
|
|
r.uarticr().modify(|w| {
|
|
|
|
w.set_peic(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ris.feris() {
|
|
|
|
warn!("Framing error");
|
|
|
|
r.uarticr().modify(|w| {
|
|
|
|
w.set_feic(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ris.beris() {
|
|
|
|
warn!("Break error");
|
|
|
|
r.uarticr().modify(|w| {
|
|
|
|
w.set_beic(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ris.oeris() {
|
|
|
|
warn!("Overrun error");
|
|
|
|
r.uarticr().modify(|w| {
|
|
|
|
w.set_oeic(true);
|
|
|
|
});
|
|
|
|
}
|
2022-08-26 09:05:12 +02:00
|
|
|
|
2022-09-27 07:45:10 +02:00
|
|
|
if !r.uartfr().read().rxfe() {
|
2022-09-09 10:36:27 +02:00
|
|
|
let buf = self.buf.push_buf();
|
2022-08-26 09:05:12 +02:00
|
|
|
if !buf.is_empty() {
|
|
|
|
buf[0] = r.uartdr().read().data();
|
2022-09-09 10:36:27 +02:00
|
|
|
self.buf.push(1);
|
2022-08-26 09:05:12 +02:00
|
|
|
} else {
|
|
|
|
warn!("RX buffer full, discard received byte");
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
if self.buf.is_full() {
|
|
|
|
self.waker.wake();
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:45:10 +02:00
|
|
|
if ris.rtris() {
|
2022-09-09 10:36:27 +02:00
|
|
|
self.waker.wake();
|
2022-08-26 09:05:12 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
2022-08-26 09:05:12 +02:00
|
|
|
|
2022-09-21 06:00:35 +02:00
|
|
|
impl<'d, T: Instance> TxStateInner<'d, T>
|
|
|
|
where
|
|
|
|
Self: 'd,
|
|
|
|
{
|
|
|
|
fn write(&mut self, buf: &[u8], waker: &Waker) -> (Poll<Result<usize, Error>>, bool) {
|
|
|
|
let empty = self.buf.is_empty();
|
|
|
|
let tx_buf = self.buf.push_buf();
|
|
|
|
if tx_buf.is_empty() {
|
|
|
|
self.waker.register(waker);
|
|
|
|
return (Poll::Pending, empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let n = core::cmp::min(tx_buf.len(), buf.len());
|
|
|
|
tx_buf[..n].copy_from_slice(&buf[..n]);
|
|
|
|
self.buf.push(n);
|
|
|
|
|
|
|
|
(Poll::Ready(Ok(n)), empty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self, waker: &Waker) -> Poll<Result<(), Error>> {
|
|
|
|
if !self.buf.is_empty() {
|
|
|
|
self.waker.register(waker);
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:36:27 +02:00
|
|
|
impl<'d, T: Instance> PeripheralState for TxStateInner<'d, T>
|
|
|
|
where
|
|
|
|
Self: 'd,
|
|
|
|
{
|
|
|
|
type Interrupt = T::Interrupt;
|
|
|
|
fn on_interrupt(&mut self) {
|
2022-08-26 09:05:12 +02:00
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
2022-09-27 07:45:10 +02:00
|
|
|
let buf = self.buf.pop_buf();
|
|
|
|
if !buf.is_empty() {
|
|
|
|
r.uartimsc().modify(|w| {
|
|
|
|
w.set_txim(true);
|
|
|
|
});
|
|
|
|
r.uartdr().write(|w| w.set_data(buf[0].into()));
|
|
|
|
self.buf.pop(1);
|
|
|
|
self.waker.wake();
|
|
|
|
} else {
|
|
|
|
// Disable interrupt until we have something to transmit again
|
|
|
|
r.uartimsc().modify(|w| {
|
|
|
|
w.set_txim(false);
|
|
|
|
});
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl embedded_io::Error for Error {
|
|
|
|
fn kind(&self) -> embedded_io::ErrorKind {
|
|
|
|
embedded_io::ErrorKind::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_io::Io for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> embedded_io::Io for BufferedUartRx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> embedded_io::Io for BufferedUartTx<'d, T> {
|
2022-09-09 10:36:27 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2022-08-26 09:05:12 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Read for BufferedUart<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
|
2022-09-09 10:36:27 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
poll_fn(move |cx| {
|
2022-09-21 06:00:35 +02:00
|
|
|
let (res, do_pend) = self.inner.with(|state| {
|
2022-09-09 10:36:27 +02:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
2022-09-21 06:00:35 +02:00
|
|
|
state.rx.read(buf, cx.waker())
|
2022-09-09 10:36:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if do_pend {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Read for BufferedUartRx<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
|
2022-08-26 09:05:12 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
poll_fn(move |cx| {
|
2022-09-21 06:00:35 +02:00
|
|
|
let (res, do_pend) = self.inner.with(|state| {
|
2022-08-26 09:05:12 +02:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
2022-09-21 06:00:35 +02:00
|
|
|
state.read(buf, cx.waker())
|
2022-08-26 09:05:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if do_pend {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::BufRead for BufferedUart<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> + 'a
|
2022-08-26 09:05:12 +02:00
|
|
|
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);
|
2022-09-21 06:00:35 +02:00
|
|
|
state.rx.fill_buf(cx.waker())
|
2022-09-09 10:36:27 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
2022-09-21 06:00:35 +02:00
|
|
|
let signal = self.inner.with(|state| state.rx.consume(amt));
|
2022-09-09 10:36:27 +02:00
|
|
|
if signal {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::BufRead for BufferedUartRx<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> + 'a
|
2022-09-09 10:36:27 +02:00
|
|
|
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);
|
2022-09-21 06:00:35 +02:00
|
|
|
state.fill_buf(cx.waker())
|
2022-08-26 09:05:12 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
2022-09-21 06:00:35 +02:00
|
|
|
let signal = self.inner.with(|state| state.consume(amt));
|
2022-08-26 09:05:12 +02:00
|
|
|
if signal {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Write for BufferedUart<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
|
2022-08-26 09:05:12 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
poll_fn(move |cx| {
|
2022-09-21 06:00:35 +02:00
|
|
|
let (poll, empty) = self.inner.with(|state| state.tx.write(buf, cx.waker()));
|
2022-09-09 10:36:27 +02:00
|
|
|
if empty {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
poll
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:47:29 +02:00
|
|
|
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a
|
2022-09-09 10:36:27 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
2022-09-21 06:00:35 +02:00
|
|
|
poll_fn(move |cx| self.inner.with(|state| state.tx.flush(cx.waker())))
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Write for BufferedUartTx<'d, T> {
|
2022-10-26 16:47:29 +02:00
|
|
|
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
|
2022-09-09 10:36:27 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
poll_fn(move |cx| {
|
2022-09-21 06:00:35 +02:00
|
|
|
let (poll, empty) = self.inner.with(|state| state.write(buf, cx.waker()));
|
2022-08-26 09:05:12 +02:00
|
|
|
if empty {
|
|
|
|
self.inner.pend();
|
|
|
|
}
|
|
|
|
poll
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:47:29 +02:00
|
|
|
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a
|
2022-08-26 09:05:12 +02:00
|
|
|
where
|
|
|
|
Self: 'a;
|
|
|
|
|
|
|
|
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
2022-09-21 06:00:35 +02:00
|
|
|
poll_fn(move |cx| self.inner.with(|state| state.flush(cx.waker())))
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|