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 `ERASE_TIMEOUT_MULT` field of the [ExtCsd] register.
    EraseTimeoutMult: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the timeout multiplier for high-capacity erase operations.
        erase_timeout_mult: 7, 0;
    }
}

impl EraseTimeoutMult {
    /// Represents the timeout unit value (in milliseconds).
    pub const TIMEOUT_UNIT: u32 = 300;

    /// Gets the effective timeout value (in milliseconds).
    ///
    /// # Note
    ///
    /// The value is calculated with the algorithm:
    ///
    /// ```no_build,no_run
    /// timeout = 300ms * ERASE_TIMEOUT_MULT
    /// ```
    pub const fn timeout(&self) -> u32 {
        Self::TIMEOUT_UNIT * (self.0 as u32)
    }
}

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

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

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

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

        assert_eq!(ext_csd.erase_timeout_mult(), EraseTimeoutMult::new());

        (0..=u8::MAX)
            .map(EraseTimeoutMult::from_inner)
            .for_each(|timeout| {
                ext_csd.set_erase_timeout_mult(timeout);
                assert_eq!(ext_csd.erase_timeout_mult(), timeout);
                assert_eq!(
                    timeout.timeout(),
                    EraseTimeoutMult::TIMEOUT_UNIT * (timeout.erase_timeout_mult() as u32)
                );
            });
    }
}