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

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5    /// Argumentument for CMD55.
6    pub Argument(u32): u16 {
7        /// Programs the RCA value for card select on the CMD line.
8        raw_rca: 31, 16;
9    }
10}
11
12impl Argument {
13    /// Represents the byte length of the [Argument].
14    pub const LEN: usize = 4;
15    /// Represents the raw 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    /// Gets the `RCA` field for the card select on the CMD line.
24    pub const fn rca(&self) -> u16 {
25        self.raw_rca()
26    }
27
28    /// Sets the `RCA` field for the card select on the CMD line.
29    pub fn set_rca(&mut self, val: u16) {
30        self.set_raw_rca(val as u32)
31    }
32
33    /// Converts a [`u32`] into an [Argument].
34    pub const fn from_bits(val: u32) -> Self {
35        Self(val)
36    }
37
38    /// Attempts to convert a [`u32`] into an [Argument].
39    pub const fn try_from_bits(val: u32) -> Result<Self> {
40        Ok(Self(val))
41    }
42
43    /// Converts the [Argument] into a byte array.
44    pub const fn bytes(&self) -> [u8; Self::LEN] {
45        self.0.to_be_bytes()
46    }
47
48    /// Attempts to convert a byte slice into an [Argument].
49    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
50        match val.len() {
51            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
52            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
53        }
54    }
55}
56
57impl Default for Argument {
58    fn default() -> Self {
59        Self::new()
60    }
61}
62
63impl From<u32> for Argument {
64    fn from(val: u32) -> Self {
65        Self::from_bits(val)
66    }
67}
68
69impl From<Argument> for u32 {
70    fn from(val: Argument) -> Self {
71        val.bits()
72    }
73}
74
75impl From<Argument> for [u8; Argument::LEN] {
76    fn from(val: Argument) -> Self {
77        val.bytes()
78    }
79}
80
81impl TryFrom<&[u8]> for Argument {
82    type Error = Error;
83
84    fn try_from(val: &[u8]) -> Result<Self> {
85        Self::try_from_bytes(val)
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
97impl<const N: usize> TryFrom<[u8; N]> for Argument {
98    type Error = Error;
99
100    fn try_from(val: [u8; N]) -> Result<Self> {
101        Self::try_from_bytes(val.as_ref())
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_fields() {
111        (1..=u16::BITS)
112            .map(|r| ((1u32 << r) - 1) as u16)
113            .for_each(|rca| {
114                let raw = (rca as u32) << 16;
115                let raw_bytes = raw.to_be_bytes();
116
117                let mut exp_arg = Argument(raw);
118
119                assert_eq!(Argument::try_from_bits(raw), Ok(exp_arg));
120                assert_eq!(Argument::from_bits(raw), exp_arg);
121                assert_eq!(Argument::from(raw), exp_arg);
122
123                assert_eq!(Argument::try_from_bytes(&raw_bytes), Ok(exp_arg));
124                assert_eq!(Argument::try_from(raw_bytes), Ok(exp_arg));
125                assert_eq!(Argument::try_from(&raw_bytes), Ok(exp_arg));
126
127                assert_eq!(exp_arg.bytes(), raw_bytes);
128                assert_eq!(<[u8; Argument::LEN]>::from(exp_arg), raw_bytes);
129
130                assert_eq!(exp_arg.rca(), rca);
131
132                exp_arg.set_rca(0);
133                assert_eq!(exp_arg.rca(), 0);
134
135                exp_arg.set_rca(rca);
136                assert_eq!(exp_arg.rca(), rca);
137            });
138    }
139}