sdmmc-core 0.5.0

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

mod access;

pub use access::*;

lib_bitfield! {
    /// Argument for [Cmd6](super::Cmd6).
    pub Argument(u32): u8 {
        raw_access: 25, 24;
        raw_index: 23, 16;
        raw_value: 15, 8;
        raw_command_set: 2, 0;
    }
}

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

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

    /// Gets the `EXT_CSD` [Access] mode for the [Argument].
    pub const fn access(&self) -> Access {
        Access::from_raw_unchecked(self.raw_access())
    }

    /// Sets the `EXT_CSD` [Access] mode for the [Argument].
    pub fn set_access(&mut self, val: Access) {
        self.set_raw_access(val.into_raw() as u32);
    }

    /// Gets the `EXT_CSD` byte index  for the [Argument].
    pub const fn index(&self) -> usize {
        self.raw_index() as usize
    }

    /// Sets the `EXT_CSD` byte index  for the [Argument].
    pub fn set_index(&mut self, val: usize) {
        self.set_raw_index(val as u32);
    }

    /// Gets the `EXT_CSD` byte value  for the [Argument].
    pub const fn value(&self) -> u8 {
        self.raw_value()
    }

    /// Sets the `EXT_CSD` byte value  for the [Argument].
    pub fn set_value(&mut self, val: u8) {
        self.set_raw_value(val as u32);
    }

    /// Gets the command set selected by the [Argument].
    pub const fn command_set(&self) -> Result<MmcCommandSet> {
        MmcCommandSet::try_from_inner(self.raw_command_set())
    }

    /// Sets the command set selected by the [Argument].
    pub fn set_command_set(&mut self, val: MmcCommandSet) {
        self.set_raw_command_set(val.into_inner() as u32);
    }

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

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

    /// 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 new_access = Access::new();
        let new_command_set = MmcCommandSet::new();
        let new_index = 0;
        let new_value = 0;

        (1..u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let mut exp_arg = Argument(raw_arg);
                let exp_access = Access::from_raw_unchecked(((raw_arg >> 24) & 0b11) as u8);
                let exp_index = ((raw_arg >> 16) & 0xff) as usize;
                let exp_value = ((raw_arg >> 8) & 0xff) as u8;
                let raw_command_set = (raw_arg & 0b111) as u8;
                let raw = raw_arg.to_be_bytes();

                match MmcCommandSet::try_from_inner(raw_command_set) {
                    Ok(exp_command_set) => {
                        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.access(), exp_access);

                        exp_arg.set_access(new_access);
                        assert_eq!(exp_arg.access(), new_access);

                        exp_arg.set_access(exp_access);
                        assert_eq!(exp_arg.access(), exp_access);

                        assert_eq!(exp_arg.index(), exp_index);

                        exp_arg.set_index(new_index);
                        assert_eq!(exp_arg.index(), new_index);

                        exp_arg.set_index(exp_index);
                        assert_eq!(exp_arg.index(), exp_index);

                        assert_eq!(exp_arg.value(), exp_value);

                        exp_arg.set_value(new_value);
                        assert_eq!(exp_arg.value(), new_value);

                        exp_arg.set_value(exp_value);
                        assert_eq!(exp_arg.value(), exp_value);

                        assert_eq!(exp_arg.command_set(), Ok(exp_command_set));

                        exp_arg.set_command_set(new_command_set);
                        assert_eq!(exp_arg.command_set(), Ok(new_command_set));

                        exp_arg.set_command_set(exp_command_set);
                        assert_eq!(exp_arg.command_set(), Ok(exp_command_set));
                    }
                    Err(err) => {
                        let exp_err = Error::invalid_field_variant(
                            "cmd6::arg::command_set",
                            raw_command_set as usize,
                        );

                        assert_eq!(Argument::try_from_bits(raw_arg), Err(exp_err));
                        assert_eq!(Argument::try_from_bytes(&raw), Err(exp_err));
                    }
                }
            });
    }
}