sdmmc-core 0.5.0

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

use super::{ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `BKOPS_EN` field of the [ExtCsd] register.
    pub BackgroundOperationsEnable(u8): u8 {
        /// Indicates whether automatic control of background operations is enabled.
        pub auto: 1;
        /// Indicates whether manual control of background operations is enabled.
        pub manual: 0;
    }
}

impl BackgroundOperationsEnable {
    /// Represents the bitmask of the [BackgroundOperationsEnable].
    pub const MASK: u8 = 0x3;
    /// Represents the default byte value of the [BackgroundOperationsEnable].
    pub const DEFAULT: u8 = 0;

    /// Creates a new [BackgroundOperationsEnable].
    pub const fn new() -> Self {
        Self(Self::DEFAULT)
    }

    /// Converts an inner representation into a [BackgroundOperationsEnable].
    pub const fn from_inner(val: u8) -> Self {
        Self(val & Self::MASK)
    }

    /// Attempts to convert an inner representation into a [BackgroundOperationsEnable].
    pub const fn try_from_inner(val: u8) -> Result<Self> {
        Ok(Self::from_inner(val))
    }

    /// Converts a [BackgroundOperationsEnable] an inner representation into.
    pub const fn into_inner(self) -> u8 {
        self.0
    }
}

impl Default for BackgroundOperationsEnable {
    fn default() -> Self {
        Self::new()
    }
}

impl From<u8> for BackgroundOperationsEnable {
    fn from(val: u8) -> Self {
        Self::from_inner(val)
    }
}

impl From<BackgroundOperationsEnable> for u8 {
    fn from(val: BackgroundOperationsEnable) -> Self {
        val.into_inner()
    }
}

impl ExtCsd {
    /// Gets the `BKOPS_EN` field of the [ExtCsd] register.
    pub const fn background_operations_enable(&self) -> BackgroundOperationsEnable {
        BackgroundOperationsEnable::from_inner(self.0[ExtCsdIndex::BkopsEn.into_inner()])
    }

    /// Sets the `BKOPS_EN` field of the [ExtCsd] register.
    pub fn set_background_operations_enable(&mut self, val: BackgroundOperationsEnable) {
        self.0[ExtCsdIndex::BkopsEn.into_inner()] = val.into_inner();
    }
}

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

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

        assert_eq!(
            ext_csd.background_operations_enable(),
            BackgroundOperationsEnable::new()
        );

        (0..=u8::MAX)
            .map(BackgroundOperationsEnable::from_inner)
            .for_each(|ops| {
                ext_csd.set_background_operations_enable(ops);
                assert_eq!(ext_csd.background_operations_enable(), ops);
            });
    }
}