2020-12-29 02:48:26 +00:00
|
|
|
//! Async low power UARTE.
|
2020-12-28 15:17:36 +00:00
|
|
|
//!
|
2020-12-29 02:48:26 +00:00
|
|
|
//! The peripheral is autmatically enabled and disabled as required to save power.
|
|
|
|
//! Lowest power consumption can only be guaranteed if the send receive futures
|
|
|
|
//! are dropped correctly (e.g. not using `mem::forget()`).
|
|
|
|
|
2020-12-28 15:17:36 +00:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::cmp::min;
|
2020-12-29 02:48:26 +00:00
|
|
|
use core::future::Future;
|
2020-12-28 15:17:36 +00:00
|
|
|
use core::marker::PhantomPinned;
|
|
|
|
use core::ops::Deref;
|
|
|
|
use core::pin::Pin;
|
|
|
|
use core::ptr;
|
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use core::task::{Context, Poll};
|
2020-12-28 19:13:43 +00:00
|
|
|
use cortex_m::singleton;
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
use embassy::util::Signal;
|
2020-12-29 18:33:50 +00:00
|
|
|
use embedded_dma::{StaticReadBuffer, StaticWriteBuffer, WriteBuffer};
|
2020-12-29 02:48:26 +00:00
|
|
|
|
|
|
|
use crate::fmt::assert;
|
2020-12-28 19:13:43 +00:00
|
|
|
use crate::hal::dma::config::DmaConfig;
|
2020-12-29 18:33:50 +00:00
|
|
|
use crate::hal::dma::{
|
|
|
|
Channel4, Channel7, MemoryToPeripheral, PeripheralToMemory, Stream2, Stream7, StreamsTuple,
|
|
|
|
Transfer,
|
|
|
|
};
|
2020-12-29 02:48:26 +00:00
|
|
|
use crate::hal::gpio::gpioa::{PA10, PA9};
|
2020-12-28 15:55:49 +00:00
|
|
|
use crate::hal::gpio::{Alternate, AF10, AF7, AF9};
|
2020-12-28 22:43:29 +00:00
|
|
|
use crate::hal::gpio::{Floating, Input, Output, PushPull};
|
2020-12-29 02:48:26 +00:00
|
|
|
use crate::hal::pac;
|
|
|
|
use crate::hal::prelude::*;
|
2020-12-28 22:43:29 +00:00
|
|
|
use crate::hal::rcc::Clocks;
|
|
|
|
use crate::hal::serial::config::{
|
|
|
|
Config as SerialConfig, DmaConfig as SerialDmaConfig, Parity, StopBits, WordLength,
|
2020-12-28 19:13:43 +00:00
|
|
|
};
|
2020-12-28 22:43:29 +00:00
|
|
|
use crate::hal::serial::Serial;
|
|
|
|
use crate::hal::time::Bps;
|
2020-12-28 19:13:43 +00:00
|
|
|
|
2020-12-28 15:17:36 +00:00
|
|
|
use crate::interrupt;
|
2020-12-29 02:48:26 +00:00
|
|
|
|
2020-12-28 22:43:29 +00:00
|
|
|
use crate::pac::Interrupt;
|
|
|
|
use crate::pac::{DMA2, USART1};
|
|
|
|
|
2020-12-28 15:55:49 +00:00
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +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};
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
/// Interface to the UARTE peripheral
|
|
|
|
pub struct Uarte {
|
|
|
|
instance: Serial<USART1, (PA9<Alternate<AF7>>, PA10<Alternate<AF7>>)>,
|
|
|
|
usart: USART1,
|
|
|
|
dma: DMA2,
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
struct State {
|
|
|
|
tx_done: Signal<()>,
|
|
|
|
rx_done: Signal<u32>,
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
static STATE: State = State {
|
|
|
|
tx_done: Signal::new(),
|
|
|
|
rx_done: Signal::new(),
|
|
|
|
};
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
pub struct Pins {
|
|
|
|
pub rxd: PA10<Alternate<AF7>>,
|
|
|
|
pub txd: PA9<Alternate<AF7>>,
|
|
|
|
pub dma: DMA2,
|
|
|
|
pub usart: USART1,
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
impl Uarte {
|
|
|
|
pub fn new(mut pins: Pins, parity: Parity, baudrate: Bps, clocks: Clocks) -> Self {
|
|
|
|
// // Enable interrupts
|
|
|
|
// uarte.events_endtx.reset();
|
|
|
|
// uarte.events_endrx.reset();
|
|
|
|
// uarte
|
|
|
|
// .intenset
|
|
|
|
// .write(|w| w.endtx().set().txstopped().set().endrx().set().rxto().set());
|
|
|
|
// // TODO: Set interrupt priority?
|
|
|
|
// interrupt::unpend(interrupt::UARTE0_UART0);
|
|
|
|
// interrupt::enable(interrupt::UARTE0_UART0);
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-28 15:55:49 +00:00
|
|
|
// Serial<USART1, (PA9<Alternate<AF7>>, PA10<Alternate<AF7>>)>
|
|
|
|
let mut serial = Serial::usart1(
|
2020-12-28 22:43:29 +00:00
|
|
|
pins.usart,
|
2020-12-28 15:55:49 +00:00
|
|
|
(pins.txd, pins.rxd),
|
2020-12-28 22:43:29 +00:00
|
|
|
SerialConfig {
|
|
|
|
baudrate: baudrate,
|
2020-12-28 15:55:49 +00:00
|
|
|
wordlength: WordLength::DataBits8,
|
|
|
|
parity: Parity::ParityNone,
|
|
|
|
stopbits: StopBits::STOP1,
|
2020-12-28 22:43:29 +00:00
|
|
|
dma: SerialDmaConfig::TxRx,
|
2020-12-28 15:55:49 +00:00
|
|
|
},
|
|
|
|
clocks,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
// let is_set = dma.hifcr.read().tcif7.bit_is_set();
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
Uarte {
|
|
|
|
instance: serial,
|
|
|
|
dma: pins.dma,
|
|
|
|
usart: pins.usart,
|
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
/// Sends serial data.
|
|
|
|
///
|
|
|
|
/// `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
|
2020-12-29 18:33:50 +00:00
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
2020-12-29 02:48:26 +00:00
|
|
|
{
|
|
|
|
SendFuture {
|
|
|
|
uarte: self,
|
|
|
|
buf: tx_buffer,
|
2020-12-29 18:33:50 +00:00
|
|
|
transfer: None,
|
2020-12-29 02:48:26 +00:00
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
/// Receives serial data.
|
|
|
|
///
|
|
|
|
/// 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
|
2020-12-29 18:33:50 +00:00
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
2020-12-29 02:48:26 +00:00
|
|
|
{
|
|
|
|
ReceiveFuture {
|
|
|
|
uarte: self,
|
2020-12-29 18:33:50 +00:00
|
|
|
buf: rx_buffer,
|
|
|
|
transfer: None,
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 02:48:26 +00:00
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
/// Future for the [`LowPowerUarte::send()`] method.
|
2020-12-29 18:33:50 +00:00
|
|
|
pub struct SendFuture<'a, B: WriteBuffer<Word = u8> + 'static> {
|
2020-12-29 02:48:26 +00:00
|
|
|
uarte: &'a Uarte,
|
2020-12-29 18:33:50 +00:00
|
|
|
transfer: Option<&'a Transfer<Stream7<DMA2>, Channel4, USART1, MemoryToPeripheral, B>>,
|
2020-12-29 02:48:26 +00:00
|
|
|
buf: B,
|
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
impl<'a, B> Drop for SendFuture<'a, B>
|
|
|
|
where
|
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
|
|
|
{
|
2020-12-29 02:48:26 +00:00
|
|
|
fn drop(self: &mut Self) {
|
2020-12-29 18:33:50 +00:00
|
|
|
drop(self.transfer);
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
2020-12-29 02:48:26 +00:00
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
impl<'a, B> Future for SendFuture<'a, B>
|
|
|
|
where
|
2020-12-29 18:33:50 +00:00
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
2020-12-29 02:48:26 +00:00
|
|
|
{
|
|
|
|
type Output = ();
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
2020-12-29 18:33:50 +00:00
|
|
|
if !self.transfer.is_none() && self.transfer.unwrap().is_done() {
|
2020-12-29 02:48:26 +00:00
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
2020-12-29 18:33:50 +00:00
|
|
|
self.transfer = Some(&mut Transfer::init(
|
|
|
|
StreamsTuple::new(self.uarte.dma).7,
|
|
|
|
self.uarte.usart,
|
2020-12-29 02:48:26 +00:00
|
|
|
self.buf,
|
|
|
|
// Some(second_buffer),
|
|
|
|
None,
|
|
|
|
DmaConfig::default()
|
|
|
|
.transfer_complete_interrupt(true)
|
|
|
|
.memory_increment(true)
|
|
|
|
.double_buffer(false),
|
2020-12-29 18:33:50 +00:00
|
|
|
));
|
2020-12-29 02:48:26 +00:00
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
waker_interrupt!(DMA2_STREAM7, cx.waker().clone());
|
2020-12-29 02:48:26 +00:00
|
|
|
Poll::Pending
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
/// Future for the [`Uarte::receive()`] method.
|
2020-12-29 18:33:50 +00:00
|
|
|
pub struct ReceiveFuture<'a, B: WriteBuffer<Word = u8> + 'static> {
|
2020-12-29 02:48:26 +00:00
|
|
|
uarte: &'a Uarte,
|
2020-12-29 18:33:50 +00:00
|
|
|
transfer: Option<&'a Transfer<Stream2<DMA2>, Channel4, USART1, PeripheralToMemory, B>>,
|
|
|
|
buf: B,
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
impl<'a, B> Drop for ReceiveFuture<'a, B>
|
|
|
|
where
|
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
|
|
|
{
|
2020-12-29 02:48:26 +00:00
|
|
|
fn drop(self: &mut Self) {
|
2020-12-29 18:33:50 +00:00
|
|
|
drop(self.transfer);
|
2020-12-29 02:48:26 +00:00
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
impl<'a, B> Future for ReceiveFuture<'a, B>
|
|
|
|
where
|
2020-12-29 18:33:50 +00:00
|
|
|
B: WriteBuffer<Word = u8> + 'static,
|
2020-12-29 02:48:26 +00:00
|
|
|
{
|
|
|
|
type Output = B;
|
2020-12-28 15:17:36 +00:00
|
|
|
|
2020-12-29 02:48:26 +00:00
|
|
|
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<B> {
|
2020-12-29 18:33:50 +00:00
|
|
|
if !self.transfer.is_none() && self.transfer.unwrap().is_done() {
|
|
|
|
Poll::Ready(self.buf.take());
|
2020-12-29 02:48:26 +00:00
|
|
|
} else {
|
2020-12-29 18:33:50 +00:00
|
|
|
self.transfer = Some(&mut Transfer::init(
|
|
|
|
StreamsTuple::new(self.uarte.dma).2,
|
|
|
|
self.uarte.usart,
|
2020-12-29 02:48:26 +00:00
|
|
|
self.buf,
|
|
|
|
None,
|
|
|
|
DmaConfig::default()
|
|
|
|
.transfer_complete_interrupt(true)
|
2020-12-29 18:33:50 +00:00
|
|
|
.half_transfer_interrupt(true)
|
2020-12-29 02:48:26 +00:00
|
|
|
.memory_increment(true)
|
|
|
|
.double_buffer(false),
|
2020-12-29 18:33:50 +00:00
|
|
|
));
|
2020-12-29 02:48:26 +00:00
|
|
|
|
2020-12-29 18:33:50 +00:00
|
|
|
waker_interrupt!(DMA2_STREAM2, cx.waker().clone());
|
2020-12-29 02:48:26 +00:00
|
|
|
Poll::Pending
|
|
|
|
}
|
2020-12-28 15:17:36 +00:00
|
|
|
}
|
|
|
|
}
|