sdmmc-core 0.5.0

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

use super::{ExtCsd, ExtCsdIndex};

mod info;

pub use info::*;

lib_bitfield! {
    /// Represents the `PRE_EOL_INFO` field of the [ExtCsd] register.
    PreEolInfo: u8,
    mask: 0xff,
    default: 1,
    {
        /// Represents the pre-end-of-life information about consumption of reserved blocks.
        pre_eol_info: LifetimeInformation, 7, 0;
    }
}

impl ExtCsd {
    /// Gets the `PRE_EOL_INFO` field of the [ExtCsd] register.
    pub const fn pre_eol_info(&self) -> Result<PreEolInfo> {
        PreEolInfo::try_from_inner(self.0[ExtCsdIndex::PreEolInfo.into_inner()])
    }

    /// Sets the `PRE_EOL_INFO` field of the [ExtCsd] register.
    pub(crate) fn set_pre_eol_info(&mut self, val: PreEolInfo) {
        self.0[ExtCsdIndex::PreEolInfo.into_inner()] = val.into_inner();
    }
}

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

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

        ext_csd.set_pre_eol_info(PreEolInfo::new());
        assert_eq!(ext_csd.pre_eol_info(), Ok(PreEolInfo::new()));

        (0..=u8::MAX).for_each(|raw_info| {
            // set a potentially invalid PreEolInfo
            ext_csd.set_pre_eol_info(PreEolInfo(raw_info));

            match PreEolInfo::try_from_inner(raw_info) {
                Ok(info) => assert_eq!(ext_csd.pre_eol_info(), Ok(info)),
                Err(err) => assert_eq!(ext_csd.pre_eol_info(), Err(err)),
            }
        });
    }
}