sdmmc_core/response/sd/
r4.rs

1use crate::register::IoOcr;
2use crate::result::{Error, Result};
3use crate::{lib_bitfield, response};
4
5lib_bitfield! {
6    /// Represents the response to the `IO_SEND_OP_COND` command.
7    pub R4(MSB0 [u8; 6]): u32 {
8        raw_start_bit: 47;
9        raw_direction: 46;
10        raw_c: 39;
11        raw_num_io_functions: 38, 36;
12        raw_memory_present: 35;
13        raw_stuff: 34, 33;
14        raw_s18a: 32;
15        raw_io_ocr: 31, 8;
16        raw_end_bit: 0;
17    }
18}
19
20response! {
21    R4 {
22        response_mode: Sd,
23    }
24}
25
26impl R4 {
27    /// Represents the byte length of the [R4] response.
28    pub const LEN: usize = 6;
29    pub const DEFAULT: [u8; Self::LEN] = [0x3f, 0, 0, 0, 0, 0xff];
30
31    /// Creates a new [R4].
32    pub const fn new() -> Self {
33        Self(Self::DEFAULT)
34    }
35
36    /// Gets whether the card is ready to operate after initialization.
37    pub const fn is_card_ready(&self) -> bool {
38        self.raw_c()
39    }
40
41    /// Sets whether the card is ready to operate after initialization.
42    pub fn set_is_card_ready(&mut self, val: bool) {
43        self.set_raw_c(val);
44    }
45
46    /// Gets whether the SD card has memory, or is I/O only.
47    pub const fn is_memory_present(&self) -> bool {
48        self.raw_memory_present()
49    }
50
51    /// Sets whether the SD card has memory, or is I/O only.
52    pub fn set_is_memory_present(&mut self, val: bool) {
53        self.set_raw_memory_present(val)
54    }
55
56    /// Gets the number of I/O functions supported by the card.
57    pub const fn num_io_functions(&self) -> usize {
58        self.raw_num_io_functions() as usize
59    }
60
61    /// Sets the number of I/O functions supported by the card.
62    ///
63    /// **NOTE**: valid range is `0-7`, only values in this range will be set.
64    pub fn set_num_io_functions(&mut self, val: usize) {
65        self.set_raw_num_io_functions((val & 0x7) as u32)
66    }
67
68    /// Gets whether the switch to 1.8V power is accepted.
69    pub const fn is_s18a(&self) -> bool {
70        self.raw_s18a()
71    }
72
73    /// Sets whether the switch to 1.8V power is accepted.
74    pub fn set_is_s18a(&mut self, val: bool) {
75        self.set_raw_s18a(val);
76    }
77
78    /// Gets the [IoOcr] value of the [R4] response.
79    pub const fn io_ocr(&self) -> IoOcr {
80        IoOcr::from_bits(self.raw_io_ocr())
81    }
82
83    /// Sets the [IoOcr] value of the [R4] response.
84    pub fn set_io_ocr(&mut self, io_ocr: IoOcr) {
85        self.set_raw_io_ocr(io_ocr.bits());
86    }
87
88    /// Attempts to convert a byte slice into a [R4].
89    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
90        match val.len() {
91            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
92            _ => Ok(Self([val[0], val[1], val[2], val[3], val[4], val[5]])),
93        }
94    }
95}
96
97impl Default for R4 {
98    fn default() -> Self {
99        Self::new()
100    }
101}
102
103impl From<R4> for [u8; R4::LEN] {
104    fn from(val: R4) -> Self {
105        val.bytes()
106    }
107}
108
109impl TryFrom<&[u8]> for R4 {
110    type Error = Error;
111
112    fn try_from(val: &[u8]) -> Result<Self> {
113        Self::try_from_bytes(val)
114    }
115}
116
117impl<const N: usize> TryFrom<[u8; N]> for R4 {
118    type Error = Error;
119
120    fn try_from(val: [u8; N]) -> Result<Self> {
121        Self::try_from_bytes(val.as_ref())
122    }
123}
124
125impl<const N: usize> TryFrom<&[u8; N]> for R4 {
126    type Error = Error;
127
128    fn try_from(val: &[u8; N]) -> Result<Self> {
129        Self::try_from_bytes(val.as_ref())
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136
137    #[test]
138    fn test_valid() {
139        (1..=46u64)
140            .map(|r| (1u64 << r) - 1)
141            .for_each(|raw_bitfield| {
142                let [_, _, b0, b1, b2, b3, b4, b5] = raw_bitfield.to_be_bytes();
143                let raw_bytes = [b0, b1, b2, b3, b4, b5];
144
145                let exp_card_ready = (b1 & 0x80) != 0;
146                let exp_num_io = ((b1 & 0x70) >> 4) as usize;
147                let exp_mem = (b1 & 0x8) != 0;
148                let exp_s18a = (b1 & 0x1) != 0;
149                let exp_io_ocr = IoOcr::try_from_bytes(&[b2, b3, b4]).unwrap();
150
151                let mut exp_r4 = R4(raw_bytes);
152
153                assert_eq!(R4::try_from_bytes(raw_bytes.as_ref()), Ok(exp_r4));
154
155                assert_eq!(exp_r4.is_card_ready(), exp_card_ready);
156
157                exp_r4.set_is_card_ready(!exp_card_ready);
158                assert_eq!(exp_r4.is_card_ready(), !exp_card_ready);
159
160                exp_r4.set_is_card_ready(exp_card_ready);
161                assert_eq!(exp_r4.is_card_ready(), exp_card_ready);
162
163                assert_eq!(exp_r4.num_io_functions(), exp_num_io, "{:#x}", raw_bitfield);
164
165                exp_r4.set_num_io_functions(0);
166                assert_eq!(exp_r4.num_io_functions(), 0);
167
168                exp_r4.set_num_io_functions(exp_num_io);
169                assert_eq!(exp_r4.num_io_functions(), exp_num_io);
170
171                assert_eq!(exp_r4.is_memory_present(), exp_mem);
172
173                exp_r4.set_is_memory_present(!exp_mem);
174                assert_eq!(exp_r4.is_memory_present(), !exp_mem);
175
176                exp_r4.set_is_memory_present(exp_mem);
177                assert_eq!(exp_r4.is_memory_present(), exp_mem);
178
179                assert_eq!(exp_r4.is_s18a(), exp_s18a);
180
181                exp_r4.set_is_s18a(!exp_s18a);
182                assert_eq!(exp_r4.is_s18a(), !exp_s18a);
183
184                exp_r4.set_is_s18a(exp_s18a);
185                assert_eq!(exp_r4.is_s18a(), exp_s18a);
186
187                assert_eq!(exp_r4.io_ocr(), exp_io_ocr);
188
189                exp_r4.set_io_ocr(IoOcr::new());
190                assert_eq!(exp_r4.io_ocr(), IoOcr::new());
191
192                exp_r4.set_io_ocr(exp_io_ocr);
193                assert_eq!(exp_r4.io_ocr(), exp_io_ocr);
194            });
195    }
196}