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

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4mod operation_code;
5
6pub use operation_code::*;
7
8lib_bitfield! {
9    /// Argumentument for CMD43.
10    pub Argument(u32): u8 {
11        /// Queued task ID to abort, see [OperationCode].
12        raw_task_id: 20, 16;
13        raw_operation_code: 3, 0;
14    }
15}
16
17impl Argument {
18    /// Represents the byte length of the [Argument].
19    pub const LEN: usize = 4;
20    /// Represents the default value of the [Argument].
21    pub const DEFAULT: u32 = 1;
22
23    /// Creates a new [Argument].
24    pub const fn new() -> Self {
25        Self(Self::DEFAULT)
26    }
27
28    /// Gets the task ID field for the [Argument].
29    pub const fn task_id(&self) -> u8 {
30        self.raw_task_id()
31    }
32
33    /// Sets the task ID field for the [Argument].
34    pub fn set_task_id(&mut self, val: u8) {
35        self.set_raw_task_id(val as u32);
36    }
37
38    /// Gets the `Queue Management` [OperationCode] for [Argument].
39    pub const fn operation_code(&self) -> Result<OperationCode> {
40        OperationCode::from_raw(self.raw_operation_code())
41    }
42
43    /// Sets the `Queue Management` [OperationCode] for [Argument].
44    pub fn set_operation_code(&mut self, val: OperationCode) {
45        self.set_raw_operation_code(val.into_raw() as u32);
46    }
47
48    /// Attempts to convert a [`u32`] into an [`Argument`].
49    pub const fn try_from_bits(val: u32) -> Result<Self> {
50        match Self(val) {
51            a if a.operation_code().is_err() => Err(Error::invalid_field_variant(
52                "cmd::argument::operation_code",
53                a.raw_operation_code() as usize,
54            )),
55            a => Ok(a),
56        }
57    }
58
59    /// Converts the [Argument] into a byte array.
60    pub const fn bytes(&self) -> [u8; Self::LEN] {
61        self.0.to_be_bytes()
62    }
63
64    /// Attempts to convert a byte slice into an [`Argument`].
65    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
66        match val.len() {
67            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
68            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
69        }
70    }
71}
72
73impl Default for Argument {
74    fn default() -> Self {
75        Self::new()
76    }
77}
78
79impl From<Argument> for u32 {
80    fn from(val: Argument) -> Self {
81        val.bits()
82    }
83}
84
85impl From<Argument> for [u8; Argument::LEN] {
86    fn from(val: Argument) -> Self {
87        val.bytes()
88    }
89}
90
91impl TryFrom<u32> for Argument {
92    type Error = Error;
93
94    fn try_from(val: u32) -> Result<Self> {
95        Self::try_from_bits(val)
96    }
97}
98
99impl TryFrom<&[u8]> for Argument {
100    type Error = Error;
101
102    fn try_from(val: &[u8]) -> Result<Self> {
103        Self::try_from_bytes(val)
104    }
105}
106
107impl<const N: usize> TryFrom<&[u8; N]> for Argument {
108    type Error = Error;
109
110    fn try_from(val: &[u8; N]) -> Result<Self> {
111        Self::try_from_bytes(val.as_ref())
112    }
113}
114
115impl<const N: usize> TryFrom<[u8; N]> for Argument {
116    type Error = Error;
117
118    fn try_from(val: [u8; N]) -> Result<Self> {
119        Self::try_from_bytes(val.as_ref())
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn test_fields() {
129        let new_op_code = OperationCode::new();
130
131        (1..=u32::BITS)
132            .map(|r| ((1u64 << r) - 1) as u32)
133            .for_each(|raw_arg| {
134                let raw = raw_arg.to_be_bytes();
135                let raw_op_code = (raw_arg & 0xf) as u8;
136                let exp_task_id = ((raw_arg & 0x1f_0000) >> 16) as u8;
137
138                match Argument::try_from_bits(raw_arg) {
139                    Ok(mut exp_arg) => {
140                        assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
141                        assert_eq!(Argument::try_from_bytes(raw.as_ref()), Ok(exp_arg));
142                        assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
143                        assert_eq!(Argument::try_from(raw), Ok(exp_arg));
144                        assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
145
146                        let exp_op_code = OperationCode::from_raw(raw_op_code).unwrap();
147
148                        assert_eq!(exp_arg.bits(), raw_arg);
149                        assert_eq!(exp_arg.bytes(), raw);
150
151                        assert_eq!(exp_arg.task_id(), exp_task_id);
152
153                        exp_arg.set_task_id(0);
154                        assert_eq!(exp_arg.task_id(), 0);
155
156                        exp_arg.set_task_id(exp_task_id);
157                        assert_eq!(exp_arg.task_id(), exp_task_id);
158
159                        assert_eq!(exp_arg.operation_code(), Ok(exp_op_code));
160
161                        exp_arg.set_operation_code(new_op_code);
162                        assert_eq!(exp_arg.operation_code(), Ok(new_op_code));
163
164                        exp_arg.set_operation_code(exp_op_code);
165                        assert_eq!(exp_arg.operation_code(), Ok(exp_op_code));
166                    }
167                    Err(_) => {
168                        let exp_err = Error::invalid_field_variant(
169                            "cmd::argument::operation_code",
170                            raw_op_code as usize,
171                        );
172
173                        assert_eq!(Argument::try_from_bits(raw_arg), Err(exp_err));
174                        assert_eq!(Argument::try_from_bytes(&raw), Err(exp_err));
175                        assert_eq!(Argument::try_from(raw_arg), Err(exp_err));
176                        assert_eq!(Argument::try_from(raw), Err(exp_err));
177                        assert_eq!(Argument::try_from(&raw), Err(exp_err));
178                    }
179                }
180            });
181    }
182}