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};

mod enh_attribute;
mod ext_attribute;
mod partitioning;

pub use enh_attribute::*;
pub use ext_attribute::*;
pub use partitioning::*;

lib_bitfield! {
    /// Represents the `PARTITIONING_SUPPORT` field of the [ExtCsd] register.
    PartitioningSupport: u8,
    mask: 0x7,
    default: 0,
    {
        /// Indicates device support for extended attributes.
        ext_attribute: ExtendedAttribute, 2;
        /// Indicates device support for enhanced attributes.
        enh_attribute: EnhancedAttribute, 1;
        /// Indicates device support for partitioning.
        partitioning: Partitioning, 0;
    }
}

impl PartitioningSupport {
    /// Converts an inner representation into a [PartitioningSupport].
    pub const fn from_inner(val: u8) -> Self {
        Self(val & Self::MASK)
    }
}

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

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

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

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

        assert_eq!(ext_csd.partitioning_support(), PartitioningSupport::new());

        (0..=u8::MAX)
            .map(PartitioningSupport::from_inner)
            .for_each(|ps| {
                ext_csd.set_partitioning_support(ps);
                assert_eq!(ext_csd.partitioning_support(), ps);
            });
    }
}