embassy/embassy-stm32/src/dma/dma.rs

254 lines
8.3 KiB
Rust
Raw Normal View History

2021-08-11 01:40:02 +02:00
use core::sync::atomic::{fence, Ordering};
2021-11-19 19:15:55 +01:00
use core::task::Waker;
2021-07-15 05:42:06 +02:00
use embassy::interrupt::{Interrupt, InterruptExt};
use embassy::waitqueue::AtomicWaker;
2021-07-15 05:42:06 +02:00
use crate::_generated::DMA_CHANNEL_COUNT;
2021-07-15 05:42:06 +02:00
use crate::interrupt;
use crate::pac;
use crate::pac::dma::{regs, vals};
2021-11-19 19:15:55 +01:00
use super::{Request, Word, WordSize};
2021-07-15 05:42:06 +02:00
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,
}
}
}
2021-07-15 05:42:06 +02:00
struct State {
ch_wakers: [AtomicWaker; DMA_CHANNEL_COUNT],
2021-07-15 05:42:06 +02:00
}
impl State {
const fn new() -> Self {
const AW: AtomicWaker = AtomicWaker::new();
Self {
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
pub(crate) unsafe fn init() {
foreach_interrupt! {
($peri:ident, dma, $block:ident, $signal_name:ident, $irq:ident) => {
2021-07-15 05:42:06 +02:00
interrupt::$irq::steal().enable();
};
}
crate::_generated::init_dma();
2021-07-15 05:42:06 +02:00
}
foreach_dma_channel! {
($channel_peri:ident, $dma_peri:ident, dma, $channel_num:expr, $index:expr, $dmamux:tt) => {
2021-11-19 19:15:55 +01:00
impl crate::dma::sealed::Channel for crate::peripherals::$channel_peri {
unsafe fn start_write<W: Word>(&mut self, request: Request, buf: *const [W], reg_addr: *mut W) {
let (ptr, len) = super::slice_ptr_parts(buf);
2021-11-19 19:15:55 +01:00
low_level_api::start_transfer(
2021-12-08 03:30:07 +01:00
pac::$dma_peri,
$channel_num,
2021-11-19 19:15:55 +01:00
request,
vals::Dir::MEMORYTOPERIPHERAL,
reg_addr as *const u32,
ptr as *mut u32,
len,
2021-11-19 19:15:55 +01:00
true,
vals::Size::from(W::bits()),
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
)
2021-07-15 05:42:06 +02:00
}
unsafe fn start_write_repeated<W: Word>(&mut self, request: Request, repeated: W, count: usize, reg_addr: *mut W) {
2021-11-19 19:15:55 +01:00
let buf = [repeated];
low_level_api::start_transfer(
2021-12-08 03:30:07 +01:00
pac::$dma_peri,
$channel_num,
2021-11-19 19:15:55 +01:00
request,
vals::Dir::MEMORYTOPERIPHERAL,
reg_addr as *const u32,
buf.as_ptr() as *mut u32,
count,
false,
vals::Size::from(W::bits()),
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
)
}
unsafe fn start_read<W: Word>(&mut self, request: Request, reg_addr: *const W, buf: *mut [W]) {
let (ptr, len) = super::slice_ptr_parts_mut(buf);
2021-11-19 19:15:55 +01:00
low_level_api::start_transfer(
2021-12-08 03:30:07 +01:00
pac::$dma_peri,
$channel_num,
2021-11-19 19:15:55 +01:00
request,
vals::Dir::PERIPHERALTOMEMORY,
reg_addr as *const u32,
ptr as *mut u32,
len,
2021-11-19 19:15:55 +01:00
true,
vals::Size::from(W::bits()),
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_REGS,
#[cfg(dmamux)]
<Self as super::dmamux::sealed::MuxChannel>::DMAMUX_CH_NUM,
);
}
2021-11-19 19:15:55 +01:00
fn request_stop(&mut self) {
2021-12-08 03:30:07 +01:00
unsafe {low_level_api::request_stop(pac::$dma_peri, $channel_num);}
}
2021-11-19 19:15:55 +01:00
fn is_running(&self) -> bool {
2021-12-08 03:30:07 +01:00
unsafe {low_level_api::is_running(pac::$dma_peri, $channel_num)}
}
2021-11-19 19:15:55 +01:00
fn remaining_transfers(&mut self) -> u16 {
2021-12-08 03:30:07 +01:00
unsafe {low_level_api::get_remaining_transfers(pac::$dma_peri, $channel_num)}
}
2021-11-19 19:15:55 +01:00
fn set_waker(&mut self, waker: &Waker) {
unsafe {low_level_api::set_waker($index, waker )}
2021-09-30 03:19:01 +02:00
}
fn on_irq() {
unsafe {
low_level_api::on_irq_inner(pac::$dma_peri, $channel_num, $index);
}
}
2021-07-15 05:42:06 +02:00
}
2021-11-19 19:15:55 +01:00
impl crate::dma::Channel for crate::peripherals::$channel_peri { }
2021-07-15 05:42:06 +02:00
};
}
2021-11-19 19:15:55 +01:00
mod low_level_api {
use super::*;
pub unsafe fn start_transfer(
2021-12-08 03:30:07 +01:00
dma: pac::dma::Dma,
channel_number: u8,
2021-11-19 19:15:55 +01:00
request: Request,
dir: vals::Dir,
peri_addr: *const u32,
mem_addr: *mut u32,
mem_len: usize,
incr_mem: bool,
data_size: vals::Size,
#[cfg(dmamux)] dmamux_regs: pac::dmamux::Dmamux,
#[cfg(dmamux)] dmamux_ch_num: u8,
) {
#[cfg(dmamux)]
super::super::dmamux::configure_dmamux(dmamux_regs, dmamux_ch_num, request);
// "Preceding reads and writes cannot be moved past subsequent writes."
fence(Ordering::SeqCst);
2021-12-08 03:30:07 +01:00
reset_status(dma, channel_number);
let ch = dma.st(channel_number as _);
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 _));
ch.cr().write(|w| {
w.set_dir(dir);
w.set_msize(data_size);
w.set_psize(data_size);
w.set_pl(vals::Pl::VERYHIGH);
if incr_mem {
w.set_minc(vals::Inc::INCREMENTED);
} else {
w.set_minc(vals::Inc::FIXED);
}
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_en(true);
});
}
/// Stops the DMA channel.
2021-12-08 03:30:07 +01:00
pub unsafe fn request_stop(dma: pac::dma::Dma, channel_number: u8) {
2021-11-19 19:15:55 +01:00
// get a handle on the channel itself
let ch = dma.st(channel_number as _);
2021-12-08 03:30:07 +01:00
// Disable the channel. Keep the IEs enabled so the irqs still fire.
ch.cr().write(|w| {
w.set_teie(true);
w.set_tcie(true);
});
2021-11-19 19:15:55 +01:00
// "Subsequent reads and writes cannot be moved ahead of preceding reads."
fence(Ordering::SeqCst);
}
/// Gets the running status of the channel
2021-12-08 03:30:07 +01:00
pub unsafe fn is_running(dma: pac::dma::Dma, ch: u8) -> bool {
2021-11-19 19:15:55 +01:00
// get a handle on the channel itself
let ch = dma.st(ch as _);
2021-12-08 03:30:07 +01:00
// Get whether it's enabled (running)
2021-11-19 19:15:55 +01:00
ch.cr().read().en()
}
/// Gets the total remaining transfers for the channel
/// Note: this will be zero for transfers that completed without cancellation.
2021-12-08 03:30:07 +01:00
pub unsafe fn get_remaining_transfers(dma: pac::dma::Dma, ch: u8) -> u16 {
2021-11-19 19:15:55 +01: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
pub unsafe fn set_waker(state_number: usize, waker: &Waker) {
STATE.ch_wakers[state_number].register(waker);
2021-11-19 19:15:55 +01:00
}
2021-12-08 03:30:07 +01:00
pub unsafe fn reset_status(dma: pac::dma::Dma, channel_number: u8) {
let isrn = channel_number as usize / 4;
let isrbit = channel_number as usize % 4;
2021-11-19 19:15:55 +01:00
dma.ifcr(isrn).write(|w| {
w.set_tcif(isrbit, true);
w.set_teif(isrbit, true);
});
}
/// Safety: Must be called with a matching set of parameters for a valid dma channel
pub unsafe fn on_irq_inner(dma: pac::dma::Dma, channel_num: u8, index: u8) {
let channel_num = channel_num as usize;
let index = index as usize;
let cr = dma.st(channel_num).cr();
let isr = dma.isr(channel_num / 4).read();
2022-03-09 02:55:08 +01:00
if isr.teif(channel_num % 4) {
panic!(
"DMA: error on DMA@{:08x} channel {}",
dma.0 as u32, channel_num
);
}
if isr.tcif(channel_num % 4) && cr.read().tcie() {
cr.write(|_| ()); // Disable channel interrupts with the default value.
STATE.ch_wakers[index].wake();
}
}
2021-11-19 19:15:55 +01:00
}