embedded_sdmmc/
structure.rs

1//! Useful macros for parsing SD/MMC structures.
2
3macro_rules! access_field {
4    ($self:expr, $offset:expr, $start_bit:expr, 1) => {
5        ($self.data[$offset] & (1 << $start_bit)) != 0
6    };
7    ($self:expr, $offset:expr, $start:expr, $num_bits:expr) => {
8        ($self.data[$offset] >> $start) & (((1u16 << $num_bits) - 1) as u8)
9    };
10}
11
12macro_rules! define_field {
13    ($name:ident, bool, $offset:expr, $bit:expr) => {
14        /// Get the value from the $name field
15        pub fn $name(&self) -> bool {
16            access_field!(self, $offset, $bit, 1)
17        }
18    };
19    ($name:ident, u8, $offset:expr, $start_bit:expr, $num_bits:expr) => {
20        /// Get the value from the $name field
21        pub fn $name(&self) -> u8 {
22            access_field!(self, $offset, $start_bit, $num_bits)
23        }
24    };
25    ($name:ident, $type:ty, [ $( ( $offset:expr, $start_bit:expr, $num_bits:expr ) ),+ ]) => {
26        /// Gets the value from the $name field
27        pub fn $name(&self) -> $type {
28            let mut result = 0;
29            $(
30                    result <<= $num_bits;
31                    let part = access_field!(self, $offset, $start_bit, $num_bits) as $type;
32                    result |=  part;
33            )+
34            result
35        }
36    };
37
38    ($name:ident, u8, $offset:expr) => {
39        /// Get the value from the $name field
40        pub fn $name(&self) -> u8 {
41            self.data[$offset]
42        }
43    };
44
45    ($name:ident, u16, $offset:expr) => {
46        /// Get the value from the $name field
47        pub fn $name(&self) -> u16 {
48            LittleEndian::read_u16(&self.data[$offset..$offset+2])
49        }
50    };
51
52    ($name:ident, u32, $offset:expr) => {
53        /// Get the $name field
54        pub fn $name(&self) -> u32 {
55            LittleEndian::read_u32(&self.data[$offset..$offset+4])
56        }
57    };
58}
59
60// ****************************************************************************
61//
62// End Of File
63//
64// ****************************************************************************