sdmmc_core/command/class/class11/cmd48/
arg.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#![allow(clippy::len_without_is_empty)]
#![allow(clippy::manual_range_contains)]

use crate::lib_bitfield;
use crate::result::{Error, Result};
use crate::util;

mod addr;
mod mio;

pub use addr::*;
pub use mio::*;

lib_bitfield! {
    /// Argumentument for CMD48.
    pub Argument(u32): u32 {
        raw_mio: 31;
        raw_fno: 30, 27;
        raw_addr: 25, 9;
        raw_len: 8, 0;
    }
}

impl Argument {
    /// Represents the byte length of the [Argument].
    pub const LEN: usize = 4;
    /// Represents the default value of the [Argument].
    pub const DEFAULT: u32 = 0;

    const LEN_MIN: usize = 1;
    const LEN_MAX: usize = 512;

    /// Creates a new [Argument].
    pub const fn new() -> Self {
        Self(Self::DEFAULT)
    }

    /// Gets the `mio` field of the [Argument].
    pub const fn mio(&self) -> Mio {
        Mio::from_bool(self.raw_mio())
    }

    /// Sets the `mio` field of the [Argument].
    pub fn set_mio(&mut self, val: Mio) {
        self.set_raw_mio(val.into_bool());
    }

    /// Gets the `fno` field of the [Argument].
    pub const fn fno(&self) -> u8 {
        util::fno_masked(self.mio(), self.raw_fno() as u8, false)
    }

    /// Sets the `fno` field of the [Argument].
    ///
    /// # Note
    ///
    /// The function number range depends on the [Mio] field:
    ///
    /// - [Mio::Memory]: `0 - 15`
    /// - [Mio::Io]: `0 - 7`
    pub fn set_fno(&mut self, val: u8) {
        self.set_raw_fno(util::fno_masked(self.mio(), val, true) as u32);
    }

    /// Gets the `addr` field for the [Argument].
    pub const fn addr(&self) -> Address {
        Address::from_bits(self.raw_addr())
    }

    /// Sets the `addr` field for the [Argument].
    pub fn set_addr(&mut self, val: Address) {
        self.set_raw_addr(val.bits());
    }

    /// Gets the `len` field for the [Argument].
    ///
    /// The length is adjusted to its semantic value (`raw_len + 1`).
    pub const fn len(&self) -> usize {
        (self.raw_len() as usize) + 1
    }

    /// Sets the `len` field for the [Argument].
    ///
    /// The valid range for the length is `1-512` bytes.
    ///
    /// Input `val` as the unadjusted length, it is adjusted automatically.
    ///
    /// # Note
    ///
    /// Panics if `val` is outside the valid range.
    pub fn set_len(&mut self, val: usize) {
        self.try_set_len(val).unwrap();
    }

    /// Attempts to set the `len` field for the [Argument].
    ///
    /// The valid range for the length is `1-512` bytes.
    ///
    /// Input `val` as the unadjusted length, it is adjusted automatically.
    ///
    /// # Note
    ///
    /// Returns an error if `val` is outside the valid range.
    pub fn try_set_len(&mut self, val: usize) -> Result<()> {
        if val < Self::LEN_MIN || val > Self::LEN_MAX {
            Err(Error::invalid_field_variant("cmd::argument::len", val))
        } else {
            self.set_raw_len((val - 1) as u32);
            Ok(())
        }
    }

    /// Attempts to convert a [`u32`] into an [`Argument`].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self(val))
    }

    /// Converts the [Argument] into a byte array.
    pub const fn bytes(&self) -> [u8; Self::LEN] {
        self.0.to_be_bytes()
    }

    /// Attempts to convert a byte slice into an [`Argument`].
    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
        match val.len() {
            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
        }
    }
}

impl Default for Argument {
    fn default() -> Self {
        Self::new()
    }
}

impl From<Argument> for u32 {
    fn from(val: Argument) -> Self {
        val.bits()
    }
}

impl From<Argument> for [u8; Argument::LEN] {
    fn from(val: Argument) -> Self {
        val.bytes()
    }
}

impl TryFrom<u32> for Argument {
    type Error = Error;

    fn try_from(val: u32) -> Result<Self> {
        Self::try_from_bits(val)
    }
}

impl TryFrom<&[u8]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8]) -> Result<Self> {
        Self::try_from_bytes(val)
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

impl<const N: usize> TryFrom<[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fields() {
        let new_mio = Mio::new();
        let new_addr = Address::new();

        (1..=u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let mut exp_arg = Argument(raw_arg);
                let raw = raw_arg.to_be_bytes();

                let exp_mio = Mio::from_bool((raw_arg & 0x8000_0000) != 0);

                let raw_fno = ((raw_arg & 0x7800_0000) >> 27) as u8;
                let exp_fno = util::fno_masked(exp_mio, raw_fno, false);

                let raw_addr = (raw_arg & 0x3fffe00) >> 9;
                let exp_addr = Address::from_bits(raw_addr);

                let exp_len = ((raw_arg & 0x1ff) + 1) as usize;

                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(raw), Ok(exp_arg));

                assert_eq!(exp_arg.bits(), raw_arg);
                assert_eq!(exp_arg.bytes(), raw);

                assert_eq!(exp_arg.mio(), exp_mio);

                exp_arg.set_mio(new_mio);
                assert_eq!(exp_arg.mio(), new_mio);

                exp_arg.set_mio(exp_mio);
                assert_eq!(exp_arg.mio(), exp_mio);

                assert_eq!(exp_arg.fno(), exp_fno);

                exp_arg.set_fno(0);
                assert_eq!(exp_arg.fno(), 0);

                exp_arg.set_fno(exp_fno);
                assert_eq!(exp_arg.fno(), exp_fno);

                assert_eq!(exp_arg.addr(), exp_addr);

                exp_arg.set_addr(new_addr);
                assert_eq!(exp_arg.addr(), new_addr);

                exp_arg.set_addr(exp_addr);
                assert_eq!(exp_arg.addr(), exp_addr);

                assert_eq!(exp_arg.len(), exp_len);

                exp_arg.set_len(1);
                assert_eq!(exp_arg.len(), 1);

                exp_arg.set_len(exp_len);
                assert_eq!(exp_arg.len(), exp_len);
            });
    }
}