sdmmc_core/command/class/class1/cmd45/
arg.rs

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5    /// Argumentument for CMD45.
6    pub Argument(u32): u32 {
7        /// Starting block address for a queued task.
8        pub start_block_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    /// Attempts to converts a [`u32`] into an [Argument].
24    pub const fn try_from_bits(val: u32) -> Result<Self> {
25        Ok(Self(val))
26    }
27
28    /// Converts the [Argument] into a byte array.
29    pub const fn bytes(&self) -> [u8; Self::LEN] {
30        self.0.to_be_bytes()
31    }
32
33    /// Attempts to convert a byte slice into an [`Argument`].
34    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
35        match val.len() {
36            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
37            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
38        }
39    }
40}
41
42impl Default for Argument {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl From<Argument> for u32 {
49    fn from(val: Argument) -> Self {
50        val.bits()
51    }
52}
53
54impl From<Argument> for [u8; Argument::LEN] {
55    fn from(val: Argument) -> Self {
56        val.bytes()
57    }
58}
59
60impl TryFrom<u32> for Argument {
61    type Error = Error;
62
63    fn try_from(val: u32) -> Result<Self> {
64        Self::try_from_bits(val)
65    }
66}
67
68impl TryFrom<&[u8]> for Argument {
69    type Error = Error;
70
71    fn try_from(val: &[u8]) -> Result<Self> {
72        Self::try_from_bytes(val)
73    }
74}
75
76impl<const N: usize> TryFrom<&[u8; N]> for Argument {
77    type Error = Error;
78
79    fn try_from(val: &[u8; N]) -> Result<Self> {
80        Self::try_from_bytes(val.as_ref())
81    }
82}
83
84impl<const N: usize> TryFrom<[u8; N]> for Argument {
85    type Error = Error;
86
87    fn try_from(val: [u8; N]) -> Result<Self> {
88        Self::try_from_bytes(val.as_ref())
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_fields() {
98        (1..=u32::BITS)
99            .map(|r| ((1u64 << r) - 1) as u32)
100            .for_each(|raw_arg| {
101                let raw = raw_arg.to_be_bytes();
102                let mut exp_arg = Argument(raw_arg);
103                let exp_addr = raw_arg;
104
105                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
106                assert_eq!(Argument::try_from_bytes(raw.as_ref()), Ok(exp_arg));
107                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
108                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
109                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
110
111                assert_eq!(exp_arg.bits(), raw_arg);
112                assert_eq!(exp_arg.bytes(), raw);
113
114                assert_eq!(exp_arg.start_block_address(), exp_addr);
115
116                exp_arg.set_start_block_address(0);
117                assert_eq!(exp_arg.start_block_address(), 0);
118
119                exp_arg.set_start_block_address(exp_addr);
120                assert_eq!(exp_arg.start_block_address(), exp_addr);
121            });
122    }
123}