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};

mod revision;

pub use revision::*;

lib_bitfield! {
    /// Represents the `CMD_SET_REV` field of the [ExtCsd] register.
    CommandSetRevision: u8,
    mask: 0xff,
    default: 0,
    {
        /// Represents the MMC command set revision.
        mmc_revision: MmcRevision, 7, 0;
    }
}

impl ExtCsd {
    /// Gets the `CMD_SET_REV` field of the [ExtCsd] register.
    pub const fn cmd_set_rev(&self) -> Result<CommandSetRevision> {
        CommandSetRevision::try_from_inner(self.0[ExtCsdIndex::CmdSetRev.into_inner()])
    }

    /// Sets the `CMD_SET_REV` field of the [ExtCsd] register.
    pub(crate) fn set_cmd_set_rev(&mut self, val: CommandSetRevision) {
        self.0[ExtCsdIndex::CmdSetRev.into_inner()] = val.into_inner();
    }
}

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

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

        assert_eq!(ext_csd.cmd_set_rev(), Ok(CommandSetRevision::new()));

        (0..=u8::MAX).for_each(|raw_cmd| {
            // set a potentially invalid CommandSetRevision
            ext_csd.set_cmd_set_rev(CommandSetRevision(raw_cmd));

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