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 access;
mod ack;
mod enable;

pub use access::*;
pub use ack::*;
pub use enable::*;

lib_bitfield! {
    /// Represents the `PARTITION_CONFIG` field of the [ExtCsd] register.
    PartitionConfig: u8,
    mask: 0x7f,
    default: 0,
    {
        /// Indicates whether a boot acknowledge bit was sent during boot.
        boot_ack: BootAck, 6;
        /// Indicates which partition is enabled for boot.
        boot_partition_enable: BootPartitionEnable, 5, 3;
        /// Indicates user selection of the partition to access.
        partition_access: PartitionAccess, 2, 0;
    }
}

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

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

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

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

        assert_eq!(ext_csd.partition_config(), Ok(PartitionConfig::new()));

        (0..=u8::MAX).for_each(|raw_cfg| {
            // set a potentially invalid PartitionConfig
            ext_csd.set_partition_config(PartitionConfig(raw_cfg));

            match PartitionConfig::try_from_inner(raw_cfg) {
                Ok(part_cfg) => assert_eq!(ext_csd.partition_config(), Ok(part_cfg)),
                Err(err) => assert_eq!(ext_csd.partition_config(), Err(err)),
            }
        });
    }
}