sdmmc_core/response/
end.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
use crate::lib_bitfield;
use crate::result::{Error, Result};
use crate::Crc7;

lib_bitfield! {
    /// Represents the ending byte of card responses in SD mode.
    pub End(u8): u8 {
        raw_crc: 7, 1;
        /// The LSB of the response, should always be `1`.
        pub end_bit: 0;
    }
}

impl End {
    /// Represents the default for the [End].
    pub const DEFAULT: u8 = 1;
    const CRC_SHIFT: usize = 1;

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

    /// Gets the [Crc7] for the [End].
    pub const fn crc(&self) -> Crc7 {
        Crc7::from_bits(self.raw_crc())
    }

    /// Sets the [Crc7] for the [End].
    pub fn set_crc(&mut self, val: Crc7) {
        self.set_raw_crc(val.bits());
    }

    /// Converts a [Crc7] into an [End].
    pub const fn from_crc(crc: Crc7) -> Self {
        Self((crc.bits() << Self::CRC_SHIFT) | Self::DEFAULT)
    }

    /// Converts a [`u8`] into an [End].
    ///
    /// # Note
    ///
    /// Masks the `val` to ensure a valid [End] value.
    pub const fn from_bits(val: u8) -> Self {
        Self(val | Self::DEFAULT)
    }

    /// Attempts to convert a [`u8`] into a [End].
    pub const fn try_from_bits(val: u8) -> Result<Self> {
        match Self(val) {
            e if !e.end_bit() => Err(Error::invalid_field_variant("end", val as usize)),
            e => Ok(e),
        }
    }
}

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

impl TryFrom<u8> for End {
    type Error = Error;

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

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

impl From<Crc7> for End {
    fn from(val: Crc7) -> Self {
        Self::from_crc(val)
    }
}

impl From<End> for Crc7 {
    fn from(val: End) -> Self {
        val.crc()
    }
}

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

    #[test]
    fn test_fields() {
        let mut s = End::new();

        assert_eq!(s.crc(), Crc7::new());
        assert!(s.end_bit());

        (0..=0xff)
            .map(|crc| Crc7::from_bits(crc & 0x7f))
            .for_each(|crc| {
                let raw_end = (crc.bits() << End::CRC_SHIFT) | End::DEFAULT;
                let exp_end = End(raw_end);

                assert_eq!(End::try_from_bits(raw_end), Ok(exp_end));

                s.set_crc(crc);
                assert_eq!(s.crc(), crc);

                // make sure we didn't corrupt other fields
                assert!(s.end_bit());

                assert_eq!(exp_end.bits(), raw_end);
            });
    }
}