diff --git a/src/bitmask_macro.rs b/src/bitmask_macro.rs new file mode 100644 index 0000000..784c205 --- /dev/null +++ b/src/bitmask_macro.rs @@ -0,0 +1,55 @@ +/* + * Macro builder for bit masks + * $collection: Name for the bit mask collection + * $unsigned_type: Unsigned type for the data that the bit mask will be applied onto + * ($name: name of a bit mask, $shift: shift of the bit field, $width: no. of bits in the bit field) + * Note: All bit masks must concern a set of contiguous bits + */ + +macro_rules! construct_bitmask { + ($collection: ident; $unsigned_type: ty; $($name: ident, $shift: expr, $width: expr),+) => { + #[derive(Debug, Copy, Clone)] + pub enum $collection { + $( + $name, + )* + } + + impl $collection { + pub(crate) fn get_width(self) -> u8 { + match self { + $( + $collection::$name => $width, + )* + } + } + pub(crate) fn get_shift(self) -> u8 { + match self { + $( + $collection::$name => $shift, + )* + } + } + pub(crate) fn get_bitmask(self) -> $unsigned_type { + let mut mask: $unsigned_type = 0; + for bit in 0..self.get_width() { + mask |= (1 << (self.get_shift() + bit)); + } + mask + } + pub(crate) fn get_shifted_bits(self, arg: $unsigned_type) -> $unsigned_type { + assert!(arg < (2 << self.get_width())); + (arg << self.get_shift()) + } + pub(crate) fn set_data_by_arg(self, data: &mut $unsigned_type, arg: $unsigned_type) { + // Clear bits in field, then insert shifted argument + *data &= (!self.get_bitmask()); + *data |= self.get_shifted_bits(arg); + } + pub(crate) fn get_filtered_content(self, data: $unsigned_type) -> $unsigned_type { + // Filter everything then shift bits + ((data & self.get_bitmask()) >> self.get_shift()) + } + } + } +} diff --git a/src/config_register.rs b/src/config_register.rs index 5e41884..9059535 100644 --- a/src/config_register.rs +++ b/src/config_register.rs @@ -2,59 +2,6 @@ use embedded_hal::blocking::spi::Transfer; use cortex_m_semihosting::hprintln; use crate::Error; -/* - * Macro builder for bit masks - * Assumption: Contiguous bit field - * Break the bit field up when invoking macro rule if needed - */ -macro_rules! construct_bitmask { - ($collection: ident; $unsigned_type: ty; $($name: ident, $shift: expr, $width: expr),+) => { - #[derive(Debug, Copy, Clone)] - pub enum $collection { - $( - $name, - )* - } - - impl $collection { - pub(crate) fn get_width(self) -> u8 { - match self { - $( - $collection::$name => $width, - )* - } - } - pub(crate) fn get_shift(self) -> u8 { - match self { - $( - $collection::$name => $shift, - )* - } - } - pub(crate) fn get_bitmask(self) -> $unsigned_type { - let mut mask: $unsigned_type = 0; - for bit in 0..self.get_width() { - mask |= (1 << (self.get_shift() + bit)); - } - mask - } - pub(crate) fn get_shifted_bits(self, arg: $unsigned_type) -> $unsigned_type { - assert!(arg < (2 << self.get_width())); - (arg << self.get_shift()) - } - pub(crate) fn set_data_by_arg(self, data: &mut $unsigned_type, arg: $unsigned_type) { - // Clear bits in field, then insert shifted argument - *data &= (!self.get_bitmask()); - *data |= self.get_shifted_bits(arg); - } - pub(crate) fn get_filtered_content(self, data: $unsigned_type) -> $unsigned_type { - // Filter everything - ((data & self.get_bitmask()) >> self.get_shift()) - } - } - } -} - // Bitmasks for CFG construct_bitmask!(CFGMask; u32; RF_SW, 0, 4, diff --git a/src/lib.rs b/src/lib.rs index 5c5acb9..2ffe07d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,12 +11,16 @@ use cortex_m; use cortex_m::asm::nop; use cortex_m_semihosting::hprintln; +#[macro_use] +pub mod bitmask_macro; + pub mod spi_slave; use crate::spi_slave::Parts; pub mod config_register; pub mod attenuator; + /* * Enum for structuring error */