sdmmc_core/response/spi/
r3.rs1use crate::register::Ocr;
2use crate::response::spi::r1::R1;
3use crate::result::{Error, Result};
4use crate::{lib_bitfield, response};
5
6lib_bitfield! {
7 pub R3(MSB0 [u8; 5]): u32 {
9 raw_r1: 39, 32;
10 raw_ocr: 31, 0;
11 }
12}
13
14response! {
15 R3 {
16 response_mode: Spi,
17 }
18}
19
20impl R3 {
21 pub const LEN: usize = 5;
23 pub const DEFAULT: [u8; Self::LEN] = [0u8; Self::LEN];
25
26 pub const fn new() -> Self {
28 Self(Self::DEFAULT)
29 }
30
31 pub const fn r1(&self) -> Result<R1> {
33 R1::try_from_bits(self.raw_r1() as u8)
34 }
35
36 pub fn set_r1(&mut self, r1: R1) {
38 self.set_raw_r1(r1.bits() as u32);
39 }
40
41 pub const fn ocr(&self) -> Ocr {
43 Ocr::from_bits(self.raw_ocr())
44 }
45
46 pub fn set_ocr(&mut self, ocr: Ocr) {
48 self.set_raw_ocr(ocr.bits());
49 }
50
51 pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
53 match val.len() {
54 len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
55 _ if R1::try_from_bits(val[0]).is_err() => {
56 Err(Error::invalid_field_variant("r3::r1", val[0] as usize))
57 }
58 _ => Ok(Self([val[0], val[1], val[2], val[3], val[4]])),
59 }
60 }
61}
62
63impl Default for R3 {
64 fn default() -> Self {
65 Self::new()
66 }
67}
68
69impl TryFrom<&[u8]> for R3 {
70 type Error = Error;
71
72 fn try_from(val: &[u8]) -> Result<Self> {
73 Self::try_from_bytes(val)
74 }
75}
76
77impl<const N: usize> TryFrom<[u8; N]> for R3 {
78 type Error = Error;
79
80 fn try_from(val: [u8; N]) -> Result<Self> {
81 Self::try_from_bytes(val.as_ref())
82 }
83}
84
85impl<const N: usize> TryFrom<&[u8; N]> for R3 {
86 type Error = Error;
87
88 fn try_from(val: &[u8; N]) -> Result<Self> {
89 Self::try_from_bytes(val.as_ref())
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_valid() {
99 (1..=7).map(|r| ((1u16 << r) - 1) as u8).for_each(|raw_r1| {
100 (1..=u32::BITS)
101 .map(|r| ((1u64 << r) - 1) as u32)
102 .for_each(|raw_ocr| {
103 let [ocr0, ocr1, ocr2, ocr3] = raw_ocr.to_be_bytes();
104
105 let raw_bytes = [raw_r1, ocr0, ocr1, ocr2, ocr3];
106 let exp_r1 = R1::try_from_bits(raw_r1).unwrap();
107 let exp_ocr = Ocr::from_bits(raw_ocr);
108 let mut exp_r3 = R3(raw_bytes);
109
110 assert_eq!(R3::try_from_bytes(raw_bytes.as_ref()), Ok(exp_r3));
111 assert_eq!(exp_r3.r1(), Ok(exp_r1));
112 assert_eq!(exp_r3.ocr(), exp_ocr);
113
114 exp_r3.set_r1(R1::new());
115 assert_eq!(exp_r3.r1(), Ok(R1::new()));
116
117 exp_r3.set_r1(exp_r1);
118 assert_eq!(exp_r3.r1(), Ok(exp_r1));
119
120 exp_r3.set_ocr(Ocr::new());
121 assert_eq!(exp_r3.ocr(), Ocr::new());
122
123 exp_r3.set_ocr(exp_ocr);
124 assert_eq!(exp_r3.ocr(), exp_ocr);
125 });
126 });
127 }
128
129 #[test]
130 fn test_invalid() {
131 (1..=7)
132 .map(|r| (((1u16 << r) - 1) as u8) | 0x80)
133 .for_each(|raw_r1| {
134 let exp_err = Err(Error::invalid_field_variant("r3::r1", raw_r1 as usize));
135
136 (1..=u32::BITS)
137 .map(|r| ((1u64 << r) - 1) as u32)
138 .for_each(|raw_ocr| {
139 let [ocr0, ocr1, ocr2, ocr3] = raw_ocr.to_be_bytes();
140
141 let raw_bytes = [raw_r1, ocr0, ocr1, ocr2, ocr3];
142
143 assert_eq!(R3::try_from_bytes(raw_bytes.as_ref()), exp_err);
144 });
145 });
146 }
147}