sdmmc-core 0.5.0

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

mod xpc;

pub use xpc::*;

lib_bitfield! {
    /// Argumentument for CMD5.
    pub Argument(u32): u32 {
        /// If set, requests the card's [Ocr](crate::register::ocr::Ocr) contents in the response.
        pub hcs: 30;
        raw_xpc: 28;
        /// Switching 1.8V request.
        pub s18r: 24;
        /// VDD Volatage Window (VDD1): 3.5-3.6V.
        pub vdd_3536: 23;
        /// VDD Volatage Window (VDD1): 3.4-3.5V.
        pub vdd_3435: 22;
        /// VDD Volatage Window (VDD1): 3.3-3.4V.
        pub vdd_3334: 21;
        /// VDD Volatage Window (VDD1): 3.2-3.3V.
        pub vdd_3233: 20;
        /// VDD Volatage Window (VDD1): 3.1-3.2V.
        pub vdd_3132: 19;
        /// VDD Volatage Window (VDD1): 3.0-3.1V.
        pub vdd_3031: 18;
        /// VDD Volatage Window (VDD1): 2.9-3.0V.
        pub vdd_2930: 17;
        /// VDD Volatage Window (VDD1): 2.8-2.9V.
        pub vdd_2829: 16;
        /// VDD Volatage Window (VDD1): 2.7-2.8V.
        pub vdd_2728: 15;
    }
}

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 `xpc` field of the [Argument].
    pub const fn xpc(&self) -> Xpc {
        Xpc::from_bool(self.raw_xpc())
    }

    /// Sets the `xpc` field of the [Argument].
    pub fn set_xpc(&mut self, val: Xpc) {
        self.set_raw_xpc(val.into_bool());
    }

    /// Converts a [`u32`] into an [Argument].
    pub const fn from_bits(val: u32) -> Self {
        Self(val)
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self::from_bits(val))
    }

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

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

impl From<u32> for Argument {
    fn from(val: u32) -> Self {
        Self::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::*;
    use crate::test_field;

    #[test]
    fn test_fields() {
        let mut arg = Argument::new();
        let new_xpc = Xpc::new();

        test_field!(arg, hcs: 30);
        test_field!(arg, s18r: 24);
        test_field!(arg, vdd_3536: 23);
        test_field!(arg, vdd_3435: 22);
        test_field!(arg, vdd_3334: 21);
        test_field!(arg, vdd_3233: 20);
        test_field!(arg, vdd_3132: 19);
        test_field!(arg, vdd_3031: 18);
        test_field!(arg, vdd_2930: 17);
        test_field!(arg, vdd_2829: 16);
        test_field!(arg, vdd_2728: 15);

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

                let exp_hcs = (raw_arg & (1 << 30)) != 0;
                let exp_xpc = Xpc::from_bool((raw_arg & (1 << 28)) != 0);
                let exp_s18r = (raw_arg & (1 << 24)) != 0;
                let exp_vdd_3536 = (raw_arg & (1 << 23)) != 0;
                let exp_vdd_3435 = (raw_arg & (1 << 22)) != 0;
                let exp_vdd_3334 = (raw_arg & (1 << 21)) != 0;
                let exp_vdd_3233 = (raw_arg & (1 << 20)) != 0;
                let exp_vdd_3132 = (raw_arg & (1 << 19)) != 0;
                let exp_vdd_3031 = (raw_arg & (1 << 18)) != 0;
                let exp_vdd_2930 = (raw_arg & (1 << 17)) != 0;
                let exp_vdd_2829 = (raw_arg & (1 << 16)) != 0;
                let exp_vdd_2728 = (raw_arg & (1 << 15)) != 0;

                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
                assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
                assert_eq!(Argument::from(raw_arg), exp_arg);
                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));

                assert_eq!(exp_arg.hcs(), exp_hcs);
                assert_eq!(exp_arg.xpc(), exp_xpc);
                assert_eq!(exp_arg.s18r(), exp_s18r);
                assert_eq!(exp_arg.vdd_3536(), exp_vdd_3536);
                assert_eq!(exp_arg.vdd_3435(), exp_vdd_3435);
                assert_eq!(exp_arg.vdd_3334(), exp_vdd_3334);
                assert_eq!(exp_arg.vdd_3233(), exp_vdd_3233);
                assert_eq!(exp_arg.vdd_3132(), exp_vdd_3132);
                assert_eq!(exp_arg.vdd_3031(), exp_vdd_3031);
                assert_eq!(exp_arg.vdd_2930(), exp_vdd_2930);
                assert_eq!(exp_arg.vdd_2829(), exp_vdd_2829);
                assert_eq!(exp_arg.vdd_2728(), exp_vdd_2728);

                test_field!(exp_arg, hcs: 30);
                test_field!(exp_arg, s18r: 24);
                test_field!(exp_arg, vdd_3536: 23);
                test_field!(exp_arg, vdd_3435: 22);
                test_field!(exp_arg, vdd_3334: 21);
                test_field!(exp_arg, vdd_3233: 20);
                test_field!(exp_arg, vdd_3132: 19);
                test_field!(exp_arg, vdd_3031: 18);
                test_field!(exp_arg, vdd_2930: 17);
                test_field!(exp_arg, vdd_2829: 16);
                test_field!(exp_arg, vdd_2728: 15);

                exp_arg.set_xpc(new_xpc);
                assert_eq!(exp_arg.xpc(), new_xpc);

                exp_arg.set_xpc(exp_xpc);
                assert_eq!(exp_arg.xpc(), exp_xpc);
            });
    }
}