sdmmc-core 0.5.0

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

lib_bitfield! {
    /// Represents the response to the `READ_OCR` command.
    pub R3(MSB0 [u8; 5]): u32 {
        raw_r1: 39, 32;
        raw_ocr: 31, 0;
    }
}

response! {
    R3 {
        response_mode: Spi,
    }
}

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

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

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

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

    /// Gets the [Ocr] value of the [R3] response.
    pub const fn ocr(&self) -> Ocr {
        Ocr::from_bits(self.raw_ocr())
    }

    /// Sets the `OCR` value of the [R3] response.
    pub fn set_ocr(&mut self, ocr: Ocr) {
        self.set_raw_ocr(ocr.bits());
    }

    /// Attempts to convert a byte slice into a [R3].
    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("r3::r1", val[0] as usize))
            }
            _ => Ok(Self([val[0], val[1], val[2], val[3], val[4]])),
        }
    }
}

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

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

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

impl<const N: usize> TryFrom<[u8; N]> for R3 {
    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 R3 {
    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..=7).map(|r| ((1u16 << r) - 1) as u8).for_each(|raw_r1| {
            (1..=u32::BITS)
                .map(|r| ((1u64 << r) - 1) as u32)
                .for_each(|raw_ocr| {
                    let [ocr0, ocr1, ocr2, ocr3] = raw_ocr.to_be_bytes();

                    let raw_bytes = [raw_r1, ocr0, ocr1, ocr2, ocr3];
                    let exp_r1 = R1::try_from_bits(raw_r1).unwrap();
                    let exp_ocr = Ocr::from_bits(raw_ocr);
                    let mut exp_r3 = R3(raw_bytes);

                    assert_eq!(R3::try_from_bytes(raw_bytes.as_ref()), Ok(exp_r3));
                    assert_eq!(exp_r3.r1(), Ok(exp_r1));
                    assert_eq!(exp_r3.ocr(), exp_ocr);

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

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

                    exp_r3.set_ocr(Ocr::new());
                    assert_eq!(exp_r3.ocr(), Ocr::new());

                    exp_r3.set_ocr(exp_ocr);
                    assert_eq!(exp_r3.ocr(), exp_ocr);
                });
        });
    }

    #[test]
    fn test_invalid() {
        (1..=7)
            .map(|r| (((1u16 << r) - 1) as u8) | 0x80)
            .for_each(|raw_r1| {
                let exp_err = Err(Error::invalid_field_variant("r3::r1", raw_r1 as usize));

                (1..=u32::BITS)
                    .map(|r| ((1u64 << r) - 1) as u32)
                    .for_each(|raw_ocr| {
                        let [ocr0, ocr1, ocr2, ocr3] = raw_ocr.to_be_bytes();

                        let raw_bytes = [raw_r1, ocr0, ocr1, ocr2, ocr3];

                        assert_eq!(R3::try_from_bytes(raw_bytes.as_ref()), exp_err);
                    });
            });
    }
}