sdmmc-core 0.5.0

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

use super::{EraseTimeoutMult, ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `SEC_TRIM_MULT` field of the [ExtCsd] register.
    SecureTrimMult: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the secure trim multiplier value.
        sec_trim_mult: 7, 0;
    }
}

impl SecureTrimMult {
    /// Represents the secure trim multiplier unit (in milliseconds).
    pub const TRIM_UNIT: u32 = 300;

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

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

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

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

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

        assert_eq!(ext_csd.sec_trim_mult(), SecureTrimMult::new());

        (0..u8::MAX)
            .map(SecureTrimMult::from_inner)
            .for_each(|trim_mult| {
                ext_csd.set_sec_trim_mult(trim_mult);
                assert_eq!(ext_csd.sec_trim_mult(), trim_mult);

                (0..u8::MAX)
                    .map(EraseTimeoutMult::from_inner)
                    .for_each(|timeout| {
                        assert_eq!(
                            trim_mult.timeout(timeout),
                            timeout.timeout() * trim_mult.sec_trim_mult() as u32
                        );
                    });
            });
    }
}