sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
//! R1 response type for SD mode.

use crate::response::{End, Start};
use crate::result::{Error, Result};
use crate::{Crc7, lib_bitfield, response};

mod card_status;

pub use card_status::*;

/// Represents the byte length of the [R1] response in SD mode.
pub const R1_LEN: usize = 6;

lib_bitfield! {
    /// Response sent by every command except `SEND_STATUS` in SD mode.
    pub R1(MSB0 [u8; 6]): u32 {
        raw_start: 47, 40;
        raw_card_status: 39, 8;
        raw_end: 7, 0;
    }
}

response! {
    R1 {
        response_mode: Sd,
    }
}

impl R1 {
    /// Represents the byte length of the [R1] response in SD mode.
    pub const LEN: usize = 6;
    pub const DEFAULT: [u8; Self::LEN] = [0x0, 0x0, 0x0, 0x0, 0x0, 0x1];

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

    /// Gets the [Start] for the [R1].
    pub const fn start(&self) -> Result<Start> {
        Start::try_from_bits(self.raw_start() as u8)
    }

    /// Sets the [Start] for the [R1].
    pub fn set_start(&mut self, start: Start) {
        self.set_raw_start(start.bits() as u32);
    }

    /// Gets the [CardStatus] for the [R1].
    pub const fn card_status(&self) -> Result<CardStatus> {
        CardStatus::try_from_bits(self.raw_card_status())
    }

    /// Sets the [CardStatus] for the [R1].
    pub fn set_card_status(&mut self, card_status: CardStatus) {
        self.set_raw_card_status(card_status.bits());
    }

    /// Gets the [End] for the [R1].
    pub const fn end(&self) -> Result<End> {
        End::try_from_bits(self.raw_end() as u8)
    }

    /// Sets the [End] for the [R1].
    pub fn set_end(&mut self, end: End) {
        self.set_raw_end(end.bits() as u32);
    }

    /// Checks that the CRC-7 field matches the calculated value.
    pub const fn check_crc(&self) -> Result<Crc7> {
        let [r0, r1, r2, r3, r4, r5] = self.0;
        let exp_crc = Crc7::calculate(&[r0, r1, r2, r3, r4]);
        let crc = r5 >> 1;

        if exp_crc.bits() == crc {
            Ok(exp_crc)
        } else {
            Err(Error::invalid_crc7(crc, exp_crc.bits()))
        }
    }

    /// Attempts to convert a [`u8`] into a [R1].
    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)),
            _ => match Self([val[0], val[1], val[2], val[3], val[4], val[5]]) {
                r if r.start().is_err() => Err(Error::invalid_field_variant(
                    "r1::start",
                    r.raw_start() as usize,
                )),
                r if r.card_status().is_err() => Err(Error::invalid_field_variant(
                    "r1::card_status",
                    r.raw_card_status() as usize,
                )),
                r if r.end().is_err() => Err(Error::invalid_field_variant(
                    "r1::end",
                    r.raw_end() as usize,
                )),
                r if r.check_crc().is_err() => Err(Error::invalid_field_variant(
                    "r1::crc",
                    (r.raw_end() >> 1) as usize,
                )),
                r => Ok(r),
            },
        }
    }
}

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

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

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

impl<const N: usize> TryFrom<[u8; N]> for R1 {
    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 R1 {
    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_fields() {
        let new_start = Start::new();
        let new_card_status = CardStatus::new();
        let new_end = End::new();

        (0..=u8::MAX)
            .zip(0..=u8::MAX)
            .zip(0..=u8::MAX)
            .for_each(|((start, cs), end)| {
                (0..4).map(|i| (cs << i) as u32).for_each(|cs| {
                    let [cs0, cs1, cs2, cs3] = cs.to_be_bytes();
                    let raw = [start, cs0, cs1, cs2, cs3, end];
                    let mut exp_r1 = R1(raw);

                    match (
                        exp_r1.start(),
                        exp_r1.card_status(),
                        exp_r1.end(),
                        exp_r1.check_crc(),
                    ) {
                        (Ok(start), Ok(card_status), Ok(end), Ok(crc)) => {
                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Ok(exp_r1));
                            assert_eq!(R1::try_from(raw), Ok(exp_r1));

                            exp_r1.set_start(new_start);
                            assert_eq!(exp_r1.start(), Ok(new_start));

                            exp_r1.set_start(start);
                            assert_eq!(exp_r1.start(), Ok(start));

                            exp_r1.set_card_status(new_card_status);
                            assert_eq!(exp_r1.card_status(), Ok(new_card_status));

                            exp_r1.set_card_status(card_status);
                            assert_eq!(exp_r1.card_status(), Ok(card_status));

                            exp_r1.set_end(new_end);
                            assert_eq!(exp_r1.end(), Ok(new_end));

                            exp_r1.set_end(end);
                            assert_eq!(exp_r1.end(), Ok(end));

                            assert_eq!(exp_r1.check_crc(), Ok(crc));
                        }
                        (Err(_err), _, _, _) => {
                            let exp_err = Error::invalid_field_variant("r1::start", start as usize);
                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
                            assert_eq!(R1::try_from(raw), Err(exp_err));
                        }
                        (_, Err(_err), _, _) => {
                            let exp_err =
                                Error::invalid_field_variant("r1::card_status", cs as usize);
                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
                            assert_eq!(R1::try_from(raw), Err(exp_err));
                        }
                        (_, _, Err(_err), _) => {
                            let exp_err = Error::invalid_field_variant("r1::end", end as usize);
                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
                            assert_eq!(R1::try_from(raw), Err(exp_err));
                        }
                        (_, _, _, Err(_err)) => {
                            let exp_err =
                                Error::invalid_field_variant("r1::crc", (end >> 1) as usize);
                            assert_eq!(R1::try_from_bytes(raw.as_ref()), Err(exp_err));
                            assert_eq!(R1::try_from(raw), Err(exp_err));
                        }
                    }
                });
            });
    }
}