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

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