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 outstanding;

pub use outstanding::*;

lib_bitfield! {
    /// Represents the `BKOPS_STATUS` field of the [ExtCsd] register.
    BackgroundOperationsStatus: u8,
    mask: 0x3,
    default: 0,
    {
        /// Represents the status of outstanding background operations.
        outstanding: OutstandingStatus, 1, 0;
    }
}

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

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

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

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

        assert_eq!(
            ext_csd.bkops_status(),
            Ok(BackgroundOperationsStatus::new())
        );

        (0..=u8::MAX).for_each(|raw_bkops| {
            // set a potentially invalid BackgroundOperationsStatus
            ext_csd.set_bkops_status(BackgroundOperationsStatus(raw_bkops));

            match BackgroundOperationsStatus::try_from_inner(raw_bkops) {
                Ok(bkops) => assert_eq!(ext_csd.bkops_status(), Ok(bkops)),
                Err(err) => assert_eq!(ext_csd.bkops_status(), Err(err)),
            }
        });
    }
}