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 `DEVICE_VERSION` field of the [ExtCsd] register.
    DeviceVersion: u16,
    mask: 0xffff,
    default: 0,
    {
        /// Represents the device version.
        device_version: 15, 0;
    }
}

impl ExtCsd {
    /// Gets the `DEVICE_VERSION` field of the [ExtCsd] register.
    pub const fn device_version(&self) -> DeviceVersion {
        let start = ExtCsdIndex::DEVICE_VERSION_START;

        DeviceVersion::from_inner(u16::from_le_bytes([self.0[start], self.0[start + 1]]))
    }

    /// Sets the `DEVICE_VERSION` field of the [ExtCsd] register.
    pub(crate) fn set_device_version(&mut self, val: DeviceVersion) {
        let start = ExtCsdIndex::DEVICE_VERSION_START;
        let [s0, s1] = val.into_inner().to_le_bytes();

        self.0[start] = s0;
        self.0[start + 1] = s1;
    }
}

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

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

        assert_eq!(ext_csd.device_version(), DeviceVersion::new());

        (0..=u16::BITS)
            .map(|r| DeviceVersion::from_inner(((1u32 << r) - 1) as u16))
            .for_each(|version| {
                ext_csd.set_device_version(version);
                assert_eq!(ext_csd.device_version(), version);
            });
    }
}