sdmmc-core 0.5.0

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

lib_bitfield! {
    /// Represents the response to the `IO_SEND_OP_COND` command.
    pub R4(MSB0 [u8; 6]): u32 {
        raw_start_bit: 47;
        raw_direction: 46;
        raw_c: 39;
        raw_num_io_functions: 38, 36;
        raw_memory_present: 35;
        raw_stuff: 34, 33;
        raw_s18a: 32;
        raw_io_ocr: 31, 8;
        raw_end_bit: 0;
    }
}

response! {
    R4 {
        response_mode: Sd,
    }
}

impl R4 {
    /// Represents the byte length of the [R4] response.
    pub const LEN: usize = 6;
    pub const DEFAULT: [u8; Self::LEN] = [0x3f, 0, 0, 0, 0, 0xff];

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

    /// 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 whether the switch to 1.8V power is accepted.
    pub const fn is_s18a(&self) -> bool {
        self.raw_s18a()
    }

    /// Sets whether the switch to 1.8V power is accepted.
    pub fn set_is_s18a(&mut self, val: bool) {
        self.set_raw_s18a(val);
    }

    /// 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)),
            _ => Ok(Self([val[0], val[1], val[2], val[3], val[4], val[5]])),
        }
    }
}

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

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

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..=46u64)
            .map(|r| (1u64 << r) - 1)
            .for_each(|raw_bitfield| {
                let [_, _, b0, b1, b2, b3, b4, b5] = raw_bitfield.to_be_bytes();
                let raw_bytes = [b0, b1, b2, b3, b4, b5];

                let exp_card_ready = (b1 & 0x80) != 0;
                let exp_num_io = ((b1 & 0x70) >> 4) as usize;
                let exp_mem = (b1 & 0x8) != 0;
                let exp_s18a = (b1 & 0x1) != 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.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.is_s18a(), exp_s18a);

                exp_r4.set_is_s18a(!exp_s18a);
                assert_eq!(exp_r4.is_s18a(), !exp_s18a);

                exp_r4.set_is_s18a(exp_s18a);
                assert_eq!(exp_r4.is_s18a(), exp_s18a);

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