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
use crate::lib_bitfield;
use crate::range::RangeU8;
use crate::result::Result;
use super::{ExtCsd, ExtCsdIndex};
/// Convenience alias for the `S_A_TIMEOUT` field inner value.
pub type TimeoutValue = RangeU8<0, 0x17>;
lib_bitfield! {
/// Represents the `S_A_TIMEOUT` field of the [ExtCsd] register.
SleepAwakeTimeout: u8,
mask: 0xff,
default: 0,
{
/// Represents the base `S_A_TIMEOUT` value.
sleep_notification_time: TimeoutValue, 7, 0;
}
}
impl SleepAwakeTimeout {
/// Represents the timeout multiplier in nanoseconds.
pub const TIMEOUT: u32 = 100;
/// Gets the sleep notification timeout value (in 100ns increments).
///
/// # Note
///
/// The timeout value is calculated with the algorithm:
///
/// ```no_build,no_run
/// timeout = 100ns * (2 ^ S_A_TIMEOUT)
/// ```
pub const fn timeout(&self) -> u32 {
Self::TIMEOUT * (1u32 << self.into_inner())
}
}
impl ExtCsd {
/// Gets the `S_A_TIMEOUT` field of the [ExtCsd] register.
pub const fn s_a_timeout(&self) -> Result<SleepAwakeTimeout> {
SleepAwakeTimeout::try_from_inner(self.0[ExtCsdIndex::SATimeout.into_inner()])
}
/// Sets the `S_A_TIMEOUT` field of the [ExtCsd] register.
pub(crate) fn set_s_a_timeout(&mut self, val: SleepAwakeTimeout) {
self.0[ExtCsdIndex::SATimeout.into_inner()] = val.into_inner();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_s_a_timeout() {
let mut ext_csd = ExtCsd::new();
assert_eq!(ext_csd.s_a_timeout(), Ok(SleepAwakeTimeout::new()));
(0..u8::MAX).for_each(|raw_sleep| {
// set a potentially invalid SleepAwakeTimeout
ext_csd.0[ExtCsdIndex::SATimeout.into_inner()] = raw_sleep;
match SleepAwakeTimeout::try_from_inner(raw_sleep) {
Ok(sleep) => {
assert_eq!(ext_csd.s_a_timeout(), Ok(sleep));
assert_eq!(
sleep.timeout(),
SleepAwakeTimeout::TIMEOUT * (1u32 << raw_sleep)
);
}
Err(err) => assert_eq!(ext_csd.s_a_timeout(), Err(err)),
}
});
}
}