overhaul implementation

This commit is contained in:
xoviat 2020-12-28 20:48:26 -06:00
parent 3cf85df176
commit 04944b6379
4 changed files with 250 additions and 640 deletions

View File

@ -39,4 +39,5 @@ log = { version = "0.4.11", optional = true }
cortex-m-rt = "0.6.13" cortex-m-rt = "0.6.13"
cortex-m = { version = "0.6.4" } cortex-m = { version = "0.6.4" }
embedded-hal = { version = "0.2.4" } embedded-hal = { version = "0.2.4" }
embedded-dma = { version = "0.1.2" }
stm32f4xx-hal = { version = "0.8.3", features = ["rt"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git" } stm32f4xx-hal = { version = "0.8.3", features = ["rt"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git" }

View File

@ -1,129 +0,0 @@
//! Interrupt management
//!
//! This module implements an API for managing interrupts compatible with
//! nrf_softdevice::interrupt. Intended for switching between the two at compile-time.
use core::sync::atomic::{compiler_fence, Ordering};
use crate::pac::{NVIC, NVIC_PRIO_BITS};
// Re-exports
pub use crate::pac::Interrupt;
pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt]
pub use cortex_m::interrupt::{CriticalSection, Mutex};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum Priority {
Level0 = 0,
Level1 = 1,
Level2 = 2,
Level3 = 3,
Level4 = 4,
Level5 = 5,
Level6 = 6,
Level7 = 7,
}
impl Priority {
#[inline]
fn to_nvic(self) -> u8 {
(self as u8) << (8 - NVIC_PRIO_BITS)
}
#[inline]
fn from_nvic(priority: u8) -> Self {
match priority >> (8 - NVIC_PRIO_BITS) {
0 => Self::Level0,
1 => Self::Level1,
2 => Self::Level2,
3 => Self::Level3,
4 => Self::Level4,
5 => Self::Level5,
6 => Self::Level6,
7 => Self::Level7,
_ => unreachable!(),
}
}
}
#[inline]
pub fn free<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
unsafe {
// TODO: assert that we're in privileged level
// Needed because disabling irqs in non-privileged level is a noop, which would break safety.
let primask: u32;
asm!("mrs {}, PRIMASK", out(reg) primask);
asm!("cpsid i");
// Prevent compiler from reordering operations inside/outside the critical section.
compiler_fence(Ordering::SeqCst);
let r = f(&CriticalSection::new());
compiler_fence(Ordering::SeqCst);
if primask & 1 == 0 {
asm!("cpsie i");
}
r
}
}
#[inline]
pub fn enable(irq: Interrupt) {
unsafe {
NVIC::unmask(irq);
}
}
#[inline]
pub fn disable(irq: Interrupt) {
NVIC::mask(irq);
}
#[inline]
pub fn is_active(irq: Interrupt) -> bool {
NVIC::is_active(irq)
}
#[inline]
pub fn is_enabled(irq: Interrupt) -> bool {
NVIC::is_enabled(irq)
}
#[inline]
pub fn is_pending(irq: Interrupt) -> bool {
NVIC::is_pending(irq)
}
#[inline]
pub fn pend(irq: Interrupt) {
NVIC::pend(irq)
}
#[inline]
pub fn unpend(irq: Interrupt) {
NVIC::unpend(irq)
}
#[inline]
pub fn get_priority(irq: Interrupt) -> Priority {
Priority::from_nvic(NVIC::get_priority(irq))
}
#[inline]
pub fn set_priority(irq: Interrupt, prio: Priority) {
unsafe {
cortex_m::peripheral::Peripherals::steal()
.NVIC
.set_priority(irq, prio.to_nvic())
}
}

View File

