2021-03-22 01:10:59 +00:00
|
|
|
//! Async UART
|
2020-12-23 15:18:29 +00:00
|
|
|
|
|
|
|
use core::future::Future;
|
2021-03-22 00:15:44 +00:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::pin::Pin;
|
2021-03-26 22:22:06 +00:00
|
|
|
use core::sync::atomic::{compiler_fence, AtomicBool, Ordering};
|
2021-03-22 00:15:44 +00:00
|
|
|
use core::task::Poll;
|
|
|
|
use embassy::traits::uart::{Error, Read, Write};
|
2021-03-26 22:22:06 +00:00
|
|
|
use embassy::util::{AtomicWaker, OnDrop, PeripheralBorrow};
|
|
|
|
use embassy_extras::peripheral_shared::{Peripheral, PeripheralState};
|
2021-03-22 00:15:44 +00:00
|
|
|
use embassy_extras::unborrow;
|
|
|
|
use futures::future::poll_fn;
|
2020-12-23 15:18:29 +00:00
|
|
|
|
|
|
|
use crate::fmt::{assert, *};
|
2021-03-22 01:10:59 +00:00
|
|
|
use crate::gpio::sealed::Pin as _;
|
|
|
|
use crate::gpio::{OptionalPin as GpioOptionalPin, Pin as GpioPin};
|
2020-12-23 15:18:29 +00:00
|
|
|
use crate::hal::pac;
|
|
|
|
use crate::hal::target_constants::EASY_DMA_SIZE;
|
2021-03-07 23:15:40 +00:00
|
|
|
use crate::interrupt;
|
2021-02-26 00:55:27 +00:00
|
|
|
use crate::interrupt::Interrupt;
|
2021-03-22 00:15:44 +00:00
|
|
|
use crate::peripherals;
|
2020-12-23 15:18:29 +00:00
|
|
|
|
|
|
|
// Re-export SVD variants to allow user to directly set values.
|
|
|
|
pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Config {
|
|
|
|
pub parity: Parity,
|
|
|
|
pub baudrate: Baudrate,
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
parity: Parity::EXCLUDED,
|
|
|
|
baudrate: Baudrate::BAUD115200,
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
struct State<T: Instance> {
|
|
|
|
peri: T,
|
|
|
|
did_stoprx: AtomicBool,
|
|
|
|
did_stoptx: AtomicBool,
|
|
|
|
|
|
|
|
endrx_waker: AtomicWaker,
|
|
|
|
endtx_waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
/// Interface to the UARTE peripheral
|
|
|
|
pub struct Uarte<'d, T: Instance> {
|
2021-03-26 22:22:06 +00:00
|
|
|
inner: Peripheral<State<T>>,
|
2021-03-22 00:15:44 +00:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> Uarte<'d, T> {
|
2020-12-23 15:18:29 +00:00
|
|
|
/// Creates the interface to a UARTE instance.
|
|
|
|
/// Sets the baud rate, parity and assigns the pins to the UARTE peripheral.
|
|
|
|
///
|
2021-03-07 23:15:40 +00:00
|
|
|
/// # Safety
|
2020-12-23 15:18:29 +00:00
|
|
|
///
|
|
|
|
/// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms)
|
|
|
|
/// on stack allocated buffers which which have been passed to [`send()`](Uarte::send)
|
|
|
|
/// or [`receive`](Uarte::receive).
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub unsafe fn new(
|
2021-03-22 00:15:44 +00:00
|
|
|
uarte: impl PeripheralBorrow<Target = T> + 'd,
|
|
|
|
irq: impl PeripheralBorrow<Target = T::Interrupt> + 'd,
|
|
|
|
rxd: impl PeripheralBorrow<Target = impl GpioPin> + 'd,
|
|
|
|
txd: impl PeripheralBorrow<Target = impl GpioPin> + 'd,
|
2021-03-22 01:10:59 +00:00
|
|
|
cts: impl PeripheralBorrow<Target = impl GpioOptionalPin> + 'd,
|
|
|
|
rts: impl PeripheralBorrow<Target = impl GpioOptionalPin> + 'd,
|
2021-03-22 00:15:44 +00:00
|
|
|
config: Config,
|
2020-12-23 15:18:29 +00:00
|
|
|
) -> Self {
|
2021-03-22 00:15:44 +00:00
|
|
|
unborrow!(uarte, irq, rxd, txd, cts, rts);
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
let r = uarte.regs();
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
assert!(r.enable.read().enable().is_disabled());
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 01:10:59 +00:00
|
|
|
rxd.conf().write(|w| w.input().connect().drive().h0h1());
|
|
|
|
r.psel.rxd.write(|w| unsafe { w.bits(rxd.psel_bits()) });
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
txd.set_high();
|
|
|
|
txd.conf().write(|w| w.dir().output().drive().h0h1());
|
|
|
|
r.psel.txd.write(|w| unsafe { w.bits(txd.psel_bits()) });
|
2021-03-22 01:10:59 +00:00
|
|
|
|
|
|
|
if let Some(pin) = rts.pin_mut() {
|
|
|
|
pin.set_high();
|
|
|
|
pin.conf().write(|w| w.dir().output().drive().h0h1());
|
|
|
|
}
|
|
|
|
r.psel.cts.write(|w| unsafe { w.bits(cts.psel_bits()) });
|
|
|
|
|
|
|
|
if let Some(pin) = cts.pin_mut() {
|
|
|
|
pin.conf().write(|w| w.input().connect().drive().h0h1());
|
|
|
|
}
|
|
|
|
r.psel.rts.write(|w| unsafe { w.bits(rts.psel_bits()) });
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
r.baudrate.write(|w| w.baudrate().variant(config.baudrate));
|
|
|
|
r.config.write(|w| w.parity().variant(config.parity));
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
// Disable all interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
// Enable
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
Self {
|
2021-03-26 22:22:06 +00:00
|
|
|
inner: Peripheral::new(
|
|
|
|
irq,
|
|
|
|
State {
|
|
|
|
did_stoprx: AtomicBool::new(false),
|
|
|
|
did_stoptx: AtomicBool::new(false),
|
|
|
|
peri: uarte,
|
|
|
|
endrx_waker: AtomicWaker::new(),
|
|
|
|
endtx_waker: AtomicWaker::new(),
|
|
|
|
},
|
|
|
|
),
|
2021-03-22 00:15:44 +00:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
2021-03-26 22:22:06 +00:00
|
|
|
|
|
|
|
fn inner(self: Pin<&mut Self>) -> Pin<&mut Peripheral<State<T>>> {
|
|
|
|
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
|
|
|
|
}
|
2021-03-22 01:10:59 +00:00
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
impl<T: Instance> PeripheralState for State<T> {
|
|
|
|
type Interrupt = T::Interrupt;
|
|
|
|
|
|
|
|
fn on_interrupt(&self) {
|
|
|
|
info!("irq");
|
|
|
|
|
2021-03-22 01:10:59 +00:00
|
|
|
let r = self.peri.regs();
|
2021-03-26 22:22:06 +00:00
|
|
|
if r.events_endrx.read().bits() != 0 {
|
|
|
|
self.endrx_waker.wake();
|
|
|
|
r.intenclr.write(|w| w.endrx().clear());
|
|
|
|
}
|
|
|
|
if r.events_endtx.read().bits() != 0 {
|
|
|
|
self.endtx_waker.wake();
|
|
|
|
r.intenclr.write(|w| w.endtx().clear());
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.events_rxto.read().bits() != 0 {
|
|
|
|
r.intenclr.write(|w| w.rxto().clear());
|
|
|
|
}
|
|
|
|
if r.events_txstopped.read().bits() != 0 {
|
|
|
|
r.intenclr.write(|w| w.txstopped().clear());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Instance> Drop for Uarte<'a, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
info!("uarte drop");
|
|
|
|
|
|
|
|
let s = unsafe { Pin::new_unchecked(&mut self.inner) }.state();
|
|
|
|
let r = s.peri.regs();
|
|
|
|
|
|
|
|
let did_stoprx = s.did_stoprx.load(Ordering::Relaxed);
|
|
|
|
let did_stoptx = s.did_stoptx.load(Ordering::Relaxed);
|
|
|
|
info!("did_stoprx {} did_stoptx {}", did_stoprx, did_stoptx);
|
|
|
|
|
|
|
|
// Wait for rxto or txstopped, if needed.
|
|
|
|
r.intenset.write(|w| w.rxto().set().txstopped().set());
|
|
|
|
while (did_stoprx && r.events_rxto.read().bits() == 0)
|
|
|
|
|| (did_stoptx && r.events_txstopped.read().bits() == 0)
|
|
|
|
{
|
|
|
|
info!("uarte drop: wfe");
|
|
|
|
cortex_m::asm::wfe();
|
|
|
|
}
|
|
|
|
|
|
|
|
cortex_m::asm::sev();
|
|
|
|
|
|
|
|
// Finally we can disable!
|
2021-03-22 01:10:59 +00:00
|
|
|
r.enable.write(|w| w.enable().disabled());
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
info!("uarte drop: done");
|
|
|
|
|
|
|
|
// TODO: disable pins
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
impl<'d, T: Instance> Read for Uarte<'d, T> {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a;
|
2021-01-02 18:59:37 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
fn read<'a>(mut self: Pin<&'a mut Self>, rx_buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
self.as_mut().inner().register_interrupt();
|
2021-03-22 00:15:44 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
async move {
|
2021-03-22 00:15:44 +00:00
|
|
|
let ptr = rx_buffer.as_ptr();
|
|
|
|
let len = rx_buffer.len();
|
|
|
|
assert!(len <= EASY_DMA_SIZE);
|
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
let s = self.inner().state();
|
|
|
|
let r = s.peri.regs();
|
2021-03-22 00:15:44 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
let did_stoprx = &s.did_stoprx;
|
2021-03-22 00:15:44 +00:00
|
|
|
let drop = OnDrop::new(move || {
|
|
|
|
info!("read drop: stopping");
|
|
|
|
|
|
|
|
r.intenclr.write(|w| w.endrx().clear());
|
2021-03-26 22:22:06 +00:00
|
|
|
r.events_rxto.reset();
|
2021-03-22 00:15:44 +00:00
|
|
|
r.tasks_stoprx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
while r.events_endrx.read().bits() == 0 {}
|
2021-03-26 22:22:06 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
info!("read drop: stopped");
|
2021-03-26 22:22:06 +00:00
|
|
|
did_stoprx.store(true, Ordering::Relaxed);
|
2021-03-22 00:15:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
r.rxd.ptr.write(|w| unsafe { w.ptr().bits(ptr as u32) });
|
|
|
|
r.rxd.maxcnt.write(|w| unsafe { w.maxcnt().bits(len as _) });
|
|
|
|
|
|
|
|
r.events_endrx.reset();
|
|
|
|
r.intenset.write(|w| w.endrx().set());
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
trace!("startrx");
|
|
|
|
r.tasks_startrx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
poll_fn(|cx| {
|
2021-03-26 22:22:06 +00:00
|
|
|
s.endrx_waker.register(cx.waker());
|
2021-03-22 00:15:44 +00:00
|
|
|
if r.events_endrx.read().bits() != 0 {
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-03-26 22:22:06 +00:00
|
|
|
s.did_stoprx.store(false, Ordering::Relaxed);
|
2021-03-22 00:15:44 +00:00
|
|
|
drop.defuse();
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-02 18:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 00:15:44 +00:00
|
|
|
}
|
2021-01-02 18:59:37 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
impl<'d, T: Instance> Write for Uarte<'d, T> {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Error>> + 'a;
|
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
fn write<'a>(mut self: Pin<&'a mut Self>, tx_buffer: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
self.as_mut().inner().register_interrupt();
|
2021-03-22 00:15:44 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
async move {
|
2021-03-22 00:15:44 +00:00
|
|
|
let ptr = tx_buffer.as_ptr();
|
|
|
|
let len = tx_buffer.len();
|
|
|
|
assert!(len <= EASY_DMA_SIZE);
|
|
|
|
// TODO: panic if buffer is not in SRAM
|
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
let s = self.inner().state();
|
|
|
|
let r = s.peri.regs();
|
2021-03-22 00:15:44 +00:00
|
|
|
|
2021-03-26 22:22:06 +00:00
|
|
|
let did_stoptx = &s.did_stoptx;
|
2021-03-22 00:15:44 +00:00
|
|
|
let drop = OnDrop::new(move || {
|
|
|
|
info!("write drop: stopping");
|
|
|
|
|
|
|
|
r.intenclr.write(|w| w.endtx().clear());
|
2021-03-26 22:22:06 +00:00
|
|
|
r.events_txstopped.reset();
|
2021-03-22 00:15:44 +00:00
|
|
|
r.tasks_stoptx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
// TX is stopped almost instantly, spinning is fine.
|
|
|
|
while r.events_endtx.read().bits() == 0 {}
|
|
|
|
info!("write drop: stopped");
|
2021-03-26 22:22:06 +00:00
|
|
|
did_stoptx.store(true, Ordering::Relaxed);
|
2021-03-22 00:15:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
r.txd.ptr.write(|w| unsafe { w.ptr().bits(ptr as u32) });
|
|
|
|
r.txd.maxcnt.write(|w| unsafe { w.maxcnt().bits(len as _) });
|
|
|
|
|
|
|
|
r.events_endtx.reset();
|
|
|
|
r.intenset.write(|w| w.endtx().set());
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
trace!("starttx");
|
|
|
|
r.tasks_starttx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
poll_fn(|cx| {
|
2021-03-26 22:22:06 +00:00
|
|
|
s.endtx_waker.register(cx.waker());
|
2021-03-22 00:15:44 +00:00
|
|
|
if r.events_endtx.read().bits() != 0 {
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-03-26 22:22:06 +00:00
|
|
|
s.did_stoptx.store(false, Ordering::Relaxed);
|
2021-03-22 00:15:44 +00:00
|
|
|
drop.defuse();
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-02 18:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
mod sealed {
|
|
|
|
use super::*;
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
pub trait Instance {
|
|
|
|
fn regs(&self) -> &pac::uarte0::RegisterBlock;
|
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
pub trait Instance: sealed::Instance + 'static {
|
2021-02-26 00:55:27 +00:00
|
|
|
type Interrupt: Interrupt;
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
macro_rules! make_impl {
|
|
|
|
($type:ident, $irq:ident) => {
|
|
|
|
impl sealed::Instance for peripherals::$type {
|
|
|
|
fn regs(&self) -> &pac::uarte0::RegisterBlock {
|
|
|
|
unsafe { &*pac::$type::ptr() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Instance for peripherals::$type {
|
|
|
|
type Interrupt = interrupt::$irq;
|
|
|
|
}
|
|
|
|
};
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
make_impl!(UARTE0, UARTE0_UART0);
|
2020-12-23 15:18:29 +00:00
|
|
|
#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
|
2021-03-22 00:15:44 +00:00
|
|
|
make_impl!(UARTE1, UARTE1);
|