sdmmc_core/register/
ocr.rs

1//! Operation Conditions Register (`OCR`).
2
3use crate::lib_bitfield;
4use crate::result::{Error, Result};
5
6const OCR_MASK: u32 = 0xe9ff_8000;
7
8lib_bitfield! {
9    /// Represents the SD card operation conditions register (`OCR`).
10    pub Ocr(u32): bool {
11        /// VDD Volatage Window (VDD1): 2.7-2.8V.
12        pub vdd_2728: 15;
13        /// VDD Volatage Window (VDD1): 2.8-2.9V.
14        pub vdd_2829: 16;
15        /// VDD Volatage Window (VDD1): 2.9-3.0V.
16        pub vdd_2930: 17;
17        /// VDD Volatage Window (VDD1): 3.0-3.1V.
18        pub vdd_3031: 18;
19        /// VDD Volatage Window (VDD1): 3.1-3.2V.
20        pub vdd_3132: 19;
21        /// VDD Volatage Window (VDD1): 3.2-3.3V.
22        pub vdd_3233: 20;
23        /// VDD Volatage Window (VDD1): 3.3-3.4V.
24        pub vdd_3334: 21;
25        /// VDD Volatage Window (VDD1): 3.4-3.5V.
26        pub vdd_3435: 22;
27        /// VDD Volatage Window (VDD1): 3.5-3.6V.
28        pub vdd_3536: 23;
29        /// Switching to 1.8V accepted (S18A).
30        pub s18a: 24;
31        /// Over 2TB support status (CO2T).
32        pub co2t: 27;
33        /// UHS-II Card Status.
34        pub uhs2: 29;
35        /// Card Capacity Status (CCS).
36        ///
37        /// Only valid when the card power up status bit (busy) is set.
38        pub ccs: 30;
39        /// Card power up status bit (busy).
40        ///
41        /// Set to low if the card has not finished power up routine.
42        pub busy: 31;
43    }
44}
45
46impl Ocr {
47    /// Represents the byte length of the [Ocr].
48    pub const LEN: usize = 4;
49
50    /// Creates a new [Ocr].
51    pub const fn new() -> Self {
52        Self(0)
53    }
54
55    /// Converts an [`u32`] into a [Ocr]
56    pub const fn from_bits(val: u32) -> Self {
57        Self(val & OCR_MASK)
58    }
59
60    /// Attempts to convert a byte slice into a [Ocr].
61    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
62        match val.len() {
63            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
64            _ => Ok(Self(
65                u32::from_be_bytes([val[0], val[1], val[2], val[3]]) & OCR_MASK,
66            )),
67        }
68    }
69
70    /// Converts an [Ocr] into a byte array.
71    pub const fn bytes(&self) -> [u8; Self::LEN] {
72        self.0.to_be_bytes()
73    }
74}
75
76impl From<u32> for Ocr {
77    fn from(val: u32) -> Self {
78        Self::from_bits(val)
79    }
80}
81
82impl From<Ocr> for u32 {
83    fn from(val: Ocr) -> Self {
84        val.bits()
85    }
86}
87
88impl TryFrom<&[u8]> for Ocr {
89    type Error = Error;
90
91    fn try_from(val: &[u8]) -> Result<Self> {
92        Self::try_from_bytes(val)
93    }
94}
95
96impl<const N: usize> TryFrom<&[u8; N]> for Ocr {
97    type Error = Error;
98
99    fn try_from(val: &[u8; N]) -> Result<Self> {
100        Self::try_from_bytes(val.as_ref())
101    }
102}
103
104impl<const N: usize> TryFrom<[u8; N]> for Ocr {
105    type Error = Error;
106
107    fn try_from(val: [u8; N]) -> Result<Self> {
108        Self::try_from_bytes(val.as_ref())
109    }
110}
111
112impl From<Ocr> for [u8; Ocr::LEN] {
113    fn from(val: Ocr) -> Self {
114        val.bytes()
115    }
116}
117
118impl Default for Ocr {
119    fn default() -> Self {
120        Self::new()
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use crate::test_field;
128
129    #[test]
130    fn test_fields() {
131        let mut ocr = Ocr::new();
132
133        test_field!(ocr, vdd_2728: 15);
134        test_field!(ocr, vdd_2829: 16);
135        test_field!(ocr, vdd_2930: 17);
136        test_field!(ocr, vdd_3031: 18);
137        test_field!(ocr, vdd_3132: 19);
138        test_field!(ocr, vdd_3233: 20);
139        test_field!(ocr, vdd_3334: 21);
140        test_field!(ocr, vdd_3435: 22);
141        test_field!(ocr, vdd_3536: 23);
142        test_field!(ocr, s18a: 24);
143        test_field!(ocr, co2t: 27);
144        test_field!(ocr, uhs2: 29);
145        test_field!(ocr, ccs: 30);
146        test_field!(ocr, busy: 31);
147    }
148}