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 `TRIM_MULT` field of the [ExtCsd] register.
    TrimMult: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the secure trim multiplier value.
        trim_mult: 7, 0;
    }
}

impl TrimMult {
    /// 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 * TRIM_MULT
    /// ```
    pub const fn timeout(&self) -> u32 {
        Self::TRIM_UNIT * self.trim_mult() as u32
    }
}

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

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

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

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

        assert_eq!(ext_csd.trim_mult(), TrimMult::new());

        (0..u8::MAX)
            .map(TrimMult::from_inner)
            .for_each(|trim_mult| {
                ext_csd.set_trim_mult(trim_mult);
                assert_eq!(ext_csd.trim_mult(), trim_mult);
                assert_eq!(
                    trim_mult.timeout(),
                    TrimMult::TRIM_UNIT * trim_mult.trim_mult() as u32
                );
            });
    }
}