1use core::sync::atomic::{Ordering, compiler_fence};
7
8pub use self::{rx::AnyDma2dRxChannel, tx::AnyDma2dTxChannel};
9#[cfg(esp32s31)]
10use crate::peripherals::HP_SYS;
11use crate::{
12 dma::aligned::region_needs_cache_op,
13 peripherals::DMA2D,
14 soc::{cache_invalidate_addr, cache_writeback_addr},
15 system::{GenericPeripheralGuard, Peripheral},
16};
17
18pub(crate) const BURST_128_BYTES: u8 = 4;
19pub(crate) const MACRO_BLOCK_NONE: u8 = 3;
20
21macro_rules! define_channel {
22 ($channel:ident, $doc:literal, $(($index:literal, $name:ident, $variant:ident)),*) => {
23 crate::any_peripheral! {
24 #[doc = $doc]
25 #[instability::unstable]
26 pub peripheral $channel<'d> {
27 $($variant(crate::peripherals::$name<'d>),)*
28 }
29 }
30
31 impl $channel<'_> {
32 pub(super) fn index(&self) -> usize {
33 match &self.0 {
34 $(any::Inner::$variant(_) => $index,)*
35 }
36 }
37 }
38 };
39}
40
41mod tx {
42 for_each_dma2d_channel! {
43 (tx $($channel:tt),*) => {
44 define_channel!(AnyDma2dTxChannel, "A DMA2D transmit channel singleton.", $($channel),*);
45 };
46 }
47}
48
49mod rx {
50 for_each_dma2d_channel! {
51 (rx $($channel:tt),*) => {
52 define_channel!(AnyDma2dRxChannel, "A DMA2D receive channel singleton.", $($channel),*);
53 };
54 }
55}
56
57fn init_dma2d() {
58 #[cfg(esp32s31)]
60 HP_SYS::regs()
61 .dma2d_mem_lp_ctrl()
62 .modify(|_, w| w._2ddma_mem_lp_en().clear_bit());
63
64 let dma = DMA2D::regs();
65 dma.rst_conf().modify(|_, w| {
66 w.clk_en().set_bit();
67 w.axim_rd_rst().set_bit();
68 w.axim_wr_rst().set_bit()
69 });
70 dma.rst_conf().modify(|_, w| {
71 w.axim_rd_rst().clear_bit();
72 w.axim_wr_rst().clear_bit()
73 });
74}
75
76#[derive(Debug)]
77#[cfg_attr(feature = "defmt", derive(defmt::Format))]
78pub(crate) struct Channels<'d> {
79 tx: AnyDma2dTxChannel<'d>,
80 rx: AnyDma2dRxChannel<'d>,
81 _guard: GenericPeripheralGuard<{ Peripheral::Dma2d as u8 }>,
82}
83
84impl<'d> Channels<'d> {
85 pub(crate) fn new(
86 tx: impl Into<AnyDma2dTxChannel<'d>>,
87 rx: impl Into<AnyDma2dRxChannel<'d>>,
88 ) -> Self {
89 Self {
90 tx: tx.into(),
91 rx: rx.into(),
92 _guard: GenericPeripheralGuard::new_with(init_dma2d),
93 }
94 }
95
96 pub(crate) fn indices(&self) -> (usize, usize) {
97 (self.tx.index(), self.rx.index())
98 }
99
100 pub(crate) fn reset(&mut self) {
101 let (tx, rx) = self.indices();
102 let dma = DMA2D::regs();
103 dma.out_link_conf_ch(tx)
104 .modify(|_, w| w.outlink_stop_ch().set_bit());
105 dma.in_link_conf_ch(rx)
106 .modify(|_, w| w.inlink_stop_ch().set_bit());
107 dma.out_conf0_ch(tx)
108 .modify(|_, w| w.out_cmd_disable_ch().set_bit());
109 dma.in_conf0_ch(rx)
110 .modify(|_, w| w.in_cmd_disable_ch().set_bit());
111 while !dma.out_state_ch(tx).read().out_reset_avail_ch().bit()
114 || !dma.in_state_ch(rx).read().in_reset_avail_ch().bit()
115 {
116 core::hint::spin_loop();
117 }
118 dma.out_conf0_ch(tx).modify(|_, w| w.out_rst_ch().set_bit());
119 dma.in_conf0_ch(rx).modify(|_, w| w.in_rst_ch().set_bit());
120 dma.out_conf0_ch(tx)
121 .modify(|_, w| w.out_rst_ch().clear_bit());
122 dma.in_conf0_ch(rx).modify(|_, w| w.in_rst_ch().clear_bit());
123 dma.out_conf0_ch(tx).reset();
124 dma.in_conf0_ch(rx).reset();
125 dma.out_color_convert_ch(tx).reset();
126 dma.out_scramble_ch(tx).reset();
127 dma.out_peri_sel_ch(tx).reset();
129 dma.in_peri_sel_ch(rx).reset();
130 dma.out_int_clr_ch(tx)
131 .write(|w| unsafe { w.bits(u32::MAX) });
132 dma.in_int_clr_ch(rx).write(|w| unsafe { w.bits(u32::MAX) });
133 }
134
135 pub(crate) fn swap_tx_pixel_bytes(&mut self) {
137 let tx = self.tx.index();
138 let dma = DMA2D::regs();
139 dma.out_color_convert_ch(tx).write(|w| unsafe {
140 w.out_color_input_sel_ch().bits(2);
141 w.out_color_output_sel_ch().bits(2)
142 });
143 dma.out_scramble_ch(tx)
144 .write(|w| unsafe { w.out_scramble_sel_pre_ch().bits(1) });
145 }
146
147 #[inline]
152 pub(crate) unsafe fn start(&mut self, tx_address: u32, rx_address: u32) {
153 let (tx, rx) = self.indices();
154 let dma = DMA2D::regs();
155 compiler_fence(Ordering::SeqCst);
156 dma.out_link_addr_ch(tx)
157 .write(|w| unsafe { w.outlink_addr_ch().bits(tx_address) });
158 dma.in_link_addr_ch(rx)
159 .write(|w| unsafe { w.inlink_addr_ch().bits(rx_address) });
160 dma.out_link_conf_ch(tx)
161 .modify(|_, w| w.outlink_start_ch().set_bit());
162 dma.in_link_conf_ch(rx)
163 .modify(|_, w| w.inlink_start_ch().set_bit());
164 }
165
166 pub(crate) fn connect(&mut self, trigger: u8) {
167 let (tx, rx) = self.indices();
168 let dma = DMA2D::regs();
169 dma.out_peri_sel_ch(tx)
170 .modify(|_, w| unsafe { w.out_peri_sel_ch().bits(trigger) });
171 dma.in_peri_sel_ch(rx)
172 .modify(|_, w| unsafe { w.in_peri_sel_ch().bits(trigger) });
173 }
174}
175
176pub(crate) fn writeback<T>(buffer: &[T]) {
177 let (address, size) = (buffer.as_ptr() as usize, size_of_val(buffer));
178 if region_needs_cache_op(address, size) {
179 unsafe { cache_writeback_addr(address as u32, size as u32) };
180 }
181}
182
183pub(crate) unsafe fn invalidate<T>(buffer: &[T]) {
188 let (address, size) = (buffer.as_ptr() as usize, size_of_val(buffer));
189 if region_needs_cache_op(address, size) {
190 unsafe { cache_invalidate_addr(address as u32, size as u32) };
191 }
192}