sifli_hal/dma/
mod.rs

1//! Direct Memory Access (DMA)
2
3// The following code is modified from embassy-stm32 under MIT license
4// https://github.com/embassy-rs/embassy/tree/main/embassy-stm32
5// Special thanks to the Embassy Project and its contributors for their work!
6#![macro_use]
7
8use embassy_hal_internal::{impl_peripheral, Peripheral};
9
10mod dma;
11pub use dma::*;
12
13mod util;
14pub(crate) use util::*;
15
16pub(crate) mod ringbuffer;
17pub mod word;
18
19pub use crate::_generated::Request;
20pub(crate) trait SealedChannel {
21    fn id(&self) -> u8;
22}
23
24pub(crate) trait ChannelInterrupt {
25    #[cfg_attr(not(feature = "rt"), allow(unused))]
26    unsafe fn on_irq();
27}
28
29/// DMA channel.
30#[allow(private_bounds)]
31pub trait Channel: SealedChannel + Peripheral<P = Self> + Into<AnyChannel> + 'static {
32    /// Type-erase (degrade) this pin into an `AnyChannel`.
33    ///
34    /// This converts DMA channel singletons (`DMA1_CH3`, `DMA2_CH1`, ...), which
35    /// are all different types, into the same type. It is useful for
36    /// creating arrays of channels, or avoiding generics.
37    #[inline]
38    fn degrade(self) -> AnyChannel {
39        AnyChannel { id: self.id() }
40    }
41}
42
43/// Type-erased DMA channel.
44pub struct AnyChannel {
45    pub(crate) id: u8,
46}
47impl_peripheral!(AnyChannel);
48
49// TODO: Multi DMAC in future?
50impl AnyChannel {
51    fn info(&self) -> ChannelInfo {
52        ChannelInfo { dma: crate::pac::DMAC1, num: self.id as _ }
53    }
54}
55
56impl SealedChannel for AnyChannel {
57    fn id(&self) -> u8 {
58        self.id
59    }
60}
61
62macro_rules! dma_channel_impl {
63    ($channel_peri:ident, $index:expr) => {
64        impl crate::dma::SealedChannel for crate::peripherals::$channel_peri {
65            fn id(&self) -> u8 {
66                $index
67            }
68        }
69        impl crate::dma::ChannelInterrupt for crate::peripherals::$channel_peri {
70            unsafe fn on_irq() {
71                crate::dma::AnyChannel { id: $index }.on_irq();
72            }
73        }
74
75        impl crate::dma::Channel for crate::peripherals::$channel_peri {}
76
77        impl From<crate::peripherals::$channel_peri> for crate::dma::AnyChannel {
78            fn from(x: crate::peripherals::$channel_peri) -> Self {
79                crate::dma::Channel::degrade(x)
80            }
81        }
82    };
83}
84
85impl Channel for AnyChannel {}
86
87use crate::_generated::CHANNEL_COUNT;
88static STATE: [dma::ChannelState; CHANNEL_COUNT] = [dma::ChannelState::NEW; CHANNEL_COUNT];
89
90pub(crate) unsafe fn init(cs: critical_section::CriticalSection) {
91    dma::init(cs);
92}
93
94/// "No DMA" placeholder.
95///
96/// You may pass this in place of a real DMA channel when creating a driver
97/// to indicate it should not use DMA.
98///
99/// This often causes async functionality to not be available on the instance,
100/// leaving only blocking functionality.
101pub struct NoDma;
102
103impl_peripheral!(NoDma);
104
105
106// codegen will generate the following implementations
107// We use this instead of `InterruptHandler` for the avaliblity of `AnyChannel`.
108
109// ```
110// dma_channel_impl!(DMAC1_CH1, 0);
111// #[cfg(feature = "rt")]
112// #[crate::interrupt]
113// unsafe fn DMAC1_CH1() {
114//     <crate::peripherals::DMAC1_CH1 as crate::dma::ChannelInterrupt>::on_irq();
115// }
116// ```