2021-05-11 01:04:59 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
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;
|
2021-04-14 14:01:43 +00:00
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
2021-03-22 00:15:44 +00:00
|
|
|
use core::task::Poll;
|
2021-04-14 14:01:43 +00:00
|
|
|
use embassy::interrupt::InterruptExt;
|
2021-10-13 14:35:05 +00:00
|
|
|
use embassy::traits::uart::{Error as TraitError, Read, ReadUntilIdle, Write};
|
2021-09-10 23:53:53 +00:00
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_hal_common::drop::OnDrop;
|
2021-07-29 11:44:51 +00:00
|
|
|
use embassy_hal_common::unborrow;
|
2021-03-22 00:15:44 +00:00
|
|
|
use futures::future::poll_fn;
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
use crate::chip::EASY_DMA_SIZE;
|
2021-03-22 01:10:59 +00:00
|
|
|
use crate::gpio::sealed::Pin as _;
|
2021-05-26 16:19:33 +00:00
|
|
|
use crate::gpio::{self, OptionalPin as GpioOptionalPin, Pin as GpioPin};
|
2021-02-26 00:55:27 +00:00
|
|
|
use crate::interrupt::Interrupt;
|
2021-03-28 20:39:09 +00:00
|
|
|
use crate::pac;
|
2021-10-19 08:13:08 +00:00
|
|
|
use crate::ppi::{AnyChannel, Event, OneToOneChannel, OneToTwoChannel, Ppi, Task};
|
2021-05-10 13:45:40 +00:00
|
|
|
use crate::timer::Instance as TimerInstance;
|
2021-09-02 10:02:31 +00:00
|
|
|
use crate::timer::{Frequency, Timer};
|
2020-12-23 15:18:29 +00:00
|
|
|
|
|
|
|
// Re-export SVD variants to allow user to directly set values.
|
2021-10-12 09:43:57 +00:00
|
|
|
pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
|
2020-12-23 15:18:29 +00:00
|
|
|
|
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-22 00:15:44 +00:00
|
|
|
/// Interface to the UARTE peripheral
|
|
|
|
pub struct Uarte<'d, T: Instance> {
|
|
|
|
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-05-17 09:48:58 +00:00
|
|
|
_uarte: impl Unborrow<Target = T> + 'd,
|
2021-04-14 17:59:52 +00:00
|
|
|
irq: impl Unborrow<Target = T::Interrupt> + 'd,
|
|
|
|
rxd: impl Unborrow<Target = impl GpioPin> + 'd,
|
|
|
|
txd: impl Unborrow<Target = impl GpioPin> + 'd,
|
|
|
|
cts: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
|
|
|
|
rts: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
|
2021-03-22 00:15:44 +00:00
|
|
|
config: Config,
|
2020-12-23 15:18:29 +00:00
|
|
|
) -> Self {
|
2021-05-17 09:48:58 +00:00
|
|
|
unborrow!(irq, rxd, txd, cts, rts);
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = T::regs();
|
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-28 20:39:09 +00:00
|
|
|
// Configure
|
|
|
|
let hardware_flow_control = match (rts.pin().is_some(), cts.pin().is_some()) {
|
|
|
|
(false, false) => false,
|
|
|
|
(true, true) => true,
|
|
|
|
_ => panic!("RTS and CTS pins must be either both set or none set."),
|
|
|
|
};
|
|
|
|
r.config.write(|w| {
|
|
|
|
w.hwfc().bit(hardware_flow_control);
|
|
|
|
w.parity().variant(config.parity);
|
|
|
|
w
|
|
|
|
});
|
2021-03-22 00:15:44 +00:00
|
|
|
r.baudrate.write(|w| w.baudrate().variant(config.baudrate));
|
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-27 01:08:58 +00:00
|
|
|
// Reset rxstarted, txstarted. These are used by drop to know whether a transfer was
|
|
|
|
// stopped midway or not.
|
|
|
|
r.events_rxstarted.reset();
|
|
|
|
r.events_txstarted.reset();
|
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
irq.set_handler(Self::on_interrupt);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
// Enable
|
2021-10-12 11:35:08 +00:00
|
|
|
Self::apply_workaround_for_enable_anomaly();
|
2021-03-22 00:15:44 +00:00
|
|
|
r.enable.write(|w| w.enable().enabled());
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
Self {
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
2021-03-26 22:22:06 +00:00
|
|
|
|
2021-10-12 11:35:08 +00:00
|
|
|
#[cfg(not(any(feature = "nrf9160", feature = "nrf5340")))]
|
|
|
|
fn apply_workaround_for_enable_anomaly() {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "nrf9160", feature = "nrf5340"))]
|
|
|
|
fn apply_workaround_for_enable_anomaly() {
|
|
|
|
use core::ops::Deref;
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
// Apply workaround for anomalies:
|
|
|
|
// - nRF9160 - anomaly 23
|
|
|
|
// - nRF5340 - anomaly 44
|
|
|
|
let rxenable_reg: *const u32 = ((r.deref() as *const _ as usize) + 0x564) as *const u32;
|
|
|
|
let txenable_reg: *const u32 = ((r.deref() as *const _ as usize) + 0x568) as *const u32;
|
|
|
|
|
|
|
|
// NB Safety: This is taken from Nordic's driver -
|
|
|
|
// https://github.com/NordicSemiconductor/nrfx/blob/master/drivers/src/nrfx_uarte.c#L197
|
|
|
|
if unsafe { core::ptr::read_volatile(txenable_reg) } == 1 {
|
|
|
|
r.tasks_stoptx.write(|w| unsafe { w.bits(1) });
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB Safety: This is taken from Nordic's driver -
|
|
|
|
// https://github.com/NordicSemiconductor/nrfx/blob/master/drivers/src/nrfx_uarte.c#L197
|
|
|
|
if unsafe { core::ptr::read_volatile(rxenable_reg) } == 1 {
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
r.tasks_stoprx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
let mut workaround_succeded = false;
|
|
|
|
// The UARTE is able to receive up to four bytes after the STOPRX task has been triggered.
|
|
|
|
// On lowest supported baud rate (1200 baud), with parity bit and two stop bits configured
|
|
|
|
// (resulting in 12 bits per data byte sent), this may take up to 40 ms.
|
|
|
|
for _ in 0..40000 {
|
|
|
|
// NB Safety: This is taken from Nordic's driver -
|
|
|
|
// https://github.com/NordicSemiconductor/nrfx/blob/master/drivers/src/nrfx_uarte.c#L197
|
|
|
|
if unsafe { core::ptr::read_volatile(rxenable_reg) } == 0 {
|
|
|
|
workaround_succeded = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Need to sleep for 1us here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !workaround_succeded {
|
|
|
|
panic!("Failed to apply workaround for UART");
|
|
|
|
}
|
|
|
|
|
|
|
|
let errors = r.errorsrc.read().bits();
|
|
|
|
// NB Safety: safe to write back the bits we just read to clear them
|
|
|
|
r.errorsrc.write(|w| unsafe { w.bits(errors) });
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
fn on_interrupt(_: *mut ()) {
|
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
2021-03-26 22:22:06 +00:00
|
|
|
|
|
|
|
if r.events_endrx.read().bits() != 0 {
|
2021-04-14 14:01:43 +00:00
|
|
|
s.endrx_waker.wake();
|
2021-03-26 22:22:06 +00:00
|
|
|
r.intenclr.write(|w| w.endrx().clear());
|
|
|
|
}
|
|
|
|
if r.events_endtx.read().bits() != 0 {
|
2021-04-14 14:01:43 +00:00
|
|
|
s.endtx_waker.wake();
|
2021-03-26 22:22:06 +00:00
|
|
|
r.intenclr.write(|w| w.endtx().clear());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Instance> Drop for Uarte<'a, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
info!("uarte drop");
|
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = T::regs();
|
2021-03-26 22:22:06 +00:00
|
|
|
|
2021-03-27 01:08:58 +00:00
|
|
|
let did_stoprx = r.events_rxstarted.read().bits() != 0;
|
|
|
|
let did_stoptx = r.events_txstarted.read().bits() != 0;
|
2021-03-26 22:22:06 +00:00
|
|
|
info!("did_stoprx {} did_stoptx {}", did_stoprx, did_stoptx);
|
|
|
|
|
|
|
|
// Wait for rxto or txstopped, if needed.
|
|
|
|
while (did_stoprx && r.events_rxto.read().bits() == 0)
|
|
|
|
|| (did_stoptx && r.events_txstopped.read().bits() == 0)
|
2021-10-16 01:14:47 +00:00
|
|
|
{}
|
2021-03-26 22:22:06 +00:00
|
|
|
|
|
|
|
// 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-05-26 16:19:33 +00:00
|
|
|
gpio::deconfigure_pin(r.psel.rxd.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.txd.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.rts.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.cts.read().bits());
|
2021-03-26 22:22:06 +00:00
|
|
|
|
2021-05-26 16:19:33 +00:00
|
|
|
info!("uarte drop: done");
|
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]
|
2021-10-13 14:35:05 +00:00
|
|
|
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), TraitError>> + 'a;
|
2021-01-02 18:59:37 +00:00
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
fn read<'a>(&'a mut self, rx_buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
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-04-14 14:01:43 +00:00
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
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-27 01:08:58 +00:00
|
|
|
r.events_rxstarted.reset();
|
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]
|
2021-10-13 14:35:05 +00:00
|
|
|
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), TraitError>> + 'a;
|
2021-03-22 00:15:44 +00:00
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
fn write<'a>(&'a mut self, tx_buffer: &'a [u8]) -> Self::WriteFuture<'a> {
|
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-04-14 14:01:43 +00:00
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
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-27 01:08:58 +00:00
|
|
|
r.events_txstarted.reset();
|
2021-03-22 00:15:44 +00:00
|
|
|
drop.defuse();
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-02 18:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 18:20:35 +00:00
|
|
|
/// Interface to an UARTE peripheral that uses an additional timer and two PPI channels,
|
|
|
|
/// allowing it to implement the ReadUntilIdle trait.
|
2021-05-10 13:45:40 +00:00
|
|
|
pub struct UarteWithIdle<'d, U: Instance, T: TimerInstance> {
|
|
|
|
uarte: Uarte<'d, U>,
|
2021-09-02 10:02:31 +00:00
|
|
|
timer: Timer<'d, T>,
|
2021-10-18 14:23:39 +00:00
|
|
|
ppi_ch1: Ppi<'d, AnyChannel, 1, 2>,
|
|
|
|
_ppi_ch2: Ppi<'d, AnyChannel, 1, 1>,
|
2021-05-10 13:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, U: Instance, T: TimerInstance> UarteWithIdle<'d, U, T> {
|
|
|
|
/// Creates the interface to a UARTE instance.
|
|
|
|
/// Sets the baud rate, parity and assigns the pins to the UARTE peripheral.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// 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(
|
|
|
|
uarte: impl Unborrow<Target = U> + 'd,
|
|
|
|
timer: impl Unborrow<Target = T> + 'd,
|
2021-10-18 14:23:39 +00:00
|
|
|
ppi_ch1: impl Unborrow<Target = impl OneToTwoChannel + 'd> + 'd,
|
|
|
|
ppi_ch2: impl Unborrow<Target = impl OneToOneChannel + 'd> + 'd,
|
2021-05-10 13:45:40 +00:00
|
|
|
irq: impl Unborrow<Target = U::Interrupt> + 'd,
|
|
|
|
rxd: impl Unborrow<Target = impl GpioPin> + 'd,
|
|
|
|
txd: impl Unborrow<Target = impl GpioPin> + 'd,
|
|
|
|
cts: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
|
|
|
|
rts: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
|
|
|
|
config: Config,
|
2021-10-18 14:23:39 +00:00
|
|
|
) -> Self {
|
2021-05-10 13:45:40 +00:00
|
|
|
let baudrate = config.baudrate;
|
|
|
|
let uarte = Uarte::new(uarte, irq, rxd, txd, cts, rts, config);
|
2021-09-02 10:02:31 +00:00
|
|
|
let mut timer = Timer::new(timer);
|
2021-05-10 13:45:40 +00:00
|
|
|
|
2021-06-26 07:58:36 +00:00
|
|
|
unborrow!(ppi_ch1, ppi_ch2);
|
2021-05-10 13:45:40 +00:00
|
|
|
|
|
|
|
let r = U::regs();
|
|
|
|
|
|
|
|
// BAUDRATE register values are `baudrate * 2^32 / 16000000`
|
|
|
|
// source: https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values
|
|
|
|
//
|
|
|
|
// We want to stop RX if line is idle for 2 bytes worth of time
|
|
|
|
// That is 20 bits (each byte is 1 start bit + 8 data bits + 1 stop bit)
|
|
|
|
// This gives us the amount of 16M ticks for 20 bits.
|
|
|
|
let timeout = 0x8000_0000 / (baudrate as u32 / 40);
|
|
|
|
|
2021-06-26 07:58:36 +00:00
|
|
|
timer.set_frequency(Frequency::F16MHz);
|
2021-06-29 00:33:41 +00:00
|
|
|
timer.cc(0).write(timeout);
|
|
|
|
timer.cc(0).short_compare_clear();
|
|
|
|
timer.cc(0).short_compare_stop();
|
2021-05-10 13:45:40 +00:00
|
|
|
|
2021-10-18 14:23:39 +00:00
|
|
|
let mut ppi_ch1 = Ppi::new_one_to_two(
|
|
|
|
ppi_ch1,
|
|
|
|
Event::from_reg(&r.events_rxdrdy),
|
|
|
|
timer.task_clear(),
|
|
|
|
timer.task_start(),
|
|
|
|
)
|
|
|
|
.degrade();
|
2021-05-10 13:45:40 +00:00
|
|
|
ppi_ch1.enable();
|
|
|
|
|
2021-10-18 14:23:39 +00:00
|
|
|
let mut ppi_ch2 = Ppi::new_one_to_one(
|
|
|
|
ppi_ch2,
|
|
|
|
timer.cc(0).event_compare(),
|
|
|
|
Task::from_reg(&r.tasks_stoprx),
|
|
|
|
)
|
|
|
|
.degrade();
|
2021-05-10 13:45:40 +00:00
|
|
|
ppi_ch2.enable();
|
|
|
|
|
2021-10-18 14:23:39 +00:00
|
|
|
Self {
|
2021-05-10 13:45:40 +00:00
|
|
|
uarte,
|
|
|
|
timer,
|
2021-05-10 18:16:52 +00:00
|
|
|
ppi_ch1: ppi_ch1,
|
2021-05-10 13:45:40 +00:00
|
|
|
_ppi_ch2: ppi_ch2,
|
2021-10-18 14:23:39 +00:00
|
|
|
}
|
2021-05-10 13:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, U: Instance, T: TimerInstance> ReadUntilIdle for UarteWithIdle<'d, U, T> {
|
|
|
|
#[rustfmt::skip]
|
2021-10-13 14:35:05 +00:00
|
|
|
type ReadUntilIdleFuture<'a> where Self: 'a = impl Future<Output = Result<usize, TraitError>> + 'a;
|
2021-05-10 13:45:40 +00:00
|
|
|
fn read_until_idle<'a>(&'a mut self, rx_buffer: &'a mut [u8]) -> Self::ReadUntilIdleFuture<'a> {
|
|
|
|
async move {
|
|
|
|
let ptr = rx_buffer.as_ptr();
|
|
|
|
let len = rx_buffer.len();
|
|
|
|
assert!(len <= EASY_DMA_SIZE);
|
|
|
|
|
|
|
|
let r = U::regs();
|
|
|
|
let s = U::state();
|
|
|
|
|
2021-06-26 07:58:36 +00:00
|
|
|
let drop = OnDrop::new(|| {
|
2021-05-10 13:45:40 +00:00
|
|
|
info!("read drop: stopping");
|
|
|
|
|
2021-06-26 07:58:36 +00:00
|
|
|
self.timer.stop();
|
2021-05-10 13:45:40 +00:00
|
|
|
|
|
|
|
r.intenclr.write(|w| w.endrx().clear());
|
|
|
|
r.events_rxto.reset();
|
|
|
|
r.tasks_stoprx.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
while r.events_endrx.read().bits() == 0 {}
|
|
|
|
|
|
|
|
info!("read drop: stopped");
|
|
|
|
});
|
|
|
|
|
|
|
|
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) });
|
|
|
|
|
2021-05-10 18:16:13 +00:00
|
|
|
poll_fn(|cx| {
|
2021-05-10 13:45:40 +00:00
|
|
|
s.endrx_waker.register(cx.waker());
|
|
|
|
if r.events_endrx.read().bits() != 0 {
|
2021-05-10 18:16:13 +00:00
|
|
|
return Poll::Ready(());
|
2021-05-10 13:45:40 +00:00
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-05-10 18:16:13 +00:00
|
|
|
let n = r.rxd.amount.read().amount().bits() as usize;
|
|
|
|
|
2021-05-10 13:45:40 +00:00
|
|
|
// Stop timer
|
2021-06-26 07:58:36 +00:00
|
|
|
self.timer.stop();
|
2021-05-10 18:16:52 +00:00
|
|
|
r.events_rxstarted.reset();
|
|
|
|
|
2021-05-10 13:45:40 +00:00
|
|
|
drop.defuse();
|
|
|
|
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, U: Instance, T: TimerInstance> Read for UarteWithIdle<'d, U, T> {
|
|
|
|
#[rustfmt::skip]
|
2021-10-13 14:35:05 +00:00
|
|
|
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), TraitError>> + 'a;
|
2021-05-10 13:45:40 +00:00
|
|
|
fn read<'a>(&'a mut self, rx_buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
2021-05-10 18:16:52 +00:00
|
|
|
async move {
|
|
|
|
self.ppi_ch1.disable();
|
|
|
|
let result = self.uarte.read(rx_buffer).await;
|
|
|
|
self.ppi_ch1.enable();
|
|
|
|
result
|
|
|
|
}
|
2021-05-10 13:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, U: Instance, T: TimerInstance> Write for UarteWithIdle<'d, U, T> {
|
|
|
|
#[rustfmt::skip]
|
2021-10-13 14:35:05 +00:00
|
|
|
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), TraitError>> + 'a;
|
2021-05-10 13:45:40 +00:00
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, tx_buffer: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
self.uarte.write(tx_buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
pub(crate) mod sealed {
|
2021-09-10 23:53:53 +00:00
|
|
|
use embassy::waitqueue::AtomicWaker;
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
use super::*;
|
2020-12-23 15:18:29 +00:00
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
pub struct State {
|
|
|
|
pub endrx_waker: AtomicWaker,
|
|
|
|
pub endtx_waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
impl State {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
endrx_waker: AtomicWaker::new(),
|
|
|
|
endtx_waker: AtomicWaker::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:15:44 +00:00
|
|
|
pub trait Instance {
|
2021-10-12 09:43:57 +00:00
|
|
|
fn regs() -> &'static pac::uarte0::RegisterBlock;
|
2021-04-14 14:01:43 +00:00
|
|
|
fn state() -> &'static State;
|
2021-03-22 00:15:44 +00:00
|
|
|
}
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 02:53:57 +00:00
|
|
|
pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static + Send {
|
2021-02-26 00:55:27 +00:00
|
|
|
type Interrupt: Interrupt;
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
macro_rules! impl_uarte {
|
|
|
|
($type:ident, $pac_type:ident, $irq:ident) => {
|
|
|
|
impl crate::uarte::sealed::Instance for peripherals::$type {
|
2021-10-12 09:43:57 +00:00
|
|
|
fn regs() -> &'static pac::uarte0::RegisterBlock {
|
2021-05-11 01:04:59 +00:00
|
|
|
unsafe { &*pac::$pac_type::ptr() }
|
2021-03-22 00:15:44 +00:00
|
|
|
}
|
2021-05-11 01:04:59 +00:00
|
|
|
fn state() -> &'static crate::uarte::sealed::State {
|
|
|
|
static STATE: crate::uarte::sealed::State = crate::uarte::sealed::State::new();
|
2021-04-14 14:01:43 +00:00
|
|
|
&STATE
|
|
|
|
}
|
2021-03-22 00:15:44 +00:00
|
|
|
}
|
2021-05-11 01:04:59 +00:00
|
|
|
impl crate::uarte::Instance for peripherals::$type {
|
|
|
|
type Interrupt = crate::interrupt::$irq;
|
2021-03-22 00:15:44 +00:00
|
|
|
}
|
|
|
|
};
|
2020-12-23 15:18:29 +00:00
|
|
|
}
|