sdmmc_core/command/class/class11/cmd58/
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 buc;
11mod bus;
12
13pub use buc::*;
14pub use bus::*;
15
16lib_bitfield! {
17    /// Argumentument for CMD58.
18    pub Argument(u32): u32 {
19        raw_mio: 31;
20        raw_fno: 30, 27;
21        raw_bus: 26;
22        raw_addr: 25, 9;
23        raw_buc: 8, 0;
24    }
25}
26
27impl Argument {
28    /// Represents the byte length of the [Argument].
29    pub const LEN: usize = 4;
30    /// Represents the default value of the [Argument].
31    pub const DEFAULT: u32 = 0;
32
33    /// Creates a new [Argument].
34    pub const fn new() -> Self {
35        Self(Self::DEFAULT)
36    }
37
38    /// Gets the `mio` field of the [Argument].
39    pub const fn mio(&self) -> Mio {
40        Mio::from_bool(self.raw_mio())
41    }
42
43    /// Sets the `mio` field of the [Argument].
44    pub fn set_mio(&mut self, val: Mio) {
45        self.set_raw_mio(val.into_bool());
46    }
47
48    /// Gets the `bus` field of the [Argument].
49    pub const fn bus(&self) -> BlockUnitSelect {
50        BlockUnitSelect::from_bool(self.raw_bus())
51    }
52
53    /// Sets the `bus` field of the [Argument].
54    pub fn set_bus(&mut self, val: BlockUnitSelect) {
55        self.set_raw_bus(val.into_bool());
56    }
57
58    /// Gets the `fno` field of the [Argument].
59    pub const fn fno(&self) -> u8 {
60        util::fno_masked(self.mio(), self.raw_fno() as u8, false)
61    }
62
63    /// Sets the `fno` field of the [Argument].
64    ///
65    /// # Note
66    ///
67    /// The function number range depends on the [Mio] field:
68    ///
69    /// - [Mio::Memory]: `0 - 15`
70    /// - [Mio::Io]: `0 - 7`
71    pub fn set_fno(&mut self, val: u8) {
72        self.set_raw_fno(util::fno_masked(self.mio(), val, true) as u32);
73    }
74
75    /// Gets the `addr` field for the [Argument].
76    pub const fn addr(&self) -> u32 {
77        self.raw_addr()
78    }
79
80    /// Sets the `addr` field for the [Argument].
81    ///
82    /// # Note
83    ///
84    /// Also sets the `mio` field based on the address space.
85    pub fn set_addr(&mut self, val: u32) {
86        self.set_raw_addr(val);
87    }
88
89    /// Gets the `buc` field of the [Argument].
90    pub const fn buc(&self) -> BlockUnitCount {
91        BlockUnitCount::from_bits_unchecked(self.raw_buc() as u16)
92    }
93
94    /// Sets the `buc` field of the [Argument].
95    pub fn set_buc(&mut self, val: BlockUnitCount) {
96        self.set_raw_buc(val.bits() as u32);
97    }
98
99    /// Attempts to convert a [`u32`] into an [`Argument`].
100    pub const fn try_from_bits(val: u32) -> Result<Self> {
101        Ok(Self(val))
102    }
103
104    /// Converts the [Argument] into a byte array.
105    pub const fn bytes(&self) -> [u8; Self::LEN] {
106        self.0.to_be_bytes()
107    }
108
109    /// Attempts to convert a byte slice into an [`Argument`].
110    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
111        match val.len() {
112            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
113            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
114        }
115    }
116}
117
118impl Default for Argument {
119    fn default() -> Self {
120        Self::new()
121    }
122}
123
124impl From<Argument> for u32 {
125    fn from(val: Argument) -> Self {
126        val.bits()
127    }
128}
129
130impl From<Argument> for [u8; Argument::LEN] {
131    fn from(val: Argument) -> Self {
132        val.bytes()
133    }
134}
135
136impl TryFrom<u32> for Argument {
137    type Error = Error;
138
139    fn try_from(val: u32) -> Result<Self> {
140        Self::try_from_bits(val)
141    }
142}
143
144impl TryFrom<&[u8]> for Argument {
145    type Error = Error;
146
147    fn try_from(val: &[u8]) -> Result<Self> {
148        Self::try_from_bytes(val)
149    }
150}
151
152impl<const N: usize> TryFrom<&[u8; N]> for Argument {
153    type Error = Error;
154
155    fn try_from(val: &[u8; N]) -> Result<Self> {
156        Self::try_from_bytes(val.as_ref())
157    }
158}
159
160impl<const N: usize> TryFrom<[u8; N]> for Argument {
161    type Error = Error;
162
163    fn try_from(val: [u8; N]) -> Result<Self> {
164        Self::try_from_bytes(val.as_ref())
165    }
166}
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171
172    #[test]
173    fn test_fields() {
174        let new_mio = Mio::new();
175        let new_buc = BlockUnitCount::new();
176        let new_bus = BlockUnitSelect::new();
177
178        (1..=u32::BITS)
179            .map(|r| ((1u64 << r) - 1) as u32)
180            .for_each(|raw_arg| {
181                let mut exp_arg = Argument(raw_arg);
182                let raw = raw_arg.to_be_bytes();
183
184                let exp_mio = Mio::from_bool((raw_arg & 0x8000_0000) != 0);
185
186                let raw_fno = ((raw_arg & 0x7800_0000) >> 27) as u8;
187                let exp_fno = util::fno_masked(exp_mio, raw_fno, false);
188
189                let exp_bus = BlockUnitSelect::from_bool((raw_arg & (1 << 26)) != 0);
190
191                let exp_addr = ((raw_arg & 0x3fffe00) >> 9);
192
193                let exp_buc = BlockUnitCount::from_bits_unchecked((raw_arg & 0x1ff) as u16);
194
195                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
196                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
197                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
198                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
199                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
200
201                assert_eq!(exp_arg.bits(), raw_arg);
202                assert_eq!(exp_arg.bytes(), raw);
203
204                assert_eq!(exp_arg.mio(), exp_mio);
205
206                exp_arg.set_mio(new_mio);
207                assert_eq!(exp_arg.mio(), new_mio);
208
209                exp_arg.set_mio(exp_mio);
210                assert_eq!(exp_arg.mio(), exp_mio);
211
212                assert_eq!(exp_arg.fno(), exp_fno);
213
214                exp_arg.set_fno(0);
215                assert_eq!(exp_arg.fno(), 0);
216
217                exp_arg.set_fno(exp_fno);
218                assert_eq!(exp_arg.fno(), exp_fno);
219
220                assert_eq!(exp_arg.bus(), exp_bus);
221
222                exp_arg.set_bus(new_bus);
223                assert_eq!(exp_arg.bus(), new_bus);
224
225                exp_arg.set_bus(exp_bus);
226                assert_eq!(exp_arg.bus(), exp_bus);
227
228                assert_eq!(exp_arg.fno(), exp_fno);
229
230                assert_eq!(exp_arg.addr(), exp_addr);
231
232                exp_arg.set_addr(0);
233                assert_eq!(exp_arg.addr(), 0);
234
235                exp_arg.set_addr(exp_addr);
236                assert_eq!(exp_arg.addr(), exp_addr);
237
238                assert_eq!(exp_arg.buc(), exp_buc);
239
240                exp_arg.set_buc(new_buc);
241                assert_eq!(exp_arg.buc(), new_buc);
242
243                exp_arg.set_buc(exp_buc);
244                assert_eq!(exp_arg.buc(), exp_buc);
245            });
246    }
247}