sdmmc_core/response/spi/
r1.rs

1use crate::result::{Error, Result};
2use crate::{lib_bitfield, response};
3
4lib_bitfield! {
5    /// Response sent by every command except `SEND_STATUS` in SPI mode.
6    pub R1(u8): u8 {
7        raw_msb: 7;
8        /// Invalid command argument.
9        pub parameter_error: 6;
10        /// A misaligned address that does not match block length detected.
11        pub address_error: 5;
12        /// An erase sequence error occurred.
13        pub erase_error: 4;
14        /// A communication CRC check failed.
15        pub crc_error: 3;
16        /// An illegal command code detected.
17        pub illegal_command: 2;
18        /// An erase sequence was cleared by a command received out of erase sequence.
19        pub erase_reset: 1;
20        /// Card is in idle state and running intializing process.
21        pub idle: 0;
22    }
23}
24
25response! {
26    R1 {
27        response_mode: Spi,
28    }
29}
30
31impl R1 {
32    /// Represents the default value of the [R1].
33    pub const DEFAULT: u8 = 0;
34
35    /// Creates a new [R1].
36    pub const fn new() -> Self {
37        Self(Self::DEFAULT)
38    }
39
40    /// Gets the most-significant bit (MSB) of [R1].
41    pub const fn msb(&self) -> bool {
42        self.raw_msb()
43    }
44
45    /// Attempts to convert a [`u8`] into a [R1].
46    pub const fn try_from_bits(val: u8) -> Result<Self> {
47        match Self(val) {
48            r if r.msb() => Err(Error::invalid_field_variant("r1", val as usize)),
49            r => Ok(r),
50        }
51    }
52}
53
54impl Default for R1 {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl TryFrom<u8> for R1 {
61    type Error = Error;
62
63    fn try_from(val: u8) -> Result<Self> {
64        Self::try_from_bits(val)
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use crate::test_field;
72
73    #[test]
74    fn test_fields() {
75        let mut r1 = R1::new();
76
77        test_field!(r1, idle: 0);
78        test_field!(r1, erase_reset: 1);
79        test_field!(r1, illegal_command: 2);
80        test_field!(r1, crc_error: 3);
81        test_field!(r1, erase_error: 4);
82        test_field!(r1, address_error: 5);
83        test_field!(r1, parameter_error: 6);
84
85        assert!(!r1.msb());
86
87        (0..=u8::MAX).for_each(|v| match v {
88            v if R1(v).msb() => assert_eq!(
89                R1::try_from(v),
90                Err(Error::invalid_field_variant("r1", v as usize))
91            ),
92            _ => assert_eq!(R1::try_from(v), Ok(R1(v))),
93        });
94    }
95}