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