sdmmc_core/command/class/class9/cmd52/
arg.rs

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4mod rw;
5
6pub use rw::*;
7
8lib_bitfield! {
9    /// Argumentument for CMD52.
10    pub Argument(u32): u32 {
11        raw_rw: 31;
12        raw_fno: 30, 27;
13        raw_addr: 25, 9;
14        raw_data: 7, 0;
15    }
16}
17
18impl Argument {
19    /// Represents the byte length of the [Argument].
20    pub const LEN: usize = 4;
21    /// Represents the default value of the [Argument].
22    pub const DEFAULT: u32 = 0;
23
24    /// Creates a new [Argument].
25    pub const fn new() -> Self {
26        Self(Self::DEFAULT)
27    }
28
29    /// Gets the `rw` field of the [Argument].
30    pub const fn rw(&self) -> ReadWrite {
31        ReadWrite::from_bool(self.raw_rw())
32    }
33
34    /// Sets the `rw` field of the [Argument].
35    ///
36    /// # Note
37    ///
38    /// If mode is set to [Read](ReadWrite::Read), sets the `data` field to zero.
39    pub fn set_rw(&mut self, val: ReadWrite) {
40        self.set_raw_rw(val.into_bool());
41        if matches!(val, ReadWrite::Read) {
42            self.set_raw_data(0);
43        }
44    }
45
46    /// Gets the `fno` field of the [Argument].
47    pub const fn fno(&self) -> u8 {
48        self.raw_fno() as u8
49    }
50
51    /// Sets the `fno` field of the [Argument].
52    pub fn set_fno(&mut self, val: u8) {
53        self.set_raw_fno(val as u32);
54    }
55
56    /// Gets the `addr` field for the [Argument].
57    pub const fn addr(&self) -> u32 {
58        self.raw_addr()
59    }
60
61    /// Sets the `addr` field for the [Argument].
62    pub fn set_addr(&mut self, val: u32) {
63        self.set_raw_addr(val);
64    }
65
66    /// Gets the `data` field of the [Argument].
67    ///
68    /// # Note
69    ///
70    /// Only relevant when the `rw` field is set to [Write](ReadWrite::Write).
71    pub const fn data(&self) -> u8 {
72        match self.rw() {
73            ReadWrite::Read => 0,
74            ReadWrite::Write => self.raw_data() as u8,
75        }
76    }
77
78    /// Sets the `data` field of the [Argument].
79    ///
80    /// # Note
81    ///
82    /// Also sets the `rw` field to [Write](ReadWrite::Write).
83    pub fn set_data(&mut self, val: u8) {
84        self.set_raw_data(val as u32);
85        self.set_rw(ReadWrite::Write);
86    }
87
88    /// Attempts to convert a [`u32`] into an [`Argument`].
89    pub const fn try_from_bits(val: u32) -> Result<Self> {
90        Ok(Self(val))
91    }
92
93    /// Converts the [Argument] into a byte array.
94    pub const fn bytes(&self) -> [u8; Self::LEN] {
95        self.0.to_be_bytes()
96    }
97
98    /// Attempts to convert a byte slice into an [`Argument`].
99    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
100        match val.len() {
101            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
102            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
103        }
104    }
105}
106
107impl Default for Argument {
108    fn default() -> Self {
109        Self::new()
110    }
111}
112
113impl From<Argument> for u32 {
114    fn from(val: Argument) -> Self {
115        val.bits()
116    }
117}
118
119impl From<Argument> for [u8; Argument::LEN] {
120    fn from(val: Argument) -> Self {
121        val.bytes()
122    }
123}
124
125impl TryFrom<u32> for Argument {
126    type Error = Error;
127
128    fn try_from(val: u32) -> Result<Self> {
129        Self::try_from_bits(val)
130    }
131}
132
133impl TryFrom<&[u8]> for Argument {
134    type Error = Error;
135
136    fn try_from(val: &[u8]) -> Result<Self> {
137        Self::try_from_bytes(val)
138    }
139}
140
141impl<const N: usize> TryFrom<&[u8; N]> for Argument {
142    type Error = Error;
143
144    fn try_from(val: &[u8; N]) -> Result<Self> {
145        Self::try_from_bytes(val.as_ref())
146    }
147}
148
149impl<const N: usize> TryFrom<[u8; N]> for Argument {
150    type Error = Error;
151
152    fn try_from(val: [u8; N]) -> Result<Self> {
153        Self::try_from_bytes(val.as_ref())
154    }
155}
156
157#[cfg(test)]
158mod tests {
159    use super::*;
160
161    #[test]
162    fn test_fields() {
163        let new_rw = ReadWrite::new();
164
165        (1..=u32::BITS)
166            .map(|r| ((1u64 << r) - 1) as u32)
167            .for_each(|raw_arg| {
168                let mut exp_arg = Argument(raw_arg);
169                let raw = raw_arg.to_be_bytes();
170
171                let exp_rw = ReadWrite::from_bool((raw_arg & (1 << 31)) != 0);
172                let exp_fno = ((raw_arg & 0x7800_0000) >> 27) as u8;
173                let exp_addr = (raw_arg & 0x3fffe00) >> 9;
174                let exp_data = (raw_arg & 0xff) as u8;
175
176                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
177                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
178                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
179                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
180                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
181
182                assert_eq!(exp_arg.bits(), raw_arg);
183                assert_eq!(exp_arg.bytes(), raw);
184                assert_eq!(exp_arg.addr(), exp_addr);
185
186                assert_eq!(exp_arg.rw(), exp_rw);
187                assert_eq!(exp_arg.fno(), exp_fno);
188                assert_eq!(exp_arg.addr(), exp_addr);
189                if matches!(exp_rw, ReadWrite::Write) {
190                    assert_eq!(exp_arg.data(), exp_data);
191                }
192
193                exp_arg.set_rw(new_rw);
194                assert_eq!(exp_arg.rw(), new_rw);
195                assert_eq!(exp_arg.data(), 0);
196
197                exp_arg.set_rw(exp_rw);
198                assert_eq!(exp_arg.rw(), exp_rw);
199
200                exp_arg.set_fno(0);
201                assert_eq!(exp_arg.fno(), 0);
202
203                exp_arg.set_fno(exp_fno);
204                assert_eq!(exp_arg.fno(), exp_fno);
205
206                exp_arg.set_addr(0);
207                assert_eq!(exp_arg.addr(), 0);
208
209                exp_arg.set_addr(exp_addr);
210                assert_eq!(exp_arg.addr(), exp_addr);
211
212                exp_arg.set_data(0);
213                assert_eq!(exp_arg.data(), 0);
214
215                exp_arg.set_data(exp_data);
216                assert_eq!(exp_arg.data(), exp_data);
217                assert_eq!(exp_arg.rw(), ReadWrite::Write);
218            });
219    }
220}