sdmmc-core 0.5.0

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

lib_bitfield! {
    /// Represents the argument for the [Cmd1](super::Cmd1) command.
    pub Argument(u32): u32 {
        raw_ocr: 31, 0;
    }
}

impl Argument {
    /// Represents the byte length of the [Argument].
    pub const LEN: usize = 4;
    /// Represents the raw default value of the [Argument].
    pub const DEFAULT: u32 = 0;

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

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

    /// Sets the [Ocr] field of the [Argument].
    pub fn set_ocr(&mut self, mut val: Ocr) {
        val.set_busy(false);
        self.set_raw_ocr(val.bits())
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        match Self(val) {
            arg if arg.ocr().busy() => Err(Error::invalid_field_variant(
                "cmd::arg::ocr",
                arg.raw_ocr() as usize,
            )),
            arg => Ok(arg),
        }
    }

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

    /// Attempts to convert a byte slice into an [Argument].
    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)),
            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
        }
    }
}

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

impl TryFrom<u32> for Argument {
    type Error = Error;

    fn try_from(val: u32) -> Result<Self> {
        Self::try_from_bits(val)
    }
}

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

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

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

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

impl<const N: usize> TryFrom<&[u8; N]> for Argument {
    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 Argument {
    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 mut arg = Argument::new();
        let new_ocr = Ocr::new();

        assert_eq!(arg.ocr(), new_ocr);

        (1..u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let mut exp_arg = Argument(raw_arg);
                let exp_ocr = Ocr::from_bits(raw_arg);
                let raw = raw_arg.to_be_bytes();

                if exp_ocr.busy() {
                    let exp_err = Error::invalid_field_variant("cmd::arg::ocr", raw_arg as usize);

                    assert_eq!(Argument::try_from_bits(raw_arg), Err(exp_err));
                    assert_eq!(Argument::try_from_bytes(&raw), Err(exp_err));
                    assert_eq!(Argument::try_from(&raw), Err(exp_err));
                    assert_eq!(Argument::try_from(raw), Err(exp_err));
                } else {
                    assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
                    assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
                    assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
                    assert_eq!(Argument::try_from(raw), Ok(exp_arg));

                    assert_eq!(exp_arg.bits(), raw_arg);
                    assert_eq!(exp_arg.bytes(), raw);

                    assert_eq!(exp_arg.ocr(), exp_ocr);

                    exp_arg.set_ocr(new_ocr);
                    assert_eq!(exp_arg.ocr(), new_ocr);

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