sdmmc_core/register/
io_ocr.rs

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