sdmmc-core 0.5.0

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

lib_bitfield! {
    /// Argumentument for CMD5.
    pub Argument(u32): u32 {
        /// Switching 1.8V request.
        pub s18r: 24;
        raw_io_ocr: 23, 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 = 1;

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

    /// Gets the I/O Operation Conditions Register ([IoOcr]) value.
    pub const fn io_ocr(&self) -> IoOcr {
        IoOcr::from_bits(self.raw_io_ocr())
    }

    /// Sets the I/O Operation Conditions Register ([IoOcr]) value.
    pub fn set_io_ocr(&mut self, val: IoOcr) {
        self.set_raw_io_ocr(val.bits());
    }

    /// 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 mut io_ocr = arg.io_ocr();

        test_field!(io_ocr, vdd_2021: 8);
        test_field!(io_ocr, vdd_2122: 9);
        test_field!(io_ocr, vdd_2223: 10);
        test_field!(io_ocr, vdd_2324: 11);
        test_field!(io_ocr, vdd_2425: 12);
        test_field!(io_ocr, vdd_2526: 13);
        test_field!(io_ocr, vdd_2627: 14);
        test_field!(io_ocr, vdd_2728: 15);
        test_field!(io_ocr, vdd_2829: 16);
        test_field!(io_ocr, vdd_2930: 17);
        test_field!(io_ocr, vdd_3031: 18);
        test_field!(io_ocr, vdd_3132: 19);
        test_field!(io_ocr, vdd_3233: 20);
        test_field!(io_ocr, vdd_3334: 21);
        test_field!(io_ocr, vdd_3435: 22);
        test_field!(io_ocr, vdd_3536: 23);

        arg.set_io_ocr(io_ocr);
        assert_eq!(arg.io_ocr(), io_ocr);

        test_field!(arg, s18r: 24);
    }
}