sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
//! Operation Conditions Register (`OCR`).

use crate::lib_bitfield;
use crate::result::{Error, Result};

const OCR_MASK: u32 = 0xe9ff_8000;

lib_bitfield! {
    /// Represents the SD card operation conditions register (`OCR`).
    pub Ocr(u32): bool {
        /// VDD Volatage Window (VDD1): 2.7-2.8V.
        pub vdd_2728: 15;
        /// VDD Volatage Window (VDD1): 2.8-2.9V.
        pub vdd_2829: 16;
        /// VDD Volatage Window (VDD1): 2.9-3.0V.
        pub vdd_2930: 17;
        /// VDD Volatage Window (VDD1): 3.0-3.1V.
        pub vdd_3031: 18;
        /// VDD Volatage Window (VDD1): 3.1-3.2V.
        pub vdd_3132: 19;
        /// VDD Volatage Window (VDD1): 3.2-3.3V.
        pub vdd_3233: 20;
        /// VDD Volatage Window (VDD1): 3.3-3.4V.
        pub vdd_3334: 21;
        /// VDD Volatage Window (VDD1): 3.4-3.5V.
        pub vdd_3435: 22;
        /// VDD Volatage Window (VDD1): 3.5-3.6V.
        pub vdd_3536: 23;
        /// Switching to 1.8V accepted (S18A).
        pub s18a: 24;
        /// Over 2TB support status (CO2T).
        pub co2t: 27;
        /// UHS-II Card Status.
        pub uhs2: 29;
        /// Card Capacity Status (CCS).
        ///
        /// Only valid when the card power up status bit (busy) is set.
        pub ccs: 30;
        /// Card power up status bit (busy).
        ///
        /// Set to low if the card has not finished power up routine.
        pub busy: 31;
    }
}

impl Ocr {
    /// Represents the byte length of the [Ocr].
    pub const LEN: usize = 4;

    /// Creates a new [Ocr].
    pub const fn new() -> Self {
        Self(0)
    }

    /// Converts an [`u32`] into a [Ocr]
    pub const fn from_bits(val: u32) -> Self {
        Self(val & OCR_MASK)
    }

    /// Attempts to convert a byte slice into a [Ocr].
    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
        match val.len() {
            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
            _ => Ok(Self(
                u32::from_be_bytes([val[0], val[1], val[2], val[3]]) & OCR_MASK,
            )),
        }
    }

    /// Converts an [Ocr] into a byte array.
    pub const fn bytes(&self) -> [u8; Self::LEN] {
        self.0.to_be_bytes()
    }
}

impl From<u32> for Ocr {
    fn from(val: u32) -> Self {
        Self::from_bits(val)
    }
}

impl From<Ocr> for u32 {
    fn from(val: Ocr) -> Self {
        val.bits()
    }
}

impl TryFrom<&[u8]> for Ocr {
    type Error = Error;

    fn try_from(val: &[u8]) -> Result<Self> {
        Self::try_from_bytes(val)
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for Ocr {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

impl<const N: usize> TryFrom<[u8; N]> for Ocr {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

impl From<Ocr> for [u8; Ocr::LEN] {
    fn from(val: Ocr) -> Self {
        val.bytes()
    }
}

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

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

    #[test]
    fn test_fields() {
        let mut ocr = Ocr::new();

        test_field!(ocr, vdd_2728: 15);
        test_field!(ocr, vdd_2829: 16);
        test_field!(ocr, vdd_2930: 17);
        test_field!(ocr, vdd_3031: 18);
        test_field!(ocr, vdd_3132: 19);
        test_field!(ocr, vdd_3233: 20);
        test_field!(ocr, vdd_3334: 21);
        test_field!(ocr, vdd_3435: 22);
        test_field!(ocr, vdd_3536: 23);
        test_field!(ocr, s18a: 24);
        test_field!(ocr, co2t: 27);
        test_field!(ocr, uhs2: 29);
        test_field!(ocr, ccs: 30);
        test_field!(ocr, busy: 31);
    }
}