sdmmc_core/response/
start.rs1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5 pub Start(u8): u8 {
7 pub start_bit: 7;
9 pub transmission_bit: 6;
11 pub command_index: 5, 0;
13 }
14}
15
16impl Start {
17 pub const LEN: usize = 1;
19 pub const DEFAULT: u8 = 0;
21 pub const MASK: u8 = 0b0011_1111;
23
24 pub const fn new() -> Self {
26 Self(Self::DEFAULT)
27 }
28
29 pub const fn from_bits(val: u8) -> Self {
35 Self(val & Self::MASK)
36 }
37
38 pub const fn try_from_bits(val: u8) -> Result<Self> {
40 match val {
41 v if (v & !Self::MASK) != 0 => Err(Error::invalid_field_variant("start", v as usize)),
42 v => Ok(Self(v)),
43 }
44 }
45}
46
47impl Default for Start {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl TryFrom<u8> for Start {
54 type Error = Error;
55
56 fn try_from(val: u8) -> Result<Self> {
57 Self::try_from_bits(val)
58 }
59}
60
61impl From<Start> for u8 {
62 fn from(val: Start) -> Self {
63 val.bits()
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_fields() {
73 let mut s = Start::new();
74
75 assert_eq!(s.command_index(), 0);
76 assert!(!s.transmission_bit());
77 assert!(!s.start_bit());
78
79 (0..=0x3f).for_each(|cmd| {
80 s.set_command_index(cmd);
81 assert_eq!(s.command_index(), cmd);
82
83 assert!(!s.transmission_bit());
85 assert!(!s.start_bit());
86 });
87
88 (0x40..=u8::MAX).for_each(|invalid_cmd| {
90 s.set_command_index(invalid_cmd);
91 assert_eq!(s.command_index(), invalid_cmd & Start::MASK);
92 });
93 }
94}