Skip to main content

ch32_hal/dma/
mod.rs

1//! DMA
2
3#![macro_use]
4
5use core::mem;
6
7use embassy_hal_internal::PeripheralType;
8
9use crate::{impl_peripheral, interrupt};
10
11// dma_bdma covers the V0/V1/V2/V3 "single DMA + optional BDMA" controller
12// and the CH32H4 DMA1+DMA2. The register layout is the same; H4 only renames
13// the accessors and adds the DMAMUX, both handled inside the driver.
14#[cfg(any(bdma, dma))]
15mod dma_bdma;
16#[cfg(dma_v4x7)]
17mod dma_v4x7;
18#[cfg(any(bdma, dma))]
19pub use dma_bdma::*;
20
21pub mod word;
22
23mod util;
24#[allow(unused_imports)]
25pub(crate) use util::*;
26
27pub(crate) mod ringbuffer;
28
29/// "No DMA" placeholder.
30///
31/// You may pass this in place of a real DMA channel when creating a driver
32/// to indicate it should not use DMA.
33///
34/// This often causes async functionality to not be available on the instance,
35/// leaving only blocking functionality.
36pub struct NoDma;
37
38impl_peripheral!(NoDma);
39
40#[derive(Debug, Copy, Clone, PartialEq, Eq)]
41#[cfg_attr(feature = "defmt", derive(defmt::Format))]
42enum Dir {
43    MemoryToPeripheral,
44    PeripheralToMemory,
45}
46
47/// DMA request selector.
48///
49/// On chips with fixed channel wiring this carries nothing — the channel
50/// itself is the request. On CH32H4 it is the DMAMUX request number
51/// (RM table 10-2) programmed into the channel's mux slot.
52#[cfg(not(ch32h4))]
53pub type Request = ();
54#[cfg(ch32h4)]
55pub type Request = u8;
56
57/// Marker for a channel routed through the chip's single DMAMUX.
58///
59/// Implemented for `DMA1_CH1`..`DMA1_CH8` and `DMA2_CH1`..`DMA2_CH8`, and
60/// nothing else. Signal traits (`TxDma<USART1>` and friends) blanket-impl
61/// over this, which is what keeps a request number from applying to a
62/// channel that cannot carry it. It only exists as a bound: the mux slot is
63/// derived from the channel's controller and number, not from the type.
64#[cfg(ch32h4)]
65pub trait MuxChannel: SealedMuxChannel {}
66
67#[cfg(ch32h4)]
68pub(crate) trait SealedMuxChannel {}
69
70pub(crate) trait SealedChannel {
71    fn id(&self) -> u8;
72}
73
74pub(crate) trait ChannelInterrupt {
75    #[cfg_attr(not(feature = "rt"), allow(unused))]
76    unsafe fn on_irq();
77}
78
79/// DMA channel.
80#[allow(private_bounds)]
81pub trait Channel: SealedChannel + PeripheralType + Into<AnyChannel> + 'static {
82    /// Type-erase (degrade) this pin into an `AnyChannel`.
83    ///
84    /// This converts DMA channel singletons (`DMA1_CH3`, `DMA2_CH1`, ...), which
85    /// are all different types, into the same type. It is useful for
86    /// creating arrays of channels, or avoiding generics.
87    #[inline]
88    fn degrade(self) -> AnyChannel {
89        AnyChannel { id: self.id() }
90    }
91}
92
93macro_rules! dma_channel_impl {
94    ($channel_peri:ident, $index:expr) => {
95        impl crate::dma::SealedChannel for crate::peripherals::$channel_peri {
96            fn id(&self) -> u8 {
97                $index
98            }
99        }
100        impl crate::dma::ChannelInterrupt for crate::peripherals::$channel_peri {
101            unsafe fn on_irq() {
102                crate::dma::AnyChannel { id: $index }.on_irq();
103            }
104        }
105
106        impl crate::dma::Channel for crate::peripherals::$channel_peri {}
107
108        impl From<crate::peripherals::$channel_peri> for crate::dma::AnyChannel {
109            fn from(x: crate::peripherals::$channel_peri) -> Self {
110                crate::dma::Channel::degrade(x)
111            }
112        }
113    };
114}
115/// Type-erased DMA channel.
116pub struct AnyChannel {
117    pub(crate) id: u8,
118}
119impl_peripheral!(AnyChannel);
120
121impl AnyChannel {
122    fn info(&self) -> &ChannelInfo {
123        &crate::_generated::DMA_CHANNELS[self.id as usize]
124    }
125}
126
127impl SealedChannel for AnyChannel {
128    fn id(&self) -> u8 {
129        self.id
130    }
131}
132impl Channel for AnyChannel {}
133
134const CHANNEL_COUNT: usize = crate::_generated::DMA_CHANNELS.len();
135static STATE: [ChannelState; CHANNEL_COUNT] = [ChannelState::NEW; CHANNEL_COUNT];
136
137// TODO: replace transmutes with core::ptr::metadata once it's stable
138#[allow(unused)]
139pub(crate) fn slice_ptr_parts<T>(slice: *const [T]) -> (usize, usize) {
140    unsafe { mem::transmute(slice) }
141}
142
143#[allow(unused)]
144pub(crate) fn slice_ptr_parts_mut<T>(slice: *mut [T]) -> (usize, usize) {
145    unsafe { mem::transmute(slice) }
146}
147
148// safety: must be called only once at startup. `enable_irqs` is false on a core
149// that does not own the DMA interrupts; see `dma_bdma::init`.
150pub(crate) unsafe fn init(
151    cs: critical_section::CriticalSection,
152    dma_priority: interrupt::Priority,
153    enable_irqs: bool,
154) {
155    dma_bdma::init(cs, dma_priority, enable_irqs);
156}