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 `USER_WP` field of the [ExtCsd] register.
    UserAreaWriteProtection: u8,
    mask: 0xdd,
    default: 0,
    {
        /// Indicates whether to permanently disable password protection features.
        perm_pswd_dis: 7;
        /// Indicates whether to disable the use of `PERM_WRITE_PROTECT` (`CSD[13]`).
        cd_perm_wp_dis: 6;
        /// Indicates whether to disable permanent write protection.
        us_perm_wp_dis: 4;
        /// Indicates whether to disable power-on write protection.
        us_pwr_wp_dis: 3;
        /// Indicates whether to apply permanent write protection.
        us_perm_wp_en: 2;
        /// Indicates whether to apply power-on write protection.
        us_pwr_wp_en: 0;
    }
}

impl ExtCsd {
    /// Gets the `USER_WP` field of the [ExtCsd] register.
    pub const fn user_wp(&self) -> UserAreaWriteProtection {
        UserAreaWriteProtection::from_inner(self.0[ExtCsdIndex::UserWp.into_inner()])
    }

    /// Sets the `USER_WP` field of the [ExtCsd] register.
    pub fn set_user_wp(&mut self, val: UserAreaWriteProtection) {
        self.0[ExtCsdIndex::UserWp.into_inner()] = val.into_inner();
    }
}

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

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

        assert_eq!(ext_csd.user_wp(), UserAreaWriteProtection::new());

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