| 1 | //! Direct Memory Access (DMA) |
| 2 | #![macro_use ] |
| 3 | |
| 4 | #[cfg (any(bdma, dma))] |
| 5 | mod dma_bdma; |
| 6 | #[cfg (any(bdma, dma))] |
| 7 | pub use dma_bdma::*; |
| 8 | |
| 9 | #[cfg (gpdma)] |
| 10 | pub(crate) mod gpdma; |
| 11 | #[cfg (gpdma)] |
| 12 | pub use gpdma::*; |
| 13 | |
| 14 | #[cfg (dmamux)] |
| 15 | mod dmamux; |
| 16 | #[cfg (dmamux)] |
| 17 | pub(crate) use dmamux::*; |
| 18 | |
| 19 | mod util; |
| 20 | pub(crate) use util::*; |
| 21 | |
| 22 | pub(crate) mod ringbuffer; |
| 23 | pub mod word; |
| 24 | |
| 25 | use embassy_hal_internal::{impl_peripheral, Peripheral}; |
| 26 | |
| 27 | use crate::interrupt; |
| 28 | |
| 29 | #[derive (Debug, Copy, Clone, PartialEq, Eq)] |
| 30 | #[cfg_attr (feature = "defmt" , derive(defmt::Format))] |
| 31 | enum Dir { |
| 32 | MemoryToPeripheral, |
| 33 | PeripheralToMemory, |
| 34 | } |
| 35 | |
| 36 | /// DMA request type alias. (also known as DMA channel number in some chips) |
| 37 | #[cfg (any(dma_v2, bdma_v2, gpdma, dmamux))] |
| 38 | pub type Request = u8; |
| 39 | /// DMA request type alias. (also known as DMA channel number in some chips) |
| 40 | #[cfg (not(any(dma_v2, bdma_v2, gpdma, dmamux)))] |
| 41 | pub type Request = (); |
| 42 | |
| 43 | pub(crate) trait SealedChannel { |
| 44 | fn id(&self) -> u8; |
| 45 | } |
| 46 | |
| 47 | pub(crate) trait ChannelInterrupt { |
| 48 | #[cfg_attr (not(feature = "rt" ), allow(unused))] |
| 49 | unsafe fn on_irq(); |
| 50 | } |
| 51 | |
| 52 | /// DMA channel. |
| 53 | #[allow (private_bounds)] |
| 54 | pub trait Channel: SealedChannel + Peripheral<P = Self> + Into<AnyChannel> + 'static { |
| 55 | /// Type-erase (degrade) this pin into an `AnyChannel`. |
| 56 | /// |
| 57 | /// This converts DMA channel singletons (`DMA1_CH3`, `DMA2_CH1`, ...), which |
| 58 | /// are all different types, into the same type. It is useful for |
| 59 | /// creating arrays of channels, or avoiding generics. |
| 60 | #[inline ] |
| 61 | fn degrade(self) -> AnyChannel { |
| 62 | AnyChannel { id: self.id() } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | macro_rules! dma_channel_impl { |
| 67 | ($channel_peri:ident, $index:expr) => { |
| 68 | impl crate::dma::SealedChannel for crate::peripherals::$channel_peri { |
| 69 | fn id(&self) -> u8 { |
| 70 | $index |
| 71 | } |
| 72 | } |
| 73 | impl crate::dma::ChannelInterrupt for crate::peripherals::$channel_peri { |
| 74 | unsafe fn on_irq() { |
| 75 | crate::dma::AnyChannel { id: $index }.on_irq(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl crate::dma::Channel for crate::peripherals::$channel_peri {} |
| 80 | |
| 81 | impl From<crate::peripherals::$channel_peri> for crate::dma::AnyChannel { |
| 82 | fn from(x: crate::peripherals::$channel_peri) -> Self { |
| 83 | crate::dma::Channel::degrade(x) |
| 84 | } |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | /// Type-erased DMA channel. |
| 90 | pub struct AnyChannel { |
| 91 | pub(crate) id: u8, |
| 92 | } |
| 93 | impl_peripheral!(AnyChannel); |
| 94 | |
| 95 | impl AnyChannel { |
| 96 | fn info(&self) -> &ChannelInfo { |
| 97 | &crate::_generated::DMA_CHANNELS[self.id as usize] |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl SealedChannel for AnyChannel { |
| 102 | fn id(&self) -> u8 { |
| 103 | self.id |
| 104 | } |
| 105 | } |
| 106 | impl Channel for AnyChannel {} |
| 107 | |
| 108 | const CHANNEL_COUNT: usize = crate::_generated::DMA_CHANNELS.len(); |
| 109 | static STATE: [ChannelState; CHANNEL_COUNT] = [ChannelState::NEW; CHANNEL_COUNT]; |
| 110 | |
| 111 | /// "No DMA" placeholder. |
| 112 | /// |
| 113 | /// You may pass this in place of a real DMA channel when creating a driver |
| 114 | /// to indicate it should not use DMA. |
| 115 | /// |
| 116 | /// This often causes async functionality to not be available on the instance, |
| 117 | /// leaving only blocking functionality. |
| 118 | pub struct NoDma; |
| 119 | |
| 120 | impl_peripheral!(NoDma); |
| 121 | |
| 122 | // safety: must be called only once at startup |
| 123 | pub(crate) unsafe fn init( |
| 124 | cs: critical_section::CriticalSection, |
| 125 | #[cfg (bdma)] bdma_priority: interrupt::Priority, |
| 126 | #[cfg (dma)] dma_priority: interrupt::Priority, |
| 127 | #[cfg (gpdma)] gpdma_priority: interrupt::Priority, |
| 128 | ) { |
| 129 | #[cfg (any(dma, bdma))] |
| 130 | dma_bdma::init( |
| 131 | cs, |
| 132 | #[cfg (dma)] |
| 133 | dma_priority, |
| 134 | #[cfg (bdma)] |
| 135 | bdma_priority, |
| 136 | ); |
| 137 | #[cfg (gpdma)] |
| 138 | gpdma::init(cs, irq_priority:gpdma_priority); |
| 139 | #[cfg (dmamux)] |
| 140 | dmamux::init(cs); |
| 141 | } |
| 142 | |