| 1 | //! DMA word sizes. |
| 2 | |
| 3 | #[allow (missing_docs)] |
| 4 | #[derive (Debug, Copy, Clone, PartialEq, Eq)] |
| 5 | #[cfg_attr (feature = "defmt" , derive(defmt::Format))] |
| 6 | pub enum WordSize { |
| 7 | OneByte, |
| 8 | TwoBytes, |
| 9 | FourBytes, |
| 10 | } |
| 11 | |
| 12 | impl 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 | |
| 23 | trait SealedWord {} |
| 24 | |
| 25 | /// DMA word trait. |
| 26 | /// |
| 27 | /// This is implemented for u8, u16, u32, etc. |
| 28 | #[allow (private_bounds)] |
| 29 | pub 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 | |
| 36 | macro_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 | |
| 57 | impl_word!(U1, u8, 1, OneByte); |
| 58 | impl_word!(U2, u8, 2, OneByte); |
| 59 | impl_word!(U3, u8, 3, OneByte); |
| 60 | impl_word!(U4, u8, 4, OneByte); |
| 61 | impl_word!(U5, u8, 5, OneByte); |
| 62 | impl_word!(U6, u8, 6, OneByte); |
| 63 | impl_word!(U7, u8, 7, OneByte); |
| 64 | impl_word!(_, u8, 8, OneByte); |
| 65 | impl_word!(U9, u16, 9, TwoBytes); |
| 66 | impl_word!(U10, u16, 10, TwoBytes); |
| 67 | impl_word!(U11, u16, 11, TwoBytes); |
| 68 | impl_word!(U12, u16, 12, TwoBytes); |
| 69 | impl_word!(U13, u16, 13, TwoBytes); |
| 70 | impl_word!(U14, u16, 14, TwoBytes); |
| 71 | impl_word!(U15, u16, 15, TwoBytes); |
| 72 | impl_word!(_, u16, 16, TwoBytes); |
| 73 | impl_word!(U17, u32, 17, FourBytes); |
| 74 | impl_word!(U18, u32, 18, FourBytes); |
| 75 | impl_word!(U19, u32, 19, FourBytes); |
| 76 | impl_word!(U20, u32, 20, FourBytes); |
| 77 | impl_word!(U21, u32, 21, FourBytes); |
| 78 | impl_word!(U22, u32, 22, FourBytes); |
| 79 | impl_word!(U23, u32, 23, FourBytes); |
| 80 | impl_word!(U24, u32, 24, FourBytes); |
| 81 | impl_word!(U25, u32, 25, FourBytes); |
| 82 | impl_word!(U26, u32, 26, FourBytes); |
| 83 | impl_word!(U27, u32, 27, FourBytes); |
| 84 | impl_word!(U28, u32, 28, FourBytes); |
| 85 | impl_word!(U29, u32, 29, FourBytes); |
| 86 | impl_word!(U30, u32, 30, FourBytes); |
| 87 | impl_word!(U31, u32, 31, FourBytes); |
| 88 | impl_word!(_, u32, 32, FourBytes); |
| 89 | |