sdmmc-core 0.5.0

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

mod access_mode;
mod command_system;
mod driver_strength;
mod mode;
mod power_limit;

pub use access_mode::*;
pub use command_system::*;
pub use driver_strength::*;
pub use mode::*;
pub use power_limit::*;

lib_bitfield! {
    /// Argumentument for CMD6.
    pub Argument(u32): u8 {
        raw_mode: 31;
        raw_power_limit: 15, 12;
        raw_driver_strength: 11, 8;
        raw_command_system: 7, 4;
        raw_access_mode: 3, 0;
    }
}

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

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

    /// Gets the [AccessMode] for the `CMD6` [Argument].
    pub const fn access_mode(&self) -> Result<AccessMode> {
        AccessMode::from_raw(self.raw_access_mode())
    }

    /// Sets the [AccessMode] for the `CMD6` [Argument].
    pub fn set_access_mode(&mut self, val: AccessMode) {
        self.set_raw_access_mode(val.into_raw() as u32);
    }

    /// Gets the [CommandSystem] for the `CMD6` [Argument].
    pub const fn command_system(&self) -> Result<CommandSystem> {
        CommandSystem::from_raw(self.raw_command_system())
    }

    /// Sets the [CommandSystem] for the `CMD6` [Argument].
    pub fn set_command_system(&mut self, val: CommandSystem) {
        self.set_raw_command_system(val.into_raw() as u32);
    }

    /// Gets the [DriverStrength] for the `CMD6` [Argument].
    pub const fn driver_strength(&self) -> Result<DriverStrength> {
        DriverStrength::from_raw(self.raw_driver_strength())
    }

    /// Sets the [DriverStrength] for the `CMD6` [Argument].
    pub fn set_driver_strength(&mut self, val: DriverStrength) {
        self.set_raw_driver_strength(val.into_raw() as u32);
    }

    /// Gets the [PowerLimit] for the `CMD6` [Argument].
    pub const fn power_limit(&self) -> Result<PowerLimit> {
        PowerLimit::from_raw(self.raw_power_limit())
    }

    /// Sets the [PowerLimit] for the `CMD6` [Argument].
    pub fn set_power_limit(&mut self, val: PowerLimit) {
        self.set_raw_power_limit(val.into_raw() as u32)
    }

    /// Gets the [Mode] for the `CMD6` [Argument].
    pub const fn mode(&self) -> Result<Mode> {
        Mode::from_raw(self.raw_mode() as u8)
    }

    /// Sets the [Mode] for the `CMD6` [Argument].
    pub fn set_mode(&mut self, val: Mode) {
        self.set_raw_mode(val.into_raw() != 0);
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        match Self(val) {
            arg if arg.access_mode().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::access_mode",
                arg.raw_access_mode() as usize,
            )),
            arg if arg.command_system().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::command_system",
                arg.raw_command_system() as usize,
            )),
            arg if arg.driver_strength().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::driver_strength",
                arg.raw_driver_strength() as usize,
            )),
            arg if arg.power_limit().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::power_limit",
                arg.raw_power_limit() as usize,
            )),
            arg if arg.mode().is_err() => Err(Error::invalid_field_variant(
                "cmd::argument::mode",
                arg.raw_mode() as usize,
            )),
            arg => Ok(arg),
        }
    }

    /// Converts the [Argument] into a byte array.
    pub const fn bytes(&self) -> [u8; Self::LEN] {
        self.0.to_be_bytes()
    }

    /// Attempts to convert a [`u32`] 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 TryFrom<u32> for Argument {
    type Error = Error;

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

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<&[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.access_mode(), Ok(AccessMode::new()));
        assert_eq!(arg.command_system(), Ok(CommandSystem::new()));
        assert_eq!(arg.driver_strength(), Ok(DriverStrength::new()));
        assert_eq!(arg.power_limit(), Ok(PowerLimit::new()));

        [
            AccessMode::Sdr12,
            AccessMode::Sdr25,
            AccessMode::Sdr50,
            AccessMode::Sdr104,
            AccessMode::Ddr50,
        ]
        .into_iter()
        .for_each(|exp_access_mode| {
            arg.set_access_mode(exp_access_mode);
            assert_eq!(arg.access_mode(), Ok(exp_access_mode));
        });

        [
            CommandSystem::SystemDefault,
            CommandSystem::ForEC,
            CommandSystem::Otp,
            CommandSystem::Assd,
            CommandSystem::Vendor,
        ]
        .into_iter()
        .for_each(|exp_command_system| {
            arg.set_command_system(exp_command_system);
            assert_eq!(arg.command_system(), Ok(exp_command_system));
        });

        [
            DriverStrength::TypeB,
            DriverStrength::TypeA,
            DriverStrength::TypeC,
            DriverStrength::TypeD,
        ]
        .into_iter()
        .for_each(|exp_driver_strength| {
            arg.set_driver_strength(exp_driver_strength);
            assert_eq!(arg.driver_strength(), Ok(exp_driver_strength));
        });

        [
            PowerLimit::W072,
            PowerLimit::W144,
            PowerLimit::W216,
            PowerLimit::W288,
            PowerLimit::W180,
        ]
        .into_iter()
        .for_each(|exp_power_limit| {
            arg.set_power_limit(exp_power_limit);
            assert_eq!(arg.power_limit(), Ok(exp_power_limit));
        });

        [Mode::Check, Mode::Switch]
            .into_iter()
            .for_each(|exp_mode| {
                arg.set_mode(exp_mode);
                assert_eq!(arg.mode(), Ok(exp_mode));
            });
    }
}