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
use crate::lib_bitfield;
use super::{ExtCsd, ExtCsdIndex};
lib_bitfield! {
/// Represents the `TRIM_MULT` field of the [ExtCsd] register.
TrimMult: u8,
mask: 0xff,
default: 0,
{
/// Represents the secure trim multiplier value.
trim_mult: 7, 0;
}
}
impl TrimMult {
/// Represents the secure trim multiplier unit (in milliseconds).
pub const TRIM_UNIT: u32 = 300;
/// Gets the effective timeout value (in milliseconds).
///
/// # Note
///
/// The timeout value is calculated with the algorithm:
///
/// ```no_build,no_run
/// timeout = 300ms * TRIM_MULT
/// ```
pub const fn timeout(&self) -> u32 {
Self::TRIM_UNIT * self.trim_mult() as u32
}
}
impl ExtCsd {
/// Gets the `TRIM_MULT` field of the [ExtCsd] register.
pub const fn trim_mult(&self) -> TrimMult {
TrimMult::from_inner(self.0[ExtCsdIndex::TrimMult.into_inner()])
}
/// Sets the `TRIM_MULT` field of the [ExtCsd] register.
pub(crate) fn set_trim_mult(&mut self, val: TrimMult) {
self.0[ExtCsdIndex::TrimMult.into_inner()] = val.into_inner();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trim_mult() {
let mut ext_csd = ExtCsd::new();
assert_eq!(ext_csd.trim_mult(), TrimMult::new());
(0..u8::MAX)
.map(TrimMult::from_inner)
.for_each(|trim_mult| {
ext_csd.set_trim_mult(trim_mult);
assert_eq!(ext_csd.trim_mult(), trim_mult);
assert_eq!(
trim_mult.timeout(),
TrimMult::TRIM_UNIT * trim_mult.trim_mult() as u32
);
});
}
}