use embedded_hal::blocking::spi::Transfer; use crate::Error; /* * Macro builder for bit masks */ macro_rules! construct_bitmask { ($collection: ident; $($name: ident, $shift: expr, $width: expr),+) => { 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) -> u32 { match self { $( $collection::$name => { let mut mask: u32 = 0; for bit in 0..$width { mask != 1 << ($width + bit); } mask }, )* } } } } } construct_bitmask!(CFGMask; // Bitmasks for CFG write RF_SW, 0, 4, // Reuse RF_SW for Status register LED, 4, 4, PROFILE, 8, 3, IO_UPDATE, 12, 1, MASK_NU, 13, 4, CLK_SEL0, 17, 1, SYNC_SEL, 18, 1, RST, 19, 1, IO_RST, 20, 1, CLK_SEL1, 21, 1, DIV, 22, 2, // BitMasks for CFG read SMP_ERR, 4, 4, PLL_LOCK, 8, 4, IFC_MODE, 12, 4, PROTO_KEY, 16, 7 ); pub struct ConfigRegister { spi: SPI, data: u32, } impl ConfigRegister where SPI: Transfer { pub fn new(spi: SPI) -> Self { ConfigRegister { spi, data: 0, } } pub fn set_configuration(&mut self, config: u32) -> Result> { self.data = config & 0x00FFFFFF; match self.spi.transfer(&mut [ ((config & 0x00FF0000) >> 16) as u8, ((config & 0x0000FF00) >> 8) as u8, ((config & 0x000000FF) >> 0) as u8, ]).map_err(Error::SPI) { Ok(arr) => Ok(((arr[0] as u32) << 16) | ((arr[1] as u32) << 8) | arr[2] as u32), Err(e) => Err(e), } } } impl Transfer for ConfigRegister where SPI: Transfer { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { self.spi.transfer(words).map_err(Error::SPI) } }