sdmmc-core 0.5.0

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

mod direction;

pub use direction::*;

lib_bitfield! {
    /// Argumentument for CMD44.
    pub Argument(u32): u16 {
        raw_direction: 30;
        raw_extended_address: 29, 24;
        /// Priority task.
        pub priority: 23;
        /// Queued task ID.
        raw_task_id: 20, 16;
        /// Queued number of blocks.
        raw_number_of_blocks: 15, 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 `Queue Task Info` [Direction] for [Argument].
    pub const fn direction(&self) -> Direction {
        Direction::from_bool(self.raw_direction())
    }

    /// Sets the `Queue Task Info` [Direction] for [Argument].
    pub fn set_direction(&mut self, val: Direction) {
        self.set_raw_direction(val.into_bool());
    }

    /// Gets the upper 6-bits address of 38-bit 512B unit address, for `SDUC` cards.
    pub const fn extended_address(&self) -> u8 {
        self.raw_extended_address() as u8
    }

    /// Sets the upper 6-bits address of 38-bit 512B unit address, for `SDUC` cards.
    pub fn set_extended_address(&mut self, val: u8) {
        self.set_raw_extended_address(val as u32);
    }

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

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

    /// Gets the queued number of blocks for the [Argument].
    pub const fn number_of_blocks(&self) -> u16 {
        self.raw_number_of_blocks()
    }

    /// Sets the queued number of blocks for the [Argument].
    pub fn set_number_of_blocks(&mut self, val: u16) {
        self.set_raw_number_of_blocks(val as u32);
    }

    /// 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 mut arg = Argument::new();

        assert_eq!(arg.number_of_blocks(), 0);
        assert_eq!(arg.task_id(), 0);
        assert!(!arg.priority());
        assert_eq!(arg.extended_address(), 0);
        assert_eq!(arg.direction(), Direction::new());

        (1..=u16::BITS)
            .map(|r| ((1u32 << r) - 1) as u16)
            .for_each(|exp_num| {
                arg.set_number_of_blocks(exp_num);
                assert_eq!(arg.number_of_blocks(), exp_num);
            });

        arg.set_number_of_blocks(0);

        (0..=0x1f).for_each(|exp_task_id| {
            arg.set_task_id(exp_task_id);
            assert_eq!(arg.task_id(), exp_task_id);
        });

        arg.set_task_id(0);

        test_field!(arg, priority: 23);

        (0..=0x3f).for_each(|exp_addr| {
            arg.set_extended_address(exp_addr);
            assert_eq!(arg.extended_address(), exp_addr);
        });

        arg.set_extended_address(0);

        [Direction::Write, Direction::Read]
            .into_iter()
            .for_each(|exp_direction| {
                arg.set_direction(exp_direction);
                assert_eq!(arg.direction(), exp_direction);
            });

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

                let raw = raw_arg.to_be_bytes();
                let raw_direction = (raw_arg & (1 << 30)) != 0;
                let exp_direction = Direction::from_bool(raw_direction);
                let exp_addr = ((raw_arg & 0x3f00_0000) >> 24) as u8;
                let exp_priority = (raw_arg & (1 << 23)) != 0;
                let exp_task_id = ((raw_arg & 0x1f_0000) >> 16) as u8;
                let exp_num_blocks = (raw_arg & 0xffff) 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.direction(), exp_direction);
                assert_eq!(exp_arg.extended_address(), exp_addr);
                assert_eq!(exp_arg.priority(), exp_priority);
                assert_eq!(exp_arg.task_id(), exp_task_id);
                assert_eq!(exp_arg.number_of_blocks(), exp_num_blocks);
            });
    }
}