sdmmc_core/command/class/class0/cmd4/
arg.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::lib_bitfield;
use crate::register::Dsr;
use crate::result::{Error, Result};

const DSR_SHIFT: usize = 16;

lib_bitfield! {
    /// Argumentument for [Cmd4](super::Cmd4).
    pub Argument(u32): u16 {
        /// Programs the DSR of all cards.
        raw_dsr: 31, 16;
    }
}

impl Argument {
    /// Represents the byte length of the [Argument].
    pub const LEN: usize = 4;

    /// Creates a new [Argument].
    pub const fn new() -> Self {
        Self((Dsr::DEFAULT as u32) << DSR_SHIFT)
    }

    /// Gets the [Dsr] field for the [Argument].
    pub const fn dsr(&self) -> Dsr {
        Dsr::from_bits(self.raw_dsr())
    }

    /// Sets the [Dsr] field for the [Argument].
    pub fn set_dsr(&mut self, val: Dsr) {
        self.set_raw_dsr(val.bits() as u32);
    }

    /// Converts a [`u32`] into an [Argument].
    pub const fn from_bits(val: u32) -> Self {
        Self(val)
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self::from_bits(val))
    }

    /// Converts the [Argument] into a byte array.
    pub const fn bytes(&self) -> [u8; Self::LEN] {
        self.0.to_be_bytes()
    }

    /// Attempts to convert a byte slice into an [Argument].
    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
        match val.len() {
            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
            _ => Ok(Self(u32::from_be_bytes([val[0], val[1], val[2], val[3]]))),
        }
    }
}

impl Default for Argument {
    fn default() -> Self {
        Self::new()
    }
}

impl From<u32> for Argument {
    fn from(val: u32) -> Self {
        Self::from_bits(val)
    }
}

impl From<Argument> for u32 {
    fn from(val: Argument) -> Self {
        val.bits()
    }
}

impl From<Argument> for [u8; Argument::LEN] {
    fn from(val: Argument) -> Self {
        val.bytes()
    }
}

impl TryFrom<&[u8]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8]) -> Result<Self> {
        Self::try_from_bytes(val)
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

impl<const N: usize> TryFrom<[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fields() {
        let mut arg = Argument::new();
        let new_dsr = Dsr::new();

        assert_eq!(arg.dsr(), new_dsr);

        (1..u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let mut exp_arg = Argument(raw_arg);
                let exp_dsr = Dsr::from_bits((raw_arg >> 16) as u16);
                let raw = raw_arg.to_be_bytes();

                assert_eq!(Argument::from_bits(raw_arg), exp_arg);
                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(raw), Ok(exp_arg));

                assert_eq!(exp_arg.bits(), raw_arg);
                assert_eq!(exp_arg.bytes(), raw);

                assert_eq!(exp_arg.dsr(), exp_dsr);

                exp_arg.set_dsr(new_dsr);
                assert_eq!(exp_arg.dsr(), new_dsr);

                exp_arg.set_dsr(exp_dsr);
                assert_eq!(exp_arg.dsr(), exp_dsr);
            });
    }
}