sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::lib_bitfield;

use super::{ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `BOOT_WP` field of the [ExtCsd] register.
    BootAreaWriteProtection: u8,
    mask: 0xdf,
    default: 0,
    {
        /// Indicates whether write protection settings apply to partitions selected by `B_PERM_WP_SEC_SEL` and `B_PWR_WP_SEC_SEL` fields.
        b_sec_wp_sel: 7;
        /// Indicates whether to disable the use of `B_PWR_WP_EN` setting.
        b_pwr_wp_dis: 6;
        /// Indicates whether to disable permanent write protection.
        b_perm_wp_dis: 4;
        /// Indicates whether to apply permanent write protection to a boot area.
        ///
        /// Values:
        ///
        /// - `false`: `Area 1`
        /// - `true`: `Area 2`
        b_perm_wp_sec_sel: 3;
        /// Indicates whether to apply permanent write protection.
        b_perm_wp_en: 2;
        /// Indicates whether to apply write protection to a boot area.
        ///
        /// Values:
        ///
        /// - `false`: `Area 1`
        /// - `true`: `Area 2`
        b_pwr_wp_sec_sel: 1;
        /// Indicates whether to apply power-on write protection.
        b_pwr_wp_en: 0;
    }
}

impl ExtCsd {
    /// Gets the `BOOT_WP` field of the [ExtCsd] register.
    pub const fn boot_wp(&self) -> BootAreaWriteProtection {
        BootAreaWriteProtection::from_inner(self.0[ExtCsdIndex::BootWp.into_inner()])
    }

    /// Sets the `BOOT_WP` field of the [ExtCsd] register.
    pub fn set_boot_wp(&mut self, val: BootAreaWriteProtection) {
        self.0[ExtCsdIndex::BootWp.into_inner()] = val.into_inner();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_boot_wp() {
        let mut ext_csd = ExtCsd::new();

        assert_eq!(ext_csd.boot_wp(), BootAreaWriteProtection::new());

        (0..=u8::MAX)
            .map(BootAreaWriteProtection::from_inner)
            .for_each(|user_wp| {
                ext_csd.set_boot_wp(user_wp);
                assert_eq!(ext_csd.boot_wp(), user_wp);
            });
    }
}