sdmmc-core 0.5.0

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

pub use crate::command::class9::cmd52::ReadWrite;

mod block_mode;
mod count;
mod op_code;

pub use block_mode::*;
pub use count::*;
pub use op_code::*;

lib_bitfield! {
    /// Argumentument for CMD53.
    pub Argument(u32): u32 {
        raw_rw: 31;
        raw_fno: 30, 28;
        raw_block_mode: 27;
        raw_op_code: 26;
        raw_addr: 25, 9;
        raw_count: 8, 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 = 0;

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

    /// Gets the `rw` field of the [Argument].
    pub const fn rw(&self) -> ReadWrite {
        ReadWrite::from_bool(self.raw_rw())
    }

    /// Sets the `rw` field of the [Argument].
    pub fn set_rw(&mut self, val: ReadWrite) {
        self.set_raw_rw(val.into_bool());
    }

    /// Gets the `fno` field of the [Argument].
    pub const fn fno(&self) -> u8 {
        self.raw_fno() as u8
    }

    /// Sets the `fno` field of the [Argument].
    pub fn set_fno(&mut self, val: u8) {
        self.set_raw_fno(val as u32);
    }

    /// Gets the `block_mode` field of the [Argument].
    pub const fn block_mode(&self) -> BlockMode {
        BlockMode::from_bool(self.raw_block_mode())
    }

    /// Sets the `block_mode` field of the [Argument].
    pub fn set_block_mode(&mut self, val: BlockMode) {
        self.set_raw_block_mode(val.into_bool());
    }

    /// Gets the `op_code` field of the [Argument].
    pub const fn op_code(&self) -> OpCode {
        OpCode::from_bool(self.raw_op_code())
    }

    /// Sets the `op_code` field of the [Argument].
    pub fn set_op_code(&mut self, val: OpCode) {
        self.set_raw_op_code(val.into_bool());
    }

    /// Gets the `addr` field for the [Argument].
    pub const fn addr(&self) -> u32 {
        self.raw_addr()
    }

    /// Sets the `addr` field for the [Argument].
    pub fn set_addr(&mut self, val: u32) {
        self.set_raw_addr(val);
    }

    /// Gets the `data` field of the [Argument].
    pub const fn count(&self) -> Count {
        Count::from_parts_unchecked(self.block_mode(), self.raw_count() as u16)
    }

    /// Sets the `count` field of the [Argument].
    ///
    /// # Note
    ///
    /// Also sets the `block_mode` field.
    pub fn set_count(&mut self, val: Count) {
        let (block_mode, count) = val.into_parts();

        self.set_raw_count(count as u32);
        self.set_block_mode(block_mode);
    }

    /// Attempts to convert a [`u32`] into an [`Argument`].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self(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)),
            _ => 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_rw = ReadWrite::new();
        let new_count = Count::new();
        let (new_block_mode, _) = new_count.into_parts();
        let new_op_code = OpCode::new();

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

                let exp_rw = ReadWrite::from_bool((raw_arg & (1 << 31)) != 0);
                let exp_fno = ((raw_arg & 0x7000_0000) >> 28) as u8;
                let exp_block_mode = BlockMode::from_bool((raw_arg & (1 << 27)) != 0);
                let exp_op_code = OpCode::from_bool((raw_arg & (1 << 26)) != 0);
                let exp_addr = (raw_arg & 0x3fffe00) >> 9;
                let exp_count =
                    Count::from_parts_unchecked(exp_block_mode, (raw_arg & 0x1ff) as u16);

                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_arg), 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.addr(), exp_addr);

                assert_eq!(exp_arg.rw(), exp_rw);
                assert_eq!(exp_arg.fno(), exp_fno);
                assert_eq!(exp_arg.block_mode(), exp_block_mode);
                assert_eq!(exp_arg.addr(), exp_addr);
                assert_eq!(exp_arg.count(), exp_count);

                exp_arg.set_rw(new_rw);
                assert_eq!(exp_arg.rw(), new_rw);

                exp_arg.set_rw(exp_rw);
                assert_eq!(exp_arg.rw(), exp_rw);

                exp_arg.set_fno(0);
                assert_eq!(exp_arg.fno(), 0);

                exp_arg.set_fno(exp_fno);
                assert_eq!(exp_arg.fno(), exp_fno);

                exp_arg.set_block_mode(new_block_mode);
                assert_eq!(exp_arg.block_mode(), new_block_mode);

                exp_arg.set_block_mode(exp_block_mode);
                assert_eq!(exp_arg.block_mode(), exp_block_mode);

                exp_arg.set_op_code(new_op_code);
                assert_eq!(exp_arg.op_code(), new_op_code);

                exp_arg.set_op_code(exp_op_code);
                assert_eq!(exp_arg.op_code(), exp_op_code);

                exp_arg.set_addr(0);
                assert_eq!(exp_arg.addr(), 0);

                exp_arg.set_addr(exp_addr);
                assert_eq!(exp_arg.addr(), exp_addr);

                exp_arg.set_count(new_count);
                assert_eq!(exp_arg.count(), new_count);
                assert_eq!(exp_arg.block_mode(), new_block_mode);

                exp_arg.set_count(exp_count);
                assert_eq!(exp_arg.count(), exp_count);
                assert_eq!(exp_arg.block_mode(), exp_block_mode);
            });
    }
}