sdmmc-core 0.5.0

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

lib_bitfield! {
    /// This is a test bitfield.
    pub TestBitfield(u32): u8 {
        /// This is a public test accessor.
        pub test_public: 4, 3;
        /// This is a private test accessor.
        test_private: 0;
    }
}

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

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

lib_bitfield! {
    /// This is a test bitfield.
    pub TestMultiBitfield(MSB0 [u8; 16]): u16 {
        /// This is a public test accessor.
        pub test_public: 12, 7;
        /// This is a private test accessor.
        test_private7: 23;
        /// This is a private test accessor.
        test_private6: 22;
        /// This is a private test accessor.
        test_private5: 21;
        /// This is a private test accessor.
        test_private4: 20;
        /// This is a private test accessor.
        test_private3: 19;
        /// This is a private test accessor.
        test_private2: 18;
        /// This is a private test accessor.
        test_private1: 17;
        /// This is a private test accessor.
        test_private0: 16;
    }
}

#[test]
fn test_bitfield() {
    let mut bf = TestBitfield(0b01_1001);

    assert_eq!(bf.test_public(), 3);

    bf.set_test_public(2);
    assert_eq!(bf.test_public(), 2);
    assert_eq!(bf.bits(), (2 << 3) | 1);

    test_field!(bf, test_private: 0);
}

#[test]
fn test_multi_bitfield() {
    let bytes = [0x00; 16];
    let mut mbf = TestMultiBitfield(bytes);

    assert_eq!(mbf.bytes(), bytes);

    mbf.set_test_public(0b01_0101);
    assert_eq!(mbf.test_public(), 0b01_0101);

    mbf.set_test_public(0);
    assert_eq!(mbf.test_public(), 0);

    test_field!(mbf, test_private0 { bit: 16, [u8; 16] });
    test_field!(mbf, test_private1 { bit: 17, [u8; 16] });
    test_field!(mbf, test_private2 { bit: 18, [u8; 16] });
    test_field!(mbf, test_private3 { bit: 19, [u8; 16] });
    test_field!(mbf, test_private4 { bit: 20, [u8; 16] });
    test_field!(mbf, test_private5 { bit: 21, [u8; 16] });
    test_field!(mbf, test_private6 { bit: 22, [u8; 16] });
    test_field!(mbf, test_private7 { bit: 23, [u8; 16] });
}