sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::range::RangeU8;
use crate::register::ext_csd::{ExtCsd, ExtCsdIndex};
use crate::result::Result;

/// Represents the `MAX_PACKED_WRITES` field of the [ExtCsd] register.
///
/// # Note
///
/// Indicates the maximum number of commands that can be encoded into a packed write command.
pub type MaxPackedWrites = RangeU8<3, 0xff>;

impl ExtCsd {
    /// Gets the `MAX_PACKED_WRITES` field of the [ExtCsd] register.
    pub const fn max_packed_writes(&self) -> Result<MaxPackedWrites> {
        MaxPackedWrites::try_from_inner(self.0[ExtCsdIndex::MaxPackedWrites.into_inner()])
    }

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

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

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

        ext_csd.set_max_packed_writes(MaxPackedWrites::new());
        assert_eq!(ext_csd.max_packed_writes(), Ok(MaxPackedWrites::new()));

        (0..=u8::MAX).for_each(|raw_max| {
            // set a potentially invalid MaxPackedWrites
            ext_csd.0[ExtCsdIndex::MaxPackedWrites.into_inner()] = raw_max;

            match MaxPackedWrites::try_from_inner(raw_max) {
                Ok(max) => assert_eq!(ext_csd.max_packed_writes(), Ok(max)),
                Err(err) => assert_eq!(ext_csd.max_packed_writes(), Err(err)),
            }
        });
    }
}