sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
#![allow(clippy::len_without_is_empty)]
#![allow(clippy::manual_range_contains)]

use crate::lib_bitfield;
use crate::result::{Error, Result};
use crate::util;

pub use crate::command::class11::cmd48::Mio;
pub use crate::command::class11::cmd58::{BlockUnitCount, BlockUnitSelect};

lib_bitfield! {
    /// Argumentument for CMD59.
    pub Argument(u32): u32 {
        raw_mio: 31;
        raw_fno: 30, 27;
        raw_bus: 26;
        raw_addr: 25, 9;
        raw_buc: 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 `mio` field of the [Argument].
    pub const fn mio(&self) -> Mio {
        Mio::from_bool(self.raw_mio())
    }

    /// Sets the `mio` field of the [Argument].
    pub fn set_mio(&mut self, val: Mio) {
        self.set_raw_mio(val.into_bool());
    }

    /// Gets the `bus` field of the [Argument].
    pub const fn bus(&self) -> BlockUnitSelect {
        BlockUnitSelect::from_bool(self.raw_bus())
    }

    /// Sets the `bus` field of the [Argument].
    pub fn set_bus(&mut self, val: BlockUnitSelect) {
        self.set_raw_bus(val.into_bool());
    }

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

    /// Sets the `fno` field of the [Argument].
    ///
    /// # Note
    ///
    /// The function number range depends on the [Mio] field:
    ///
    /// - [Mio::Memory]: `0 - 15`
    /// - [Mio::Io]: `0 - 7`
    pub fn set_fno(&mut self, val: u8) {
        self.set_raw_fno(util::fno_masked(self.mio(), val, true) as u32);
    }

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

    /// Sets the `addr` field for the [Argument].
    ///
    /// # Note
    ///
    /// Also sets the `mio` field based on the address space.
    pub fn set_addr(&mut self, val: u32) {
        self.set_raw_addr(val);
    }

    /// Gets the `buc` field of the [Argument].
    pub const fn buc(&self) -> BlockUnitCount {
        BlockUnitCount::from_bits_unchecked(self.raw_buc() as u16)
    }

    /// Sets the `buc` field of the [Argument].
    pub fn set_buc(&mut self, val: BlockUnitCount) {
        self.set_raw_buc(val.bits() 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 new_mio = Mio::new();
        let new_buc = BlockUnitCount::new();
        let new_bus = BlockUnitSelect::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_mio = Mio::from_bool((raw_arg & 0x8000_0000) != 0);

                let raw_fno = ((raw_arg & 0x7800_0000) >> 27) as u8;
                let exp_fno = util::fno_masked(exp_mio, raw_fno, false);

                let exp_bus = BlockUnitSelect::from_bool((raw_arg & (1 << 26)) != 0);

                let exp_addr = ((raw_arg & 0x3fffe00) >> 9);

                let exp_buc = BlockUnitCount::from_bits_unchecked((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.mio(), exp_mio);

                exp_arg.set_mio(new_mio);
                assert_eq!(exp_arg.mio(), new_mio);

                exp_arg.set_mio(exp_mio);
                assert_eq!(exp_arg.mio(), exp_mio);

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

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

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

                assert_eq!(exp_arg.bus(), exp_bus);

                exp_arg.set_bus(new_bus);
                assert_eq!(exp_arg.bus(), new_bus);

                exp_arg.set_bus(exp_bus);
                assert_eq!(exp_arg.bus(), exp_bus);

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

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

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

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

                assert_eq!(exp_arg.buc(), exp_buc);

                exp_arg.set_buc(new_buc);
                assert_eq!(exp_arg.buc(), new_buc);

                exp_arg.set_buc(exp_buc);
                assert_eq!(exp_arg.buc(), exp_buc);
            });
    }
}