sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::lib_bitfield;
use crate::register::ext_csd::{ExtCsd, ExtCsdIndex};

lib_bitfield! {
    /// Represents the `CMDQ_DEPTH` field of the [ExtCsd] register.
    CommandQueueDepth: u8,
    mask: 0x1f,
    default: 0,
    {
        /// Represents the parameter used to calculate the command queue depth.
        n: 4, 0;
    }
}

impl CommandQueueDepth {
    /// Gets the command queue depth.
    ///
    /// # Note
    ///
    /// The command queue depth is calculated with the algorithm:
    ///
    /// ```no_build,no_run
    /// depth = N + 1
    /// ```
    #[inline]
    pub const fn queue_depth(&self) -> u8 {
        self.n() + 1
    }
}

impl ExtCsd {
    /// Gets the `CMDQ_DEPTH` field of the [ExtCsd] register.
    pub const fn cmdq_depth(&self) -> CommandQueueDepth {
        CommandQueueDepth::from_inner(self.0[ExtCsdIndex::CmdqDepth.into_inner()])
    }

    /// Sets the `CMDQ_DEPTH` field of the [ExtCsd] register.
    pub(crate) fn set_cmdq_depth(&mut self, val: CommandQueueDepth) {
        self.0[ExtCsdIndex::CmdqDepth.into_inner()] = val.into_inner();
    }
}

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

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

        assert_eq!(ext_csd.cmdq_depth(), CommandQueueDepth::new());

        (0..u8::MAX)
            .map(CommandQueueDepth::from_inner)
            .for_each(|depth| {
                ext_csd.set_cmdq_depth(depth);
                assert_eq!(ext_csd.cmdq_depth(), depth);
                assert_eq!(depth.queue_depth(), depth.n() + 1);
            });
    }
}