sdmmc-core 0.5.0

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

use super::{ExtCsd, ExtCsdIndex, MmcCommandSet};

lib_bitfield! {
    /// Represents the `CMD_SET` field of the [ExtCsd] register.
    CommandSet: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the selected command set currently active on the device.
        mmc_command_set: MmcCommandSet, 7, 0;
    }
}

impl ExtCsd {
    /// Gets the `CMD_SET` field of the [ExtCsd] register.
    pub const fn cmd_set(&self) -> Result<CommandSet> {
        CommandSet::try_from_inner(self.0[ExtCsdIndex::CmdSet.into_inner()])
    }

    /// Sets the `CMD_SET` field of the [ExtCsd] register.
    pub fn set_cmd_set(&mut self, val: CommandSet) {
        self.0[ExtCsdIndex::CmdSet.into_inner()] = val.into_inner();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cmd_set() {
        let mut ext_csd = ExtCsd::new();

        (0..=u8::MAX).for_each(|raw_cmd| {
            ext_csd.set_cmd_set(CommandSet(raw_cmd));

            match CommandSet::try_from_inner(raw_cmd) {
                Ok(cmd) => assert_eq!(ext_csd.cmd_set(), Ok(cmd)),
                Err(err) => assert_eq!(ext_csd.cmd_set(), Err(err)),
            }
        });
    }
}