forked from M-Labs/humpback-dds
bitmask_macro: separated from cfg_reg
This commit is contained in:
parent
31b84bc12d
commit
495bf21575
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,59 +2,6 @@ use embedded_hal::blocking::spi::Transfer;
|
||||||
use cortex_m_semihosting::hprintln;
|
use cortex_m_semihosting::hprintln;
|
||||||
use crate::Error;
|
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
|
// Bitmasks for CFG
|
||||||
construct_bitmask!(CFGMask; u32;
|
construct_bitmask!(CFGMask; u32;
|
||||||
RF_SW, 0, 4,
|
RF_SW, 0, 4,
|
||||||
|
|
|
@ -11,12 +11,16 @@ use cortex_m;
|
||||||
use cortex_m::asm::nop;
|
use cortex_m::asm::nop;
|
||||||
use cortex_m_semihosting::hprintln;
|
use cortex_m_semihosting::hprintln;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod bitmask_macro;
|
||||||
|
|
||||||
pub mod spi_slave;
|
pub mod spi_slave;
|
||||||
use crate::spi_slave::Parts;
|
use crate::spi_slave::Parts;
|
||||||
|
|
||||||
pub mod config_register;
|
pub mod config_register;
|
||||||
pub mod attenuator;
|
pub mod attenuator;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Enum for structuring error
|
* Enum for structuring error
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue