sdmmc-core 0.5.0

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

use super::{ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `PARTITION_SWITCH_TIME` field of the [ExtCsd] register.
    PartitionSwitchTime: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the partition switch timeout value.
        timeout: 7, 0;
    }
}

impl PartitionSwitchTime {
    /// Represents the timeout multiplier in millisecond units.
    pub const TIMEOUT_MULT: u16 = 10;

    /// Gets the effective timeout in units of 10ms.
    ///
    /// The value is calculated with the equation:
    ///
    /// ```no_build,no_run
    /// timeout = PARTITION_SWITCH_TIME * 10ms
    /// ```
    #[inline]
    pub const fn timeout_ms(&self) -> u16 {
        (self.0 as u16) * Self::TIMEOUT_MULT
    }
}

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

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

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

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

        assert_eq!(ext_csd.partition_switch_time(), PartitionSwitchTime::new());

        (0..=u8::MAX)
            .map(PartitionSwitchTime::from_inner)
            .for_each(|time| {
                ext_csd.set_partition_switch_time(time);
                assert_eq!(ext_csd.partition_switch_time(), time);
            });
    }
}