sdmmc_core/register/ext_csd/
boot_size_mult.rs

1use crate::lib_bitfield;
2
3use super::{ExtCsd, ExtCsdIndex};
4
5lib_bitfield! {
6    /// Represents the `BOOT_SIZE_MULT` field of the [ExtCsd] register.
7    BootSizeMult: u8,
8    mask: 0xff,
9    default: 0,
10    {
11        /// Represents the size multiplier of the boot partition.
12        boot_size_mult: 7, 0;
13    }
14}
15
16impl BootSizeMult {
17    /// Represents the boot partition size unit (in bytes).
18    pub const BOOT_UNIT: u32 = 128 * 1024;
19
20    /// Gets the effective size of the boot partition.
21    ///
22    /// # Note
23    ///
24    /// The partition size is calculated with the algorithm:
25    ///
26    /// ```no_build,no_run
27    /// size = 128Kbytes * BOOT_SIZE_MULT
28    /// ```
29    pub const fn size(&self) -> u32 {
30        Self::BOOT_UNIT * self.boot_size_mult() as u32
31    }
32}
33
34impl ExtCsd {
35    /// Gets the `BOOT_SIZE_MULT` field of the [ExtCsd] register.
36    pub const fn boot_size_mult(&self) -> BootSizeMult {
37        BootSizeMult::from_inner(self.0[ExtCsdIndex::BootSizeMult.into_inner()])
38    }
39
40    /// Sets the `BOOT_SIZE_MULT` field of the [ExtCsd] register.
41    pub(crate) fn set_boot_size_mult(&mut self, val: BootSizeMult) {
42        self.0[ExtCsdIndex::BootSizeMult.into_inner()] = val.into_inner();
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_boot_size_mult() {
52        let mut ext_csd = ExtCsd::new();
53
54        assert_eq!(ext_csd.boot_size_mult(), BootSizeMult::new());
55
56        (0..=u8::MAX)
57            .map(BootSizeMult::from_inner)
58            .for_each(|size| {
59                ext_csd.set_boot_size_mult(size);
60                assert_eq!(ext_csd.boot_size_mult(), size);
61                assert_eq!(
62                    size.size(),
63                    BootSizeMult::BOOT_UNIT * (size.boot_size_mult() as u32)
64                );
65            });
66    }
67}