2022-11-21 23:31:31 +01:00
|
|
|
use core::future::poll_fn;
|
2023-03-31 10:43:30 +02:00
|
|
|
use core::slice;
|
2022-05-04 20:48:37 +02:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
use embassy_cortex_m::interrupt::Interrupt;
|
|
|
|
use embassy_hal_common::atomic_ring_buffer::RingBuffer;
|
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2022-05-04 20:48:37 +02:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
pub struct State {
|
|
|
|
rx_waker: AtomicWaker,
|
|
|
|
rx_buf: RingBuffer,
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
tx_waker: AtomicWaker,
|
|
|
|
tx_buf: RingBuffer,
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl State {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
rx_buf: RingBuffer::new(),
|
|
|
|
tx_buf: RingBuffer::new(),
|
|
|
|
rx_waker: AtomicWaker::new(),
|
|
|
|
tx_waker: AtomicWaker::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
pub struct BufferedUart<'d, T: BasicInstance> {
|
2023-03-31 10:43:30 +02:00
|
|
|
rx: BufferedUartRx<'d, T>,
|
|
|
|
tx: BufferedUartTx<'d, T>,
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
pub struct BufferedUartTx<'d, T: BasicInstance> {
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
pub struct BufferedUartRx<'d, T: BasicInstance> {
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
impl<'d, T: BasicInstance> BufferedUart<'d, T> {
|
2022-06-09 21:28:13 +02:00
|
|
|
pub fn new(
|
2022-10-27 11:03:37 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
2022-10-26 18:58:22 +02:00
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
2022-05-04 20:48:37 +02:00
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
2022-10-26 18:58:22 +02:00
|
|
|
config: Config,
|
2022-05-04 20:48:37 +02:00
|
|
|
) -> BufferedUart<'d, T> {
|
2022-10-27 11:03:37 +02:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config)
|
2022-10-27 11:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_rtscts(
|
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
rts: impl Peripheral<P = impl RtsPin<T>> + 'd,
|
|
|
|
cts: impl Peripheral<P = impl CtsPin<T>> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> BufferedUart<'d, T> {
|
|
|
|
into_ref!(cts, rts);
|
2022-10-26 18:58:22 +02:00
|
|
|
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2022-10-27 11:03:37 +02:00
|
|
|
unsafe {
|
|
|
|
rts.set_as_af(rts.af_num(), AFType::OutputPushPull);
|
|
|
|
cts.set_as_af(cts.af_num(), AFType::Input);
|
|
|
|
T::regs().cr3().write(|w| {
|
|
|
|
w.set_rtse(true);
|
|
|
|
w.set_ctse(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config)
|
2022-10-27 11:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 14:26:09 +01:00
|
|
|
#[cfg(not(usart_v1))]
|
|
|
|
pub fn new_with_de(
|
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
de: impl Peripheral<P = impl DePin<T>> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> BufferedUart<'d, T> {
|
|
|
|
into_ref!(de);
|
|
|
|
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
de.set_as_af(de.af_num(), AFType::OutputPushPull);
|
|
|
|
T::regs().cr3().write(|w| {
|
|
|
|
w.set_dem(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config)
|
2022-12-09 14:26:09 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 11:03:37 +02:00
|
|
|
fn new_inner(
|
|
|
|
_peri: impl Peripheral<P = T> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
irq: impl Peripheral<P = T::Interrupt> + 'd,
|
|
|
|
tx_buffer: &'d mut [u8],
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
config: Config,
|
|
|
|
) -> BufferedUart<'d, T> {
|
|
|
|
into_ref!(_peri, rx, tx, irq);
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
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) };
|
2022-10-26 18:58:22 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
let r = T::regs();
|
2022-06-09 21:28:13 +02:00
|
|
|
unsafe {
|
2022-10-26 18:58:22 +02:00
|
|
|
rx.set_as_af(rx.af_num(), AFType::Input);
|
|
|
|
tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
|
2022-10-28 09:32:05 +02:00
|
|
|
}
|
2022-10-26 18:58:22 +02:00
|
|
|
|
2022-10-28 09:32:05 +02:00
|
|
|
configure(r, &config, T::frequency(), T::MULTIPLIER, true, true);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
r.cr1().modify(|w| {
|
2022-11-30 14:11:32 +01:00
|
|
|
#[cfg(lpuart_v2)]
|
|
|
|
w.set_fifoen(true);
|
|
|
|
|
2022-06-09 21:28:13 +02:00
|
|
|
w.set_rxneie(true);
|
|
|
|
w.set_idleie(true);
|
|
|
|
});
|
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
irq.set_handler(on_interrupt::<T>);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
Self {
|
|
|
|
rx: BufferedUartRx { phantom: PhantomData },
|
|
|
|
tx: BufferedUartTx { phantom: PhantomData },
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
pub fn split(self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) {
|
|
|
|
(self.rx, self.tx)
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
2023-03-31 10:43:30 +02:00
|
|
|
}
|
2022-09-26 15:28:09 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> BufferedUartRx<'d, T> {
|
|
|
|
async fn read(&self, buf: &mut [u8]) -> Result<usize, Error> {
|
2022-09-26 15:28:09 +02:00
|
|
|
poll_fn(move |cx| {
|
2023-03-31 10:43:30 +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
|
2022-09-26 15:28:09 +02:00
|
|
|
});
|
2023-03-31 10:43:30 +02:00
|
|
|
if n == 0 {
|
|
|
|
state.rx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
// FIXME:
|
|
|
|
// (Re-)Enable the interrupt to receive more data in case it was
|
|
|
|
// disabled because the buffer was full.
|
|
|
|
// let regs = T::regs();
|
|
|
|
// unsafe {
|
|
|
|
// regs.uartimsc().write_set(|w| {
|
|
|
|
// w.set_rxim(true);
|
|
|
|
// w.set_rtim(true);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
Poll::Ready(Ok(n))
|
2022-09-26 15:28:09 +02:00
|
|
|
})
|
|
|
|
.await
|
2023-03-31 10:43:30 +02:00
|
|
|
|
|
|
|
// poll_fn(move |cx| {
|
|
|
|
// let state = T::buffered_state();
|
|
|
|
|
|
|
|
// let mut do_pend = false;
|
|
|
|
// compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
// // We have data ready in buffer? Return it.
|
|
|
|
// let data = state.rx_buf.pop_buf();
|
|
|
|
// if !data.is_empty() {
|
|
|
|
// let len = data.len().min(buf.len());
|
|
|
|
// buf[..len].copy_from_slice(&data[..len]);
|
|
|
|
|
|
|
|
// if state.rx_buf.is_full() {
|
|
|
|
// do_pend = true;
|
|
|
|
// }
|
|
|
|
// state.rx_buf.pop(len);
|
|
|
|
|
|
|
|
// return Poll::Ready(Ok(len));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// state.rx_waker.register(cx.waker());
|
|
|
|
|
|
|
|
// if do_pend {
|
|
|
|
// inner.pend();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Poll::Pending
|
|
|
|
// })
|
|
|
|
// .await
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
fn blocking_read(&self, buf: &mut [u8]) -> Result<usize, Error> {
|
2023-03-28 14:28:44 +02:00
|
|
|
loop {
|
2023-03-31 10:43:30 +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
|
2023-03-28 14:28:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if n > 0 {
|
2023-03-31 10:43:30 +02:00
|
|
|
// FIXME:
|
|
|
|
// (Re-)Enable the interrupt to receive more data in case it was
|
|
|
|
// disabled because the buffer was full.
|
|
|
|
// let regs = T::regs();
|
|
|
|
// unsafe {
|
|
|
|
// regs.uartimsc().write_set(|w| {
|
|
|
|
// w.set_rxim(true);
|
|
|
|
// w.set_rtim(true);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
2023-03-28 14:28:44 +02:00
|
|
|
return Ok(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
async fn fill_buf(&self) -> Result<&[u8], Error> {
|
2022-09-26 15:28:09 +02:00
|
|
|
poll_fn(move |cx| {
|
2023-03-31 10:43:30 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
let mut rx_reader = unsafe { state.rx_buf.reader() };
|
|
|
|
let (p, n) = rx_reader.pop_buf();
|
|
|
|
if n == 0 {
|
|
|
|
state.rx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
let buf = unsafe { slice::from_raw_parts(p, n) };
|
|
|
|
Poll::Ready(Ok(buf))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
2022-09-26 15:28:09 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
fn consume(&self, amt: usize) {
|
|
|
|
let state = T::buffered_state();
|
|
|
|
let mut rx_reader = unsafe { state.rx_buf.reader() };
|
|
|
|
let full = state.rx_buf.is_full();
|
|
|
|
rx_reader.pop_done(amt);
|
|
|
|
if full {
|
|
|
|
unsafe { T::Interrupt::steal().pend() };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 15:28:09 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> BufferedUartTx<'d, T> {
|
|
|
|
async fn write(&self, buf: &[u8]) -> Result<usize, Error> {
|
|
|
|
poll_fn(move |cx| {
|
|
|
|
let state = T::buffered_state();
|
|
|
|
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
|
2022-09-26 15:28:09 +02:00
|
|
|
});
|
2023-03-31 10:43:30 +02:00
|
|
|
if n == 0 {
|
|
|
|
state.tx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
2023-03-31 10:43:30 +02: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.
|
|
|
|
unsafe { T::Interrupt::steal() }.pend();
|
|
|
|
Poll::Ready(Ok(n))
|
2022-09-26 15:28:09 +02:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
async fn flush(&self) -> Result<(), Error> {
|
2022-09-26 15:28:09 +02:00
|
|
|
poll_fn(move |cx| {
|
2023-03-31 10:43:30 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
if !state.tx_buf.is_empty() {
|
|
|
|
state.tx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
2022-09-26 15:28:09 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
Poll::Ready(Ok(()))
|
2022-09-26 15:28:09 +02:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
fn blocking_write(&self, buf: &[u8]) -> Result<usize, Error> {
|
2023-03-28 14:28:44 +02:00
|
|
|
loop {
|
2023-03-31 10:43:30 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
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
|
2023-03-28 14:28:44 +02:00
|
|
|
});
|
2023-03-31 10:43:30 +02:00
|
|
|
|
2023-03-28 14:28:44 +02:00
|
|
|
if n != 0 {
|
2023-03-31 10:43:30 +02: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.
|
|
|
|
unsafe { T::Interrupt::steal() }.pend();
|
2023-03-28 14:28:44 +02:00
|
|
|
return Ok(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
fn blocking_flush(&self) -> Result<(), Error> {
|
2023-03-28 14:28:44 +02:00
|
|
|
loop {
|
2023-03-31 10:43:30 +02:00
|
|
|
let state = T::buffered_state();
|
|
|
|
if state.tx_buf.is_empty() {
|
2023-03-28 14:28:44 +02:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 10:43:30 +02:00
|
|
|
}
|
2023-03-28 14:28:44 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> Drop for BufferedUartRx<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let state = T::buffered_state();
|
|
|
|
unsafe {
|
|
|
|
state.rx_buf.deinit();
|
2022-09-26 15:28:09 +02:00
|
|
|
|
2023-03-31 10:43:30 +02: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-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> Drop for BufferedUartTx<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let state = T::buffered_state();
|
2022-05-04 20:48:37 +02:00
|
|
|
unsafe {
|
2023-03-31 10:43:30 +02:00
|
|
|
state.tx_buf.deinit();
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02: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-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
unsafe fn on_interrupt<T: BasicInstance>(_: *mut ()) {
|
|
|
|
let r = T::regs();
|
|
|
|
let state = T::buffered_state();
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
// RX
|
|
|
|
unsafe {
|
|
|
|
let sr = sr(r).read();
|
|
|
|
clear_interrupt_flags(r, sr);
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
if sr.rxne() {
|
|
|
|
if sr.pe() {
|
|
|
|
warn!("Parity error");
|
|
|
|
}
|
|
|
|
if sr.fe() {
|
|
|
|
warn!("Framing error");
|
|
|
|
}
|
|
|
|
if sr.ne() {
|
|
|
|
warn!("Noise error");
|
|
|
|
}
|
|
|
|
if sr.ore() {
|
|
|
|
warn!("Overrun error");
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
let mut rx_writer = state.rx_buf.writer();
|
|
|
|
let buf = rx_writer.push_slice();
|
|
|
|
if !buf.is_empty() {
|
|
|
|
// This read also clears the error and idle interrupt flags on v1.
|
|
|
|
buf[0] = rdr(r).read_volatile();
|
|
|
|
rx_writer.push_done(1);
|
|
|
|
} else {
|
|
|
|
// FIXME: Should we disable any further RX interrupts when the buffer becomes full.
|
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
if state.rx_buf.is_full() {
|
|
|
|
state.rx_waker.wake();
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
if sr.idle() {
|
|
|
|
state.rx_waker.wake();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TX
|
|
|
|
unsafe {
|
|
|
|
if sr(r).read().txe() {
|
|
|
|
let mut tx_reader = state.tx_buf.reader();
|
|
|
|
let buf = tx_reader.pop_slice();
|
|
|
|
if !buf.is_empty() {
|
|
|
|
r.cr1().modify(|w| {
|
|
|
|
w.set_txeie(true);
|
|
|
|
});
|
|
|
|
tdr(r).write_volatile(buf[0].into());
|
|
|
|
tx_reader.pop_done(1);
|
|
|
|
state.tx_waker.wake();
|
|
|
|
} else {
|
|
|
|
// Disable interrupt until we have something to transmit again
|
|
|
|
r.cr1().modify(|w| {
|
|
|
|
w.set_txeie(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl embedded_io::Error for Error {
|
|
|
|
fn kind(&self) -> embedded_io::ErrorKind {
|
|
|
|
embedded_io::ErrorKind::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
impl<'d, T: BasicInstance> embedded_io::Io for BufferedUart<'d, T> {
|
2022-05-04 20:48:37 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> embedded_io::Io for BufferedUartRx<'d, T> {
|
2022-09-26 15:28:09 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> embedded_io::Io for BufferedUartTx<'d, T> {
|
2022-09-26 15:28:09 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.rx.read(buf).await
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::read(self, buf).await
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.rx.fill_buf().await
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
2022-05-26 14:02:55 +03:00
|
|
|
|
2022-09-26 15:28:09 +02:00
|
|
|
fn consume(&mut self, amt: usize) {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.rx.consume(amt)
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 14:02:55 +03:00
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::fill_buf(self).await
|
2022-05-26 14:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&mut self, amt: usize) {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::consume(self, amt)
|
2022-05-26 14:02:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:17:03 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.tx.write(buf).await
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
2022-05-04 20:48:37 +02:00
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.tx.flush().await
|
2022-09-26 15:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> 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> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::write(self, buf).await
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:31:31 +01:00
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::flush(self).await
|
2022-05-04 20:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-28 14:28:44 +02:00
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUart<'d, T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.rx.blocking_read(buf)
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUartRx<'d, T> {
|
2023-03-28 14:28:44 +02:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.blocking_read(buf)
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.tx.blocking_write(buf)
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
self.tx.blocking_flush()
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:43:30 +02:00
|
|
|
impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUartTx<'d, T> {
|
2023-03-28 14:28:44 +02:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::blocking_write(self, buf)
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
2023-03-31 10:43:30 +02:00
|
|
|
Self::blocking_flush(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod eh02 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> 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 {
|
|
|
|
let sr = sr(r).read();
|
|
|
|
if sr.pe() {
|
|
|
|
rdr(r).read_volatile();
|
|
|
|
Err(nb::Error::Other(Error::Parity))
|
|
|
|
} else if sr.fe() {
|
|
|
|
rdr(r).read_volatile();
|
|
|
|
Err(nb::Error::Other(Error::Framing))
|
|
|
|
} else if sr.ne() {
|
|
|
|
rdr(r).read_volatile();
|
|
|
|
Err(nb::Error::Other(Error::Noise))
|
|
|
|
} else if sr.ore() {
|
|
|
|
rdr(r).read_volatile();
|
|
|
|
Err(nb::Error::Other(Error::Overrun))
|
|
|
|
} else if sr.rxne() {
|
|
|
|
Ok(rdr(r).read_volatile())
|
|
|
|
} else {
|
|
|
|
Err(nb::Error::WouldBlock)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUartTx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bflush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> 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: BasicInstance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
while !buffer.is_empty() {
|
|
|
|
match self.tx.blocking_write(buffer) {
|
|
|
|
Ok(0) => panic!("zero-length write."),
|
|
|
|
Ok(n) => buffer = &buffer[n..],
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bflush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "unstable-traits")]
|
|
|
|
mod eh1 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUart<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUartTx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUartRx<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> 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: BasicInstance> embedded_hal_1::serial::Write for BufferedUartTx<'d, T> {
|
|
|
|
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_write(buffer).map(drop)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_nb::serial::Write for BufferedUartTx<'d, T> {
|
|
|
|
fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
|
|
|
|
self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> nb::Result<(), Self::Error> {
|
|
|
|
self.blocking_flush().map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> 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: BasicInstance> embedded_hal_1::serial::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_write(buffer).map(drop)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_nb::serial::Write for BufferedUart<'d, T> {
|
|
|
|
fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_write(&[char]).map(drop).map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> nb::Result<(), Self::Error> {
|
|
|
|
self.tx.blocking_flush().map_err(nb::Error::Other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(
|
|
|
|
feature = "unstable-traits",
|
|
|
|
feature = "nightly",
|
|
|
|
feature = "_todo_embedded_hal_serial"
|
|
|
|
))]
|
|
|
|
mod eha {
|
|
|
|
use core::future::Future;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_async::serial::Write for BufferedUartTx<'d, T> {
|
|
|
|
async fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
Self::write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Self::flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_async::serial::Read for BufferedUartRx<'d, T> {
|
|
|
|
async fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
Self::read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_async::serial::Write for BufferedUart<'d, T> {
|
|
|
|
async fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.tx.write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn flush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
self.tx.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: BasicInstance> embedded_hal_async::serial::Read for BufferedUart<'d, T> {
|
|
|
|
async fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.rx.read(buf)
|
|
|
|
}
|
2023-03-28 14:28:44 +02:00
|
|
|
}
|
|
|
|
}
|