embassy_stm32/dma/
word.rs

1//! DMA word sizes.
2
3#[allow(missing_docs)]
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub enum WordSize {
7    OneByte,
8    TwoBytes,
9    FourBytes,
10}
11
12impl WordSize {
13    /// Amount of bytes of this word size.
14    pub fn bytes(&self) -> usize {
15        match self {
16            Self::OneByte => 1,
17            Self::TwoBytes => 2,
18            Self::FourBytes => 4,
19        }
20    }
21}
22
23trait SealedWord {}
24
25/// DMA word trait.
26///
27/// This is implemented for u8, u16, u32, etc.
28#[allow(private_bounds)]
29pub trait Word: SealedWord + Default + Copy + 'static {
30    /// Word size
31    fn size() -> WordSize;
32    /// Amount of bits of this word size.
33    fn bits() -> usize;
34}
35
36macro_rules! impl_word {
37    (_, $T:ident, $bits:literal, $size:ident) => {
38        impl SealedWord for $T {}
39        impl Word for $T {
40            fn bits() -> usize {
41                $bits
42            }
43            fn size() -> WordSize {
44                WordSize::$size
45            }
46        }
47    };
48    ($T:ident, $uX:ident, $bits:literal, $size:ident) => {
49        #[repr(transparent)]
50        #[derive(Copy, Clone, Default)]
51        #[doc = concat!(stringify!($T), " word size")]
52        pub struct $T(pub $uX);
53        impl_word!(_, $T, $bits, $size);
54    };
55}
56
57impl_word!(U1, u8, 1, OneByte);
58impl_word!(U2, u8, 2, OneByte);
59impl_word!(U3, u8, 3, OneByte);
60impl_word!(U4, u8, 4, OneByte);
61impl_word!(U5, u8, 5, OneByte);
62impl_word!(U6, u8, 6, OneByte);
63impl_word!(U7, u8, 7, OneByte);
64impl_word!(_, u8, 8, OneByte);
65impl_word!(U9, u16, 9, TwoBytes);
66impl_word!(U10, u16, 10, TwoBytes);
67impl_word!(U11, u16, 11, TwoBytes);
68impl_word!(U12, u16, 12, TwoBytes);
69impl_word!(U13, u16, 13, TwoBytes);
70impl_word!(U14, u16, 14, TwoBytes);
71impl_word!(U15, u16, 15, TwoBytes);
72impl_word!(_, u16, 16, TwoBytes);
73impl_word!(U17, u32, 17, FourBytes);
74impl_word!(U18, u32, 18, FourBytes);
75impl_word!(U19, u32, 19, FourBytes);
76impl_word!(U20, u32, 20, FourBytes);
77impl_word!(U21, u32, 21, FourBytes);
78impl_word!(U22, u32, 22, FourBytes);
79impl_word!(U23, u32, 23, FourBytes);
80impl_word!(U24, u32, 24, FourBytes);
81impl_word!(U25, u32, 25, FourBytes);
82impl_word!(U26, u32, 26, FourBytes);
83impl_word!(U27, u32, 27, FourBytes);
84impl_word!(U28, u32, 28, FourBytes);
85impl_word!(U29, u32, 29, FourBytes);
86impl_word!(U30, u32, 30, FourBytes);
87impl_word!(U31, u32, 31, FourBytes);
88impl_word!(_, u32, 32, FourBytes);