2022-11-07 00:27:21 +01:00
|
|
|
use core::future::{poll_fn, Future};
|
|
|
|
use core::slice;
|
|
|
|
use core::task::Poll;
|
2022-08-26 09:05:12 +02:00
|
|
|
|
2023-04-30 04:21:11 +02:00
|
|
|
use atomic_polyfill::{AtomicU8, Ordering};
|
2022-11-07 00:27:21 +01:00
|
|
|
use embassy_cortex_m::interrupt::{Interrupt, InterruptExt};
|
|
|
|
use embassy_hal_common::atomic_ring_buffer::RingBuffer;
|
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2023-04-30 08:04:21 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
2022-08-26 09:05:12 +02:00
|
|
|
|
|
|
|
use super::*;
|
2023-04-30 08:04:21 +02:00
|
|
|
use crate::clocks::clk_peri_freq;
|
2023-01-05 18:45:58 +01:00
|
|
|
use crate::RegExt;
|
2022-08-26 09:05:12 +02:00
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
pub struct State {
|
|
|
|
tx_waker: AtomicWaker,
|
|
|
|
tx_buf: RingBuffer,
|
|
|
|
rx_waker: AtomicWaker,
|
|
|
|
rx_buf: RingBuffer,
|
2023-04-30 04:21:11 +02:00
|
|
|
rx_error: AtomicU8,
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 04:21:11 +02:00
|
|
|
// these must match bits 8..11 in UARTDR
|
|
|
|
const RXE_OVERRUN: u8 = 8;
|
|
|
|
const RXE_BREAK: u8 = 4;
|
|
|
|
const RXE_PARITY: u8 = 2;
|
|
|
|
const RXE_FRAMING: u8 = 1;
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
impl State {
|
2022-09-09 10:36:27 +02:00
|
|
|
pub const fn new() -> Self {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self {
|
|
|
|
rx_buf: RingBuffer::new(),
|
|
|
|
tx_buf: RingBuffer::new(),
|
|
|
|
rx_waker: AtomicWaker::new(),
|
|
|
|
tx_waker: AtomicWaker::new(),
|
2023-04-30 04:21:11 +02:00
|
|
|
rx_error: AtomicU8::new(0),
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 09:05:12 +02:00
|
|
|
pub struct BufferedUart<'d, T: Instance> {
|
2023-02-16 08:47:22 +01:00
|
|
|
pub(crate) rx: BufferedUartRx<'d, T>,
|
|
|
|
pub(crate) tx: BufferedUartTx<'d, T>,
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
pub struct BufferedUartRx<'d, T: Instance> {
|
2023-02-16 08:47:22 +01:00
|
|
|
pub(crate) phantom: PhantomData<&'d mut T>,
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
pub struct BufferedUartTx<'d, T: Instance> {
|
2023-02-16 08:47:22 +01:00
|
|
|
pub(crate) phantom: PhantomData<&'d mut T>,
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-16 08:47:22 +01:00
|
|
|
pub(crate) fn init_buffers<'d, T: Instance + 'd>(
|
2023-01-01 21:34:20 +01:00
|
|
|
irq: PeripheralRef<'d, T::Interrupt>,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
) {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2023-01-01 21:34:20 +01:00
|
|
|
let len = tx_buffer.len();
|
|
|
|
unsafe { state.tx_buf.init(tx_buffer.as_mut_ptr(), len) };
|
|
|
|
let len = rx_buffer.len();
|
|
|
|
unsafe { state.rx_buf.init(rx_buffer.as_mut_ptr(), len) };
|
|
|
|
|
2023-01-05 18:45:58 +01:00
|
|
|
// From the datasheet:
|
|
|
|
// "The transmit interrupt is based on a transition through a level, rather
|
|
|
|
// than on the level itself. When the interrupt and the UART is enabled
|
|
|
|
// before any data is written to the transmit FIFO the interrupt is not set.
|
|
|
|
// The interrupt is only set, after written data leaves the single location
|
|
|
|
// of the transmit FIFO and it becomes empty."
|
|
|
|
//
|
|
|
|
// This means we can leave the interrupt enabled the whole time as long as
|
|
|
|
// we clear it after it happens. The downside is that the we manually have
|
|
|
|
// to pend the ISR when we want data transmission to start.
|
|
|
|
let regs = T::regs();
|
2023-02-16 08:47:22 +01:00
|
|
|
unsafe {
|
|
|
|
regs.uartimsc().write_set(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
|
|
|
w.set_txim(true);
|
|
|
|
});
|
|
|
|
};
|
2023-01-05 18:45:58 +01:00
|
|
|
|
2023-01-01 21:34:20 +01:00
|
|
|
irq.set_handler(on_interrupt::<T>);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
|
|
|
}
|
|
|
|
|
2022-08-26 09:05:12 +02:00
|
|
|
impl<'d, T: Instance> BufferedUart<'d, T> {
|
2022-11-07 00:27:21 +01:00
|
|
|
pub fn new(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, tx, rx);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(Some(tx.map_into()), Some(rx.map_into()), None, None, config);
|
|
|
|
init_buffers::<T>(irq, tx_buffer, rx_buffer);
|
|
|
|
|
2023-01-01 22:02:45 +01:00
|
|
|
Self {
|
|
|
|
rx: BufferedUartRx { phantom: PhantomData },
|
|
|
|
tx: BufferedUartTx { phantom: PhantomData },
|
|
|
|
}
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_rtscts(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
2022-08-26 09:05:12 +02:00
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
2022-11-07 00:27:21 +01:00
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
rts: impl Peripheral<P = impl RtsPin<T>> + 'd,
|
|
|
|
cts: impl Peripheral<P = impl CtsPin<T>> + 'd,
|
2022-08-26 09:05:12 +02:00
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
2022-11-07 00:27:21 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, tx, rx, cts, rts);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(
|
2023-01-01 21:34:20 +01:00
|
|
|
Some(tx.map_into()),
|
|
|
|
Some(rx.map_into()),
|
2022-11-07 00:27:21 +01:00
|
|
|
Some(rts.map_into()),
|
|
|
|
Some(cts.map_into()),
|
|
|
|
config,
|
|
|
|
);
|
2023-02-16 08:47:22 +01:00
|
|
|
init_buffers::<T>(irq, tx_buffer, rx_buffer);
|
|
|
|
|
2023-01-01 22:02:45 +01:00
|
|
|
Self {
|
|
|
|
rx: BufferedUartRx { phantom: PhantomData },
|
|
|
|
tx: BufferedUartTx { phantom: PhantomData },
|
|
|
|
}
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
2022-12-22 23:03:05 +01:00
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
pub fn blocking_write(&mut self, buffer: &[u8]) -> Result<usize, Error> {
|
2023-02-16 08:47:22 +01:00
|
|
|
self.tx.blocking_write(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_flush(&mut self) -> Result<(), Error> {
|
|
|
|
self.tx.blocking_flush()
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
pub fn blocking_read(&mut self, buffer: &mut [u8]) -> Result<usize, Error> {
|
2023-02-16 08:47:22 +01:00
|
|
|
self.rx.blocking_read(buffer)
|
|
|
|
}
|
|
|
|
|
2023-04-30 08:04:21 +02:00
|
|
|
pub fn busy(&self) -> bool {
|
|
|
|
self.tx.busy()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_break(&mut self, bits: u32) {
|
|
|
|
self.tx.send_break(bits).await
|
|
|
|
}
|
|
|
|
|
2023-01-01 22:02:45 +01:00
|
|
|
pub fn split(self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) {
|
|
|
|
(self.rx, self.tx)
|
2022-12-22 23:03:05 +01:00
|
|
|
}
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> BufferedUartRx<'d, T> {
|
2022-11-07 00:27:21 +01:00
|
|
|
pub fn new(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
2022-09-09 10:36:27 +02:00
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
2022-11-07 00:27:21 +01:00
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
2022-09-09 10:36:27 +02:00
|
|
|
rx_buffer: &'d mut [u8],
|
2022-11-07 00:27:21 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, rx);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(None, Some(rx.map_into()), None, None, config);
|
|
|
|
init_buffers::<T>(irq, &mut [], rx_buffer);
|
|
|
|
|
2023-01-01 21:34:20 +01:00
|
|
|
Self { phantom: PhantomData }
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_rts(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
rts: impl Peripheral<P = impl RtsPin<T>> + 'd,
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, rx, rts);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(None, Some(rx.map_into()), Some(rts.map_into()), None, config);
|
|
|
|
init_buffers::<T>(irq, &mut [], rx_buffer);
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
Self { phantom: PhantomData }
|
|
|
|
}
|
|
|
|
|
2023-04-30 07:05:42 +02:00
|
|
|
fn read<'a>(buf: &'a mut [u8]) -> impl Future<Output = Result<usize, Error>> + 'a
|
|
|
|
where
|
|
|
|
T: 'd,
|
|
|
|
{
|
2022-11-07 00:27:21 +01:00
|
|
|
poll_fn(move |cx| {
|
2023-04-30 07:05:42 +02:00
|
|
|
if let Poll::Ready(r) = Self::try_read(buf) {
|
|
|
|
return Poll::Ready(r);
|
2023-04-01 14:31:24 +02:00
|
|
|
}
|
2023-04-30 07:05:42 +02:00
|
|
|
T::buffered_state().rx_waker.register(cx.waker());
|
|
|
|
Poll::Pending
|
2022-11-07 00:27:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-30 04:21:11 +02:00
|
|
|
fn get_rx_error() -> Option<Error> {
|
|
|
|
let errs = T::buffered_state().rx_error.swap(0, Ordering::Relaxed);
|
|
|
|
if errs & RXE_OVERRUN != 0 {
|
|
|
|
Some(Error::Overrun)
|
|
|
|
} else if errs & RXE_BREAK != 0 {
|
|
|
|
Some(Error::Break)
|
|
|
|
} else if errs & RXE_PARITY != 0 {
|
|
|
|
Some(Error::Parity)
|
|
|
|
} else if errs & RXE_FRAMING != 0 {
|
|
|
|
Some(Error::Framing)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_read(buf: &mut [u8]) -> Poll<Result<usize, Error>>
|
|
|
|
where
|
|
|
|
T: 'd,
|
|
|
|
{
|
2023-04-01 14:31:24 +02:00
|
|
|
if buf.is_empty() {
|
2023-04-30 07:05:42 +02:00
|
|
|
return Poll::Ready(Ok(0));
|
2023-04-01 14:31:24 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 07:05:42 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
let mut rx_reader = unsafe { state.rx_buf.reader() };
|
|
|
|
let n = rx_reader.pop(|data| {
|
|
|
|
let n = data.len().min(buf.len());
|
|
|
|
buf[..n].copy_from_slice(&data[..n]);
|
|
|
|
n
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = if n == 0 {
|
2023-04-30 04:21:11 +02:00
|
|
|
match Self::get_rx_error() {
|
|
|
|
None => return Poll::Pending,
|
|
|
|
Some(e) => Err(e),
|
|
|
|
}
|
2023-04-30 07:05:42 +02:00
|
|
|
} else {
|
|
|
|
Ok(n)
|
|
|
|
};
|
|
|
|
|
|
|
|
// (Re-)Enable the interrupt to receive more data in case it was
|
2023-04-30 04:21:11 +02:00
|
|
|
// disabled because the buffer was full or errors were detected.
|
2023-04-30 07:05:42 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
unsafe {
|
|
|
|
regs.uartimsc().write_set(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
2023-02-16 08:47:22 +01:00
|
|
|
});
|
2023-04-30 07:05:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Ready(result)
|
|
|
|
}
|
2023-02-16 08:47:22 +01:00
|
|
|
|
2023-04-30 07:05:42 +02:00
|
|
|
pub fn blocking_read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
|
|
|
|
loop {
|
|
|
|
match Self::try_read(buf) {
|
|
|
|
Poll::Ready(res) => return res,
|
|
|
|
Poll::Pending => continue,
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 04:21:11 +02:00
|
|
|
fn fill_buf<'a>() -> impl Future<Output = Result<&'a [u8], Error>>
|
|
|
|
where
|
|
|
|
T: 'd,
|
|
|
|
{
|
2022-11-07 00:27:21 +01:00
|
|
|
poll_fn(move |cx| {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-11-07 00:27:21 +01:00
|
|
|
let mut rx_reader = unsafe { state.rx_buf.reader() };
|
|
|
|
let (p, n) = rx_reader.pop_buf();
|
2023-04-30 04:21:11 +02:00
|
|
|
let result = if n == 0 {
|
|
|
|
match Self::get_rx_error() {
|
|
|
|
None => {
|
|
|
|
state.rx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
Some(e) => Err(e),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let buf = unsafe { slice::from_raw_parts(p, n) };
|
|
|
|
Ok(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
Poll::Ready(result)
|
2022-11-07 00:27:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(amt: usize) {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-11-07 00:27:21 +01:00
|
|
|
let mut rx_reader = unsafe { state.rx_buf.reader() };
|
2023-01-01 21:34:20 +01:00
|
|
|
rx_reader.pop_done(amt);
|
2023-01-05 18:45:58 +01:00
|
|
|
|
|
|
|
// (Re-)Enable the interrupt to receive more data in case it was
|
2023-04-30 04:21:11 +02:00
|
|
|
// disabled because the buffer was full or errors were detected.
|
2023-01-05 18:45:58 +01:00
|
|
|
let regs = T::regs();
|
|
|
|
unsafe {
|
|
|
|
regs.uartimsc().write_set(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
|
|
|
});
|
|
|
|
}
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance> BufferedUartTx<'d, T> {
|
2022-11-07 00:27:21 +01:00
|
|
|
pub fn new(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
2022-09-09 10:36:27 +02:00
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
2022-11-07 00:27:21 +01:00
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
2022-09-09 10:36:27 +02:00
|
|
|
tx_buffer: &'d mut [u8],
|
2022-11-07 00:27:21 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, tx);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(Some(tx.map_into()), None, None, None, config);
|
|
|
|
init_buffers::<T>(irq, tx_buffer, &mut []);
|
|
|
|
|
2023-01-01 21:34:20 +01:00
|
|
|
Self { phantom: PhantomData }
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_cts(
|
|
|
|
_uart: impl Peripheral<P = T> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
cts: impl Peripheral<P = impl CtsPin<T>> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-01-01 21:34:20 +01:00
|
|
|
into_ref!(irq, tx, cts);
|
2023-02-16 08:47:22 +01:00
|
|
|
|
|
|
|
super::Uart::<'d, T, Async>::init(Some(tx.map_into()), None, None, Some(cts.map_into()), config);
|
|
|
|
init_buffers::<T>(irq, tx_buffer, &mut []);
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
Self { phantom: PhantomData }
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
fn write<'a>(buf: &'a [u8]) -> impl Future<Output = Result<usize, Error>> + 'a {
|
|
|
|
poll_fn(move |cx| {
|
2023-04-02 14:36:32 +02:00
|
|
|
if buf.is_empty() {
|
|
|
|
return Poll::Ready(Ok(0));
|
|
|
|
}
|
|
|
|
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-11-07 00:27:21 +01:00
|
|
|
let mut tx_writer = unsafe { state.tx_buf.writer() };
|
|
|
|
let n = tx_writer.push(|data| {
|
|
|
|
let n = data.len().min(buf.len());
|
|
|
|
data[..n].copy_from_slice(&buf[..n]);
|
|
|
|
n
|
|
|
|
});
|
|
|
|
if n == 0 {
|
|
|
|
state.tx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
|
2023-01-05 18:45:58 +01:00
|
|
|
// The TX interrupt only triggers when the there was data in the
|
|
|
|
// FIFO and the number of bytes drops below a threshold. When the
|
|
|
|
// FIFO was empty we have to manually pend the interrupt to shovel
|
|
|
|
// TX data from the buffer into the FIFO.
|
2023-01-01 21:34:20 +01:00
|
|
|
unsafe { T::Interrupt::steal() }.pend();
|
2022-11-07 00:27:21 +01:00
|
|
|
Poll::Ready(Ok(n))
|
|
|
|
})
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
fn flush() -> impl Future<Output = Result<(), Error>> {
|
|
|
|
poll_fn(move |cx| {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-11-07 00:27:21 +01:00
|
|
|
if !state.tx_buf.is_empty() {
|
|
|
|
state.tx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
2022-09-21 06:00:35 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
})
|
2022-09-21 06:00:35 +02:00
|
|
|
}
|
2023-02-16 08:47:22 +01:00
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
pub fn blocking_write(&mut self, buf: &[u8]) -> Result<usize, Error> {
|
2023-04-02 14:36:32 +02:00
|
|
|
if buf.is_empty() {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
2023-02-16 08:47:22 +01:00
|
|
|
loop {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2023-02-16 08:47:22 +01:00
|
|
|
let mut tx_writer = unsafe { state.tx_buf.writer() };
|
|
|
|
let n = tx_writer.push(|data| {
|
|
|
|
let n = data.len().min(buf.len());
|
|
|
|
data[..n].copy_from_slice(&buf[..n]);
|
|
|
|
n
|
|
|
|
});
|
|
|
|
|
|
|
|
if n != 0 {
|
|
|
|
// The TX interrupt only triggers when the there was data in the
|
|
|
|
// FIFO and the number of bytes drops below a threshold. When the
|
|
|
|
// FIFO was empty we have to manually pend the interrupt to shovel
|
|
|
|
// TX data from the buffer into the FIFO.
|
|
|
|
unsafe { T::Interrupt::steal() }.pend();
|
2023-03-23 14:18:19 +01:00
|
|
|
return Ok(n);
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_flush(&mut self) -> Result<(), Error> {
|
|
|
|
loop {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2023-02-16 08:47:22 +01:00
|
|
|
if state.tx_buf.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-30 08:04:21 +02:00
|
|
|
|
|
|
|
pub fn busy(&self) -> bool {
|
|
|
|
unsafe { T::regs().uartfr().read().busy() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assert a break condition after waiting for the transmit buffers to empty,
|
|
|
|
/// for the specified number of bit times. This condition must be asserted
|
|
|
|
/// for at least two frame times to be effective, `bits` will adjusted
|
|
|
|
/// according to frame size, parity, and stop bit settings to ensure this.
|
|
|
|
///
|
|
|
|
/// This method may block for a long amount of time since it has to wait
|
|
|
|
/// for the transmit fifo to empty, which may take a while on slow links.
|
|
|
|
pub async fn send_break(&mut self, bits: u32) {
|
|
|
|
let regs = T::regs();
|
|
|
|
let bits = bits.max(unsafe {
|
|
|
|
let lcr = regs.uartlcr_h().read();
|
|
|
|
let width = lcr.wlen() as u32 + 5;
|
|
|
|
let parity = lcr.pen() as u32;
|
|
|
|
let stops = 1 + lcr.stp2() as u32;
|
|
|
|
2 * (1 + width + parity + stops)
|
|
|
|
});
|
|
|
|
let divx64 = unsafe {
|
|
|
|
((regs.uartibrd().read().baud_divint() as u32) << 6) + regs.uartfbrd().read().baud_divfrac() as u32
|
|
|
|
} as u64;
|
|
|
|
let div_clk = clk_peri_freq() as u64 * 64;
|
|
|
|
let wait_usecs = (1_000_000 * bits as u64 * divx64 * 16 + div_clk - 1) / div_clk;
|
|
|
|
|
|
|
|
Self::flush().await.unwrap();
|
|
|
|
while self.busy() {}
|
|
|
|
unsafe {
|
|
|
|
regs.uartlcr_h().write_set(|w| w.set_brk(true));
|
|
|
|
}
|
|
|
|
Timer::after(Duration::from_micros(wait_usecs)).await;
|
|
|
|
unsafe {
|
|
|
|
regs.uartlcr_h().write_clear(|w| w.set_brk(true));
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
2022-09-21 06:00:35 +02:00
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
impl<'d, T: Instance> Drop for BufferedUartRx<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-11-07 00:27:21 +01:00
|
|
|
unsafe {
|
|
|
|
state.rx_buf.deinit();
|
2023-01-01 22:02:45 +01:00
|
|
|
|
|
|
|
// TX is inactive if the the buffer is not available.
|
|
|
|
// We can now unregister the interrupt handler
|
|
|
|
if state.tx_buf.len() == 0 {
|
|
|
|
T::Interrupt::steal().disable();
|
|
|
|
}
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
2022-09-21 06:00:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
impl<'d, T: Instance> Drop for BufferedUartTx<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
2023-05-01 15:22:39 +02:00
|
|
|
let state = T::buffered_state();
|
2022-08-26 09:05:12 +02:00
|
|
|
unsafe {
|
2022-11-07 00:27:21 +01:00
|
|
|
state.tx_buf.deinit();
|
2023-01-01 22:02:45 +01:00
|
|
|
|
|
|
|
// RX is inactive if the the buffer is not available.
|
|
|
|
// We can now unregister the interrupt handler
|
|
|
|
if state.rx_buf.len() == 0 {
|
|
|
|
T::Interrupt::steal().disable();
|
|
|
|
}
|
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-11-07 00:27:21 +01:00
|
|
|
pub(crate) unsafe fn on_interrupt<T: Instance>(_: *mut ()) {
|
|
|
|
let r = T::regs();
|
2023-05-01 15:22:39 +02:00
|
|
|
let s = T::buffered_state();
|
2022-09-21 06:00:35 +02:00
|
|
|
|
2022-11-07 00:27:21 +01:00
|
|
|
unsafe {
|
2023-01-05 18:45:58 +01:00
|
|
|
// Clear TX and error interrupt flags
|
|
|
|
// RX interrupt flags are cleared by reading from the FIFO.
|
2022-11-07 00:27:21 +01:00
|
|
|
let ris = r.uartris().read();
|
2023-01-05 18:45:58 +01:00
|
|
|
r.uarticr().write(|w| {
|
|
|
|
w.set_txic(ris.txris());
|
|
|
|
w.set_feic(ris.feris());
|
|
|
|
w.set_peic(ris.peris());
|
|
|
|
w.set_beic(ris.beris());
|
|
|
|
w.set_oeic(ris.oeris());
|
|
|
|
});
|
|
|
|
|
2023-01-05 22:00:44 +01:00
|
|
|
trace!("on_interrupt ris={:#X}", ris.0);
|
2022-09-21 06:00:35 +02:00
|
|
|
|
2023-01-04 16:40:54 +01:00
|
|
|
// Errors
|
2022-11-07 00:27:21 +01:00
|
|
|
if ris.feris() {
|
|
|
|
warn!("Framing error");
|
2023-01-04 16:40:54 +01:00
|
|
|
}
|
|
|
|
if ris.peris() {
|
|
|
|
warn!("Parity error");
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
|
|
|
if ris.beris() {
|
|
|
|
warn!("Break error");
|
|
|
|
}
|
|
|
|
if ris.oeris() {
|
|
|
|
warn!("Overrun error");
|
|
|
|
}
|
2022-09-21 06:00:35 +02:00
|
|
|
|
2023-01-04 16:40:54 +01:00
|
|
|
// RX
|
2022-11-07 00:27:21 +01:00
|
|
|
let mut rx_writer = s.rx_buf.writer();
|
2022-12-27 10:20:51 +01:00
|
|
|
let rx_buf = rx_writer.push_slice();
|
|
|
|
let mut n_read = 0;
|
2023-04-30 04:21:11 +02:00
|
|
|
let mut error = false;
|
2022-12-27 10:20:51 +01:00
|
|
|
for rx_byte in rx_buf {
|
|
|
|
if r.uartfr().read().rxfe() {
|
|
|
|
break;
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
2023-04-30 04:21:11 +02:00
|
|
|
let dr = r.uartdr().read();
|
|
|
|
if (dr.0 >> 8) != 0 {
|
|
|
|
s.rx_error.fetch_or((dr.0 >> 8) as u8, Ordering::Relaxed);
|
|
|
|
error = true;
|
|
|
|
// only fill the buffer with valid characters. the current character is fine
|
|
|
|
// if the error is an overrun, but if we add it to the buffer we'll report
|
|
|
|
// the overrun one character too late. drop it instead and pretend we were
|
|
|
|
// a bit slower at draining the rx fifo than we actually were.
|
|
|
|
// this is consistent with blocking uart error reporting.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*rx_byte = dr.data();
|
2022-12-27 10:20:51 +01:00
|
|
|
n_read += 1;
|
|
|
|
}
|
|
|
|
if n_read > 0 {
|
|
|
|
rx_writer.push_done(n_read);
|
2022-11-07 00:27:21 +01:00
|
|
|
s.rx_waker.wake();
|
2023-04-30 04:21:11 +02:00
|
|
|
} else if error {
|
|
|
|
s.rx_waker.wake();
|
2022-11-07 00:27:21 +01:00
|
|
|
}
|
2023-04-30 04:21:11 +02:00
|
|
|
// Disable any further RX interrupts when the buffer becomes full or
|
|
|
|
// errors have occured. this lets us buffer additional errors in the
|
|
|
|
// fifo without needing more error storage locations, and most applications
|
|
|
|
// will want to do a full reset of their uart state anyway once an error
|
|
|
|
// has happened.
|
|
|
|
if s.rx_buf.is_full() || error {
|
2023-01-05 18:45:58 +01:00
|
|
|
r.uartimsc().write_clear(|w| {
|
|
|
|
w.set_rxim(true);
|
|
|
|
w.set_rtim(true);
|
|
|
|
});
|
|
|
|
}
|
2022-11-07 00:27:21 +01:00
|
|
|
|
|
|
|
// TX
|
|
|
|
let mut tx_reader = s.tx_buf.reader();
|
2022-12-27 10:20:51 +01:00
|
|
|
let tx_buf = tx_reader.pop_slice();
|
2023-01-04 16:40:54 +01:00
|
|
|
let mut n_written = 0;
|
|
|
|
for tx_byte in tx_buf.iter_mut() {
|
|
|
|
if r.uartfr().read().txff() {
|
|
|
|
break;
|
2022-12-27 10:20:51 +01:00
|
|
|
}
|
2023-01-04 16:40:54 +01:00
|
|
|
r.uartdr().write(|w| w.set_data(*tx_byte));
|
|
|
|
n_written += 1;
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
2023-01-04 16:40:54 +01:00
|
|
|
if n_written > 0 {
|
|
|
|
tx_reader.pop_done(n_written);
|
|
|
|
s.tx_waker.wake();
|
|
|
|
}
|
2023-01-05 18:45:58 +01:00
|
|
|
// The TX interrupt only triggers once when the FIFO threshold is
|
|
|
|
// crossed. No need to disable it when the buffer becomes empty
|
|
|
|
// as it does re-trigger anymore once we have cleared it.
|
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-11-21 23:31:31 +01:00
|
|
|
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
BufferedUartRx::<'d, T>::read(buf).await
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Read for BufferedUartRx<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self::read(buf).await
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::BufRead for BufferedUart<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
BufferedUartRx::<'d, T>::fill_buf().await
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
2022-11-07 00:27:21 +01:00
|
|
|
BufferedUartRx::<'d, T>::consume(amt)
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 05:51:31 +02:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::BufRead for BufferedUartRx<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self::fill_buf().await
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self::consume(amt)
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::asynch::Write for BufferedUart<'d, T> {
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
BufferedUartTx::<'d, T>::write(buf).await
|
2022-09-09 10:36:27 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
BufferedUartTx::<'d, T>::flush().await
|
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-11-21 23:31:31 +01:00
|
|
|
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self::write(buf).await
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
2022-11-07 00:27:21 +01:00
|
|
|
Self::flush().await
|
2022-08-26 09:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-16 08:47:22 +01:00
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
impl<'d, T: Instance + 'd> embedded_io::blocking::Read for BufferedUart<'d, T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
|
|
|
self.rx.blocking_read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::blocking::Read for BufferedUartRx<'d, T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
|
|
|
self.blocking_read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::blocking::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
|
|
|
self.tx.blocking_write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance + 'd> embedded_io::blocking::Write for BufferedUartTx<'d, T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
|
|
|
self.blocking_write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 08:47:22 +01:00
|
|
|
mod eh02 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUartRx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
|
|
|
|
let r = T::regs();
|
|
|
|
unsafe {
|
|
|
|
if r.uartfr().read().rxfe() {
|
|
|
|
return Err(nb::Error::WouldBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dr = r.uartdr().read();
|
|
|
|
|
|
|
|
if dr.oe() {
|
|
|
|
Err(nb::Error::Other(Error::Overrun))
|
|
|
|
} else if dr.be() {
|
|
|
|
Err(nb::Error::Other(Error::Break))
|
|
|
|
} else if dr.pe() {
|
|
|
|
Err(nb::Error::Other(Error::Parity))
|
|
|
|
} else if dr.fe() {
|
|
|
|
Err(nb::Error::Other(Error::Framing))
|
|
|
|
} else {
|
|
|
|
Ok(dr.data())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUartTx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
while !buffer.is_empty() {
|
|
|
|
match self.blocking_write(buffer) {
|
|
|
|
Ok(0) => panic!("zero-length write."),
|
|
|
|
Ok(n) => buffer = &buffer[n..],
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bflush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_02::serial::Read<u8> for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
|
|
|
|
embedded_hal_02::serial::Read::read(&mut self.rx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
2023-03-23 14:18:19 +01:00
|
|
|
fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
while !buffer.is_empty() {
|
|
|
|
match self.blocking_write(buffer) {
|
|
|
|
Ok(0) => panic!("zero-length write."),
|
|
|
|
Ok(n) => buffer = &buffer[n..],
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bflush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "unstable-traits")]
|
|
|
|
mod eh1 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_1::serial::ErrorType for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_1::serial::ErrorType for BufferedUartTx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_1::serial::ErrorType for BufferedUartRx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_nb::serial::Read for BufferedUartRx<'d, T> {
|
|
|
|
fn read(&mut self) -> nb::Result<u8, Self::Error> {
|
|
|
|
embedded_hal_02::serial::Read::read(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_1::serial::Write for BufferedUartTx<'d, T> {
|
|
|
|
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
2023-03-23 14:26:37 +01:00
|
|
|
self.blocking_write(buffer).map(drop)
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_nb::serial::Write for BufferedUartTx<'d, T> {
|
|
|
|
fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
|
2023-03-23 14:26:37 +01:00
|
|
|
self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> nb::Result<(), Self::Error> {
|
|
|
|
self.blocking_flush().map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_nb::serial::Read for BufferedUart<'d, T> {
|
|
|
|
fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
|
|
|
|
embedded_hal_02::serial::Read::read(&mut self.rx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_1::serial::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
2023-03-23 14:26:37 +01:00
|
|
|
self.blocking_write(buffer).map(drop)
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal_nb::serial::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
|
2023-03-23 14:26:37 +01:00
|
|
|
self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
|
2023-02-16 08:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> nb::Result<(), Self::Error> {
|
|
|
|
self.blocking_flush().map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|