sdmmc_core/command/class/class2/cmd18/
arg.rs

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5    /// Argumentument for CMD18.
6    pub Argument(u32): u32 {
7        /// Reads data blocks starting at `data_address` until a `STOP_TRANSMISSION` command is sent.
8        pub data_address: 31, 0;
9    }
10}
11
12impl Argument {
13    /// Represents the byte length of the [Argument].
14    pub const LEN: usize = 4;
15    /// Represents the default value of the [Argument].
16    pub const DEFAULT: u32 = 0;
17
18    /// Creates a new [Argument].
19    pub const fn new() -> Self {
20        Self(Self::DEFAULT)
21    }
22
23    /// Converts a [`u32`] into an [Argument].
24    pub const fn from_bits(val: u32) -> Self {
25        Self(val)
26    }
27
28    /// Converts a [`u32`] into an [Argument].
29    pub const fn try_from_bits(val: u32) -> Result<Self> {
30        Ok(Self::from_bits(val))
31    }
32
33    /// Converts the [Argument] into a byte array.
34    pub const fn bytes(&self) -> [u8; Self::LEN] {
35        self.0.to_be_bytes()
36    }
37
38    /// Attempts to convert a byte slice into an [Argument].
39    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
40        match val.len() {
41            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
42            _ => Ok(Self::from_bits(u32::from_be_bytes([
43                val[0], val[1], val[2], val[3],
44            ]))),
45        }
46    }
47}
48
49impl Default for Argument {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55impl From<u32> for Argument {
56    fn from(val: u32) -> Self {
57        Self::from_bits(val)
58    }
59}
60
61impl From<Argument> for u32 {
62    fn from(val: Argument) -> Self {
63        val.bits()
64    }
65}
66
67impl From<Argument> for [u8; Argument::LEN] {
68    fn from(val: Argument) -> Self {
69        val.bytes()
70    }
71}
72
73impl TryFrom<&[u8]> for Argument {
74    type Error = Error;
75
76    fn try_from(val: &[u8]) -> Result<Self> {
77        Self::try_from_bytes(val)
78    }
79}
80
81impl<const N: usize> TryFrom<&[u8; N]> for Argument {
82    type Error = Error;
83
84    fn try_from(val: &[u8; N]) -> Result<Self> {
85        Self::try_from_bytes(val.as_ref())
86    }
87}
88
89impl<const N: usize> TryFrom<[u8; N]> for Argument {
90    type Error = Error;
91
92    fn try_from(val: [u8; N]) -> Result<Self> {
93        Self::try_from_bytes(val.as_ref())
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_fields() {
103        (1..=u32::BITS)
104            .map(|r| ((1u64 << r) - 1) as u32)
105            .for_each(|data_address| {
106                let raw = data_address;
107                let raw_bytes = raw.to_be_bytes();
108
109                let mut exp_arg = Argument(raw);
110
111                assert_eq!(Argument::try_from_bits(raw), Ok(exp_arg));
112                assert_eq!(Argument::from_bits(raw), exp_arg);
113                assert_eq!(Argument::from(raw), exp_arg);
114
115                assert_eq!(Argument::try_from_bytes(&raw_bytes), Ok(exp_arg));
116                assert_eq!(Argument::try_from(raw_bytes), Ok(exp_arg));
117                assert_eq!(Argument::try_from(&raw_bytes), Ok(exp_arg));
118
119                assert_eq!(exp_arg.bytes(), raw_bytes);
120                assert_eq!(<[u8; Argument::LEN]>::from(exp_arg), raw_bytes);
121
122                assert_eq!(exp_arg.data_address(), data_address);
123
124                exp_arg.set_data_address(0);
125                assert_eq!(exp_arg.data_address(), 0);
126
127                exp_arg.set_data_address(data_address);
128                assert_eq!(exp_arg.data_address(), data_address);
129            });
130    }
131}