sdmmc_core/command/class/class8/acmd6/
arg.rs

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4mod bus_width;
5
6pub use bus_width::*;
7
8lib_bitfield! {
9    /// Argumentument for ACMD6.
10    pub Argument(u32): u8 {
11        raw_bus_width: 1, 0;
12    }
13}
14
15impl Argument {
16    /// Represents the byte length of the [Argument].
17    pub const LEN: usize = 4;
18    /// Represents the raw default value of the [Argument].
19    pub const DEFAULT: u32 = 0;
20
21    /// Creates a new [Argument].
22    pub const fn new() -> Self {
23        Self(0)
24    }
25
26    /// Gets the `bus width` field for the [Argument].
27    pub const fn bus_width(&self) -> Result<BusWidth> {
28        BusWidth::from_raw(self.raw_bus_width())
29    }
30
31    /// Sets the `bus width` field for the [Argument].
32    pub fn set_bus_width(&mut self, val: BusWidth) {
33        self.set_raw_bus_width(val.into_raw() as u32);
34    }
35
36    /// Attempts to convert a [`u32`] into an [Argument].
37    pub const fn try_from_bits(val: u32) -> Result<Self> {
38        match Self(val) {
39            arg if arg.bus_width().is_err() => Err(Error::invalid_field_variant(
40                "command::argument::bus_width",
41                arg.raw_bus_width() as usize,
42            )),
43            arg => Ok(arg),
44        }
45    }
46
47    /// Converts the [Argument] into a byte array.
48    pub const fn bytes(&self) -> [u8; Self::LEN] {
49        self.0.to_be_bytes()
50    }
51
52    /// Attempts to convert a [`u32`] into an [Argument].
53    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
54        match val.len() {
55            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
56            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
57        }
58    }
59}
60
61impl Default for Argument {
62    fn default() -> Self {
63        Self::new()
64    }
65}
66
67impl TryFrom<u32> for Argument {
68    type Error = Error;
69
70    fn try_from(val: u32) -> Result<Self> {
71        Self::try_from_bits(val)
72    }
73}
74
75impl From<Argument> for u32 {
76    fn from(val: Argument) -> Self {
77        val.bits()
78    }
79}
80
81impl From<Argument> for [u8; Argument::LEN] {
82    fn from(val: Argument) -> Self {
83        val.bytes()
84    }
85}
86
87impl TryFrom<&[u8]> for Argument {
88    type Error = Error;
89
90    fn try_from(val: &[u8]) -> Result<Self> {
91        Self::try_from_bytes(val)
92    }
93}
94
95impl<const N: usize> TryFrom<&[u8; N]> for Argument {
96    type Error = Error;
97
98    fn try_from(val: &[u8; N]) -> Result<Self> {
99        Self::try_from_bytes(val.as_ref())
100    }
101}
102
103impl<const N: usize> TryFrom<[u8; N]> for Argument {
104    type Error = Error;
105
106    fn try_from(val: [u8; N]) -> Result<Self> {
107        Self::try_from_bytes(val.as_ref())
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn test_fields() {
117        let new_bus_width = BusWidth::new();
118
119        (1..=u32::BITS)
120            .map(|r| ((1u64 << r) - 1) as u32)
121            .for_each(|raw_arg| {
122                let raw = raw_arg.to_be_bytes();
123
124                match Argument::try_from_bits(raw_arg) {
125                    Ok(mut exp_arg) => {
126                        let exp_bus_width = BusWidth::from_raw((raw_arg & 0b11) as u8).unwrap();
127
128                        assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
129                        assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
130                        assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
131                        assert_eq!(Argument::try_from(raw), Ok(exp_arg));
132                        assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
133
134                        assert_eq!(exp_arg.bus_width(), Ok(exp_bus_width));
135
136                        exp_arg.set_bus_width(new_bus_width);
137                        assert_eq!(exp_arg.bus_width(), Ok(new_bus_width));
138
139                        exp_arg.set_bus_width(exp_bus_width);
140                        assert_eq!(exp_arg.bus_width(), Ok(exp_bus_width));
141                    }
142                    Err(err) => {
143                        let exp_err = Error::invalid_field_variant(
144                            "command::argument::bus_width",
145                            (raw_arg & 0b11) as usize,
146                        );
147                        assert_eq!(err, exp_err);
148                        assert_eq!(Argument::try_from_bytes(&raw), Err(exp_err));
149                        assert_eq!(Argument::try_from(raw_arg), Err(exp_err));
150                        assert_eq!(Argument::try_from(raw), Err(exp_err));
151                        assert_eq!(Argument::try_from(&raw), Err(exp_err));
152                    }
153                }
154            });
155    }
156}