sdmmc_core/command/class/class9/cmd5/
arg.rs

1use crate::lib_bitfield;
2use crate::register::IoOcr;
3use crate::result::{Error, Result};
4
5lib_bitfield! {
6    /// Argumentument for CMD5.
7    pub Argument(u32): u32 {
8        /// Switching 1.8V request.
9        pub s18r: 24;
10        raw_io_ocr: 23, 0;
11    }
12}
13
14impl Argument {
15    /// Represents the byte length of the [Argument].
16    pub const LEN: usize = 4;
17    /// Represents the raw default value of the [Argument].
18    pub const DEFAULT: u32 = 1;
19
20    /// Creates a new [Argument].
21    pub const fn new() -> Self {
22        Self(1)
23    }
24
25    /// Gets the I/O Operation Conditions Register ([IoOcr]) value.
26    pub const fn io_ocr(&self) -> IoOcr {
27        IoOcr::from_bits(self.raw_io_ocr())
28    }
29
30    /// Sets the I/O Operation Conditions Register ([IoOcr]) value.
31    pub fn set_io_ocr(&mut self, val: IoOcr) {
32        self.set_raw_io_ocr(val.bits());
33    }
34
35    /// Converts a [`u32`] into an [Argument].
36    pub const fn from_bits(val: u32) -> Self {
37        Self(val)
38    }
39
40    /// Attempts to convert a [`u32`] into an [Argument].
41    pub const fn try_from_bits(val: u32) -> Result<Self> {
42        Ok(Self::from_bits(val))
43    }
44
45    /// Converts the [Argument] into a byte array.
46    pub const fn bytes(&self) -> [u8; Self::LEN] {
47        self.0.to_be_bytes()
48    }
49
50    /// Attempts to convert a byte slice into an [Argument].
51    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
52        match val.len() {
53            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
54            _ => Ok(Self(u32::from_be_bytes([val[0], val[1], val[2], val[3]]))),
55        }
56    }
57}
58
59impl Default for Argument {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65impl From<u32> for Argument {
66    fn from(val: u32) -> Self {
67        Self::from_bits(val)
68    }
69}
70
71impl From<Argument> for u32 {
72    fn from(val: Argument) -> Self {
73        val.bits()
74    }
75}
76
77impl From<Argument> for [u8; Argument::LEN] {
78    fn from(val: Argument) -> Self {
79        val.bytes()
80    }
81}
82
83impl TryFrom<&[u8]> for Argument {
84    type Error = Error;
85
86    fn try_from(val: &[u8]) -> Result<Self> {
87        Self::try_from_bytes(val)
88    }
89}
90
91impl<const N: usize> TryFrom<&[u8; N]> for Argument {
92    type Error = Error;
93
94    fn try_from(val: &[u8; N]) -> Result<Self> {
95        Self::try_from_bytes(val.as_ref())
96    }
97}
98
99impl<const N: usize> TryFrom<[u8; N]> for Argument {
100    type Error = Error;
101
102    fn try_from(val: [u8; N]) -> Result<Self> {
103        Self::try_from_bytes(val.as_ref())
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use crate::test_field;
111
112    #[test]
113    fn test_fields() {
114        let mut arg = Argument::new();
115
116        let mut io_ocr = arg.io_ocr();
117
118        test_field!(io_ocr, vdd_2021: 8);
119        test_field!(io_ocr, vdd_2122: 9);
120        test_field!(io_ocr, vdd_2223: 10);
121        test_field!(io_ocr, vdd_2324: 11);
122        test_field!(io_ocr, vdd_2425: 12);
123        test_field!(io_ocr, vdd_2526: 13);
124        test_field!(io_ocr, vdd_2627: 14);
125        test_field!(io_ocr, vdd_2728: 15);
126        test_field!(io_ocr, vdd_2829: 16);
127        test_field!(io_ocr, vdd_2930: 17);
128        test_field!(io_ocr, vdd_3031: 18);
129        test_field!(io_ocr, vdd_3132: 19);
130        test_field!(io_ocr, vdd_3233: 20);
131        test_field!(io_ocr, vdd_3334: 21);
132        test_field!(io_ocr, vdd_3435: 22);
133        test_field!(io_ocr, vdd_3536: 23);
134
135        arg.set_io_ocr(io_ocr);
136        assert_eq!(arg.io_ocr(), io_ocr);
137
138        test_field!(arg, s18r: 24);
139    }
140}