2021-07-20 17:38:16 +02:00
|
|
|
use core::future::Future;
|
2021-08-11 01:40:02 +02:00
|
|
|
use core::sync::atomic::{fence, Ordering};
|
2021-09-29 02:37:39 +02:00
|
|
|
use core::task::{Poll, Waker};
|
2021-07-15 05:42:06 +02:00
|
|
|
|
|
|
|
use embassy::interrupt::{Interrupt, InterruptExt};
|
2021-09-11 01:53:53 +02:00
|
|
|
use embassy::waitqueue::AtomicWaker;
|
|
|
|
use embassy_hal_common::drop::OnDrop;
|
2021-07-15 05:42:06 +02:00
|
|
|
use futures::future::poll_fn;
|
|
|
|
|
|
|
|
use crate::interrupt;
|
|
|
|
use crate::pac;
|
|
|
|
use crate::pac::dma::{regs, vals};
|
|
|
|
use crate::rcc::sealed::RccPeripheral;
|
|
|
|
|
|
|
|
use super::{Channel, Request};
|
|
|
|
|
|
|
|
const CH_COUNT: usize = pac::peripheral_count!(DMA) * 8;
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
ch_wakers: [AtomicWaker; CH_COUNT],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
const fn new() -> Self {
|
|
|
|
const AW: AtomicWaker = AtomicWaker::new();
|
|
|
|
Self {
|
|
|
|
ch_wakers: [AW; CH_COUNT],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static STATE: State = State::new();
|
|
|
|
|
|
|
|
//async unsafe fn do_transfer(ch: &mut impl Channel, ch_func: u8, src: *const u8, dst: &mut [u8]) {
|
|
|
|
|
2021-07-16 20:40:25 +02:00
|
|
|
#[allow(unused)]
|
2021-07-20 17:38:16 +02:00
|
|
|
pub(crate) unsafe fn do_transfer(
|
2021-07-15 05:42:06 +02:00
|
|
|
dma: pac::dma::Dma,
|
|
|
|
channel_number: u8,
|
|
|
|
state_number: u8,
|
|
|
|
request: Request,
|
|
|
|
dir: vals::Dir,
|
|
|
|
peri_addr: *const u8,
|
|
|
|
mem_addr: *mut u8,
|
|
|
|
mem_len: usize,
|
2021-07-20 21:20:16 +02:00
|
|
|
incr_mem: bool,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)] dmamux_regs: pac::dmamux::Dmamux,
|
|
|
|
#[cfg(dmamux)] dmamux_ch_num: u8,
|
2021-07-20 17:38:16 +02:00
|
|
|
) -> impl Future<Output = ()> {
|
2021-07-15 05:42:06 +02:00
|
|
|
// ndtr is max 16 bits.
|
|
|
|
assert!(mem_len <= 0xFFFF);
|
|
|
|
|
|
|
|
// Reset status
|
2021-07-22 00:12:06 +02:00
|
|
|
let isrn = channel_number as usize / 4;
|
|
|
|
let isrbit = channel_number as usize % 4;
|
|
|
|
dma.ifcr(isrn).write(|w| {
|
|
|
|
w.set_tcif(isrbit, true);
|
|
|
|
w.set_teif(isrbit, true);
|
|
|
|
});
|
2021-07-15 05:42:06 +02:00
|
|
|
|
|
|
|
let ch = dma.st(channel_number as _);
|
|
|
|
|
2021-07-20 17:38:16 +02:00
|
|
|
let on_drop = OnDrop::new(move || unsafe {
|
2021-09-29 02:37:39 +02:00
|
|
|
_stop(&dma, channel_number);
|
2021-07-16 20:40:25 +02:00
|
|
|
});
|
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
super::dmamux::configure_dmamux(dmamux_regs, dmamux_ch_num, request);
|
|
|
|
|
2021-08-11 01:40:02 +02:00
|
|
|
// "Preceding reads and writes cannot be moved past subsequent writes."
|
|
|
|
fence(Ordering::Release);
|
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
unsafe {
|
|
|
|
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 _));
|
|
|
|
ch.cr().write(|w| {
|
|
|
|
w.set_dir(dir);
|
|
|
|
w.set_msize(vals::Size::BITS8);
|
|
|
|
w.set_psize(vals::Size::BITS8);
|
2021-07-21 20:09:24 +02:00
|
|
|
if incr_mem {
|
|
|
|
w.set_minc(vals::Inc::INCREMENTED);
|
|
|
|
} else {
|
|
|
|
w.set_minc(vals::Inc::FIXED);
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
w.set_pinc(vals::Inc::FIXED);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
2021-07-17 08:01:20 +02:00
|
|
|
#[cfg(dma_v1)]
|
2021-07-16 20:40:25 +02:00
|
|
|
w.set_trbuff(true);
|
2021-07-15 05:42:06 +02:00
|
|
|
|
|
|
|
#[cfg(dma_v2)]
|
|
|
|
w.set_chsel(request);
|
2021-07-21 22:45:43 +02:00
|
|
|
|
|
|
|
w.set_en(true);
|
2021-07-15 05:42:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-20 17:38:16 +02:00
|
|
|
async move {
|
|
|
|
let res = poll_fn(|cx| {
|
2021-07-21 22:45:43 +02:00
|
|
|
let n = state_number as usize;
|
2021-07-20 17:38:16 +02:00
|
|
|
STATE.ch_wakers[n].register(cx.waker());
|
2021-07-22 00:12:06 +02:00
|
|
|
|
|
|
|
let isr = dma.isr(isrn).read();
|
|
|
|
|
|
|
|
// TODO handle error
|
|
|
|
assert!(!isr.teif(isrbit));
|
|
|
|
|
|
|
|
if isr.tcif(isrbit) {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
2021-07-20 17:38:16 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
drop(on_drop)
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 02:37:39 +02:00
|
|
|
/// Stops the DMA channel.
|
|
|
|
unsafe fn _stop(dma: &pac::dma::Dma, ch: u8) {
|
|
|
|
// get a handle on the channel itself
|
|
|
|
let ch = dma.st(ch as _);
|
|
|
|
|
|
|
|
// Disable the channel and interrupts with the default value.
|
|
|
|
ch.cr().write(|_| ());
|
|
|
|
|
|
|
|
// Wait for the transfer to complete when it was ongoing.
|
|
|
|
while ch.cr().read().en() {}
|
|
|
|
|
|
|
|
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
|
|
|
|
fence(Ordering::Acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the running status of the channel
|
2021-09-29 04:35:07 +02:00
|
|
|
unsafe fn _is_stopped(dma: &pac::dma::Dma, ch: u8) -> bool {
|
2021-09-29 02:37:39 +02:00
|
|
|
// get a handle on the channel itself
|
|
|
|
let ch = dma.st(ch as _);
|
|
|
|
|
|
|
|
// Wait for the transfer to complete when it was ongoing.
|
|
|
|
ch.cr().read().en()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the total remaining transfers for the channel
|
|
|
|
/// Note: this will be zero for transfers that completed without cancellation.
|
2021-09-29 04:35:07 +02:00
|
|
|
unsafe fn _get_remaining_transfers(dma: &pac::dma::Dma, ch: u8) -> u16 {
|
2021-09-29 02:37:39 +02:00
|
|
|
// get a handle on the channel itself
|
|
|
|
let ch = dma.st(ch as _);
|
|
|
|
// read the remaining transfer count. If this is zero, the transfer completed fully.
|
|
|
|
ch.ndtr().read().ndt()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the waker for the specified DMA channel
|
2021-09-29 04:35:07 +02:00
|
|
|
unsafe fn _set_waker(dma: &pac::dma::Dma, state_number: u8, waker: &Waker) {
|
2021-09-29 02:37:39 +02:00
|
|
|
let n = state_number as usize;
|
|
|
|
STATE.ch_wakers[n].register(waker);
|
|
|
|
}
|
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
macro_rules! dma_num {
|
|
|
|
(DMA1) => {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
(DMA2) => {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_irq() {
|
|
|
|
pac::peripherals! {
|
|
|
|
(dma, $dma:ident) => {
|
|
|
|
for isrn in 0..2 {
|
|
|
|
let isr = pac::$dma.isr(isrn).read();
|
|
|
|
|
|
|
|
for chn in 0..4 {
|
2021-07-22 00:12:06 +02:00
|
|
|
let cr = pac::$dma.st(isrn * 4 + chn).cr();
|
|
|
|
|
|
|
|
if isr.tcif(chn) && cr.read().tcie() {
|
|
|
|
cr.write(|_| ()); // Disable channel interrupts with the default value.
|
|
|
|
let n = dma_num!($dma) * 8 + isrn * 4 + chn;
|
2021-07-15 05:42:06 +02:00
|
|
|
STATE.ch_wakers[n].wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// safety: must be called only once
|
|
|
|
pub(crate) unsafe fn init() {
|
|
|
|
pac::interrupts! {
|
2021-07-27 19:23:33 +02:00
|
|
|
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
|
2021-07-15 05:42:06 +02:00
|
|
|
interrupt::$irq::steal().enable();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
pac::peripherals! {
|
|
|
|
(dma, $peri:ident) => {
|
|
|
|
crate::peripherals::$peri::enable();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pac::dma_channels! {
|
2021-07-17 07:35:59 +02:00
|
|
|
($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $dmamux:tt) => {
|
2021-07-15 05:42:06 +02:00
|
|
|
impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri {}
|
|
|
|
impl Channel for crate::peripherals::$channel_peri
|
|
|
|
{
|
2021-08-04 19:39:54 +02:00
|
|
|
type ReadFuture<'a> = impl Future<Output = ()> + 'a;
|
|
|
|
type WriteFuture<'a> = impl Future<Output = ()> + 'a;
|
2021-07-15 05:42:06 +02:00
|
|
|
|
|
|
|
fn read<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
request: Request,
|
|
|
|
src: *mut u8,
|
|
|
|
buf: &'a mut [u8],
|
|
|
|
) -> Self::ReadFuture<'a> {
|
|
|
|
unsafe {
|
|
|
|
do_transfer(
|
|
|
|
crate::pac::$dma_peri,
|
|
|
|
$channel_num,
|
|
|
|
(dma_num!($dma_peri) * 8) + $channel_num,
|
|
|
|
request,
|
|
|
|
vals::Dir::PERIPHERALTOMEMORY,
|
|
|
|
src,
|
|
|
|
buf.as_mut_ptr(),
|
|
|
|
buf.len(),
|
2021-07-20 21:20:16 +02:00
|
|
|
true,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)]
|
2021-07-17 07:49:49 +02:00
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)]
|
2021-07-17 07:49:49 +02:00
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
|
2021-07-15 05:42:06 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
request: Request,
|
|
|
|
buf: &'a [u8],
|
|
|
|
dst: *mut u8,
|
|
|
|
) -> Self::WriteFuture<'a> {
|
|
|
|
unsafe {
|
|
|
|
do_transfer(
|
|
|
|
crate::pac::$dma_peri,
|
|
|
|
$channel_num,
|
|
|
|
(dma_num!($dma_peri) * 8) + $channel_num,
|
|
|
|
request,
|
|
|
|
vals::Dir::MEMORYTOPERIPHERAL,
|
|
|
|
dst,
|
|
|
|
buf.as_ptr() as *mut u8,
|
|
|
|
buf.len(),
|
2021-07-20 21:20:16 +02:00
|
|
|
true,
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_x<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
request: Request,
|
|
|
|
word: &u8,
|
|
|
|
num: usize,
|
|
|
|
dst: *mut u8,
|
|
|
|
) -> Self::WriteFuture<'a> {
|
|
|
|
unsafe {
|
|
|
|
do_transfer(
|
|
|
|
crate::pac::$dma_peri,
|
|
|
|
$channel_num,
|
|
|
|
(dma_num!($dma_peri) * 8) + $channel_num,
|
|
|
|
request,
|
|
|
|
vals::Dir::MEMORYTOPERIPHERAL,
|
|
|
|
dst,
|
|
|
|
word as *const u8 as *mut u8,
|
|
|
|
num,
|
|
|
|
false,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)]
|
2021-07-17 07:49:49 +02:00
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(dmamux)]
|
2021-07-17 07:49:49 +02:00
|
|
|
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
|
2021-07-15 05:42:06 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 02:37:39 +02:00
|
|
|
fn stop <'a>(&'a mut self){
|
|
|
|
unsafe {_stop(&crate::pac::$dma_peri, $channel_num);}
|
|
|
|
}
|
|
|
|
fn is_stopped<'a>(&'a self) -> bool {
|
|
|
|
unsafe {_is_stopped(&crate::pac::$dma_peri, $channel_num)}
|
|
|
|
}
|
|
|
|
fn remaining_transfers<'a>(&'a mut self) -> u16 {
|
|
|
|
unsafe {_get_remaining_transfers(&crate::pac::$dma_peri, $channel_num)}
|
|
|
|
}
|
|
|
|
fn set_waker<'a>(&'a mut self, waker: &'a Waker) {
|
|
|
|
unsafe {_set_waker(&crate::pac::$dma_peri, $channel_num, waker )}
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pac::interrupts! {
|
2021-07-27 18:52:01 +02:00
|
|
|
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
|
2021-07-15 05:42:06 +02:00
|
|
|
#[crate::interrupt]
|
|
|
|
unsafe fn $irq () {
|
|
|
|
on_irq()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|