1#![macro_use]
4
5use core::mem;
6
7use embassy_hal_internal::PeripheralType;
8
9use crate::{impl_peripheral, interrupt};
10
11#[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
29pub 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#[cfg(not(ch32h4))]
53pub type Request = ();
54#[cfg(ch32h4)]
55pub type Request = u8;
56
57#[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#[allow(private_bounds)]
81pub trait Channel: SealedChannel + PeripheralType + Into<AnyChannel> + 'static {
82 #[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}
115pub 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#[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
148pub(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}