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 `GENERIC_CMD6_TIME` field of the [ExtCsd] register.
    GenericCmd6Time: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the multiplier value for the generic CMD6 timeout.
        generic_cmd6_time: 7, 0;
    }
}

impl GenericCmd6Time {
    /// Represents the timeout unit value (in milliseconds).
    pub const TIMEOUT_UNIT: u16 = 10;

    /// Gets the effective generic CMD6 timeout value.
    ///
    /// # Note
    ///
    /// The timeout value is calculated with the algorithm:
    ///
    /// ```no_build,no_run
    /// timeout = 10ms * GENERIC_CMD6_TIME
    /// ```
    pub const fn timeout(&self) -> u16 {
        Self::TIMEOUT_UNIT * self.generic_cmd6_time() as u16
    }
}

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

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

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

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

        assert_eq!(ext_csd.generic_cmd6_time(), GenericCmd6Time::new());

        (0..=u8::MAX)
            .map(GenericCmd6Time::from_inner)
            .for_each(|time| {
                ext_csd.set_generic_cmd6_time(time);
                assert_eq!(ext_csd.generic_cmd6_time(), time);
            });
    }
}