sdmmc_core/command/class/class0/cmd6/
arg.rs

1use crate::lib_bitfield;
2use crate::register::ext_csd::MmcCommandSet;
3use crate::result::{Error, Result};
4
5mod access;
6
7pub use access::*;
8
9lib_bitfield! {
10    /// Argument for [Cmd6](super::Cmd6).
11    pub Argument(u32): u8 {
12        raw_access: 25, 24;
13        raw_index: 23, 16;
14        raw_value: 15, 8;
15        raw_command_set: 2, 0;
16    }
17}
18
19impl Argument {
20    /// Represents the byte length of the [Argument].
21    pub const LEN: usize = 4;
22    /// Represents the default byte value of the [Argument].
23    pub const DEFAULT: u32 = 0;
24
25    /// Creates a new [Argument].
26    pub const fn new() -> Self {
27        Self(Self::DEFAULT)
28    }
29
30    /// Gets the `EXT_CSD` [Access] mode for the [Argument].
31    pub const fn access(&self) -> Access {
32        Access::from_raw_unchecked(self.raw_access())
33    }
34
35    /// Sets the `EXT_CSD` [Access] mode for the [Argument].
36    pub fn set_access(&mut self, val: Access) {
37        self.set_raw_access(val.into_raw() as u32);
38    }
39
40    /// Gets the `EXT_CSD` byte index  for the [Argument].
41    pub const fn index(&self) -> usize {
42        self.raw_index() as usize
43    }
44
45    /// Sets the `EXT_CSD` byte index  for the [Argument].
46    pub fn set_index(&mut self, val: usize) {
47        self.set_raw_index(val as u32);
48    }
49
50    /// Gets the `EXT_CSD` byte value  for the [Argument].
51    pub const fn value(&self) -> u8 {
52        self.raw_value()
53    }
54
55    /// Sets the `EXT_CSD` byte value  for the [Argument].
56    pub fn set_value(&mut self, val: u8) {
57        self.set_raw_value(val as u32);
58    }
59
60    /// Gets the command set selected by the [Argument].
61    pub const fn command_set(&self) -> Result<MmcCommandSet> {
62        MmcCommandSet::try_from_inner(self.raw_command_set())
63    }
64
65    /// Sets the command set selected by the [Argument].
66    pub fn set_command_set(&mut self, val: MmcCommandSet) {
67        self.set_raw_command_set(val.into_inner() as u32);
68    }
69
70    /// Converts the [Argument] into a byte array.
71    pub const fn bytes(&self) -> [u8; Self::LEN] {
72        self.0.to_be_bytes()
73    }
74
75    /// Attempts to convert a [`u32`] into an [Argument].
76    pub const fn try_from_bits(val: u32) -> Result<Self> {
77        match Self(val) {
78            a if a.command_set().is_err() => Err(Error::invalid_field_variant(
79                "cmd6::arg::command_set",
80                a.raw_command_set() as usize,
81            )),
82            a => Ok(a),
83        }
84    }
85
86    /// Attempts to convert a byte slice into an [Argument].
87    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
88        match val.len() {
89            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
90            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
91        }
92    }
93}
94
95impl Default for Argument {
96    fn default() -> Self {
97        Self::new()
98    }
99}
100
101impl TryFrom<u32> for Argument {
102    type Error = Error;
103
104    fn try_from(val: u32) -> Result<Self> {
105        Self::try_from_bits(val)
106    }
107}
108
109impl From<Argument> for u32 {
110    fn from(val: Argument) -> Self {
111        val.bits()
112    }
113}
114
115impl From<Argument> for [u8; Argument::LEN] {
116    fn from(val: Argument) -> Self {
117        val.bytes()
118    }
119}
120
121impl TryFrom<&[u8]> for Argument {
122    type Error = Error;
123
124    fn try_from(val: &[u8]) -> Result<Self> {
125        Self::try_from_bytes(val)
126    }
127}
128
129impl<const N: usize> TryFrom<&[u8; N]> for Argument {
130    type Error = Error;
131
132    fn try_from(val: &[u8; N]) -> Result<Self> {
133        Self::try_from_bytes(val.as_ref())
134    }
135}
136
137impl<const N: usize> TryFrom<[u8; N]> for Argument {
138    type Error = Error;
139
140    fn try_from(val: [u8; N]) -> Result<Self> {
141        Self::try_from_bytes(val.as_ref())
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn test_fields() {
151        let new_access = Access::new();
152        let new_command_set = MmcCommandSet::new();
153        let new_index = 0;
154        let new_value = 0;
155
156        (1..u32::BITS)
157            .map(|r| ((1u64 << r) - 1) as u32)
158            .for_each(|raw_arg| {
159                let mut exp_arg = Argument(raw_arg);
160                let exp_access = Access::from_raw_unchecked(((raw_arg >> 24) & 0b11) as u8);
161                let exp_index = ((raw_arg >> 16) & 0xff) as usize;
162                let exp_value = ((raw_arg >> 8) & 0xff) as u8;
163                let raw_command_set = (raw_arg & 0b111) as u8;
164                let raw = raw_arg.to_be_bytes();
165
166                match MmcCommandSet::try_from_inner(raw_command_set) {
167                    Ok(exp_command_set) => {
168                        assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
169                        assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
170                        assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
171                        assert_eq!(Argument::try_from(raw), Ok(exp_arg));
172
173                        assert_eq!(exp_arg.bits(), raw_arg);
174                        assert_eq!(exp_arg.bytes(), raw);
175
176                        assert_eq!(exp_arg.access(), exp_access);
177
178                        exp_arg.set_access(new_access);
179                        assert_eq!(exp_arg.access(), new_access);
180
181                        exp_arg.set_access(exp_access);
182                        assert_eq!(exp_arg.access(), exp_access);
183
184                        assert_eq!(exp_arg.index(), exp_index);
185
186                        exp_arg.set_index(new_index);
187                        assert_eq!(exp_arg.index(), new_index);
188
189                        exp_arg.set_index(exp_index);
190                        assert_eq!(exp_arg.index(), exp_index);
191
192                        assert_eq!(exp_arg.value(), exp_value);
193
194                        exp_arg.set_value(new_value);
195                        assert_eq!(exp_arg.value(), new_value);
196
197                        exp_arg.set_value(exp_value);
198                        assert_eq!(exp_arg.value(), exp_value);
199
200                        assert_eq!(exp_arg.command_set(), Ok(exp_command_set));
201
202                        exp_arg.set_command_set(new_command_set);
203                        assert_eq!(exp_arg.command_set(), Ok(new_command_set));
204
205                        exp_arg.set_command_set(exp_command_set);
206                        assert_eq!(exp_arg.command_set(), Ok(exp_command_set));
207                    }
208                    Err(err) => {
209                        let exp_err = Error::invalid_field_variant(
210                            "cmd6::arg::command_set",
211                            raw_command_set as usize,
212                        );
213
214                        assert_eq!(Argument::try_from_bits(raw_arg), Err(exp_err));
215                        assert_eq!(Argument::try_from_bytes(&raw), Err(exp_err));
216                    }
217                }
218            });
219    }
220}