sdmmc_core/register/
io_ocr.rs1use crate::lib_bitfield;
4use crate::result::{Error, Result};
5
6const IO_OCR_LEN: usize = 3;
8
9lib_bitfield! {
10 pub IoOcr(MSB0 [u8; IO_OCR_LEN]): bool {
12 pub vdd_3536: 23;
14 pub vdd_3435: 22;
16 pub vdd_3334: 21;
18 pub vdd_3233: 20;
20 pub vdd_3132: 19;
22 pub vdd_3031: 18;
24 pub vdd_2930: 17;
26 pub vdd_2829: 16;
28 pub vdd_2728: 15;
30 pub vdd_2627: 14;
32 pub vdd_2526: 13;
34 pub vdd_2425: 12;
36 pub vdd_2324: 11;
38 pub vdd_2223: 10;
40 pub vdd_2122: 9;
42 pub vdd_2021: 8;
44 }
45}
46
47impl IoOcr {
48 pub const LEN: usize = IO_OCR_LEN;
50
51 pub const fn new() -> Self {
53 Self([0; Self::LEN])
54 }
55
56 pub const fn bits(&self) -> u32 {
58 u32::from_be_bytes([0, self.0[0], self.0[1], self.0[2]])
59 }
60
61 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 pub const fn into_bytes(self) -> [u8; Self::LEN] {
69 self.0
70 }
71
72 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}