py32_hal/dma/
word.rs

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