sdmmc_core/command/class/class11/cmd49/
arg.rs

1#![allow(clippy::len_without_is_empty)]
2#![allow(clippy::manual_range_contains)]
3
4use crate::lib_bitfield;
5use crate::result::{Error, Result};
6use crate::util;
7
8pub use crate::command::class11::cmd48::Mio;
9
10mod addr;
11mod mask;
12
13pub use addr::*;
14pub use mask::*;
15
16lib_bitfield! {
17    /// Argumentument for CMD49.
18    pub Argument(u32): u32 {
19        raw_mio: 31;
20        raw_fno: 30, 27;
21        raw_mw: 26;
22        raw_addr: 25, 9;
23        raw_len: 8, 0;
24        raw_mask: 7, 0;
25    }
26}
27
28impl Argument {
29    /// Represents the byte length of the [Argument].
30    pub const LEN: usize = 4;
31    /// Represents the default value of the [Argument].
32    pub const DEFAULT: u32 = 0;
33
34    const LEN_MIN: usize = 1;
35    const LEN_MAX: usize = 512;
36
37    /// Creates a new [Argument].
38    pub const fn new() -> Self {
39        Self(Self::DEFAULT)
40    }
41
42    /// Gets the `mio` field of the [Argument].
43    pub const fn mio(&self) -> Mio {
44        Mio::from_bool(self.raw_mio())
45    }
46
47    /// Sets the `mio` field of the [Argument].
48    pub fn set_mio(&mut self, val: Mio) {
49        self.set_raw_mio(val.into_bool());
50    }
51
52    /// Gets the `mw` field of the [Argument].
53    pub const fn mw(&self) -> Mask {
54        Mask::from_bool(self.raw_mw())
55    }
56
57    /// Sets the `mw` field of the [Argument].
58    pub fn set_mw(&mut self, val: Mask) {
59        self.set_raw_mw(val.into_bool());
60    }
61
62    /// Gets the `fno` field of the [Argument].
63    pub const fn fno(&self) -> u8 {
64        util::fno_masked(self.mio(), self.raw_fno() as u8, false)
65    }
66
67    /// Sets the `fno` field of the [Argument].
68    ///
69    /// # Note
70    ///
71    /// The function number range depends on the [Mio] field:
72    ///
73    /// - [Mio::Memory]: `0 - 15`
74    /// - [Mio::Io]: `0 - 7`
75    pub fn set_fno(&mut self, val: u8) {
76        self.set_raw_fno(util::fno_masked(self.mio(), val, true) as u32);
77    }
78
79    /// Gets the `addr` field for the [Argument].
80    pub const fn addr(&self) -> Address {
81        Address::from_parts(self.mio(), self.raw_addr() as usize)
82    }
83
84    /// Sets the `addr` field for the [Argument].
85    ///
86    /// # Note
87    ///
88    /// Also sets the `mio` field based on the [Address] space.
89    pub fn set_addr(&mut self, val: Address) {
90        let (mio, addr) = val.into_parts();
91
92        self.set_mio(mio);
93        self.set_raw_addr(addr as u32);
94    }
95
96    /// Gets the `len` field for the [Argument].
97    ///
98    /// The length is adjusted to its semantic value (`raw_len + 1`).
99    pub const fn len(&self) -> usize {
100        match self.mw() {
101            Mask::Disabled => (self.raw_len() as usize) + 1,
102            Mask::Enabled => 1,
103        }
104    }
105
106    /// Sets the `len` field for the [Argument].
107    ///
108    /// The valid range for the length is `1-512` bytes.
109    ///
110    /// Input `val` as the unadjusted length, it is adjusted automatically.
111    ///
112    /// # Note
113    ///
114    /// Panics if `val` is outside the valid range.
115    ///
116    /// Also sets the `mw` field to [disabled](Mask::Disabled).
117    pub fn set_len(&mut self, val: usize) {
118        self.try_set_len(val).unwrap();
119    }
120
121    /// Attempts to set the `len` field for the [Argument].
122    ///
123    /// The valid range for the length is `1-512` bytes.
124    ///
125    /// Input `val` as the unadjusted length, it is adjusted automatically.
126    ///
127    /// # Note
128    ///
129    /// Returns an error if `val` is outside the valid range.
130    ///
131    /// Also sets the `mw` field to [disabled](Mask::Disabled).
132    pub fn try_set_len(&mut self, val: usize) -> Result<()> {
133        if val < Self::LEN_MIN || val > Self::LEN_MAX {
134            Err(Error::invalid_field_variant("cmd::argument::len", val))
135        } else {
136            self.set_raw_len((val - 1) as u32);
137            self.set_mw(Mask::Disabled);
138            Ok(())
139        }
140    }
141
142    /// Gets the `mask` field of the [Argument].
143    ///
144    /// # Note
145    ///
146    /// Only relevant when the `mw` field is [enabled](Mask::Enabled).
147    pub const fn mask(&self) -> u8 {
148        match self.mw() {
149            Mask::Disabled => 0,
150            Mask::Enabled => self.raw_mask() as u8,
151        }
152    }
153
154    /// Sets the `mask` field of the [Argument].
155    ///
156    /// # Note
157    ///
158    /// Also sets the `mw` field to [enabled](Mask::Enabled).
159    pub fn set_mask(&mut self, val: u8) {
160        self.set_raw_mask(val as u32);
161        self.set_mw(Mask::Enabled);
162    }
163
164    /// Attempts to convert a [`u32`] into an [`Argument`].
165    pub const fn try_from_bits(val: u32) -> Result<Self> {
166        Ok(Self(val))
167    }
168
169    /// Converts the [Argument] into a byte array.
170    pub const fn bytes(&self) -> [u8; Self::LEN] {
171        self.0.to_be_bytes()
172    }
173
174    /// Attempts to convert a byte slice into an [`Argument`].
175    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
176        match val.len() {
177            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
178            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
179        }
180    }
181}
182
183impl Default for Argument {
184    fn default() -> Self {
185        Self::new()
186    }
187}
188
189impl From<Argument> for u32 {
190    fn from(val: Argument) -> Self {
191        val.bits()
192    }
193}
194
195impl From<Argument> for [u8; Argument::LEN] {
196    fn from(val: Argument) -> Self {
197        val.bytes()
198    }
199}
200
201impl TryFrom<u32> for Argument {
202    type Error = Error;
203
204    fn try_from(val: u32) -> Result<Self> {
205        Self::try_from_bits(val)
206    }
207}
208
209impl TryFrom<&[u8]> for Argument {
210    type Error = Error;
211
212    fn try_from(val: &[u8]) -> Result<Self> {
213        Self::try_from_bytes(val)
214    }
215}
216
217impl<const N: usize> TryFrom<&[u8; N]> for Argument {
218    type Error = Error;
219
220    fn try_from(val: &[u8; N]) -> Result<Self> {
221        Self::try_from_bytes(val.as_ref())
222    }
223}
224
225impl<const N: usize> TryFrom<[u8; N]> for Argument {
226    type Error = Error;
227
228    fn try_from(val: [u8; N]) -> Result<Self> {
229        Self::try_from_bytes(val.as_ref())
230    }
231}
232
233#[cfg(test)]
234mod tests {
235    use super::*;
236
237    #[test]
238    fn test_fields() {
239        let new_mio = Mio::new();
240        let new_mw = Mask::new();
241        let new_addr = Address::new();
242
243        (1..=u32::BITS)
244            .map(|r| ((1u64 << r) - 1) as u32)
245            .for_each(|raw_arg| {
246                let mut exp_arg = Argument(raw_arg);
247                let raw = raw_arg.to_be_bytes();
248
249                let exp_mio = Mio::from_bool((raw_arg & 0x8000_0000) != 0);
250
251                let raw_fno = ((raw_arg & 0x7800_0000) >> 27) as u8;
252                let exp_fno = util::fno_masked(exp_mio, raw_fno, false);
253
254                let exp_mw = Mask::from_bool((raw_arg & (1 << 26)) != 0);
255
256                let raw_addr = ((raw_arg & 0x3fffe00) >> 9) as usize;
257                let exp_addr = Address::from_parts(exp_mio, raw_addr);
258
259                let exp_len = ((raw_arg & 0x1ff) + 1) as usize;
260                let exp_mask = (raw_arg & 0xff) as u8;
261
262                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
263                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
264                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
265                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
266                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
267
268                assert_eq!(exp_arg.bits(), raw_arg);
269                assert_eq!(exp_arg.bytes(), raw);
270
271                assert_eq!(exp_arg.mio(), exp_mio);
272
273                exp_arg.set_mio(new_mio);
274                assert_eq!(exp_arg.mio(), new_mio);
275
276                exp_arg.set_mio(exp_mio);
277                assert_eq!(exp_arg.mio(), exp_mio);
278
279                assert_eq!(exp_arg.fno(), exp_fno);
280
281                exp_arg.set_fno(0);
282                assert_eq!(exp_arg.fno(), 0);
283
284                exp_arg.set_fno(exp_fno);
285                assert_eq!(exp_arg.fno(), exp_fno);
286
287                assert_eq!(exp_arg.mw(), exp_mw);
288
289                exp_arg.set_mw(new_mw);
290                assert_eq!(exp_arg.mw(), new_mw);
291
292                exp_arg.set_mw(exp_mw);
293                assert_eq!(exp_arg.mw(), exp_mw);
294
295                assert_eq!(exp_arg.fno(), exp_fno);
296
297                assert_eq!(exp_arg.addr(), exp_addr);
298
299                exp_arg.set_addr(new_addr);
300                assert_eq!(exp_arg.addr(), new_addr);
301
302                exp_arg.set_addr(exp_addr);
303                assert_eq!(exp_arg.addr(), exp_addr);
304
305                match exp_mw {
306                    Mask::Disabled => {
307                        assert_eq!(exp_arg.len(), exp_len);
308
309                        exp_arg.set_len(1);
310                        assert_eq!(exp_arg.len(), 1);
311
312                        exp_arg.set_len(exp_len);
313                        assert_eq!(exp_arg.len(), exp_len);
314                        assert_eq!(exp_arg.mw(), Mask::Disabled);
315
316                        exp_arg.set_mask(0);
317                        assert_eq!(exp_arg.mask(), 0);
318                        assert_eq!(exp_arg.mw(), Mask::Enabled);
319                    }
320                    Mask::Enabled => {
321                        assert_eq!(exp_arg.mask(), exp_mask);
322
323                        exp_arg.set_mask(0);
324                        assert_eq!(exp_arg.mask(), 0);
325
326                        exp_arg.set_mask(exp_mask);
327                        assert_eq!(exp_arg.mask(), exp_mask);
328                        assert_eq!(exp_arg.mw(), Mask::Enabled);
329
330                        exp_arg.set_len(1);
331                        assert_eq!(exp_arg.len(), 1);
332                        assert_eq!(exp_arg.mw(), Mask::Disabled);
333                    }
334                }
335            });
336    }
337}