sdmmc_core/register/
ocr.rs1use crate::lib_bitfield;
4use crate::result::{Error, Result};
5
6const OCR_MASK: u32 = 0xe9ff_8000;
7
8lib_bitfield! {
9 pub Ocr(u32): bool {
11 pub vdd_2728: 15;
13 pub vdd_2829: 16;
15 pub vdd_2930: 17;
17 pub vdd_3031: 18;
19 pub vdd_3132: 19;
21 pub vdd_3233: 20;
23 pub vdd_3334: 21;
25 pub vdd_3435: 22;
27 pub vdd_3536: 23;
29 pub s18a: 24;
31 pub co2t: 27;
33 pub uhs2: 29;
35 pub ccs: 30;
39 pub busy: 31;
43 }
44}
45
46impl Ocr {
47 pub const LEN: usize = 4;
49
50 pub const fn new() -> Self {
52 Self(0)
53 }
54
55 pub const fn from_bits(val: u32) -> Self {
57 Self(val & OCR_MASK)
58 }
59
60 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 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}