2023-04-17 00:04:54 +02:00
|
|
|
use core::future::Future;
|
2023-04-18 16:16:33 +02:00
|
|
|
use core::marker::PhantomData;
|
2023-04-17 00:04:54 +02:00
|
|
|
use core::pin::Pin;
|
2021-08-11 01:40:02 +02:00
|
|
|
use core::sync::atomic::{fence, Ordering};
|
2023-04-18 16:16:33 +02:00
|
|
|
use core::task::{Context, Poll, Waker};
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2022-11-23 10:11:19 +01:00
|
|
|
use embassy_cortex_m::interrupt::Priority;
|
2023-04-17 00:04:54 +02:00
|
|
|
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2023-04-17 00:04:54 +02:00
|
|
|
use pac::dma::regs;
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
use super::ringbuffer::{DmaCtrl, DmaRingBuffer, OverrunError};
|
2023-04-18 20:56:23 +02:00
|
|
|
use super::word::{Word, WordSize};
|
|
|
|
use super::Dir;
|
2022-03-04 17:42:38 +01:00
|
|
|
use crate::_generated::DMA_CHANNEL_COUNT;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::interrupt::{Interrupt, InterruptExt};
|
2023-04-17 00:04:54 +02:00
|
|
|
use crate::pac::dma::vals;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::{interrupt, pac};
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct TransferOptions {
|
|
|
|
/// Peripheral burst transfer configuration
|
|
|
|
pub pburst: Burst,
|
|
|
|
/// Memory burst transfer configuration
|
|
|
|
pub mburst: Burst,
|
|
|
|
/// Flow control configuration
|
|
|
|
pub flow_ctrl: FlowControl,
|
|
|
|
/// FIFO threshold for DMA FIFO mode. If none, direct mode is used.
|
|
|
|
pub fifo_threshold: Option<FifoThreshold>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TransferOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
pburst: Burst::Single,
|
|
|
|
mburst: Burst::Single,
|
|
|
|
flow_ctrl: FlowControl::Dma,
|
|
|
|
fifo_threshold: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
impl From<WordSize> for vals::Size {
|
|
|
|
fn from(raw: WordSize) -> Self {
|
|
|
|
match raw {
|
|
|
|
WordSize::OneByte => Self::BITS8,
|
|
|
|
WordSize::TwoBytes => Self::BITS16,
|
|
|
|
WordSize::FourBytes => Self::BITS32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
impl From<Dir> for vals::Dir {
|
|
|
|
fn from(raw: Dir) -> Self {
|
|
|
|
match raw {
|
|
|
|
Dir::MemoryToPeripheral => Self::MEMORYTOPERIPHERAL,
|
|
|
|
Dir::PeripheralToMemory => Self::PERIPHERALTOMEMORY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Burst {
|
|
|
|
/// Single transfer
|
|
|
|
Single,
|
|
|
|
/// Incremental burst of 4 beats
|
|
|
|
Incr4,
|
|
|
|
/// Incremental burst of 8 beats
|
|
|
|
Incr8,
|
|
|
|
/// Incremental burst of 16 beats
|
|
|
|
Incr16,
|
|
|
|
}
|
|
|
|
|
2022-03-16 17:52:06 +01:00
|
|
|
impl From<Burst> for vals::Burst {
|
|
|
|
fn from(burst: Burst) -> Self {
|
|
|
|
match burst {
|
|
|
|
Burst::Single => vals::Burst::SINGLE,
|
|
|
|
Burst::Incr4 => vals::Burst::INCR4,
|
|
|
|
Burst::Incr8 => vals::Burst::INCR8,
|
|
|
|
Burst::Incr16 => vals::Burst::INCR16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum FlowControl {
|
|
|
|
/// Flow control by DMA
|
|
|
|
Dma,
|
|
|
|
/// Flow control by peripheral
|
|
|
|
Peripheral,
|
|
|
|
}
|
|
|
|
|
2022-03-16 17:52:06 +01:00
|
|
|
impl From<FlowControl> for vals::Pfctrl {
|
|
|
|
fn from(flow: FlowControl) -> Self {
|
|
|
|
match flow {
|
|
|
|
FlowControl::Dma => vals::Pfctrl::DMA,
|
|
|
|
FlowControl::Peripheral => vals::Pfctrl::PERIPHERAL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum FifoThreshold {
|
|
|
|
/// 1/4 full FIFO
|
|
|
|
Quarter,
|
|
|
|
/// 1/2 full FIFO
|
|
|
|
Half,
|
|
|
|
/// 3/4 full FIFO
|
|
|
|
ThreeQuarters,
|
|
|
|
/// Full FIFO
|
|
|
|
Full,
|
|
|
|
}
|
|
|
|
|
2023-02-18 00:35:35 +01:00
|
|
|
impl From<FifoThreshold> for vals::Fth {
|
|
|
|
fn from(value: FifoThreshold) -> Self {
|
|
|
|
match value {
|
|
|
|
FifoThreshold::Quarter => vals::Fth::QUARTER,
|
|
|
|
FifoThreshold::Half => vals::Fth::HALF,
|
|
|
|
FifoThreshold::ThreeQuarters => vals::Fth::THREEQUARTERS,
|
|
|
|
FifoThreshold::Full => vals::Fth::FULL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
struct State {
|
2023-04-17 00:04:54 +02:00
|
|
|
ch_wakers: [AtomicWaker; DMA_CHANNEL_COUNT],
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
const fn new() -> Self {
|
2023-04-17 00:04:54 +02:00
|
|
|
const AW: AtomicWaker = AtomicWaker::new();
|
2021-07-15 05:42:06 +02:00
|
|
|
Self {
|
2023-04-17 00:04:54 +02:00
|
|
|
ch_wakers: [AW; DMA_CHANNEL_COUNT],
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static STATE: State = State::new();
|
|
|
|
|
|
|
|
/// safety: must be called only once
|
2022-11-23 10:11:19 +01:00
|
|
|
pub(crate) unsafe fn init(irq_priority: Priority) {
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_interrupt! {
|
2021-07-27 19:23:33 +02:00
|
|
|
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
|
2022-11-23 10:11:19 +01:00
|
|
|
let irq = interrupt::$irq::steal();
|
|
|
|
irq.set_priority(irq_priority);
|
|
|
|
irq.enable();
|
2021-07-15 05:42:06 +02:00
|
|
|
};
|
|
|
|
}
|
2022-03-04 17:42:38 +01:00
|
|
|
crate::_generated::init_dma();
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_dma_channel! {
|
|
|
|
($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $index:expr, $dmamux:tt) => {
|
2023-04-17 00:04:54 +02:00
|
|
|
impl sealed::Channel for crate::peripherals::$channel_peri {
|
|
|
|
fn regs(&self) -> pac::dma::Dma {
|
|
|
|
pac::$dma_peri
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
fn num(&self) -> usize {
|
|
|
|
$channel_num
|
2021-07-20 21:20:16 +02:00
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
fn index(&self) -> usize {
|
|
|
|
$index
|
2021-09-29 02:37:39 +02:00
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
fn on_irq() {
|
|
|
|
unsafe { on_irq_inner(pac::$dma_peri, $channel_num, $index) }
|
2022-04-12 14:06:53 +02:00
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
}
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
impl Channel for crate::peripherals::$channel_peri {}
|
|
|
|
};
|
|
|
|
}
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
/// Safety: Must be called with a matching set of parameters for a valid dma channel
|
|
|
|
pub(crate) unsafe fn on_irq_inner(dma: pac::dma::Dma, channel_num: usize, index: usize) {
|
|
|
|
let cr = dma.st(channel_num).cr();
|
|
|
|
let isr = dma.isr(channel_num / 4).read();
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
if isr.teif(channel_num % 4) {
|
|
|
|
panic!("DMA: error on DMA@{:08x} channel {}", dma.0 as u32, channel_num);
|
|
|
|
}
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
if isr.tcif(channel_num % 4) && cr.read().tcie() {
|
|
|
|
/* acknowledge transfer complete interrupt */
|
|
|
|
dma.ifcr(channel_num / 4).write(|w| w.set_tcif(channel_num % 4, true));
|
|
|
|
STATE.ch_wakers[index].wake();
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[cfg(any(dma_v2, dmamux))]
|
|
|
|
pub type Request = u8;
|
|
|
|
#[cfg(not(any(dma_v2, dmamux)))]
|
|
|
|
pub type Request = ();
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
pub trait Channel: sealed::Channel + Peripheral<P = Self> + 'static + super::dmamux::MuxChannel {}
|
|
|
|
#[cfg(not(dmamux))]
|
|
|
|
pub trait Channel: sealed::Channel + Peripheral<P = Self> + 'static {}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
2022-03-08 20:52:33 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub trait Channel {
|
|
|
|
fn regs(&self) -> pac::dma::Dma;
|
|
|
|
fn num(&self) -> usize;
|
|
|
|
fn index(&self) -> usize;
|
|
|
|
fn on_irq();
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
|
|
|
pub struct Transfer<'a, C: Channel> {
|
|
|
|
channel: PeripheralRef<'a, C>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C: Channel> Transfer<'a, C> {
|
|
|
|
pub unsafe fn new_read<W: Word>(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
request: Request,
|
|
|
|
peri_addr: *mut W,
|
|
|
|
buf: &'a mut [W],
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
Self::new_read_raw(channel, request, peri_addr, buf, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn new_read_raw<W: Word>(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
request: Request,
|
|
|
|
peri_addr: *mut W,
|
|
|
|
buf: *mut [W],
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(channel);
|
|
|
|
|
|
|
|
let (ptr, len) = super::slice_ptr_parts_mut(buf);
|
|
|
|
assert!(len > 0 && len <= 0xFFFF);
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
channel,
|
|
|
|
request,
|
|
|
|
Dir::PeripheralToMemory,
|
|
|
|
peri_addr as *const u32,
|
|
|
|
ptr as *mut u32,
|
|
|
|
len,
|
|
|
|
true,
|
2023-04-18 20:56:23 +02:00
|
|
|
W::size(),
|
2023-04-17 00:04:54 +02:00
|
|
|
options,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn new_write<W: Word>(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
request: Request,
|
|
|
|
buf: &'a [W],
|
|
|
|
peri_addr: *mut W,
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
Self::new_write_raw(channel, request, buf, peri_addr, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn new_write_raw<W: Word>(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
request: Request,
|
|
|
|
buf: *const [W],
|
|
|
|
peri_addr: *mut W,
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(channel);
|
|
|
|
|
|
|
|
let (ptr, len) = super::slice_ptr_parts(buf);
|
|
|
|
assert!(len > 0 && len <= 0xFFFF);
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
channel,
|
|
|
|
request,
|
|
|
|
Dir::MemoryToPeripheral,
|
|
|
|
peri_addr as *const u32,
|
|
|
|
ptr as *mut u32,
|
|
|
|
len,
|
|
|
|
true,
|
2023-04-18 20:56:23 +02:00
|
|
|
W::size(),
|
2023-04-17 00:04:54 +02:00
|
|
|
options,
|
|
|
|
)
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub unsafe fn new_write_repeated<W: Word>(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
2021-11-19 19:15:55 +01:00
|
|
|
request: Request,
|
2023-04-17 00:04:54 +02:00
|
|
|
repeated: &'a W,
|
|
|
|
count: usize,
|
|
|
|
peri_addr: *mut W,
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(channel);
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
channel,
|
|
|
|
request,
|
|
|
|
Dir::MemoryToPeripheral,
|
|
|
|
peri_addr as *const u32,
|
|
|
|
repeated as *const W as *mut u32,
|
|
|
|
count,
|
|
|
|
false,
|
2023-04-18 20:56:23 +02:00
|
|
|
W::size(),
|
2023-04-17 00:04:54 +02:00
|
|
|
options,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn new_inner(
|
|
|
|
channel: PeripheralRef<'a, C>,
|
|
|
|
_request: Request,
|
|
|
|
dir: Dir,
|
2021-11-19 19:15:55 +01:00
|
|
|
peri_addr: *const u32,
|
|
|
|
mem_addr: *mut u32,
|
|
|
|
mem_len: usize,
|
|
|
|
incr_mem: bool,
|
2023-04-17 00:04:54 +02:00
|
|
|
data_size: WordSize,
|
2022-03-16 17:52:06 +01:00
|
|
|
options: TransferOptions,
|
2023-04-17 00:04:54 +02:00
|
|
|
) -> Self {
|
|
|
|
let ch = channel.regs().st(channel.num());
|
2021-11-19 19:15:55 +01:00
|
|
|
|
|
|
|
// "Preceding reads and writes cannot be moved past subsequent writes."
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let mut this = Self { channel };
|
|
|
|
this.clear_irqs();
|
|
|
|
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
super::dmamux::configure_dmamux(&mut *this.channel, _request);
|
2021-12-08 03:30:07 +01:00
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
ch.par().write_value(peri_addr as u32);
|
|
|
|
ch.m0ar().write_value(mem_addr as u32);
|
|
|
|
ch.ndtr().write_value(regs::Ndtr(mem_len as _));
|
2023-02-18 00:35:35 +01:00
|
|
|
ch.fcr().write(|w| {
|
|
|
|
if let Some(fth) = options.fifo_threshold {
|
|
|
|
// FIFO mode
|
|
|
|
w.set_dmdis(vals::Dmdis::DISABLED);
|
|
|
|
w.set_fth(fth.into());
|
|
|
|
} else {
|
|
|
|
// Direct mode
|
|
|
|
w.set_dmdis(vals::Dmdis::ENABLED);
|
|
|
|
}
|
|
|
|
});
|
2021-11-19 19:15:55 +01:00
|
|
|
ch.cr().write(|w| {
|
2023-04-17 00:04:54 +02:00
|
|
|
w.set_dir(dir.into());
|
|
|
|
w.set_msize(data_size.into());
|
|
|
|
w.set_psize(data_size.into());
|
2022-04-12 14:06:53 +02:00
|
|
|
w.set_pl(vals::Pl::VERYHIGH);
|
2023-04-17 00:04:54 +02:00
|
|
|
w.set_minc(match incr_mem {
|
|
|
|
true => vals::Inc::INCREMENTED,
|
|
|
|
false => vals::Inc::FIXED,
|
|
|
|
});
|
2022-04-12 14:06:53 +02:00
|
|
|
w.set_pinc(vals::Inc::FIXED);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
#[cfg(dma_v1)]
|
|
|
|
w.set_trbuff(true);
|
|
|
|
|
|
|
|
#[cfg(dma_v2)]
|
2023-04-17 00:04:54 +02:00
|
|
|
w.set_chsel(_request);
|
2022-04-12 14:06:53 +02:00
|
|
|
|
|
|
|
w.set_pburst(options.pburst.into());
|
|
|
|
w.set_mburst(options.mburst.into());
|
|
|
|
w.set_pfctrl(options.flow_ctrl.into());
|
|
|
|
|
|
|
|
w.set_en(true);
|
|
|
|
});
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
this
|
2022-04-12 14:06:53 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
fn clear_irqs(&mut self) {
|
|
|
|
let isrn = self.channel.num() / 4;
|
|
|
|
let isrbit = self.channel.num() % 4;
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
unsafe {
|
|
|
|
self.channel.regs().ifcr(isrn).write(|w| {
|
|
|
|
w.set_tcif(isrbit, true);
|
|
|
|
w.set_teif(isrbit, true);
|
|
|
|
})
|
|
|
|
}
|
2022-04-12 14:06:53 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub fn request_stop(&mut self) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2021-12-08 03:30:07 +01:00
|
|
|
// Disable the channel. Keep the IEs enabled so the irqs still fire.
|
2023-04-17 00:04:54 +02:00
|
|
|
unsafe {
|
|
|
|
ch.cr().write(|w| {
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
})
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub fn is_running(&mut self) -> bool {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.cr().read() }.en()
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the total remaining transfers for the channel
|
|
|
|
/// Note: this will be zero for transfers that completed without cancellation.
|
2023-04-17 00:04:54 +02:00
|
|
|
pub fn get_remaining_transfers(&self) -> u16 {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.ndtr().read() }.ndt()
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
pub fn blocking_wait(mut self) {
|
|
|
|
while self.is_running() {}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
|
|
|
|
fence(Ordering::SeqCst);
|
2021-12-08 03:30:07 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
core::mem::forget(self);
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
}
|
2022-03-08 20:52:33 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
impl<'a, C: Channel> Drop for Transfer<'a, C> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.request_stop();
|
|
|
|
while self.is_running() {}
|
2022-03-08 20:52:33 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 20:52:33 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
impl<'a, C: Channel> Unpin for Transfer<'a, C> {}
|
|
|
|
impl<'a, C: Channel> Future for Transfer<'a, C> {
|
|
|
|
type Output = ();
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
STATE.ch_wakers[self.channel.index()].register(cx.waker());
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
if self.is_running() {
|
|
|
|
Poll::Pending
|
|
|
|
} else {
|
|
|
|
Poll::Ready(())
|
2022-03-08 20:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
2023-04-18 16:16:33 +02:00
|
|
|
|
|
|
|
// ==================================
|
|
|
|
|
|
|
|
pub struct DoubleBuffered<'a, C: Channel, W: Word> {
|
|
|
|
channel: PeripheralRef<'a, C>,
|
|
|
|
_phantom: PhantomData<W>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C: Channel, W: Word> DoubleBuffered<'a, C, W> {
|
|
|
|
pub unsafe fn new_read(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
_request: Request,
|
|
|
|
peri_addr: *mut W,
|
|
|
|
buf0: *mut W,
|
|
|
|
buf1: *mut W,
|
|
|
|
len: usize,
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(channel);
|
|
|
|
assert!(len > 0 && len <= 0xFFFF);
|
|
|
|
|
|
|
|
let dir = Dir::PeripheralToMemory;
|
2023-04-18 20:56:23 +02:00
|
|
|
let data_size = W::size();
|
2023-04-18 16:16:33 +02:00
|
|
|
|
|
|
|
let channel_number = channel.num();
|
|
|
|
let dma = channel.regs();
|
|
|
|
|
|
|
|
// "Preceding reads and writes cannot be moved past subsequent writes."
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let mut this = Self {
|
|
|
|
channel,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
};
|
|
|
|
this.clear_irqs();
|
|
|
|
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
super::dmamux::configure_dmamux(&mut *this.channel, _request);
|
|
|
|
|
|
|
|
let ch = dma.st(channel_number);
|
|
|
|
ch.par().write_value(peri_addr as u32);
|
|
|
|
ch.m0ar().write_value(buf0 as u32);
|
|
|
|
ch.m1ar().write_value(buf1 as u32);
|
|
|
|
ch.ndtr().write_value(regs::Ndtr(len as _));
|
|
|
|
ch.fcr().write(|w| {
|
|
|
|
if let Some(fth) = options.fifo_threshold {
|
|
|
|
// FIFO mode
|
|
|
|
w.set_dmdis(vals::Dmdis::DISABLED);
|
|
|
|
w.set_fth(fth.into());
|
|
|
|
} else {
|
|
|
|
// Direct mode
|
|
|
|
w.set_dmdis(vals::Dmdis::ENABLED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ch.cr().write(|w| {
|
|
|
|
w.set_dir(dir.into());
|
|
|
|
w.set_msize(data_size.into());
|
|
|
|
w.set_psize(data_size.into());
|
|
|
|
w.set_pl(vals::Pl::VERYHIGH);
|
|
|
|
w.set_minc(vals::Inc::INCREMENTED);
|
|
|
|
w.set_pinc(vals::Inc::FIXED);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
#[cfg(dma_v1)]
|
|
|
|
w.set_trbuff(true);
|
|
|
|
|
|
|
|
#[cfg(dma_v2)]
|
|
|
|
w.set_chsel(_request);
|
|
|
|
|
|
|
|
w.set_pburst(options.pburst.into());
|
|
|
|
w.set_mburst(options.mburst.into());
|
|
|
|
w.set_pfctrl(options.flow_ctrl.into());
|
|
|
|
|
|
|
|
w.set_en(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_irqs(&mut self) {
|
|
|
|
let channel_number = self.channel.num();
|
|
|
|
let dma = self.channel.regs();
|
|
|
|
let isrn = channel_number / 4;
|
|
|
|
let isrbit = channel_number % 4;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
dma.ifcr(isrn).write(|w| {
|
|
|
|
w.set_tcif(isrbit, true);
|
|
|
|
w.set_teif(isrbit, true);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn set_buffer0(&mut self, buffer: *mut W) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
ch.m0ar().write_value(buffer as _);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn set_buffer1(&mut self, buffer: *mut W) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
ch.m1ar().write_value(buffer as _);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_buffer0_accessible(&mut self) -> bool {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.cr().read() }.ct() == vals::Ct::MEMORY1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_waker(&mut self, waker: &Waker) {
|
|
|
|
STATE.ch_wakers[self.channel.index()].register(waker);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn request_stop(&mut self) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
|
|
|
|
// Disable the channel. Keep the IEs enabled so the irqs still fire.
|
|
|
|
unsafe {
|
|
|
|
ch.cr().write(|w| {
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_running(&mut self) -> bool {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.cr().read() }.en()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the total remaining transfers for the channel
|
|
|
|
/// Note: this will be zero for transfers that completed without cancellation.
|
|
|
|
pub fn get_remaining_transfers(&self) -> u16 {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.ndtr().read() }.ndt()
|
|
|
|
}
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
2023-04-18 16:16:33 +02:00
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
impl<'a, C: Channel, W: Word> Drop for DoubleBuffered<'a, C, W> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.request_stop();
|
2023-04-18 16:16:33 +02:00
|
|
|
while self.is_running() {}
|
|
|
|
|
|
|
|
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
|
|
|
|
fence(Ordering::SeqCst);
|
2023-04-26 10:51:23 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-18 16:16:33 +02:00
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
// ==============================
|
|
|
|
|
|
|
|
impl<C: Channel> DmaCtrl for C {
|
|
|
|
fn tcif(&self) -> bool {
|
|
|
|
let channel_number = self.num();
|
|
|
|
let dma = self.regs();
|
|
|
|
let isrn = channel_number / 4;
|
|
|
|
let isrbit = channel_number % 4;
|
|
|
|
|
|
|
|
unsafe { dma.isr(isrn).read() }.tcif(isrbit)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_tcif(&mut self) {
|
|
|
|
let channel_number = self.num();
|
|
|
|
let dma = self.regs();
|
|
|
|
let isrn = channel_number / 4;
|
|
|
|
let isrbit = channel_number % 4;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
dma.ifcr(isrn).write(|w| {
|
|
|
|
w.set_tcif(isrbit, true);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ndtr(&self) -> usize {
|
|
|
|
let ch = self.regs().st(self.num());
|
|
|
|
unsafe { ch.ndtr().read() }.ndt() as usize
|
2023-04-18 16:16:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 10:51:23 +02:00
|
|
|
pub struct RingBuffer<'a, C: Channel, W: Word> {
|
|
|
|
cr: regs::Cr,
|
|
|
|
channel: PeripheralRef<'a, C>,
|
|
|
|
ringbuf: DmaRingBuffer<'a, W>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C: Channel, W: Word> RingBuffer<'a, C, W> {
|
|
|
|
pub unsafe fn new_read(
|
|
|
|
channel: impl Peripheral<P = C> + 'a,
|
|
|
|
_request: Request,
|
|
|
|
peri_addr: *mut W,
|
|
|
|
buffer: &'a mut [W],
|
|
|
|
options: TransferOptions,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(channel);
|
|
|
|
|
|
|
|
let len = buffer.len();
|
|
|
|
assert!(len > 0 && len <= 0xFFFF);
|
|
|
|
|
|
|
|
let dir = Dir::PeripheralToMemory;
|
|
|
|
let data_size = W::size();
|
|
|
|
|
|
|
|
let channel_number = channel.num();
|
|
|
|
let dma = channel.regs();
|
|
|
|
|
|
|
|
// "Preceding reads and writes cannot be moved past subsequent writes."
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let mut w = regs::Cr(0);
|
|
|
|
w.set_dir(dir.into());
|
|
|
|
w.set_msize(data_size.into());
|
|
|
|
w.set_psize(data_size.into());
|
|
|
|
w.set_pl(vals::Pl::VERYHIGH);
|
|
|
|
w.set_minc(vals::Inc::INCREMENTED);
|
|
|
|
w.set_pinc(vals::Inc::FIXED);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(false);
|
|
|
|
w.set_circ(vals::Circ::ENABLED);
|
|
|
|
#[cfg(dma_v1)]
|
|
|
|
w.set_trbuff(true);
|
|
|
|
#[cfg(dma_v2)]
|
|
|
|
w.set_chsel(_request);
|
|
|
|
w.set_pburst(options.pburst.into());
|
|
|
|
w.set_mburst(options.mburst.into());
|
|
|
|
w.set_pfctrl(options.flow_ctrl.into());
|
|
|
|
w.set_en(true);
|
|
|
|
|
|
|
|
let buffer_ptr = buffer.as_mut_ptr();
|
|
|
|
let mut this = Self {
|
|
|
|
channel,
|
|
|
|
cr: w,
|
|
|
|
ringbuf: DmaRingBuffer::new(buffer),
|
|
|
|
};
|
|
|
|
this.clear_irqs();
|
|
|
|
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
super::dmamux::configure_dmamux(&mut *this.channel, _request);
|
|
|
|
|
|
|
|
let ch = dma.st(channel_number);
|
|
|
|
ch.par().write_value(peri_addr as u32);
|
|
|
|
ch.m0ar().write_value(buffer_ptr as u32);
|
|
|
|
ch.ndtr().write_value(regs::Ndtr(len as _));
|
|
|
|
ch.fcr().write(|w| {
|
|
|
|
if let Some(fth) = options.fifo_threshold {
|
|
|
|
// FIFO mode
|
|
|
|
w.set_dmdis(vals::Dmdis::DISABLED);
|
|
|
|
w.set_fth(fth.into());
|
|
|
|
} else {
|
|
|
|
// Direct mode
|
|
|
|
w.set_dmdis(vals::Dmdis::ENABLED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(&mut self) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.cr().write_value(self.cr) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.ringbuf.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read bytes from the ring buffer
|
|
|
|
/// OverrunError is returned if the portion to be read was overwritten by the DMA controller.
|
|
|
|
pub fn read(&mut self, buf: &mut [W]) -> Result<usize, OverrunError> {
|
|
|
|
self.ringbuf.read(&mut *self.channel, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_irqs(&mut self) {
|
|
|
|
let channel_number = self.channel.num();
|
|
|
|
let dma = self.channel.regs();
|
|
|
|
let isrn = channel_number / 4;
|
|
|
|
let isrbit = channel_number % 4;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
dma.ifcr(isrn).write(|w| {
|
|
|
|
w.set_tcif(isrbit, true);
|
|
|
|
w.set_teif(isrbit, true);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn request_stop(&mut self) {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
|
|
|
|
// Disable the channel. Keep the IEs enabled so the irqs still fire.
|
|
|
|
unsafe {
|
|
|
|
ch.cr().write(|w| {
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_running(&mut self) -> bool {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.cr().read() }.en()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the total remaining transfers for the channel
|
|
|
|
/// Note: this will be zero for transfers that completed without cancellation.
|
|
|
|
pub fn get_remaining_transfers(&self) -> usize {
|
|
|
|
let ch = self.channel.regs().st(self.channel.num());
|
|
|
|
unsafe { ch.ndtr().read() }.ndt() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_ndtr(&mut self, ndtr: usize) {
|
|
|
|
self.ringbuf.ndtr = ndtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C: Channel, W: Word> Drop for RingBuffer<'a, C, W> {
|
2023-04-18 16:16:33 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.request_stop();
|
|
|
|
while self.is_running() {}
|
|
|
|
|
|
|
|
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|