sdmmc-core 0.5.0

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

mod attribute;

pub use attribute::*;

use super::{ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `PARTITIONS_ATTRIBUTE` field of the [ExtCsd] register.
    PartitionsAttribute: u8,
    mask: 0x1f,
    default: 0,
    {
        /// Enhanced attribute setting for the `General Purpose partition 4`.
        enh4: EnhancedAttribute, 4;
        /// Enhanced attribute setting for the `General Purpose partition 3`.
        enh3: EnhancedAttribute, 3;
        /// Enhanced attribute setting for the `General Purpose partition 2`.
        enh2: EnhancedAttribute, 2;
        /// Enhanced attribute setting for the `General Purpose partition 1`.
        enh1: EnhancedAttribute, 1;
        /// Enhanced attribute setting for the `User Data Area`.
        enh_usr: EnhancedAttribute, 0;
    }
}

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

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

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

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

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

        assert_eq!(ext_csd.partitions_attribute(), PartitionsAttribute::new());

        (0..=u8::MAX)
            .map(PartitionsAttribute::from_inner)
            .for_each(|pa| {
                ext_csd.set_partitions_attribute(pa);
                assert_eq!(ext_csd.partitions_attribute(), pa);
            });
    }
}