sdmmc_core/response/sd/
r1.rs

1//! R1 response type for SD mode.
2
3use crate::response::{End, Start};
4use crate::result::{Error, Result};
5use crate::{Crc7, lib_bitfield, response};
6
7mod card_status;
8
9pub use card_status::*;
10
11/// Represents the byte length of the [R1] response in SD mode.
12pub const R1_LEN: usize = 6;
13
14lib_bitfield! {
15    /// Response sent by every command except `SEND_STATUS` in SD mode.
16    pub R1(MSB0 [u8; 6]): u32 {
17        raw_start: 47, 40;
18        raw_card_status: 39, 8;
19        raw_end: 7, 0;
20    }
21}
22
23response! {
24    R1 {
25        response_mode: Sd,
26    }
27}
28
29impl R1 {
30    /// Represents the byte length of the [R1] response in SD mode.
31    pub const LEN: usize = 6;
32    pub const DEFAULT: [u8; Self::LEN] = [0x0, 0x0, 0x0, 0x0, 0x0, 0x1];
33
34    /// Creates a new [R1].
35    pub const fn new() -> Self {
36        Self(Self::DEFAULT)
37    }
38
39    /// Gets the [Start] for the [R1].
40    pub const fn start(&self) -> Result<Start> {
41        Start::try_from_bits(self.raw_start() as u8)
42    }
43
44    /// Sets the [Start] for the [R1].
45    pub fn set_start(&mut self, start: Start) {
46        self.set_raw_start(start.bits() as u32);
47    }
48
49    /// Gets the [CardStatus] for the [R1].
50    pub const fn card_status(&self) -> Result<CardStatus> {
51        CardStatus::try_from_bits(self.raw_card_status())
52    }
53
54    /// Sets the [CardStatus] for the [R1].
55    pub fn set_card_status(&mut self, card_status: CardStatus) {
56        self.set_raw_card_status(card_status.bits());
57    }
58
59    /// Gets the [End] for the [R1].
60    pub const fn end(&self) -> Result<End> {
61        End::try_from_bits(self.raw_end() as u8)
62    }
63
64    /// Sets the [End] for the [R1].
65    pub fn set_end(&mut self, end: End) {
66        self.set_raw_end(end.bits() as u32);
67    }
68
69    /// Checks that the CRC-7 field matches the calculated value.
70    pub const fn check_crc(&self) -> Result<Crc7> {
71        let [r0, r1, r2, r3, r4, r5] = self.0;
72        let exp_crc = Crc7::calculate(&[r0, r1, r2, r3, r4]);
73        let crc = r5 >> 1;
74
75        if exp_crc.bits() == crc {
76            Ok(exp_crc)
77        } else {
78            Err(Error::invalid_crc7(crc, exp_crc.bits()))
79        }
80    }
81
82    /// Attempts to convert a [`u8`] into a [R1].
83    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
84        match val.len() {
85            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
86            _ => match Self([val[0], val[1], val[2], val[3], val[4], val[5]]) {
87                r if r.start().is_err() => Err(Error::invalid_field_variant(
88                    "r1::start",
89                    r.raw_start() as usize,
90                )),
91                r if r.card_status().is_err() => Err(Error::invalid_field_variant(
92                    "r1::card_status",
93                    r.raw_card_status() as usize,
94                )),
95                r if r.end().is_err() => Err(Error::invalid_field_variant(
96                    "r1::end",
97                    r.raw_end() as usize,
98                )),
99                r if r.check_crc().is_err() => Err(Error::invalid_field_variant(
100                    "r1::crc",
101                    (r.raw_end() >> 1) as usize,
102                )),
103                r => Ok(r),
104            },
105        }
106    }
107}
108
109impl Default for R1 {
110    fn default() -> Self {
111        Self::new()
112    }
113}
114
115impl TryFrom<&[u8]> for R1 {
116    type Error = Error;
117
118    fn try_from(val: &[u8]) -> Result<Self> {
119        Self::try_from_bytes(val)
120    }
121}
122
123impl<const N: usize> TryFrom<[u8; N]> for R1 {
124    type Error = Error;
125
126    fn try_from(val: [u8; N]) -> Result<Self> {
127        Self::try_from_bytes(val.as_ref())
128    }
129}
130
131impl<const N: usize> TryFrom<&[u8; N]> for R1 {
132    type Error = Error;
133
134    fn try_from(val: &[u8; N]) -> Result<Self> {
135        Self::try_from_bytes(val.as_ref())
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn test_fields() {
145        let new_start = Start::new();
146        let new_card_status = CardStatus::new();
147        let new_end = End::new();
148
149        (0..=u8::MAX)
150            .zip(0..=u8::MAX)
151            .zip(0..=u8::MAX)
152            .for_each(|((start, cs), end)| {
153                (0..4).map(|i| (cs << i) as u32).for_each(|cs| {
154                    let [cs0, cs1, cs2, cs3] = cs.to_be_bytes();
155                    let raw = [start, cs0, cs1, cs2, cs3, end];
156                    let mut exp_r1 = R1(raw);
157
158                    match (
159                        exp_r1.start(),
160                        exp_r1.card_status(),
161                        exp_r1.end(),
162                        exp_r1.check_crc(),
163                    ) {
164                        (Ok(start), Ok(card_status), Ok(end), Ok(crc)) => {
165                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Ok(exp_r1));
166                            assert_eq!(R1::try_from(raw), Ok(exp_r1));
167
168                            exp_r1.set_start(new_start);
169                            assert_eq!(exp_r1.start(), Ok(new_start));
170
171                            exp_r1.set_start(start);
172                            assert_eq!(exp_r1.start(), Ok(start));
173
174                            exp_r1.set_card_status(new_card_status);
175                            assert_eq!(exp_r1.card_status(), Ok(new_card_status));
176
177                            exp_r1.set_card_status(card_status);
178                            assert_eq!(exp_r1.card_status(), Ok(card_status));
179
180                            exp_r1.set_end(new_end);
181                            assert_eq!(exp_r1.end(), Ok(new_end));
182
183                            exp_r1.set_end(end);
184                            assert_eq!(exp_r1.end(), Ok(end));
185
186                            assert_eq!(exp_r1.check_crc(), Ok(crc));
187                        }
188                        (Err(_err), _, _, _) => {
189                            let exp_err = Error::invalid_field_variant("r1::start", start as usize);
190                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
191                            assert_eq!(R1::try_from(raw), Err(exp_err));
192                        }
193                        (_, Err(_err), _, _) => {
194                            let exp_err =
195                                Error::invalid_field_variant("r1::card_status", cs as usize);
196                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
197                            assert_eq!(R1::try_from(raw), Err(exp_err));
198                        }
199                        (_, _, Err(_err), _) => {
200                            let exp_err = Error::invalid_field_variant("r1::end", end as usize);
201                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
202                            assert_eq!(R1::try_from(raw), Err(exp_err));
203                        }
204                        (_, _, _, Err(_err)) => {
205                            let exp_err =
206                                Error::invalid_field_variant("r1::crc", (end >> 1) as usize);
207                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
208                            assert_eq!(R1::try_from(raw), Err(exp_err));
209                        }
210                    }
211                });
212            });
213    }
214}