sdmmc_core/command/class/class4/cmd23/
arg.rs1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5 pub Argument(u32): u16 {
7 raw_reliable_write_request: 31;
9 pub packed: 30;
11 raw_tag_request: 29;
13 raw_context_id: 28, 25;
15 raw_forced_programming: 24;
17 pub block_count: 15, 0;
19 }
20}
21
22impl Argument {
23 pub const LEN: usize = 4;
25 pub const DEFAULT: u32 = 0;
27
28 pub const fn new() -> Self {
30 Self(Self::DEFAULT)
31 }
32
33 pub const fn from_bits(val: u32) -> Self {
35 Self(val)
36 }
37
38 pub const fn try_from_bits(val: u32) -> Result<Self> {
40 Ok(Self::from_bits(val))
41 }
42
43 pub const fn bytes(&self) -> [u8; Self::LEN] {
45 self.0.to_be_bytes()
46 }
47
48 pub const fn reliable_write_request(&self) -> bool {
54 !self.packed() && self.raw_reliable_write_request()
55 }
56
57 pub const fn tag_request(&self) -> bool {
63 !self.packed() && self.raw_tag_request()
64 }
65
66 pub fn set_tag_request(&mut self, val: bool) {
67 if val {
68 self.set_packed(false);
69 self.set_context_id(0);
70 }
71
72 self.set_raw_tag_request(val);
73 }
74
75 pub const fn context_id(&self) -> u8 {
81 match self.packed() {
82 false if !self.tag_request() => self.raw_context_id() as u8,
83 _ => 0,
84 }
85 }
86
87 pub fn set_context_id(&mut self, id: u8) {
93 if id != 0 {
94 self.set_packed(false);
95 self.set_tag_request(false);
96 }
97
98 self.set_raw_context_id(id as u32);
99 }
100
101 pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
103 match val.len() {
104 len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
105 _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
106 }
107 }
108}
109
110impl Default for Argument {
111 fn default() -> Self {
112 Self::new()
113 }
114}
115
116impl From<Argument> for u32 {
117 fn from(val: Argument) -> Self {
118 val.bits()
119 }
120}
121
122impl From<Argument> for [u8; Argument::LEN] {
123 fn from(val: Argument) -> Self {
124 val.bytes()
125 }
126}
127
128impl From<u32> for Argument {
129 fn from(val: u32) -> Self {
130 Self::from_bits(val)
131 }
132}
133
134impl TryFrom<&[u8]> for Argument {
135 type Error = Error;
136
137 fn try_from(val: &[u8]) -> Result<Self> {
138 Self::try_from_bytes(val)
139 }
140}
141
142impl<const N: usize> TryFrom<&[u8; N]> for Argument {
143 type Error = Error;
144
145 fn try_from(val: &[u8; N]) -> Result<Self> {
146 Self::try_from_bytes(val.as_ref())
147 }
148}
149
150impl<const N: usize> TryFrom<[u8; N]> for Argument {
151 type Error = Error;
152
153 fn try_from(val: [u8; N]) -> Result<Self> {
154 Self::try_from_bytes(val.as_ref())
155 }
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn test_fields() {
164 (1..=u32::BITS)
165 .map(|r| ((1u64 << r) - 1) as u32)
166 .for_each(|raw_arg| {
167 let exp_block_count = (raw_arg & 0xffff) as u16;
168 let raw = raw_arg.to_be_bytes();
169 let mut exp_arg = Argument(raw_arg);
170
171 assert_eq!(Argument::from_bits(raw_arg), exp_arg);
172 assert_eq!(Argument::from(raw_arg), exp_arg);
173
174 assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
175 assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
176 assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
177 assert_eq!(Argument::try_from(raw), Ok(exp_arg));
178
179 assert_eq!(exp_arg.block_count(), exp_block_count);
180
181 exp_arg.set_block_count(0);
182 assert_eq!(exp_arg.block_count(), 0);
183
184 exp_arg.set_block_count(exp_block_count as u32);
185 assert_eq!(exp_arg.block_count(), exp_block_count);
186
187 assert_eq!(exp_arg.bits(), raw_arg);
188 assert_eq!(exp_arg.bytes(), raw);
189 });
190 }
191}