sdmmc-core 0.5.0

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

lib_bitfield! {
    /// Argumentument for CMD38.
    pub Argument(u32): u16 {
        /// Secure request.
        ///
        /// Only supported if the `SEC_ER_EN` bit is set.
        pub secure: 31;
        reserved0: 30, 16;
        /// Force garbage collect request.
        ///
        /// Only supported if `SEC_GB_CL_END` is set.
        pub force_garbage_collect: 15;
        reserved1: 14, 2;
        /// Discard enable.
        discard_enable: 1;
        /// Identify write blocks for erase (TRIM enable).
        trim_enable: 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 whether the `DISCARD` function is configured.
    ///
    /// This is true when `TRIM` is disabled, and `DISCARD` is enabled.
    #[inline]
    pub const fn discard(&self) -> bool {
        !self.trim_enable() && self.discard_enable()
    }

    /// Gets whether the `DISCARD` function is configured.
    ///
    /// This also clears the `TRIM` bit if `val` is true.
    pub fn set_discard(&mut self, val: bool) {
        self.set_discard_enable(val);

        let trim = if val { !val } else { self.trim_enable() };

        self.set_trim_enable(trim);
    }

    /// Gets whether the `TRIM` function is configured.
    ///
    /// This is true when `TRIM` is enabled, and `DISCARD` is enabled.
    #[inline]
    pub const fn trim(&self) -> bool {
        self.trim_enable() && self.discard_enable()
    }

    /// Sets whether the `TRIM` function is configured.
    ///
    /// This also sets the `DISCARD` bit if `val` is true.
    pub fn set_trim(&mut self, val: bool) {
        self.set_trim_enable(val);

        let discard = if val { val } else { self.discard_enable() };

        self.set_discard_enable(discard);
    }

    /// Gets whether the `ERASE` function is configured.
    ///
    /// This is true when all [Argument] bits are zero.
    pub const fn erase(&self) -> bool {
        self.0 == Self::DEFAULT
    }

    /// Gets the `ERASE` function for the `CMD38` [Argument].
    pub fn set_erase(&mut self) {
        self.0 = Self::DEFAULT;
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        match Self(val) {
            a if a.reserved0() != 0 || a.reserved1() != 0 => Err(Error::invalid_field_variant(
                "cmd::argument::reserved",
                val 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() {
        (1..=u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw_arg| {
                let raw = raw_arg.to_be_bytes();
                let exp_addr = raw_arg;

                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_secure = (raw_arg & (1 << 31)) != 0;
                        let exp_force_gc = (raw_arg & (1 << 15)) != 0;

                        let exp_discard_en = (raw_arg & (1 << 1)) != 0;
                        let exp_trim_en = (raw_arg & (1 << 0)) != 0;

                        let exp_discard = exp_discard_en && !exp_trim_en;
                        let exp_trim = exp_discard_en && exp_trim_en;

                        let exp_erase = raw_arg == Argument::DEFAULT;

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

                        assert_eq!(exp_arg.erase(), exp_erase);

                        assert_eq!(exp_arg.secure(), exp_secure);
                        test_field!(exp_arg, secure: 31);

                        assert_eq!(exp_arg.force_garbage_collect(), exp_force_gc);
                        test_field!(exp_arg, force_garbage_collect: 15);

                        assert_eq!(exp_arg.discard(), exp_discard);
                        assert_eq!(exp_arg.discard_enable(), exp_discard_en);

                        assert_eq!(exp_arg.trim(), exp_trim);
                        assert_eq!(exp_arg.trim_enable(), exp_trim_en);

                        exp_arg.set_discard(true);
                        assert!(exp_arg.discard());
                        assert!(!exp_arg.trim_enable());

                        exp_arg.set_discard(false);
                        assert!(!exp_arg.discard());
                        assert!(!exp_arg.trim_enable());

                        exp_arg.set_trim(true);
                        assert!(exp_arg.trim());
                        assert!(exp_arg.discard_enable());

                        exp_arg.set_trim(false);
                        assert!(!exp_arg.trim());
                        assert!(exp_arg.discard_enable());

                        exp_arg.set_erase();
                        assert_eq!(exp_arg.bits(), Argument::DEFAULT);
                    }
                    Err(_) => {
                        let exp_err = Error::invalid_field_variant(
                            "cmd::argument::reserved",
                            raw_arg 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));
                    }
                }
            });
    }
}