sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
pub const PRODUCT_REV_LEN: usize = 1;

const MAJOR_MASK: u8 = 0xf0;
const MAJOR_SHIFT: usize = 4;
const MINOR_MASK: u8 = 0x0f;

/// Represents the product revision as a Binary Coded Decimal (BCD).
///
/// Uses MSB 4-bits for major revision, LSB 4-bits for minor revision.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ProductRevision(u8);

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

    /// Gets the bit value for the [ProductRevision].
    pub const fn bits(&self) -> u8 {
        self.0
    }

    /// Converts a [`u8`] into a [ProductRevision].
    pub const fn from_bits(val: u8) -> Self {
        Self(val)
    }

    /// Gets the major version of the [ProductRevision].
    pub const fn major(&self) -> u8 {
        (self.0 & MAJOR_MASK) >> MAJOR_SHIFT
    }

    /// Gets the minor version of the [ProductRevision].
    pub const fn minor(&self) -> u8 {
        self.0 & MINOR_MASK
    }
}

impl Default for ProductRevision {
    fn default() -> Self {
        Self::new()
    }
}

impl From<u8> for ProductRevision {
    fn from(val: u8) -> Self {
        Self::from_bits(val)
    }
}

impl From<ProductRevision> for u8 {
    fn from(val: ProductRevision) -> Self {
        val.bits()
    }
}

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

    #[test]
    fn test_valid() {
        (0..=u8::MAX).for_each(|prv| {
            let exp_prv = ProductRevision(prv);
            let exp_major = prv >> MAJOR_SHIFT;
            let exp_minor = prv & MINOR_MASK;

            assert_eq!(ProductRevision::from_bits(prv), exp_prv);
            assert_eq!(exp_prv.bits(), prv);
            assert_eq!(exp_prv.major(), exp_major);
            assert_eq!(exp_prv.minor(), exp_minor);
        });
    }
}