sdmmc-core 0.5.0

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

use super::{ExtCsd, ExtCsdIndex};

mod implementation;

pub use implementation::*;

lib_bitfield! {
    /// Represents the `HPI_FEATURES` field of the [ExtCsd] register.
    HpiFeatures: u8,
    mask: 0x3,
    default: 1,
    {
        /// Indicates the HPI mechanism implementation.
        ///
        /// # Note
        ///
        /// The boolean values indicate:
        ///
        /// - `false`: HPI implementation based on `CMD13`
        /// - `true`: HPI implementation based on `CMD12`
        hpi_implementation: 1;
        /// Indicates support for HPI features.
        hpi_support: 0;
    }
}

impl HpiFeatures {
    /// Gets the HPI mechanism implementation version.
    pub const fn implementation(&self) -> HpiImplementation {
        HpiImplementation::from_bool(self.hpi_implementation())
    }
}

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

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

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

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

        ext_csd.set_hpi_features(HpiFeatures::new());
        assert_eq!(ext_csd.hpi_features(), HpiFeatures::new());

        (0..=u8::MAX).map(HpiFeatures::from_inner).for_each(|hpi| {
            ext_csd.set_hpi_features(hpi);
            assert_eq!(ext_csd.hpi_features(), hpi);
            assert_eq!(
                hpi.implementation(),
                HpiImplementation::from_bool(hpi.hpi_implementation())
            );
        });
    }
}