sdmmc_core/command/class/class10/cmd6/
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::lib_bitfield;
use crate::result::{Error, Result};

mod access_mode;
mod command_system;
mod driver_strength;
mod mode;
mod power_limit;

pub use access_mode::*;
pub use command_system::*;
pub use driver_strength::*;
pub use mode::*;
pub use power_limit::*;

lib_bitfield! {
    /// Argumentument for CMD6.
    pub Argument(u32): u8 {
        raw_mode: 31;
        raw_power_limit: 15, 12;
        raw_driver_strength: 11, 8;
        raw_command_system: 7, 4;
        raw_access_mode: 3, 0;
    }
}

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

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

    /// Gets the [AccessMode] for the `CMD6` [Argument].
    pub const fn access_mode(&self) -> Result<AccessMode> {
        AccessMode::from_raw(self.raw_access_mode())
    }

    /// Sets the [AccessMode] for the `CMD6` [Argument].
    pub fn set_access_mode(&mut self, val: AccessMode) {
        self.set_raw_access_mode(val.into_raw() as u32);
    }

    /// Gets the [CommandSystem] for the `CMD6` [Argument].
    pub const fn command_system(&self) -> Result<CommandSystem> {
        CommandSystem::from_raw(self.raw_command_system())
    }

    /// Sets the [CommandSystem] for the `CMD6` [Argument].
    pub fn set_command_system(&mut self, val: CommandSystem) {
        self.set_raw_command_system(val.into_raw() as u32);
    }

    /// Gets the [DriverStrength] for the `CMD6` [Argument].
    pub const fn driver_strength(&self) -> Result<DriverStrength> {
        DriverStrength::from_raw(self.raw_driver_strength())
    }

    /// Sets the [DriverStrength] for the `CMD6` [Argument].
    pub fn set_driver_strength(&mut self, val: DriverStrength) {
        self.set_raw_driver_strength(val.into_raw() as u32);
    }

    /// Gets the [PowerLimit] for the `CMD6` [Argument].
    pub const fn power_limit(&self) -> Result<PowerLimit> {
        PowerLimit::from_raw(self.raw_power_limit())
    }

    /// Sets the [PowerLimit] for the `CMD6` [Argument].
    pub fn set_power_limit(&mut self, val: PowerLimit) {
        self.set_raw_power_limit(val.into_raw() as u32)
    }

    /// Gets the [Mode] for the `CMD6` [Argument].
    pub const fn mode(&self) -> Result<Mode> {
        Mode::from_raw(self.raw_mode() as u8)
    }

    /// Sets the [Mode] for the `CMD6` [Argument].
    pub fn set_mode(&mut self, val: Mode) {
        self.set_raw_mode(val.into_raw() != 0);
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        match Self(val) {
            arg if arg.access_mode().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::access_mode",
                arg.raw_access_mode() as usize,
            )),
            arg if arg.command_system().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::command_system",
                arg.raw_command_system() as usize,
            )),
            arg if arg.driver_strength().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::driver_strength",
                arg.raw_driver_strength() as usize,
            )),
            arg if arg.power_limit().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::power_limit",
                arg.raw_power_limit() as usize,
            )),
            arg if arg.mode().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::mode",
                arg.raw_mode() as usize,
            )),
            arg => Ok(arg),
        }
    }

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

    /// Attempts to convert a [`u32`] 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 TryFrom<u32> for Argument {
    type Error = Error;

    fn try_from(val: u32) -> Result<Self> {
        Self::try_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();

        assert_eq!(arg.access_mode(), Ok(AccessMode::new()));
        assert_eq!(arg.command_system(), Ok(CommandSystem::new()));
        assert_eq!(arg.driver_strength(), Ok(DriverStrength::new()));
        assert_eq!(arg.power_limit(), Ok(PowerLimit::new()));

        [
            AccessMode::Sdr12,
            AccessMode::Sdr25,
            AccessMode::Sdr50,
            AccessMode::Sdr104,
            AccessMode::Ddr50,
        ]
        .into_iter()
        .for_each(|exp_access_mode| {
            arg.set_access_mode(exp_access_mode);
            assert_eq!(arg.access_mode(), Ok(exp_access_mode));
        });

        [
            CommandSystem::SystemDefault,
            CommandSystem::ForEC,
            CommandSystem::Otp,
            CommandSystem::Assd,
            CommandSystem::Vendor,
        ]
        .into_iter()
        .for_each(|exp_command_system| {
            arg.set_command_system(exp_command_system);
            assert_eq!(arg.command_system(), Ok(exp_command_system));
        });

        [
            DriverStrength::TypeB,
            DriverStrength::TypeA,
            DriverStrength::TypeC,
            DriverStrength::TypeD,
        ]
        .into_iter()
        .for_each(|exp_driver_strength| {
            arg.set_driver_strength(exp_driver_strength);
            assert_eq!(arg.driver_strength(), Ok(exp_driver_strength));
        });

        [
            PowerLimit::W072,
            PowerLimit::W144,
            PowerLimit::W216,
            PowerLimit::W288,
            PowerLimit::W180,
        ]
        .into_iter()
        .for_each(|exp_power_limit| {
            arg.set_power_limit(exp_power_limit);
            assert_eq!(arg.power_limit(), Ok(exp_power_limit));
        });

        [Mode::Check, Mode::Switch]
            .into_iter()
            .for_each(|exp_mode| {
                arg.set_mode(exp_mode);
                assert_eq!(arg.mode(), Ok(exp_mode));
            });
    }
}