2020-09-22 16:03:43 +00:00
|
|
|
use core::cmp::min;
|
2021-05-17 10:23:04 +00:00
|
|
|
use core::marker::PhantomData;
|
2021-01-03 00:40:40 +00:00
|
|
|
use core::mem;
|
2020-09-22 16:03:43 +00:00
|
|
|
use core::pin::Pin;
|
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use core::task::{Context, Poll};
|
2021-02-28 23:44:38 +00:00
|
|
|
use embassy::interrupt::InterruptExt;
|
2020-09-22 16:03:43 +00:00
|
|
|
use embassy::io::{AsyncBufRead, AsyncWrite, Result};
|
2021-04-14 17:59:52 +00:00
|
|
|
use embassy::util::{Unborrow, WakerRegistration};
|
2021-07-29 11:44:51 +00:00
|
|
|
use embassy_hal_common::peripheral::{PeripheralMutex, PeripheralState};
|
|
|
|
use embassy_hal_common::ring_buffer::RingBuffer;
|
|
|
|
use embassy_hal_common::{low_power_wait_until, unborrow};
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
use crate::gpio::sealed::Pin as _;
|
|
|
|
use crate::gpio::{OptionalPin as GpioOptionalPin, Pin as GpioPin};
|
2021-01-03 00:40:40 +00:00
|
|
|
use crate::pac;
|
2021-03-28 20:41:45 +00:00
|
|
|
use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task};
|
2021-06-26 07:58:36 +00:00
|
|
|
use crate::timer::Frequency;
|
2021-03-28 20:41:45 +00:00
|
|
|
use crate::timer::Instance as TimerInstance;
|
2021-06-26 07:58:36 +00:00
|
|
|
use crate::timer::Timer;
|
2021-03-28 20:41:45 +00:00
|
|
|
use crate::uarte::{Config, Instance as UarteInstance};
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-02 19:31:50 +00:00
|
|
|
// Re-export SVD variants to allow user to directly set values
|
2021-01-06 22:36:46 +00:00
|
|
|
pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
enum RxState {
|
|
|
|
Idle,
|
|
|
|
Receiving,
|
|
|
|
}
|
2021-01-05 20:14:04 +00:00
|
|
|
|
2020-09-22 16:03:43 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
enum TxState {
|
|
|
|
Idle,
|
|
|
|
Transmitting(usize),
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
struct State<'d, U: UarteInstance, T: TimerInstance> {
|
2021-05-17 10:23:04 +00:00
|
|
|
phantom: PhantomData<&'d mut U>,
|
2021-06-26 07:58:36 +00:00
|
|
|
timer: Timer<'d, T>,
|
2021-03-28 20:41:45 +00:00
|
|
|
_ppi_ch1: Ppi<'d, AnyConfigurableChannel>,
|
|
|
|
_ppi_ch2: Ppi<'d, AnyConfigurableChannel>,
|
2021-01-05 20:14:04 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
rx: RingBuffer<'d>,
|
2021-01-05 20:14:04 +00:00
|
|
|
rx_state: RxState,
|
|
|
|
rx_waker: WakerRegistration,
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
tx: RingBuffer<'d>,
|
2021-01-05 20:14:04 +00:00
|
|
|
tx_state: TxState,
|
|
|
|
tx_waker: WakerRegistration,
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:03:43 +00:00
|
|
|
/// Interface to a UARTE instance
|
|
|
|
///
|
|
|
|
/// This is a very basic interface that comes with the following limitations:
|
|
|
|
/// - The UARTE instances share the same address space with instances of UART.
|
|
|
|
/// You need to make sure that conflicting instances
|
|
|
|
/// are disabled before using `Uarte`. See product specification:
|
|
|
|
/// - nrf52832: Section 15.2
|
|
|
|
/// - nrf52840: Section 6.1.2
|
2021-03-28 20:41:45 +00:00
|
|
|
pub struct BufferedUarte<'d, U: UarteInstance, T: TimerInstance> {
|
|
|
|
inner: PeripheralMutex<State<'d, U, T>>,
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
|
|
|
|
/// unsafe: may not leak self or futures
|
|
|
|
pub unsafe fn new(
|
2021-05-17 10:23:04 +00:00
|
|
|
_uarte: impl Unborrow<Target = U> + 'd,
|
2021-04-14 17:59:52 +00:00
|
|
|
timer: impl Unborrow<Target = T> + 'd,
|
|
|
|
ppi_ch1: impl Unborrow<Target = impl ConfigurableChannel> + 'd,
|
|
|
|
ppi_ch2: impl Unborrow<Target = impl ConfigurableChannel> + 'd,
|
|
|
|
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,
|
2021-03-28 20:41:45 +00:00
|
|
|
config: Config,
|
|
|
|
rx_buffer: &'d mut [u8],
|
|
|
|
tx_buffer: &'d mut [u8],
|
2020-12-29 00:53:17 +00:00
|
|
|
) -> Self {
|
2021-06-26 07:58:36 +00:00
|
|
|
unborrow!(ppi_ch1, ppi_ch2, irq, rxd, txd, cts, rts);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = U::regs();
|
2021-06-26 07:58:36 +00:00
|
|
|
|
2021-06-29 00:33:41 +00:00
|
|
|
let mut timer = Timer::new_irqless(timer);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
rxd.conf().write(|w| w.input().connect().drive().h0h1());
|
|
|
|
r.psel.rxd.write(|w| unsafe { w.bits(rxd.psel_bits()) });
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +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()) });
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +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()) });
|
|
|
|
|
|
|
|
r.baudrate.write(|w| w.baudrate().variant(config.baudrate));
|
|
|
|
r.config.write(|w| w.parity().variant(config.parity));
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
// Configure
|
2021-03-28 20:41:45 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
r.baudrate.write(|w| w.baudrate().variant(config.baudrate));
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
// Enable interrupts
|
|
|
|
r.intenset.write(|w| w.endrx().set().endtx().set());
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-05 00:57:05 +00:00
|
|
|
// Disable the irq, let the Registration enable it when everything is set up.
|
|
|
|
irq.disable();
|
2021-01-03 00:40:40 +00:00
|
|
|
irq.pend();
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
// Enable UARTE instance
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
2021-01-06 22:36:46 +00:00
|
|
|
// 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.
|
2021-03-28 20:41:45 +00:00
|
|
|
let timeout = 0x8000_0000 / (config.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-01-06 22:36:46 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
let mut ppi_ch1 = Ppi::new(ppi_ch1.degrade_configurable());
|
|
|
|
ppi_ch1.set_event(Event::from_reg(&r.events_rxdrdy));
|
2021-06-26 07:58:36 +00:00
|
|
|
ppi_ch1.set_task(timer.task_clear());
|
|
|
|
ppi_ch1.set_fork_task(timer.task_start());
|
2021-03-28 20:41:45 +00:00
|
|
|
ppi_ch1.enable();
|
2021-01-06 22:36:46 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
let mut ppi_ch2 = Ppi::new(ppi_ch2.degrade_configurable());
|
2021-06-29 00:33:41 +00:00
|
|
|
ppi_ch2.set_event(timer.cc(0).event_compare());
|
2021-03-28 20:41:45 +00:00
|
|
|
ppi_ch2.set_task(Task::from_reg(&r.tasks_stoprx));
|
|
|
|
ppi_ch2.enable();
|
2021-01-06 22:36:46 +00:00
|
|
|
|
2020-12-28 22:57:50 +00:00
|
|
|
BufferedUarte {
|
2021-01-05 00:57:05 +00:00
|
|
|
inner: PeripheralMutex::new(
|
2021-01-03 00:40:40 +00:00
|
|
|
State {
|
2021-05-17 10:23:04 +00:00
|
|
|
phantom: PhantomData,
|
2021-01-06 22:36:46 +00:00
|
|
|
timer,
|
2021-03-28 20:41:45 +00:00
|
|
|
_ppi_ch1: ppi_ch1,
|
|
|
|
_ppi_ch2: ppi_ch2,
|
2021-01-03 00:40:40 +00:00
|
|
|
|
|
|
|
rx: RingBuffer::new(rx_buffer),
|
|
|
|
rx_state: RxState::Idle,
|
|
|
|
rx_waker: WakerRegistration::new(),
|
|
|
|
|
|
|
|
tx: RingBuffer::new(tx_buffer),
|
|
|
|
tx_state: TxState::Idle,
|
|
|
|
tx_waker: WakerRegistration::new(),
|
|
|
|
},
|
2021-01-06 21:48:54 +00:00
|
|
|
irq,
|
2021-01-03 00:40:40 +00:00
|
|
|
),
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 20:14:04 +00:00
|
|
|
|
2021-01-11 09:40:37 +00:00
|
|
|
pub fn set_baudrate(self: Pin<&mut Self>, baudrate: Baudrate) {
|
2021-06-07 22:30:22 +00:00
|
|
|
let mut inner = self.inner();
|
2021-07-29 05:11:26 +00:00
|
|
|
unsafe { inner.as_mut().register_interrupt_unchecked() }
|
2021-07-27 07:28:52 +00:00
|
|
|
inner.with(|state| {
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = U::regs();
|
2021-03-28 20:41:45 +00:00
|
|
|
|
2021-01-11 09:40:37 +00:00
|
|
|
let timeout = 0x8000_0000 / (baudrate as u32 / 40);
|
2021-06-29 00:33:41 +00:00
|
|
|
state.timer.cc(0).write(timeout);
|
2021-06-26 07:58:36 +00:00
|
|
|
state.timer.clear();
|
2021-01-11 09:40:37 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
r.baudrate.write(|w| w.baudrate().variant(baudrate));
|
2021-01-11 09:40:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'d, U, T>>> {
|
2021-01-05 20:14:04 +00:00
|
|
|
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
|
|
|
|
}
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
impl<'d, U: UarteInstance, T: TimerInstance> AsyncBufRead for BufferedUarte<'d, U, T> {
|
2020-09-22 16:03:43 +00:00
|
|
|
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
|
2021-03-18 01:29:03 +00:00
|
|
|
let mut inner = self.inner();
|
2021-07-29 05:11:26 +00:00
|
|
|
unsafe { inner.as_mut().register_interrupt_unchecked() }
|
2021-07-27 07:28:52 +00:00
|
|
|
inner.with(|state| {
|
2021-01-05 20:14:04 +00:00
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// before any DMA action has started
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
trace!("poll_read");
|
|
|
|
|
|
|
|
// We have data ready in buffer? Return it.
|
|
|
|
let buf = state.rx.pop_buf();
|
2021-02-14 00:41:36 +00:00
|
|
|
if !buf.is_empty() {
|
2021-01-05 20:14:04 +00:00
|
|
|
trace!(" got {:?} {:?}", buf.as_ptr() as u32, buf.len());
|
|
|
|
let buf: &[u8] = buf;
|
|
|
|
let buf: &[u8] = unsafe { mem::transmute(buf) };
|
|
|
|
return Poll::Ready(Ok(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(" empty");
|
|
|
|
state.rx_waker.register(cx.waker());
|
|
|
|
Poll::<Result<&[u8]>>::Pending
|
2021-01-03 00:40:40 +00:00
|
|
|
})
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(self: Pin<&mut Self>, amt: usize) {
|
2021-03-18 01:29:03 +00:00
|
|
|
let mut inner = self.inner();
|
2021-07-29 05:11:26 +00:00
|
|
|
unsafe { inner.as_mut().register_interrupt_unchecked() }
|
2021-07-27 07:28:52 +00:00
|
|
|
inner.as_mut().with(|state| {
|
2021-01-05 20:14:04 +00:00
|
|
|
trace!("consume {:?}", amt);
|
|
|
|
state.rx.pop(amt);
|
2021-07-27 07:28:52 +00:00
|
|
|
});
|
|
|
|
inner.pend();
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
impl<'d, U: UarteInstance, T: TimerInstance> AsyncWrite for BufferedUarte<'d, U, T> {
|
2020-09-22 16:03:43 +00:00
|
|
|
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
2021-03-18 01:29:03 +00:00
|
|
|
let mut inner = self.inner();
|
2021-07-29 05:11:26 +00:00
|
|
|
unsafe { inner.as_mut().register_interrupt_unchecked() }
|
2021-07-27 07:28:52 +00:00
|
|
|
let poll = inner.as_mut().with(|state| {
|
2021-01-05 20:14:04 +00:00
|
|
|
trace!("poll_write: {:?}", buf.len());
|
|
|
|
|
|
|
|
let tx_buf = state.tx.push_buf();
|
2021-02-14 00:41:36 +00:00
|
|
|
if tx_buf.is_empty() {
|
2021-01-05 20:14:04 +00:00
|
|
|
trace!("poll_write: pending");
|
|
|
|
state.tx_waker.register(cx.waker());
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-05 20:14:04 +00:00
|
|
|
let n = min(tx_buf.len(), buf.len());
|
|
|
|
tx_buf[..n].copy_from_slice(&buf[..n]);
|
|
|
|
state.tx.push(n);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-05 20:14:04 +00:00
|
|
|
trace!("poll_write: queued {:?}", n);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-05 20:14:04 +00:00
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// before any DMA action has started
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-01-05 20:14:04 +00:00
|
|
|
Poll::Ready(Ok(n))
|
2021-07-27 07:28:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
inner.pend();
|
|
|
|
|
|
|
|
poll
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
impl<'a, U: UarteInstance, T: TimerInstance> Drop for State<'a, U, T> {
|
2021-03-18 01:01:29 +00:00
|
|
|
fn drop(&mut self) {
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = U::regs();
|
2021-03-28 20:41:45 +00:00
|
|
|
|
|
|
|
// TODO this probably deadlocks. do like Uarte instead.
|
|
|
|
|
2021-06-26 07:58:36 +00:00
|
|
|
self.timer.stop();
|
2021-01-11 09:39:59 +00:00
|
|
|
if let RxState::Receiving = self.rx_state {
|
2021-03-28 20:41:45 +00:00
|
|
|
r.tasks_stoprx.write(|w| unsafe { w.bits(1) });
|
2021-01-11 09:39:59 +00:00
|
|
|
}
|
|
|
|
if let TxState::Transmitting(_) = self.tx_state {
|
2021-03-28 20:41:45 +00:00
|
|
|
r.tasks_stoptx.write(|w| unsafe { w.bits(1) });
|
2021-01-11 09:39:59 +00:00
|
|
|
}
|
|
|
|
if let RxState::Receiving = self.rx_state {
|
2021-03-28 20:41:45 +00:00
|
|
|
low_power_wait_until(|| r.events_endrx.read().bits() == 1);
|
2021-01-11 09:39:59 +00:00
|
|
|
}
|
|
|
|
if let TxState::Transmitting(_) = self.tx_state {
|
2021-03-28 20:41:45 +00:00
|
|
|
low_power_wait_until(|| r.events_endtx.read().bits() == 1);
|
2021-01-11 09:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 05:11:26 +00:00
|
|
|
impl<'a, U: UarteInstance, T: TimerInstance> PeripheralState for State<'a, U, T> {
|
2021-01-06 21:48:54 +00:00
|
|
|
type Interrupt = U::Interrupt;
|
2020-09-22 16:03:43 +00:00
|
|
|
fn on_interrupt(&mut self) {
|
|
|
|
trace!("irq: start");
|
2021-04-14 14:01:43 +00:00
|
|
|
let r = U::regs();
|
2021-03-28 20:41:45 +00:00
|
|
|
|
2021-01-06 22:36:46 +00:00
|
|
|
loop {
|
2020-09-22 16:03:43 +00:00
|
|
|
match self.rx_state {
|
|
|
|
RxState::Idle => {
|
|
|
|
trace!(" irq_rx: in state idle");
|
|
|
|
|
|
|
|
let buf = self.rx.push_buf();
|
2021-02-14 00:41:36 +00:00
|
|
|
if !buf.is_empty() {
|
2020-09-22 16:03:43 +00:00
|
|
|
trace!(" irq_rx: starting {:?}", buf.len());
|
|
|
|
self.rx_state = RxState::Receiving;
|
|
|
|
|
|
|
|
// Set up the DMA read
|
2021-03-28 20:41:45 +00:00
|
|
|
r.rxd.ptr.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// The PTR field is a full 32 bits wide and accepts the full range
|
|
|
|
// of values.
|
|
|
|
unsafe { w.ptr().bits(buf.as_ptr() as u32) });
|
2021-03-28 20:41:45 +00:00
|
|
|
r.rxd.maxcnt.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// We're giving it the length of the buffer, so no danger of
|
|
|
|
// accessing invalid memory. We have verified that the length of the
|
|
|
|
// buffer fits in an `u8`, so the cast to `u8` is also fine.
|
|
|
|
//
|
|
|
|
// The MAXCNT field is at least 8 bits wide and accepts the full
|
|
|
|
// range of values.
|
|
|
|
unsafe { w.maxcnt().bits(buf.len() as _) });
|
|
|
|
trace!(" irq_rx: buf {:?} {:?}", buf.as_ptr() as u32, buf.len());
|
|
|
|
|
|
|
|
// Start UARTE Receive transaction
|
2021-03-28 20:41:45 +00:00
|
|
|
r.tasks_startrx.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// `1` is a valid value to write to task registers.
|
|
|
|
unsafe { w.bits(1) });
|
|
|
|
}
|
2021-01-06 22:36:46 +00:00
|
|
|
break;
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
RxState::Receiving => {
|
|
|
|
trace!(" irq_rx: in state receiving");
|
2021-03-28 20:41:45 +00:00
|
|
|
if r.events_endrx.read().bits() != 0 {
|
2021-06-26 07:58:36 +00:00
|
|
|
self.timer.stop();
|
2020-09-22 16:03:43 +00:00
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
let n: usize = r.rxd.amount.read().amount().bits() as usize;
|
2020-09-22 16:03:43 +00:00
|
|
|
trace!(" irq_rx: endrx {:?}", n);
|
|
|
|
self.rx.push(n);
|
|
|
|
|
2021-03-28 20:41:45 +00:00
|
|
|
r.events_endrx.reset();
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
self.rx_waker.wake();
|
|
|
|
self.rx_state = RxState::Idle;
|
2021-01-06 22:36:46 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 22:36:46 +00:00
|
|
|
loop {
|
2020-09-22 16:03:43 +00:00
|
|
|
match self.tx_state {
|
|
|
|
TxState::Idle => {
|
|
|
|
trace!(" irq_tx: in state Idle");
|
|
|
|
let buf = self.tx.pop_buf();
|
2021-02-14 00:41:36 +00:00
|
|
|
if !buf.is_empty() {
|
2020-09-22 16:03:43 +00:00
|
|
|
trace!(" irq_tx: starting {:?}", buf.len());
|
|
|
|
self.tx_state = TxState::Transmitting(buf.len());
|
|
|
|
|
|
|
|
// Set up the DMA write
|
2021-03-28 20:41:45 +00:00
|
|
|
r.txd.ptr.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// The PTR field is a full 32 bits wide and accepts the full range
|
|
|
|
// of values.
|
|
|
|
unsafe { w.ptr().bits(buf.as_ptr() as u32) });
|
2021-03-28 20:41:45 +00:00
|
|
|
r.txd.maxcnt.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// We're giving it the length of the buffer, so no danger of
|
|
|
|
// accessing invalid memory. We have verified that the length of the
|
|
|
|
// buffer fits in an `u8`, so the cast to `u8` is also fine.
|
|
|
|
//
|
|
|
|
// The MAXCNT field is 8 bits wide and accepts the full range of
|
|
|
|
// values.
|
|
|
|
unsafe { w.maxcnt().bits(buf.len() as _) });
|
|
|
|
|
|
|
|
// Start UARTE Transmit transaction
|
2021-03-28 20:41:45 +00:00
|
|
|
r.tasks_starttx.write(|w|
|
2020-09-22 16:03:43 +00:00
|
|
|
// `1` is a valid value to write to task registers.
|
|
|
|
unsafe { w.bits(1) });
|
|
|
|
}
|
2021-01-06 22:36:46 +00:00
|
|
|
break;
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
TxState::Transmitting(n) => {
|
|
|
|
trace!(" irq_tx: in state Transmitting");
|
2021-03-28 20:41:45 +00:00
|
|
|
if r.events_endtx.read().bits() != 0 {
|
|
|
|
r.events_endtx.reset();
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
trace!(" irq_tx: endtx {:?}", n);
|
|
|
|
self.tx.pop(n);
|
|
|
|
self.tx_waker.wake();
|
|
|
|
self.tx_state = TxState::Idle;
|
2021-01-06 22:36:46 +00:00
|
|
|
} else {
|
|
|
|
break;
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trace!("irq: end");
|
|
|
|
}
|
|
|
|
}
|