sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::register::IoOcr;
use crate::response::sdio::r1::R1;
use crate::result::{Error, Result};
use crate::{lib_bitfield, response};

lib_bitfield! {
    /// Represents the response to the `IO_SEND_OP_COND` command in SPI mode.
    pub R4(MSB0 [u8; 5]): u32 {
        /// Modified [R1](crate::response::sdio::r1::R1) response for SDIO mode.
        raw_r1: 39, 32;
        /// Indicates if card is ready to operate after initialization.
        raw_c: 31;
        /// Total number of I/O functions.
        raw_num_io_functions: 30, 28;
        /// Indicates whether the SD card has memory, or is I/O only.
        raw_memory_present: 27;
        /// Stuff bits (unused, shall always be zero).
        raw_stuff: 26, 24;
        /// Gets the raw bits for the [IoOcr](crate::register::IoOcr) register.
        raw_io_ocr: 23, 0;
    }
}

response! {
    R4 {
        response_mode: Spi,
    }
}

impl R4 {
    /// Represents the byte length of the [R4] response in SPI mode.
    pub const LEN: usize = 5;
    /// Represents the default byte value of [R4].
    pub const DEFAULT: [u8; Self::LEN] = [0u8; Self::LEN];

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

    /// Gets the [R1] value of the [R4] response.
    pub const fn r1(&self) -> Result<R1> {
        R1::try_from_bits(self.raw_r1() as u8)
    }

    /// Sets the [R1] value of the [R4] response.
    pub fn set_r1(&mut self, r1: R1) {
        self.set_raw_r1(r1.bits() as u32);
    }

    /// Gets whether the card is ready to operate after initialization.
    pub const fn is_card_ready(&self) -> bool {
        self.raw_c()
    }

    /// Sets whether the card is ready to operate after initialization.
    pub fn set_is_card_ready(&mut self, val: bool) {
        self.set_raw_c(val);
    }

    /// Gets whether the SD card has memory, or is I/O only.
    pub const fn is_memory_present(&self) -> bool {
        self.raw_memory_present()
    }

    /// Sets whether the SD card has memory, or is I/O only.
    pub fn set_is_memory_present(&mut self, val: bool) {
        self.set_raw_memory_present(val)
    }

    /// Gets the number of I/O functions supported by the card.
    pub const fn num_io_functions(&self) -> usize {
        self.raw_num_io_functions() as usize
    }

    /// Sets the number of I/O functions supported by the card.
    ///
    /// **NOTE**: valid range is `0-7`, only values in this range will be set.
    pub fn set_num_io_functions(&mut self, val: usize) {
        self.set_raw_num_io_functions((val & 0x7) as u32)
    }

    /// Gets the [IoOcr] value of the [R4] response.
    pub const fn io_ocr(&self) -> IoOcr {
        IoOcr::from_bits(self.raw_io_ocr())
    }

    /// Sets the [IoOcr] value of the [R4] response.
    pub fn set_io_ocr(&mut self, io_ocr: IoOcr) {
        self.set_raw_io_ocr(io_ocr.bits());
    }

    /// Attempts to convert a byte slice into a [R4].
    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)),
            _ if R1::try_from_bits(val[0]).is_err() => {
                Err(Error::invalid_field_variant("r4::r1", val[0] as usize))
            }
            _ => Ok(Self([val[0], val[1], val[2], val[3], val[4]])),
        }
    }
}

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

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

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

impl<const N: usize> TryFrom<[u8; N]> for R4 {
    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 R4 {
    type Error = Error;

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

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

    #[test]
    fn test_valid() {
        (1..=40u64)
            .map(|r| (1u64 << r) - 1)
            .for_each(|raw_bitfield| {
                let [_, _, _, b0, b1, b2, b3, b4] = raw_bitfield.to_be_bytes();

                let b0 = b0 & !R1::MASK;
                let raw_bytes = [b0, b1, b2, b3, b4];

                let exp_r1 = R1::try_from_bits(b0).unwrap();
                let exp_card_ready = (b1 & 0x80) != 0;
                let exp_num_io = ((b1 & 0x70) >> 4) as usize;
                let exp_mem = (b1 & 0x8) != 0;
                let exp_io_ocr = IoOcr::try_from_bytes(&[b2, b3, b4]).unwrap();

                let mut exp_r4 = R4(raw_bytes);

                assert_eq!(R4::try_from_bytes(raw_bytes.as_ref()), Ok(exp_r4));

                assert_eq!(exp_r4.r1(), Ok(exp_r1));

                exp_r4.set_r1(R1::new());
                assert_eq!(exp_r4.r1(), Ok(R1::new()));

                exp_r4.set_r1(exp_r1);
                assert_eq!(exp_r4.r1(), Ok(exp_r1));

                assert_eq!(exp_r4.is_card_ready(), exp_card_ready);

                exp_r4.set_is_card_ready(!exp_card_ready);
                assert_eq!(exp_r4.is_card_ready(), !exp_card_ready);

                exp_r4.set_is_card_ready(exp_card_ready);
                assert_eq!(exp_r4.is_card_ready(), exp_card_ready);

                assert_eq!(exp_r4.num_io_functions(), exp_num_io, "{:#x}", raw_bitfield);

                exp_r4.set_num_io_functions(0);
                assert_eq!(exp_r4.num_io_functions(), 0);

                exp_r4.set_num_io_functions(exp_num_io);
                assert_eq!(exp_r4.num_io_functions(), exp_num_io);

                assert_eq!(exp_r4.is_memory_present(), exp_mem);

                exp_r4.set_is_memory_present(!exp_mem);
                assert_eq!(exp_r4.is_memory_present(), !exp_mem);

                exp_r4.set_is_memory_present(exp_mem);
                assert_eq!(exp_r4.is_memory_present(), exp_mem);

                assert_eq!(exp_r4.io_ocr(), exp_io_ocr);

                exp_r4.set_io_ocr(IoOcr::new());
                assert_eq!(exp_r4.io_ocr(), IoOcr::new());

                exp_r4.set_io_ocr(exp_io_ocr);
                assert_eq!(exp_r4.io_ocr(), exp_io_ocr);
            });
    }
}