2021-07-03 22:58:59 +02:00
|
|
|
use core::future::Future;
|
|
|
|
use core::task::Poll;
|
|
|
|
|
|
|
|
use atomic_polyfill::{AtomicU8, Ordering};
|
|
|
|
use embassy::interrupt::{Interrupt, InterruptExt};
|
|
|
|
use embassy::util::{AtomicWaker, OnDrop};
|
|
|
|
use futures::future::poll_fn;
|
|
|
|
|
2021-07-08 20:55:27 +02:00
|
|
|
use crate::dma_traits::{ReadDma, WriteDma};
|
2021-07-03 22:58:59 +02:00
|
|
|
use crate::interrupt;
|
|
|
|
use crate::pac;
|
|
|
|
use crate::pac::bdma::vals;
|
2021-07-08 20:55:27 +02:00
|
|
|
use crate::rcc::sealed::RccPeripheral;
|
2021-07-03 22:58:59 +02:00
|
|
|
|
|
|
|
const CH_COUNT: usize = pac::peripheral_count!(DMA) * 8;
|
|
|
|
const CH_STATUS_NONE: u8 = 0;
|
|
|
|
const CH_STATUS_COMPLETED: u8 = 1;
|
|
|
|
const CH_STATUS_ERROR: u8 = 2;
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
ch_wakers: [AtomicWaker; CH_COUNT],
|
|
|
|
ch_status: [AtomicU8; CH_COUNT],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
const fn new() -> Self {
|
|
|
|
const AW: AtomicWaker = AtomicWaker::new();
|
|
|
|
const AU: AtomicU8 = AtomicU8::new(CH_STATUS_NONE);
|
|
|
|
Self {
|
|
|
|
ch_wakers: [AW; CH_COUNT],
|
|
|
|
ch_status: [AU; CH_COUNT],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static STATE: State = State::new();
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2021-07-04 20:40:33 +02:00
|
|
|
pub(crate) async unsafe fn transfer_p2m(
|
|
|
|
regs: pac::bdma::Ch,
|
|
|
|
state_number: usize,
|
|
|
|
src: *const u8,
|
|
|
|
dst: &mut [u8],
|
|
|
|
) {
|
2021-07-03 22:58:59 +02:00
|
|
|
// ndtr is max 16 bits.
|
|
|
|
assert!(dst.len() <= 0xFFFF);
|
|
|
|
|
|
|
|
// Reset status
|
|
|
|
// Generate a DMB here to flush the store buffer (M7) before enabling the DMA
|
|
|
|
STATE.ch_status[state_number].store(CH_STATUS_NONE, Ordering::Release);
|
|
|
|
|
|
|
|
let on_drop = OnDrop::new(|| unsafe {
|
|
|
|
regs.cr().modify(|w| {
|
|
|
|
w.set_tcie(false);
|
|
|
|
w.set_teie(false);
|
|
|
|
w.set_en(false);
|
|
|
|
});
|
|
|
|
while regs.cr().read().en() {}
|
|
|
|
});
|
|
|
|
|
2021-07-08 20:55:27 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
crate::dmamux::configure_channel(1, 2);
|
|
|
|
|
2021-07-03 22:58:59 +02:00
|
|
|
regs.par().write_value(src as u32);
|
|
|
|
regs.mar().write_value(dst.as_mut_ptr() as u32);
|
|
|
|
regs.ndtr().write(|w| w.set_ndt(dst.len() as u16));
|
|
|
|
regs.cr().write(|w| {
|
|
|
|
w.set_psize(vals::Size::BITS8);
|
|
|
|
w.set_msize(vals::Size::BITS8);
|
|
|
|
w.set_minc(vals::Inc::ENABLED);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
w.set_en(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
|
|
|
STATE.ch_wakers[state_number].register(cx.waker());
|
|
|
|
match STATE.ch_status[state_number].load(Ordering::Acquire) {
|
|
|
|
CH_STATUS_NONE => Poll::Pending,
|
|
|
|
x => Poll::Ready(x),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// TODO handle error
|
|
|
|
assert!(res == CH_STATUS_COMPLETED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2021-07-04 20:40:33 +02:00
|
|
|
pub(crate) async unsafe fn transfer_m2p(
|
|
|
|
regs: pac::bdma::Ch,
|
|
|
|
state_number: usize,
|
|
|
|
src: &[u8],
|
|
|
|
dst: *mut u8,
|
2021-07-08 21:59:39 +02:00
|
|
|
#[cfg(dmamux)] dmamux_regs: pac::dmamux::Dmamux,
|
2021-07-08 20:55:27 +02:00
|
|
|
#[cfg(dmamux)] dmamux_ch_num: u8,
|
|
|
|
#[cfg(dmamux)] request: u8,
|
2021-07-04 20:40:33 +02:00
|
|
|
) {
|
2021-07-03 22:58:59 +02:00
|
|
|
// ndtr is max 16 bits.
|
|
|
|
assert!(src.len() <= 0xFFFF);
|
|
|
|
|
|
|
|
// Reset status
|
|
|
|
// Generate a DMB here to flush the store buffer (M7) before enabling the DMA
|
|
|
|
STATE.ch_status[state_number].store(CH_STATUS_NONE, Ordering::Release);
|
|
|
|
|
|
|
|
let on_drop = OnDrop::new(|| unsafe {
|
|
|
|
regs.cr().modify(|w| {
|
|
|
|
w.set_tcie(false);
|
|
|
|
w.set_teie(false);
|
|
|
|
w.set_en(false);
|
|
|
|
});
|
|
|
|
while regs.cr().read().en() {}
|
|
|
|
});
|
|
|
|
|
2021-07-08 20:55:27 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
crate::dmamux::configure_dmamux(dmamux_regs, dmamux_ch_num, request);
|
|
|
|
|
2021-07-03 22:58:59 +02:00
|
|
|
regs.par().write_value(dst as u32);
|
|
|
|
regs.mar().write_value(src.as_ptr() as u32);
|
|
|
|
regs.ndtr().write(|w| w.set_ndt(src.len() as u16));
|
|
|
|
regs.cr().write(|w| {
|
|
|
|
w.set_psize(vals::Size::BITS8);
|
|
|
|
w.set_msize(vals::Size::BITS8);
|
|
|
|
w.set_minc(vals::Inc::ENABLED);
|
|
|
|
w.set_dir(vals::Dir::FROMMEMORY);
|
|
|
|
w.set_teie(true);
|
|
|
|
w.set_tcie(true);
|
|
|
|
w.set_en(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
|
|
|
STATE.ch_wakers[state_number].register(cx.waker());
|
|
|
|
match STATE.ch_status[state_number].load(Ordering::Acquire) {
|
|
|
|
CH_STATUS_NONE => Poll::Pending,
|
|
|
|
x => Poll::Ready(x),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// TODO handle error
|
|
|
|
assert!(res == CH_STATUS_COMPLETED);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn on_irq() {
|
|
|
|
pac::peripherals! {
|
|
|
|
(bdma, $dma:ident) => {
|
|
|
|
let isr = pac::$dma.isr().read();
|
|
|
|
pac::$dma.ifcr().write_value(isr);
|
|
|
|
let dman = <crate::peripherals::$dma as sealed::Dma>::num() as usize;
|
|
|
|
|
|
|
|
for chn in 0..7 {
|
|
|
|
let n = dman * 8 + chn;
|
|
|
|
if isr.teif(chn) {
|
|
|
|
STATE.ch_status[n].store(CH_STATUS_ERROR, Ordering::Relaxed);
|
|
|
|
STATE.ch_wakers[n].wake();
|
|
|
|
} else if isr.tcif(chn) {
|
|
|
|
STATE.ch_status[n].store(CH_STATUS_COMPLETED, Ordering::Relaxed);
|
|
|
|
STATE.ch_wakers[n].wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// safety: must be called only once
|
|
|
|
pub(crate) unsafe fn init() {
|
|
|
|
pac::interrupts! {
|
|
|
|
(DMA, $irq:ident) => {
|
|
|
|
crate::interrupt::$irq::steal().enable();
|
|
|
|
};
|
|
|
|
}
|
2021-07-04 23:34:37 +02:00
|
|
|
pac::peripherals! {
|
|
|
|
(bdma, DMA1) => {
|
2021-07-08 20:55:27 +02:00
|
|
|
//critical_section::with(|_| {
|
|
|
|
//pac::RCC.ahbenr().modify(|w| w.set_dmaen(true));
|
|
|
|
//});
|
|
|
|
crate::peripherals::DMA1::enable();
|
2021-07-04 23:34:37 +02:00
|
|
|
};
|
|
|
|
}
|
2021-07-03 22:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Dma {
|
|
|
|
fn num() -> u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Channel {
|
2021-07-04 20:40:33 +02:00
|
|
|
fn dma_regs() -> pac::bdma::Dma;
|
2021-07-03 22:58:59 +02:00
|
|
|
|
|
|
|
fn state_num(&self) -> usize;
|
|
|
|
|
|
|
|
fn ch_num(&self) -> u8;
|
|
|
|
|
|
|
|
fn regs(&self) -> pac::bdma::Ch {
|
|
|
|
Self::dma_regs().ch(self.ch_num() as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Dma: sealed::Dma + Sized {}
|
|
|
|
pub trait Channel: sealed::Channel + Sized {}
|
|
|
|
|
|
|
|
macro_rules! impl_dma {
|
|
|
|
($peri:ident, $num:expr) => {
|
|
|
|
impl Dma for crate::peripherals::$peri {}
|
|
|
|
impl sealed::Dma for crate::peripherals::$peri {
|
|
|
|
fn num() -> u8 {
|
|
|
|
$num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_dma_channel {
|
|
|
|
($channel_peri:ident, $dma_peri:ident, $dma_num:expr, $ch_num:expr) => {
|
|
|
|
impl Channel for crate::peripherals::$channel_peri {}
|
|
|
|
impl sealed::Channel for crate::peripherals::$channel_peri {
|
|
|
|
#[inline]
|
2021-07-04 20:40:33 +02:00
|
|
|
fn dma_regs() -> pac::bdma::Dma {
|
|
|
|
crate::pac::$dma_peri
|
2021-07-03 22:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn state_num(&self) -> usize {
|
|
|
|
($dma_num * 8) + $ch_num
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ch_num(&self) -> u8 {
|
|
|
|
$ch_num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:55:27 +02:00
|
|
|
#[cfg(not(dmamux))]
|
2021-07-03 22:58:59 +02:00
|
|
|
impl<T> WriteDma<T> for crate::peripherals::$channel_peri
|
|
|
|
where
|
2021-07-08 20:55:27 +02:00
|
|
|
Self: crate::dmamux::sealed::PeripheralChannel<T, crate::dmamux::M2P>,
|
2021-07-03 22:58:59 +02:00
|
|
|
T: 'static,
|
|
|
|
{
|
|
|
|
type WriteDmaFuture<'a> = impl Future<Output = ()>;
|
|
|
|
|
|
|
|
fn transfer<'a>(&'a mut self, buf: &'a [u8], dst: *mut u8) -> Self::WriteDmaFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
2021-07-04 20:40:33 +02:00
|
|
|
use sealed::Channel as _Channel;
|
|
|
|
|
|
|
|
let state_num = self.state_num();
|
|
|
|
let regs = self.regs();
|
2021-07-08 20:55:27 +02:00
|
|
|
|
2021-07-04 20:40:33 +02:00
|
|
|
unsafe { transfer_m2p(regs, state_num, buf, dst) }
|
2021-07-03 22:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:55:27 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
impl<T> WriteDma<T> for crate::peripherals::$channel_peri
|
|
|
|
where
|
|
|
|
Self: crate::dmamux::sealed::PeripheralChannel<T, crate::dmamux::M2P>,
|
|
|
|
T: 'static,
|
|
|
|
{
|
|
|
|
type WriteDmaFuture<'a> = impl Future<Output = ()>;
|
|
|
|
|
|
|
|
fn transfer<'a>(&'a mut self, buf: &'a [u8], dst: *mut u8) -> Self::WriteDmaFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
|
|
|
use sealed::Channel as _Channel;
|
|
|
|
|
|
|
|
let state_num = self.state_num();
|
|
|
|
let regs = self.regs();
|
|
|
|
|
|
|
|
use crate::dmamux::sealed::Channel as _MuxChannel;
|
|
|
|
use crate::dmamux::sealed::PeripheralChannel;
|
|
|
|
let dmamux_regs = self.dmamux_regs();
|
2021-07-08 21:59:39 +02:00
|
|
|
let dmamux_ch_num = self.dmamux_ch_num();
|
2021-07-08 20:55:27 +02:00
|
|
|
let request = PeripheralChannel::<T, crate::dmamux::M2P>::request(self);
|
|
|
|
unsafe {
|
|
|
|
transfer_m2p(
|
|
|
|
regs,
|
|
|
|
state_num,
|
|
|
|
buf,
|
|
|
|
dst,
|
|
|
|
dmamux_regs,
|
|
|
|
dmamux_ch_num,
|
|
|
|
request,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:58:59 +02:00
|
|
|
impl<T> ReadDma<T> for crate::peripherals::$channel_peri
|
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
{
|
|
|
|
type ReadDmaFuture<'a> = impl Future<Output = ()>;
|
|
|
|
|
|
|
|
fn transfer<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
src: *const u8,
|
|
|
|
buf: &'a mut [u8],
|
|
|
|
) -> Self::ReadDmaFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
2021-07-04 20:40:33 +02:00
|
|
|
use sealed::Channel as _Channel;
|
|
|
|
|
|
|
|
let state_num = self.state_num();
|
|
|
|
let regs = self.regs();
|
|
|
|
unsafe { transfer_p2m(regs, state_num, src, buf) }
|
2021-07-03 22:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pac::peripherals! {
|
|
|
|
(bdma, DMA1) => {
|
|
|
|
impl_dma!(DMA1, 0);
|
|
|
|
pac::dma_channels! {
|
|
|
|
($channel_peri:ident, DMA1, $channel_num:expr) => {
|
|
|
|
impl_dma_channel!($channel_peri, DMA1, 0, $channel_num);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(bdma, DMA2) => {
|
|
|
|
impl_dma!(DMA2, 1);
|
|
|
|
pac::dma_channels! {
|
|
|
|
($channel_peri:ident, DMA2, $channel_num:expr) => {
|
|
|
|
impl_dma_channel!($channel_peri, DMA2, 1, $channel_num);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2021-07-09 15:12:11 +02:00
|
|
|
// Because H7cm changes the naming
|
|
|
|
(bdma, BDMA) => {
|
|
|
|
impl_dma!(BDMA, 0);
|
|
|
|
pac::dma_channels! {
|
|
|
|
($channel_peri:ident, DMA1, $channel_num:expr) => {
|
|
|
|
impl_dma_channel!($channel_peri, BDMA, 0, $channel_num);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2021-07-03 22:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pac::interrupts! {
|
|
|
|
(DMA, $irq:ident) => {
|
|
|
|
#[crate::interrupt]
|
|
|
|
unsafe fn $irq () {
|
|
|
|
on_irq()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-07-04 23:34:37 +02:00
|
|
|
|
|
|
|
#[cfg(usart)]
|
|
|
|
use crate::usart;
|
2021-07-08 20:55:27 +02:00
|
|
|
|
|
|
|
#[cfg(not(dmamux))]
|
2021-07-04 23:34:37 +02:00
|
|
|
pac::peripheral_dma_channels! {
|
|
|
|
($peri:ident, usart, $kind:ident, RX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
|
|
|
|
|
|
|
($peri:ident, usart, $kind:ident, TX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
2021-07-08 20:55:27 +02:00
|
|
|
|
|
|
|
($peri:ident, uart, $kind:ident, RX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
|
|
|
|
|
|
|
($peri:ident, uart, $kind:ident, TX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
pac::peripherals! {
|
|
|
|
(usart, $peri:ident) => {
|
|
|
|
pac::dma_channels! {
|
|
|
|
($channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(uart, $peri:ident) => {
|
|
|
|
pac::dma_channels! {
|
|
|
|
($channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
|
|
|
|
impl usart::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
impl usart::sealed::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2021-07-04 23:34:37 +02:00
|
|
|
}
|