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

lib_bitfield! {
    /// Represents the `HPI_MGMT` field of the [ExtCsd] register.
    pub HpiManagement(u8): u8 {
        /// Indicates if the HPI mechanism is enabled.
        pub hpi: 0;
    }
}

impl HpiManagement {
    /// Represents the bitmask of the [HpiManagement].
    pub const MASK: u8 = 0b1;
    /// Represents the default byte value of the [HpiManagement].
    pub const DEFAULT: u8 = 0b0;

    /// Creates a new [HpiManagement].
    pub const fn new() -> Self {
        Self(Self::DEFAULT)
    }

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

    /// Attempts to convert an inner representation into a [HpiManagement].
    pub const fn try_from_inner(val: u8) -> Result<Self> {
        Ok(Self::from_inner(val))
    }

    /// Converts a [HpiManagement] into an inner representation.
    pub const fn into_inner(self) -> u8 {
        self.0
    }
}

impl Default for HpiManagement {
    fn default() -> Self {
        Self::new()
    }
}

impl From<bool> for HpiManagement {
    fn from(val: bool) -> Self {
        Self::from_inner(val as u8)
    }
}

impl From<HpiManagement> for bool {
    fn from(val: HpiManagement) -> Self {
        val.into_inner() != 0
    }
}

impl From<u8> for HpiManagement {
    fn from(val: u8) -> Self {
        Self::from_inner(val)
    }
}

impl From<HpiManagement> for u8 {
    fn from(val: HpiManagement) -> Self {
        val.into_inner()
    }
}

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

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

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

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

        assert_eq!(ext_csd.hpi_mgmt(), HpiManagement::new());

        (0..=u8::MAX)
            .map(HpiManagement::from_inner)
            .for_each(|hpi| {
                ext_csd.set_hpi_mgmt(hpi);
                assert_eq!(ext_csd.hpi_mgmt(), hpi);
            });
    }
}