@ -331,6 +331,7 @@ pub use stm32f4xx_hal::stm32 as pac;
macro_rules! waker_interrupt { macro_rules! waker_interrupt {
($INT:ident, $waker:expr) => {{ ($INT:ident, $waker:expr) => {{
use core::sync::atomic::{self, Ordering}; use core::sync::atomic::{self, Ordering};
use core::task::Waker;
use stm32f4xx_hal::pac::{interrupt, Interrupt, NVIC}; use stm32f4xx_hal::pac::{interrupt, Interrupt, NVIC};
static mut WAKER: Option<Waker> = None; static mut WAKER: Option<Waker> = None;
@ -359,7 +360,6 @@ macro_rules! waker_interrupt {
// This mod MUST go first, so that the others see its macros. // This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt; pub(crate) mod fmt;
pub mod interrupt;
pub mod uarte; pub mod uarte;
pub use cortex_m_rt::interrupt; pub use cortex_m_rt::interrupt;

View File

@ -1,11 +1,12 @@
//! HAL interface to the UARTE peripheral //! Async low power UARTE.
//! //!
//! See product specification: //! The peripheral is autmatically enabled and disabled as required to save power.
//! //! Lowest power consumption can only be guaranteed if the send receive futures
//! - nrf52832: Section 35 //! are dropped correctly (e.g. not using `mem::forget()`).
//! - nrf52840: Section 6.34
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::cmp::min; use core::cmp::min;
use core::future::Future;
use core::marker::PhantomPinned; use core::marker::PhantomPinned;
use core::ops::Deref; use core::ops::Deref;
use core::pin::Pin; use core::pin::Pin;
@ -14,10 +15,17 @@ use core::sync::atomic::{compiler_fence, Ordering};
use core::task::{Context, Poll}; use core::task::{Context, Poll};
use cortex_m::singleton; use cortex_m::singleton;
use embassy::util::Signal;
use embedded_dma::{StaticReadBuffer, StaticWriteBuffer};
use crate::fmt::assert;
use crate::hal::dma::config::DmaConfig; use crate::hal::dma::config::DmaConfig;
use crate::hal::dma::{Channel4, PeripheralToMemory, Stream2, StreamsTuple, Transfer}; use crate::hal::dma::{Channel4, PeripheralToMemory, Stream2, StreamsTuple, Transfer};
use crate::hal::gpio::gpioa::{PA10, PA9};
use crate::hal::gpio::{Alternate, AF10, AF7, AF9}; use crate::hal::gpio::{Alternate, AF10, AF7, AF9};
use crate::hal::gpio::{Floating, Input, Output, PushPull}; use crate::hal::gpio::{Floating, Input, Output, PushPull};
use crate::hal::pac;
use crate::hal::prelude::*;
use crate::hal::rcc::Clocks; use crate::hal::rcc::Clocks;
use crate::hal::serial::config::{ use crate::hal::serial::config::{
Config as SerialConfig, DmaConfig as SerialDmaConfig, Parity, StopBits, WordLength, Config as SerialConfig, DmaConfig as SerialDmaConfig, Parity, StopBits, WordLength,
@ -26,156 +34,51 @@ use crate::hal::serial::Serial;
use crate::hal::time::Bps; use crate::hal::time::Bps;
use crate::interrupt; use crate::interrupt;
use crate::interrupt::CriticalSection;
use crate::pac::Interrupt; use crate::pac::Interrupt;
use crate::pac::{DMA2, USART1}; use crate::pac::{DMA2, USART1};
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
// Re-export SVD variants to allow user to directly set values // Re-export SVD variants to allow user to directly set values.
// pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; // pub use pac::uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
use embassy::io::{AsyncBufRead, AsyncWrite, Result}; /// Interface to the UARTE peripheral
use embassy::util::WakerStore; pub struct Uarte {
instance: Serial<USART1, (PA9<Alternate<AF7>>, PA10<Alternate<AF7>>)>,
use crate::fmt::{assert, panic, todo, *}; usart: USART1,
dma: DMA2,
//use crate::trace;
const RINGBUF_SIZE: usize = 512;
struct RingBuf {
buf: [u8; RINGBUF_SIZE],
start: usize,
end: usize,
empty: bool,
} }
impl RingBuf { struct State {
fn new() -> Self { tx_done: Signal<()>,
RingBuf { rx_done: Signal<u32>,
buf: [0; RINGBUF_SIZE],
start: 0,
end: 0,
empty: true,
}
}
fn push_buf(&mut self) -> &mut [u8] {
if self.start == self.end && !self.empty {
trace!(" ringbuf: push_buf empty");
return &mut self.buf[..0];
}
let n = if self.start <= self.end {
RINGBUF_SIZE - self.end
} else {
self.start - self.end
};
trace!(" ringbuf: push_buf {:?}..{:?}", self.end, self.end + n);
&mut self.buf[self.end..self.end + n]
}
fn push(&mut self, n: usize) {
trace!(" ringbuf: push {:?}", n);
if n == 0 {
return;
}
self.end = Self::wrap(self.end + n);
self.empty = false;
}
fn pop_buf(&mut self) -> &mut [u8] {
if self.empty {
trace!(" ringbuf: pop_buf empty");
return &mut self.buf[..0];
}
let n = if self.end <= self.start {
RINGBUF_SIZE - self.start
} else {
self.end - self.start
};
trace!(" ringbuf: pop_buf {:?}..{:?}", self.start, self.start + n);
&mut self.buf[self.start..self.start + n]
}
fn pop(&mut self, n: usize) {
trace!(" ringbuf: pop {:?}", n);
if n == 0 {
return;
}
self.start = Self::wrap(self.start + n);
self.empty = self.start == self.end;
}
fn wrap(n: usize) -> usize {
assert!(n <= RINGBUF_SIZE);
if n == RINGBUF_SIZE {
0
} else {
n
}
}
} }
#[derive(Copy, Clone, Debug, PartialEq)] static STATE: State = State {
enum RxState { tx_done: Signal::new(),
Idle, rx_done: Signal::new(),
Receiving, };
ReceivingReady,
Stopping, pub struct Pins {
} pub rxd: PA10<Alternate<AF7>>,
#[derive(Copy, Clone, Debug, PartialEq)] pub txd: PA9<Alternate<AF7>>,
enum TxState { pub dma: DMA2,
Idle, pub usart: USART1,
Transmitting(usize),
} }
/// Interface to a UARTE instance impl Uarte {
/// pub fn new(mut pins: Pins, parity: Parity, baudrate: Bps, clocks: Clocks) -> Self {
/// This is a very basic interface that comes with the following limitations: // // Enable interrupts
/// - The UARTE instances share the same address space with instances of UART. // uarte.events_endtx.reset();
/// You need to make sure that conflicting instances // uarte.events_endrx.reset();
/// are disabled before using `Uarte`. See product specification: // uarte
/// - nrf52832: Section 15.2 // .intenset
/// - nrf52840: Section 6.1.2 // .write(|w| w.endtx().set().txstopped().set().endrx().set().rxto().set());
pub struct Uarte<T: Instance> { // // TODO: Set interrupt priority?
started: bool, // interrupt::unpend(interrupt::UARTE0_UART0);
state: UnsafeCell<UarteState<T>>, // interrupt::enable(interrupt::UARTE0_UART0);
}
// public because it needs to be used in Instance::{get_state, set_state}, but
// should not be used outside the module
#[doc(hidden)]
pub struct UarteState<T> {
inner: T,
rx: RingBuf,
rx_state: RxState,
rx_waker: WakerStore,
tx: RingBuf,
tx_state: TxState,
tx_waker: WakerStore,
_pin: PhantomPinned,
}
#[cfg(any(feature = "52833", feature = "52840"))]
fn port_bit(port: GpioPort) -> bool {
match port {
GpioPort::Port0 => false,
GpioPort::Port1 => true,
}
}
impl<T: Instance> Uarte<T> {
pub fn new(uarte: T, mut pins: Pins, parity: Parity, baudrate: Bps, clocks: Clocks) -> Self {
// Select pins
// Serial<USART1, (PA9<Alternate<AF7>>, PA10<Alternate<AF7>>)> // Serial<USART1, (PA9<Alternate<AF7>>, PA10<Alternate<AF7>>)>
let mut serial = Serial::usart1( let mut serial = Serial::usart1(
pins.usart, pins.usart,
@ -191,411 +94,246 @@ impl<T: Instance> Uarte<T> {
) )
.unwrap(); .unwrap();
let isr = pins.dma.hisr; let isr = pins.dma.hisr;0
// self.isr().$tcifX().bit_is_clear()
// Enable interrupts Uarte { instance: serial, dma: pins.dma, usart: pins.usart }
// serial.listen(Event::Txe); }
// serial.listen(Event::Txe);
// TODO: Enable idle interrupt? Use DMA interrupt? /// Sets the baudrate, parity and assigns the pins to the UARTE peripheral.
// TODO: Make it take the same `Pins` structs nrf-hal (with optional RTS/CTS).
// STREAM: Stream, // // TODO: #[cfg()] for smaller device variants without port register (nrf52810, ...).
// CHANNEL: Channel, // pub fn configure(
// DIR: Direction, // &mut self,
// PERIPHERAL: PeriAddress, // rxd: &Pin<Input<Floating>>,
// BUF: WriteBuffer<Word = <PERIPHERAL as PeriAddress>::MemSize> + 'static, // txd: &mut Pin<Output<PushPull>>,
// parity: Parity,
// baudrate: Baudrate,
// ) {
// let uarte = &self.instance;
// assert!(uarte.enable.read().enable().is_disabled());
// //
// (Stream2<DMA2>, Channel4, Rx<pac::USART1>, PeripheralToMemory), //USART1_RX // uarte.psel.rxd.write(|w| {
// (Stream7<DMA2>, Channel4, Tx<pac::USART1>, MemoryToPeripheral), //USART1_TX // let w = unsafe { w.pin().bits(rxd.pin()) };
// let w = w.port().bit(rxd.port().bit());
/* // w.connect().connected()
Taken from https://gist.github.com/thalesfragoso/a07340c5df6eee3b04c42fdc69ecdcb1
*/
// let rcc = unsafe { &*RCC::ptr() };
// rcc.apb2enr.modify(|_, w| w.adc1en().enabled());
// rcc.apb2rstr.modify(|_, w| w.adcrst().set_bit());
// rcc.apb2rstr.modify(|_, w| w.adcrst().clear_bit());
// let adc = cx.device.ADC1;
// adc.cr2.modify(|_, w| {
// w.dma()
// .enabled()
// .cont()
// .continuous()
// .dds()
// .continuous()
// .adon()
// .enabled()
// }); // });
//
// Configure // txd.set_high().unwrap();
//let hardware_flow_control = pins.rts.is_some() && pins.cts.is_some(); // uarte.psel.txd.write(|w| {
//uarte // let w = unsafe { w.pin().bits(txd.pin()) };
// .config // let w = w.port().bit(txd.port().bit());
// .write(|w| w.hwfc().bit(hardware_flow_control).parity().variant(parity)); // w.connect().connected()
// });
// Configure frequency //
// uarte.baudrate.write(|w| w.baudrate().variant(baudrate)); // uarte.baudrate.write(|w| w.baudrate().variant(baudrate));
// uarte.config.write(|w| w.parity().variant(parity));
// }
Uarte { // fn enable(&mut self) {
started: false, // self.instance.enable.write(|w| w.enable().enabled());
state: UnsafeCell::new(UarteState { // }
inner: uarte,
rx: RingBuf::new(), /// Sends serial data.
rx_state: RxState::Idle, ///
rx_waker: WakerStore::new(), /// `tx_buffer` is marked as static as per `embedded-dma` requirements.
/// It it safe to use a buffer with a non static lifetime if memory is not
/// reused until the future has finished.
pub fn send<'a, B>(&'a mut self, tx_buffer: B) -> SendFuture<'a, B>
where
B: StaticReadBuffer<Word = u8>,
{
// Panic if TX is running which can happen if the user has called
// `mem::forget()` on a previous future after polling it once.
assert!(!self.tx_started());
tx: RingBuf::new(), self.enable();
tx_state: TxState::Idle,
tx_waker: WakerStore::new(),
_pin: PhantomPinned, SendFuture {
}), uarte: self,
buf: tx_buffer,
} }
} }
fn with_state<'a, R>( fn tx_started(&self) -> bool {
self: Pin<&'a mut Self>, // self.instance.events_txstarted.read().bits() != 0
f: impl FnOnce(Pin<&'a mut UarteState<T>>) -> R, false
) -> R {
let Self { state, started } = unsafe { self.get_unchecked_mut() };
interrupt::free(|cs| {
let ptr = state.get();
if !*started {
T::set_state(cs, ptr);
*started = true;
// safety: safe because critical section ensures only one *mut UartState
// exists at the same time.
unsafe { Pin::new_unchecked(&mut *ptr) }.start();
} }
// safety: safe because critical section ensures only one *mut UartState /// Receives serial data.
// exists at the same time. ///
f(unsafe { Pin::new_unchecked(&mut *ptr) }) /// The future is pending until the buffer is completely filled.
}) /// A common pattern is to use [`stop()`](ReceiveFuture::stop) to cancel
/// unfinished transfers after a timeout to prevent lockup when no more data
/// is incoming.
///
/// `rx_buffer` is marked as static as per `embedded-dma` requirements.
/// It it safe to use a buffer with a non static lifetime if memory is not
/// reused until the future has finished.
pub fn receive<'a, B>(&'a mut self, rx_buffer: B) -> ReceiveFuture<'a, B>
where
B: StaticWriteBuffer<Word = u8>,
{
// Panic if RX is running which can happen if the user has called
// `mem::forget()` on a previous future after polling it once.
assert!(!self.rx_started());
self.enable();
ReceiveFuture {
uarte: self,
buf: Some(rx_buffer),
}
}
fn rx_started(&self) -> bool {
self.instance.events_rxstarted.read().bits() != 0
} }
} }
impl<T: Instance> Drop for Uarte<T> { /// Future for the [`LowPowerUarte::send()`] method.
fn drop(&mut self) { pub struct SendFuture<'a, B> {
// stop DMA before dropping, because DMA is using the buffer in `self`. uarte: &'a Uarte,
todo!() buf: B,
}
impl<'a, B> Drop for SendFuture<'a, B> {
fn drop(self: &mut Self) {
if self.uarte.tx_started() {
trace!("stoptx");
// Stop the transmitter to minimize the current consumption.
self.uarte
.instance
.tasks_stoptx
.write(|w| unsafe { w.bits(1) });
self.uarte.instance.events_txstarted.reset();
}
} }
} }
impl<T: Instance> AsyncBufRead for Uarte<T> { impl<'a, B> Future for SendFuture<'a, B>
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { where
self.with_state(|s| s.poll_fill_buf(cx)) B: StaticReadBuffer<Word = u8>,
} {
type Output = ();
fn consume(self: Pin<&mut Self>, amt: usize) { fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
self.with_state(|s| s.consume(amt)) if self.is_ready() {
} Poll::Ready(())
} } else {
// Start DMA transaction
let uarte = &self.uarte.instance;
impl<T: Instance> AsyncWrite for Uarte<T> { STATE.tx_done.reset();
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
self.with_state(|s| s.poll_write(cx, buf))
}
}
impl<T: Instance> UarteState<T> { let (ptr, len) = unsafe { self.buf.read_buffer() };
pub fn start(self: Pin<&mut Self>) { // assert!(len <= EASY_DMA_SIZE);
interrupt::set_priority(T::interrupt(), interrupt::Priority::Level7); // TODO: panic if buffer is not in SRAM
interrupt::enable(T::interrupt());
interrupt::pend(T::interrupt());
}
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
let this = unsafe { self.get_unchecked_mut() };
// 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); compiler_fence(Ordering::SeqCst);
trace!("poll_read"); // uarte.txd.ptr.write(|w| unsafe { w.ptr().bits(ptr as u32) });
// uarte
// .txd
// .maxcnt
// .write(|w| unsafe { w.maxcnt().bits(len as _) });
// We have data ready in buffer? Return it.
let buf = this.rx.pop_buf();
if buf.len() != 0 {
trace!(" got {:?} {:?}", buf.as_ptr() as u32, buf.len());
return Poll::Ready(Ok(buf));
}
trace!(" empty");
if this.rx_state == RxState::ReceivingReady {
trace!(" stopping");
this.rx_state = RxState::Stopping;
this.inner.tasks_stoprx.write(|w| unsafe { w.bits(1) });
}
this.rx_waker.store(cx.waker());
Poll::Pending
}
fn consume(self: Pin<&mut Self>, amt: usize) {
let this = unsafe { self.get_unchecked_mut() };
trace!("consume {:?}", amt);
this.rx.pop(amt);
interrupt::pend(T::interrupt());
}
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
let this = unsafe { self.get_unchecked_mut() };
trace!("poll_write: {:?}", buf.len());
let tx_buf = this.tx.push_buf();
if tx_buf.len() == 0 {
trace!("poll_write: pending");
this.tx_waker.store(cx.waker());
return Poll::Pending;
}
let n = min(tx_buf.len(), buf.len());
tx_buf[..n].copy_from_slice(&buf[..n]);
this.tx.push(n);
trace!("poll_write: queued {:?}", n);
// 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);
interrupt::pend(T::interrupt());
Poll::Ready(Ok(n))
}
fn on_interrupt(&mut self) {
trace!("irq: start");
let mut more_work = true;
while more_work {
more_work = false;
match self.rx_state {
RxState::Idle => {
trace!(" irq_rx: in state idle");
if self.inner.events_rxdrdy.read().bits() != 0 {
trace!(" irq_rx: rxdrdy?????");
self.inner.events_rxdrdy.reset();
}
if self.inner.events_endrx.read().bits() != 0 {
panic!("unexpected endrx");
}
let buf = self.rx.push_buf();
if buf.len() != 0 {
trace!(" irq_rx: starting {:?}", buf.len());
self.rx_state = RxState::Receiving;
// Set up the DMA read
self.inner.rxd.ptr.write(|w|
// 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) });
self.inner.rxd.maxcnt.write(|w|
// 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());
// Enable RXRDY interrupt.
self.inner.events_rxdrdy.reset();
self.inner.intenset.write(|w| w.rxdrdy().set());
// Start UARTE Receive transaction
self.inner.tasks_startrx.write(|w|
// `1` is a valid value to write to task registers.
unsafe { w.bits(1) });
}
}
RxState::Receiving => {
trace!(" irq_rx: in state receiving");
if self.inner.events_rxdrdy.read().bits() != 0 {
trace!(" irq_rx: rxdrdy");
// Disable the RXRDY event interrupt
// RXRDY is triggered for every byte, but we only care about whether we have
// some bytes or not. So as soon as we have at least one, disable it, to avoid
// wasting CPU cycles in interrupts.
self.inner.intenclr.write(|w| w.rxdrdy().clear());
self.inner.events_rxdrdy.reset();
self.rx_waker.wake();
self.rx_state = RxState::ReceivingReady;
more_work = true; // in case we also have endrx pending
}
}
RxState::ReceivingReady | RxState::Stopping => {
trace!(" irq_rx: in state ReceivingReady");
if self.inner.events_rxdrdy.read().bits() != 0 {
trace!(" irq_rx: rxdrdy");
self.inner.events_rxdrdy.reset();
}
if self.inner.events_endrx.read().bits() != 0 {
let n: usize = self.inner.rxd.amount.read().amount().bits() as usize;
trace!(" irq_rx: endrx {:?}", n);
self.rx.push(n);
self.inner.events_endrx.reset();
self.rx_waker.wake();
self.rx_state = RxState::Idle;
more_work = true; // start another rx if possible
}
}
}
}
more_work = true;
while more_work {
more_work = false;
match self.tx_state {
TxState::Idle => {
trace!(" irq_tx: in state Idle");
let buf = self.tx.pop_buf();
if buf.len() != 0 {
trace!(" irq_tx: starting {:?}", buf.len());
self.tx_state = TxState::Transmitting(buf.len());
// Set up the DMA write
self.inner.txd.ptr.write(|w|
// 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) });
self.inner.txd.maxcnt.write(|w|
// 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
self.inner.tasks_starttx.write(|w|
// `1` is a valid value to write to task registers.
unsafe { w.bits(1) });
}
}
TxState::Transmitting(n) => {
trace!(" irq_tx: in state Transmitting");
// Start the DMA transfer // Start the DMA transfer
// See https://github.com/mwkroening/async-stm32f1xx/blob/78c46d1bff124eae4ebc7a2f4d40e6ed74def8b5/src/serial.rs#L118-L129 // See https://github.com/mwkroening/async-stm32f1xx/blob/78c46d1bff124eae4ebc7a2f4d40e6ed74def8b5/src/serial.rs#L118-L129
// https://github.com/stm32-rs/stm32f1xx-hal/blob/68fd3d6f282173816fd3181e795988d314cb17d0/src/serial.rs#L649-L671 // https://github.com/stm32-rs/stm32f1xx-hal/blob/68fd3d6f282173816fd3181e795988d314cb17d0/src/serial.rs#L649-L671
let first_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap(); // let first_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap();
let second_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap(); // let second_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap();
let triple_buffer = Some(singleton!(: [u8; 128] = [0; 128]).unwrap()); // let triple_buffer = Some(singleton!(: [u8; 128] = [0; 128]).unwrap());
let transfer = Transfer::init( let transfer = Transfer::init(
StreamsTuple::new(pins.dma).7, StreamsTuple::new(self.dma).2,
pins.usart, self.usart,
first_buffer, self.buf,
Some(second_buffer), // Some(second_buffer),
None,
DmaConfig::default() DmaConfig::default()
.transfer_complete_interrupt(true) .transfer_complete_interrupt(true)
.memory_increment(true) .memory_increment(true)
.double_buffer(true), .double_buffer(false),
); );
if self.inner.events_endtx.read().bits() != 0 { waker_interrupt!(DMA2_STREAM2, cx.waker().clone());
self.inner.events_endtx.reset(); Poll::Pending
trace!(" irq_tx: endtx {:?}", n);
self.tx.pop(n);
self.tx_waker.wake();
self.tx_state = TxState::Idle;
more_work = true; // start another tx if possible
} }
} }
}
}
trace!("irq: end");
}
} }
pub struct Pins { /// Future for the [`Uarte::receive()`] method.
pub rxd: PA10<Alternate<AF7>>, pub struct ReceiveFuture<'a, B> {
pub txd: PA9<Alternate<AF7>>, uarte: &'a Uarte,
pub dma: DMA2, buf: Option<B>,
pub usart: USART1,
// pub cts: Option<GpioPin<Input<Floating>>>,
// pub rts: Option<GpioPin<Output<PushPull>>>,
} }
mod private { impl<'a, B> Drop for ReceiveFuture<'a, B> {
pub trait Sealed {} fn drop(self: &mut Self) {
if self.uarte.rx_started() {
trace!("stoprx");
impl Sealed for crate::pac::UARTE0 {} self.uarte
#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] .instance
impl Sealed for crate::pac::UARTE1 {} .tasks_stoprx
} .write(|w| unsafe { w.bits(1) });
self.uarte.instance.events_rxstarted.reset();
pub trait Instance: Deref<Target = uarte0::RegisterBlock> + Sized + private::Sealed {
fn interrupt() -> Interrupt;
#[doc(hidden)]
fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self>;
#[doc(hidden)]
fn set_state(_cs: &CriticalSection, state: *mut UarteState<Self>);
}
#[interrupt]
unsafe fn DMA2_CHANNEL2() {
interrupt::free(|cs| UARTE0::get_state(cs).as_mut().unwrap().on_interrupt());
}
#[interrupt]
unsafe fn DMA2_CHANNEL7() {
interrupt::free(|cs| UARTE1::get_state(cs).as_mut().unwrap().on_interrupt());
}
static mut UARTE0_STATE: *mut UarteState<UARTE0> = ptr::null_mut();
#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
static mut UARTE1_STATE: *mut UarteState<UARTE1> = ptr::null_mut();
impl Instance for DMA_CHANNEL1 {
fn interrupt() -> Interrupt {
Interrupt::UARTE0_UART0
} }
fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> {
unsafe { UARTE0_STATE } // Safe because of CriticalSection
}
fn set_state(_cs: &CriticalSection, state: *mut UarteState<Self>) {
unsafe { UARTE0_STATE = state } // Safe because of CriticalSection
} }
} }
#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] impl<'a, B> Future for ReceiveFuture<'a, B>
impl Instance for UARTE1 { where
fn interrupt() -> Interrupt { B: StaticWriteBuffer<Word = u8>,
Interrupt::UARTE1 {
} type Output = B;
fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> { fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<B> {
unsafe { UARTE1_STATE } // Safe because of CriticalSection if self.is_ready() {
Poll::Ready(())
} else {
// Start DMA transaction
compiler_fence(Ordering::SeqCst);
// uarte.txd.ptr.write(|w| unsafe { w.ptr().bits(ptr as u32) });
// uarte
// .txd
// .maxcnt
// .write(|w| unsafe { w.maxcnt().bits(len as _) });
// Start the DMA transfer
// See https://github.com/mwkroening/async-stm32f1xx/blob/78c46d1bff124eae4ebc7a2f4d40e6ed74def8b5/src/serial.rs#L118-L129
// https://github.com/stm32-rs/stm32f1xx-hal/blob/68fd3d6f282173816fd3181e795988d314cb17d0/src/serial.rs#L649-L671
// let first_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap();
// let second_buffer = singleton!(: [u8; 128] = [0; 128]).unwrap();
// let triple_buffer = Some(singleton!(: [u8; 128] = [0; 128]).unwrap());
let transfer = Transfer::init(
StreamsTuple::new(self.dma).7,
self.usart,
self.buf,
// Some(second_buffer),
None,
DmaConfig::default()
.transfer_complete_interrupt(true)
.memory_increment(true)
.double_buffer(false),
);
waker_interrupt!(DMA2_STREAM7, cx.waker().clone());
Poll::Pending
} }
fn set_state(_cs: &CriticalSection, state: *mut UarteState<Self>) { }
unsafe { UARTE1_STATE = state } // Safe because of CriticalSection }
/// Future for the [`receive()`] method.
impl<'a, B> ReceiveFuture<'a, B> {
/// Stops the ongoing reception and returns the number of bytes received.
pub async fn stop(mut self) -> (B, usize) {
let buf = self.buf.take().unwrap();
drop(self);
let len = STATE.rx_done.wait().await;
(buf, len as _)
} }
} }