sdmmc-core 0.5.0

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

mod operation_code;

pub use operation_code::*;

lib_bitfield! {
    /// Argumentument for CMD43.
    pub Argument(u32): u8 {
        /// Queued task ID to abort, see [OperationCode].
        raw_task_id: 20, 16;
        raw_operation_code: 3, 0;
    }
}

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

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

    /// Gets the task ID field for the [Argument].
    pub const fn task_id(&self) -> u8 {
        self.raw_task_id()
    }

    /// Sets the task ID field for the [Argument].
    pub fn set_task_id(&mut self, val: u8) {
        self.set_raw_task_id(val as u32);
    }

    /// Gets the `Queue Management` [OperationCode] for [Argument].
    pub const fn operation_code(&self) -> Result<OperationCode> {
        OperationCode::from_raw(self.raw_operation_code())
    }

    /// Sets the `Queue Management` [OperationCode] for [Argument].
    pub fn set_operation_code(&mut self, val: OperationCode) {
        self.set_raw_operation_code(val.into_raw() as u32);
    }

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

    /// 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 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<u32> for Argument {
    type Error = Error;

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

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_op_code = OperationCode::new();

        (1..=u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let raw = raw_arg.to_be_bytes();
                let raw_op_code = (raw_arg & 0xf) as u8;
                let exp_task_id = ((raw_arg & 0x1f_0000) >> 16) as u8;

                match Argument::try_from_bits(raw_arg) {
                    Ok(mut exp_arg) => {
                        assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
                        assert_eq!(Argument::try_from_bytes(raw.as_ref()), Ok(exp_arg));
                        assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
                        assert_eq!(Argument::try_from(raw), Ok(exp_arg));
                        assert_eq!(Argument::try_from(&raw), Ok(exp_arg));

                        let exp_op_code = OperationCode::from_raw(raw_op_code).unwrap();

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

                        assert_eq!(exp_arg.task_id(), exp_task_id);

                        exp_arg.set_task_id(0);
                        assert_eq!(exp_arg.task_id(), 0);

                        exp_arg.set_task_id(exp_task_id);
                        assert_eq!(exp_arg.task_id(), exp_task_id);

                        assert_eq!(exp_arg.operation_code(), Ok(exp_op_code));

                        exp_arg.set_operation_code(new_op_code);
                        assert_eq!(exp_arg.operation_code(), Ok(new_op_code));

                        exp_arg.set_operation_code(exp_op_code);
                        assert_eq!(exp_arg.operation_code(), Ok(exp_op_code));
                    }
                    Err(_) => {
                        let exp_err = Error::invalid_field_variant(
                            "cmd::argument::operation_code",
                            raw_op_code 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_arg), Err(exp_err));
                        assert_eq!(Argument::try_from(raw), Err(exp_err));
                        assert_eq!(Argument::try_from(&raw), Err(exp_err));
                    }
                }
            });
    }
}