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
/// Represents the worst-case for clock-dependent factor of the data access time.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NsAccessCycles(u8);
impl NsAccessCycles {
/// Represents the base unit for [NsAccessCycles] clock cycles.
pub const BASE_UNIT: u16 = 100;
/// Creates a new [NsAccessCycles].
pub const fn new() -> Self {
Self(0)
}
/// Gets the bit value for the [NsAccessCycles].
pub const fn bits(&self) -> u8 {
self.0
}
/// Converts a [`u8`] into a [NsAccessCycles].
pub const fn from_bits(val: u8) -> Self {
Self(val)
}
/// Gets the total clock cycles for the [NsAccessCycles].
///
/// # Example
///
/// ```rust
/// # use sdmmc_core::register::csd::NsAccessCycles;
/// let raw_nsac = 255;
/// let cycles = NsAccessCycles::from_bits(raw_nsac).cycles();
/// assert_eq!(cycles, (raw_nsac as u16) * NsAccessCycles::BASE_UNIT);
/// ```
pub const fn cycles(&self) -> u16 {
(self.0 as u16) * Self::BASE_UNIT
}
}
impl Default for NsAccessCycles {
fn default() -> Self {
Self::new()
}
}
impl From<u8> for NsAccessCycles {
fn from(val: u8) -> Self {
Self::from_bits(val)
}
}
impl From<NsAccessCycles> for u8 {
fn from(val: NsAccessCycles) -> Self {
val.bits()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_valid() {
(0..=u8::MAX).for_each(|raw| {
let exp_nsac = NsAccessCycles(raw);
let exp_cycles = (raw as u16) * NsAccessCycles::BASE_UNIT;
assert_eq!(NsAccessCycles::from_bits(raw), exp_nsac);
assert_eq!(NsAccessCycles::from(raw), exp_nsac);
assert_eq!(exp_nsac.bits(), raw);
assert_eq!(u8::from(exp_nsac), raw);
assert_eq!(exp_nsac.cycles(), exp_cycles);
});
}
}