sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
/// Represents the worst-case for clock-dependent factor of the data access time.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NsAccessCycles(u8);

impl NsAccessCycles {
    /// Represents the base unit for [NsAccessCycles] clock cycles.
    pub const BASE_UNIT: u16 = 100;

    /// Creates a new [NsAccessCycles].
    pub const fn new() -> Self {
        Self(0)
    }

    /// Gets the bit value for the [NsAccessCycles].
    pub const fn bits(&self) -> u8 {
        self.0
    }

    /// Converts a [`u8`] into a [NsAccessCycles].
    pub const fn from_bits(val: u8) -> Self {
        Self(val)
    }

    /// Gets the total clock cycles for the [NsAccessCycles].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use sdmmc_core::register::csd::NsAccessCycles;
    /// let raw_nsac = 255;
    /// let cycles = NsAccessCycles::from_bits(raw_nsac).cycles();
    /// assert_eq!(cycles, (raw_nsac as u16) * NsAccessCycles::BASE_UNIT);
    /// ```
    pub const fn cycles(&self) -> u16 {
        (self.0 as u16) * Self::BASE_UNIT
    }
}

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

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

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

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

    #[test]
    fn test_valid() {
        (0..=u8::MAX).for_each(|raw| {
            let exp_nsac = NsAccessCycles(raw);
            let exp_cycles = (raw as u16) * NsAccessCycles::BASE_UNIT;

            assert_eq!(NsAccessCycles::from_bits(raw), exp_nsac);
            assert_eq!(NsAccessCycles::from(raw), exp_nsac);

            assert_eq!(exp_nsac.bits(), raw);
            assert_eq!(u8::from(exp_nsac), raw);

            assert_eq!(exp_nsac.cycles(), exp_cycles);
        });
    }
}