aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/dma
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-04-18 20:56:23 +0200
committerDario Nieuwenhuis <[email protected]>2023-04-18 20:56:23 +0200
commit2080d8bb6de58eb2da2ca5df2437ac8cc6577661 (patch)
tree06bc1eb9fc73e6b545b9718714d285f8400e820e /embassy-stm32/src/dma
parenta673b9aa294a55c441f3f61c48481484629b4347 (diff)
stm32/spi: add support for all word sizes.
Co-Authored-By: anton smeenk <[email protected]>
Diffstat (limited to 'embassy-stm32/src/dma')
-rw-r--r--embassy-stm32/src/dma/bdma.rs9
-rw-r--r--embassy-stm32/src/dma/dma.rs11
-rw-r--r--embassy-stm32/src/dma/gpdma.rs9
-rw-r--r--embassy-stm32/src/dma/mod.rs49
-rw-r--r--embassy-stm32/src/dma/word.rs79
5 files changed, 97 insertions, 60 deletions
diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs
index cf1222c46..a23bb8cd7 100644
--- a/embassy-stm32/src/dma/bdma.rs
+++ b/embassy-stm32/src/dma/bdma.rs
@@ -9,7 +9,8 @@ use embassy_cortex_m::interrupt::Priority;
9use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; 9use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
10use embassy_sync::waitqueue::AtomicWaker; 10use embassy_sync::waitqueue::AtomicWaker;
11 11
12use super::{Dir, Word, WordSize}; 12use super::word::{Word, WordSize};
13use super::Dir;
13use crate::_generated::BDMA_CHANNEL_COUNT; 14use crate::_generated::BDMA_CHANNEL_COUNT;
14use crate::interrupt::{Interrupt, InterruptExt}; 15use crate::interrupt::{Interrupt, InterruptExt};
15use crate::pac; 16use crate::pac;
@@ -167,7 +168,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
167 ptr as *mut u32, 168 ptr as *mut u32,
168 len, 169 len,
169 true, 170 true,
170 W::bits(), 171 W::size(),
171 options, 172 options,
172 ) 173 )
173 } 174 }
@@ -202,7 +203,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
202 ptr as *mut u32, 203 ptr as *mut u32,
203 len, 204 len,
204 true, 205 true,
205 W::bits(), 206 W::size(),
206 options, 207 options,
207 ) 208 )
208 } 209 }
@@ -225,7 +226,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
225 repeated as *const W as *mut u32, 226 repeated as *const W as *mut u32,
226 count, 227 count,
227 false, 228 false,
228 W::bits(), 229 W::size(),
229 options, 230 options,
230 ) 231 )
231 } 232 }
diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs
index 62c092241..ef1d27573 100644
--- a/embassy-stm32/src/dma/dma.rs
+++ b/embassy-stm32/src/dma/dma.rs
@@ -9,7 +9,8 @@ use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
9use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
10use pac::dma::regs; 10use pac::dma::regs;
11 11
12use super::{Dir, Word, WordSize}; 12use super::word::{Word, WordSize};
13use super::Dir;
13use crate::_generated::DMA_CHANNEL_COUNT; 14use crate::_generated::DMA_CHANNEL_COUNT;
14use crate::interrupt::{Interrupt, InterruptExt}; 15use crate::interrupt::{Interrupt, InterruptExt};
15use crate::pac::dma::vals; 16use crate::pac::dma::vals;
@@ -246,7 +247,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
246 ptr as *mut u32, 247 ptr as *mut u32,
247 len, 248 len,
248 true, 249 true,
249 W::bits(), 250 W::size(),
250 options, 251 options,
251 ) 252 )
252 } 253 }
@@ -281,7 +282,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
281 ptr as *mut u32, 282 ptr as *mut u32,
282 len, 283 len,
283 true, 284 true,
284 W::bits(), 285 W::size(),
285 options, 286 options,
286 ) 287 )
287 } 288 }
@@ -304,7 +305,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
304 repeated as *const W as *mut u32, 305 repeated as *const W as *mut u32,
305 count, 306 count,
306 false, 307 false,
307 W::bits(), 308 W::size(),
308 options, 309 options,
309 ) 310 )
310 } 311 }
@@ -464,7 +465,7 @@ impl<'a, C: Channel, W: Word> DoubleBuffered<'a, C, W> {
464 assert!(len > 0 && len <= 0xFFFF); 465 assert!(len > 0 && len <= 0xFFFF);
465 466
466 let dir = Dir::PeripheralToMemory; 467 let dir = Dir::PeripheralToMemory;
467 let data_size = W::bits(); 468 let data_size = W::size();
468 469
469 let channel_number = channel.num(); 470 let channel_number = channel.num();
470 let dma = channel.regs(); 471 let dma = channel.regs();
diff --git a/embassy-stm32/src/dma/gpdma.rs b/embassy-stm32/src/dma/gpdma.rs
index 5c6676a5f..5a516ccda 100644
--- a/embassy-stm32/src/dma/gpdma.rs
+++ b/embassy-stm32/src/dma/gpdma.rs
@@ -9,7 +9,8 @@ use embassy_cortex_m::interrupt::Priority;
9use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; 9use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
10use embassy_sync::waitqueue::AtomicWaker; 10use embassy_sync::waitqueue::AtomicWaker;
11 11
12use super::{Dir, Word, WordSize}; 12use super::word::{Word, WordSize};
13use super::Dir;
13use crate::_generated::GPDMA_CHANNEL_COUNT; 14use crate::_generated::GPDMA_CHANNEL_COUNT;
14use crate::interrupt::{Interrupt, InterruptExt}; 15use crate::interrupt::{Interrupt, InterruptExt};
15use crate::pac; 16use crate::pac;
@@ -165,7 +166,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
165 ptr as *mut u32, 166 ptr as *mut u32,
166 len, 167 len,
167 true, 168 true,
168 W::bits(), 169 W::size(),
169 options, 170 options,
170 ) 171 )
171 } 172 }
@@ -200,7 +201,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
200 ptr as *mut u32, 201 ptr as *mut u32,
201 len, 202 len,
202 true, 203 true,
203 W::bits(), 204 W::size(),
204 options, 205 options,
205 ) 206 )
206 } 207 }
@@ -223,7 +224,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
223 repeated as *const W as *mut u32, 224 repeated as *const W as *mut u32,
224 count, 225 count,
225 false, 226 false,
226 W::bits(), 227 W::size(),
227 options, 228 options,
228 ) 229 )
229 } 230 }
diff --git a/embassy-stm32/src/dma/mod.rs b/embassy-stm32/src/dma/mod.rs
index d29ef4a1f..3312ca752 100644
--- a/embassy-stm32/src/dma/mod.rs
+++ b/embassy-stm32/src/dma/mod.rs
@@ -21,6 +21,8 @@ pub use gpdma::*;
21#[cfg(dmamux)] 21#[cfg(dmamux)]
22mod dmamux; 22mod dmamux;
23 23
24pub mod word;
25
24use core::mem; 26use core::mem;
25 27
26use embassy_cortex_m::interrupt::Priority; 28use embassy_cortex_m::interrupt::Priority;
@@ -36,53 +38,6 @@ enum Dir {
36 PeripheralToMemory, 38 PeripheralToMemory,
37} 39}
38 40
39#[derive(Debug, Copy, Clone, PartialEq, Eq)]
40#[cfg_attr(feature = "defmt", derive(defmt::Format))]
41pub enum WordSize {
42 OneByte,
43 TwoBytes,
44 FourBytes,
45}
46
47impl WordSize {
48 pub fn bytes(&self) -> usize {
49 match self {
50 Self::OneByte => 1,
51 Self::TwoBytes => 2,
52 Self::FourBytes => 4,
53 }
54 }
55}
56
57mod word_sealed {
58 pub trait Word {}
59}
60
61pub trait Word: word_sealed::Word {
62 fn bits() -> WordSize;
63}
64
65impl word_sealed::Word for u8 {}
66impl Word for u8 {
67 fn bits() -> WordSize {
68 WordSize::OneByte
69 }
70}
71
72impl word_sealed::Word for u16 {}
73impl Word for u16 {
74 fn bits() -> WordSize {
75 WordSize::TwoBytes
76 }
77}
78
79impl word_sealed::Word for u32 {}
80impl Word for u32 {
81 fn bits() -> WordSize {
82 WordSize::FourBytes
83 }
84}
85
86pub struct NoDma; 41pub struct NoDma;
87 42
88impl_peripheral!(NoDma); 43impl_peripheral!(NoDma);
diff --git a/embassy-stm32/src/dma/word.rs b/embassy-stm32/src/dma/word.rs
new file mode 100644
index 000000000..aef6e9700
--- /dev/null
+++ b/embassy-stm32/src/dma/word.rs
@@ -0,0 +1,79 @@
1#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3pub enum WordSize {
4 OneByte,
5 TwoBytes,
6 FourBytes,
7}
8
9impl WordSize {
10 pub fn bytes(&self) -> usize {
11 match self {
12 Self::OneByte => 1,
13 Self::TwoBytes => 2,
14 Self::FourBytes => 4,
15 }
16 }
17}
18
19mod sealed {
20 pub trait Word {}
21}
22
23pub trait Word: sealed::Word + Default + Copy + 'static {
24 fn size() -> WordSize;
25 fn bits() -> usize;
26}
27
28macro_rules! impl_word {
29 (_, $T:ident, $bits:literal, $size:ident) => {
30 impl sealed::Word for $T {}
31 impl Word for $T {
32 fn bits() -> usize {
33 $bits
34 }
35 fn size() -> WordSize {
36 WordSize::$size
37 }
38 }
39 };
40 ($T:ident, $uX:ident, $bits:literal, $size:ident) => {
41 #[repr(transparent)]
42 #[derive(Copy, Clone, Default)]
43 pub struct $T(pub $uX);
44 impl_word!(_, $T, $bits, $size);
45 };
46}
47
48impl_word!(U1, u8, 1, OneByte);
49impl_word!(U2, u8, 2, OneByte);
50impl_word!(U3, u8, 3, OneByte);
51impl_word!(U4, u8, 4, OneByte);
52impl_word!(U5, u8, 5, OneByte);
53impl_word!(U6, u8, 6, OneByte);
54impl_word!(U7, u8, 7, OneByte);
55impl_word!(_, u8, 8, OneByte);
56impl_word!(U9, u16, 9, TwoBytes);
57impl_word!(U10, u16, 10, TwoBytes);
58impl_word!(U11, u16, 11, TwoBytes);
59impl_word!(U12, u16, 12, TwoBytes);
60impl_word!(U13, u16, 13, TwoBytes);
61impl_word!(U14, u16, 14, TwoBytes);
62impl_word!(U15, u16, 15, TwoBytes);
63impl_word!(_, u16, 16, TwoBytes);
64impl_word!(U17, u32, 17, FourBytes);
65impl_word!(U18, u32, 18, FourBytes);
66impl_word!(U19, u32, 19, FourBytes);
67impl_word!(U20, u32, 20, FourBytes);
68impl_word!(U21, u32, 21, FourBytes);
69impl_word!(U22, u32, 22, FourBytes);
70impl_word!(U23, u32, 23, FourBytes);
71impl_word!(U24, u32, 24, FourBytes);
72impl_word!(U25, u32, 25, FourBytes);
73impl_word!(U26, u32, 26, FourBytes);
74impl_word!(U27, u32, 27, FourBytes);
75impl_word!(U28, u32, 28, FourBytes);
76impl_word!(U29, u32, 29, FourBytes);
77impl_word!(U30, u32, 30, FourBytes);
78impl_word!(U31, u32, 31, FourBytes);
79impl_word!(_, u32, 32, FourBytes);