sdmmc_core/command/class/class1/cmd44/
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::lib_bitfield;
use crate::result::{Error, Result};

mod direction;

pub use direction::*;

lib_bitfield! {
    /// Argumentument for CMD44.
    pub Argument(u32): u16 {
        raw_direction: 30;
        raw_extended_address: 29, 24;
        /// Priority task.
        pub priority: 23;
        /// Queued task ID.
        raw_task_id: 20, 16;
        /// Queued number of blocks.
        raw_number_of_blocks: 15, 0;
    }
}

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

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

    /// Gets the `Queue Task Info` [Direction] for [Argument].
    pub const fn direction(&self) -> Direction {
        Direction::from_bool(self.raw_direction())
    }

    /// Sets the `Queue Task Info` [Direction] for [Argument].
    pub fn set_direction(&mut self, val: Direction) {
        self.set_raw_direction(val.into_bool());
    }

    /// Gets the upper 6-bits address of 38-bit 512B unit address, for `SDUC` cards.
    pub const fn extended_address(&self) -> u8 {
        self.raw_extended_address() as u8
    }

    /// Sets the upper 6-bits address of 38-bit 512B unit address, for `SDUC` cards.
    pub fn set_extended_address(&mut self, val: u8) {
        self.set_raw_extended_address(val as u32);
    }

    /// Gets the queued task ID for the [Argument].
    pub const fn task_id(&self) -> u8 {
        self.raw_task_id() as u8
    }

    /// Sets the queued task ID for the [Argument].
    pub fn set_task_id(&mut self, val: u8) {
        self.set_raw_task_id(val as u32);
    }

    /// Gets the queued number of blocks for the [Argument].
    pub const fn number_of_blocks(&self) -> u16 {
        self.raw_number_of_blocks()
    }

    /// Sets the queued number of blocks for the [Argument].
    pub fn set_number_of_blocks(&mut self, val: u16) {
        self.set_raw_number_of_blocks(val as u32);
    }

    /// Attempts to convert a [`u32`] into an [`Argument`].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self(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)),
            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
        }
    }
}

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

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<u32> for Argument {
    type Error = Error;

    fn try_from(val: u32) -> Result<Self> {
        Self::try_from_bits(val)
    }
}

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();

        assert_eq!(arg.number_of_blocks(), 0);
        assert_eq!(arg.task_id(), 0);
        assert!(!arg.priority());
        assert_eq!(arg.extended_address(), 0);
        assert_eq!(arg.direction(), Direction::new());

        (1..=u16::BITS)
            .map(|r| ((1u32 << r) - 1) as u16)
            .for_each(|exp_num| {
                arg.set_number_of_blocks(exp_num);
                assert_eq!(arg.number_of_blocks(), exp_num);
            });

        arg.set_number_of_blocks(0);

        (0..=0x1f).for_each(|exp_task_id| {
            arg.set_task_id(exp_task_id);
            assert_eq!(arg.task_id(), exp_task_id);
        });

        arg.set_task_id(0);

        test_field!(arg, priority: 23);

        (0..=0x3f).for_each(|exp_addr| {
            arg.set_extended_address(exp_addr);
            assert_eq!(arg.extended_address(), exp_addr);
        });

        arg.set_extended_address(0);

        [Direction::Write, Direction::Read]
            .into_iter()
            .for_each(|exp_direction| {
                arg.set_direction(exp_direction);
                assert_eq!(arg.direction(), exp_direction);
            });

        (1..=u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let exp_arg = Argument(raw_arg);

                let raw = raw_arg.to_be_bytes();
                let raw_direction = (raw_arg & (1 << 30)) != 0;
                let exp_direction = Direction::from_bool(raw_direction);
                let exp_addr = ((raw_arg & 0x3f00_0000) >> 24) as u8;
                let exp_priority = (raw_arg & (1 << 23)) != 0;
                let exp_task_id = ((raw_arg & 0x1f_0000) >> 16) as u8;
                let exp_num_blocks = (raw_arg & 0xffff) as u16;

                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(raw_arg), 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.direction(), exp_direction);
                assert_eq!(exp_arg.extended_address(), exp_addr);
                assert_eq!(exp_arg.priority(), exp_priority);
                assert_eq!(exp_arg.task_id(), exp_task_id);
                assert_eq!(exp_arg.number_of_blocks(), exp_num_blocks);
            });
    }
}