sdmmc_core/command/class/class1/cmd44/
arg.rs1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4mod direction;
5
6pub use direction::*;
7
8lib_bitfield! {
9 pub Argument(u32): u16 {
11 raw_direction: 30;
12 raw_extended_address: 29, 24;
13 pub priority: 23;
15 raw_task_id: 20, 16;
17 raw_number_of_blocks: 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 direction(&self) -> Direction {
35 Direction::from_bool(self.raw_direction())
36 }
37
38 pub fn set_direction(&mut self, val: Direction) {
40 self.set_raw_direction(val.into_bool());
41 }
42
43 pub const fn extended_address(&self) -> u8 {
45 self.raw_extended_address() as u8
46 }
47
48 pub fn set_extended_address(&mut self, val: u8) {
50 self.set_raw_extended_address(val as u32);
51 }
52
53 pub const fn task_id(&self) -> u8 {
55 self.raw_task_id() as u8
56 }
57
58 pub fn set_task_id(&mut self, val: u8) {
60 self.set_raw_task_id(val as u32);
61 }
62
63 pub const fn number_of_blocks(&self) -> u16 {
65 self.raw_number_of_blocks()
66 }
67
68 pub fn set_number_of_blocks(&mut self, val: u16) {
70 self.set_raw_number_of_blocks(val as u32);
71 }
72
73 pub const fn try_from_bits(val: u32) -> Result<Self> {
75 Ok(Self(val))
76 }
77
78 pub const fn bytes(&self) -> [u8; Self::LEN] {
80 self.0.to_be_bytes()
81 }
82
83 pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
85 match val.len() {
86 len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
87 _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
88 }
89 }
90}
91
92impl Default for Argument {
93 fn default() -> Self {
94 Self::new()
95 }
96}
97
98impl From<Argument> for u32 {
99 fn from(val: Argument) -> Self {
100 val.bits()
101 }
102}
103
104impl From<Argument> for [u8; Argument::LEN] {
105 fn from(val: Argument) -> Self {
106 val.bytes()
107 }
108}
109
110impl TryFrom<u32> for Argument {
111 type Error = Error;
112
113 fn try_from(val: u32) -> Result<Self> {
114 Self::try_from_bits(val)
115 }
116}
117
118impl TryFrom<&[u8]> for Argument {
119 type Error = Error;
120
121 fn try_from(val: &[u8]) -> Result<Self> {
122 Self::try_from_bytes(val)
123 }
124}
125
126impl<const N: usize> TryFrom<&[u8; N]> for Argument {
127 type Error = Error;
128
129 fn try_from(val: &[u8; N]) -> Result<Self> {
130 Self::try_from_bytes(val.as_ref())
131 }
132}
133
134impl<const N: usize> TryFrom<[u8; N]> for Argument {
135 type Error = Error;
136
137 fn try_from(val: [u8; N]) -> Result<Self> {
138 Self::try_from_bytes(val.as_ref())
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn test_fields() {
148 let mut arg = Argument::new();
149
150 assert_eq!(arg.number_of_blocks(), 0);
151 assert_eq!(arg.task_id(), 0);
152 assert!(!arg.priority());
153 assert_eq!(arg.extended_address(), 0);
154 assert_eq!(arg.direction(), Direction::new());
155
156 (1..=u16::BITS)
157 .map(|r| ((1u32 << r) - 1) as u16)
158 .for_each(|exp_num| {
159 arg.set_number_of_blocks(exp_num);
160 assert_eq!(arg.number_of_blocks(), exp_num);
161 });
162
163 arg.set_number_of_blocks(0);
164
165 (0..=0x1f).for_each(|exp_task_id| {
166 arg.set_task_id(exp_task_id);
167 assert_eq!(arg.task_id(), exp_task_id);
168 });
169
170 arg.set_task_id(0);
171
172 test_field!(arg, priority: 23);
173
174 (0..=0x3f).for_each(|exp_addr| {
175 arg.set_extended_address(exp_addr);
176 assert_eq!(arg.extended_address(), exp_addr);
177 });
178
179 arg.set_extended_address(0);
180
181 [Direction::Write, Direction::Read]
182 .into_iter()
183 .for_each(|exp_direction| {
184 arg.set_direction(exp_direction);
185 assert_eq!(arg.direction(), exp_direction);
186 });
187
188 (1..=u32::BITS)
189 .map(|r| ((1u64 << r) - 1) as u32)
190 .for_each(|raw_arg| {
191 let exp_arg = Argument(raw_arg);
192
193 let raw = raw_arg.to_be_bytes();
194 let raw_direction = (raw_arg & (1 << 30)) != 0;
195 let exp_direction = Direction::from_bool(raw_direction);
196 let exp_addr = ((raw_arg & 0x3f00_0000) >> 24) as u8;
197 let exp_priority = (raw_arg & (1 << 23)) != 0;
198 let exp_task_id = ((raw_arg & 0x1f_0000) >> 16) as u8;
199 let exp_num_blocks = (raw_arg & 0xffff) as u16;
200
201 assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
202 assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
203 assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
204 assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
205 assert_eq!(Argument::try_from(raw), Ok(exp_arg));
206
207 assert_eq!(exp_arg.bits(), raw_arg);
208 assert_eq!(exp_arg.bytes(), raw);
209
210 assert_eq!(exp_arg.direction(), exp_direction);
211 assert_eq!(exp_arg.extended_address(), exp_addr);
212 assert_eq!(exp_arg.priority(), exp_priority);
213 assert_eq!(exp_arg.task_id(), exp_task_id);
214 assert_eq!(exp_arg.number_of_blocks(), exp_num_blocks);
215 });
216 }
217}