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_READS` field of the [ExtCsd] register.
///
/// # Note
///
/// Indicates the maximum number of commands that can be encoded into a packed read command.
pub type MaxPackedReads = RangeU8<5, 0xff>;

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

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

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

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

        ext_csd.set_max_packed_reads(MaxPackedReads::new());
        assert_eq!(ext_csd.max_packed_reads(), Ok(MaxPackedReads::new()));

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

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