From a1a10d7afe2aedf54e9de52aee8aa6cbee275079 Mon Sep 17 00:00:00 2001 From: occheung Date: Tue, 11 Aug 2020 11:29:47 +0800 Subject: [PATCH] cfg_reg: init --- src/attenuator.rs | 34 ++++++++++++++++++++++++++++------ src/config_register.rs | 29 +++++++++++++++++++++++++++++ src/lib.rs | 3 ++- src/main.rs | 5 +---- src/spi_slave.rs | 9 ++++----- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 src/config_register.rs diff --git a/src/attenuator.rs b/src/attenuator.rs index f8e278b..6b4068f 100644 --- a/src/attenuator.rs +++ b/src/attenuator.rs @@ -1,7 +1,7 @@ use embedded_hal::blocking::spi::Transfer; use cortex_m::asm::nop; use cortex_m_semihosting::hprintln; -//use core::clone; +use core::assert; use crate::Error; @@ -34,14 +34,36 @@ where self.data[i] = self.data[i] << 2; } let mut clone = self.data.clone(); - hprintln!("Before Attenuation: {:?}", clone).unwrap(); match self.spi.transfer(&mut clone).map_err(Error::SPI) { Ok(arr) => { - hprintln!("Attenuation array: {:?}", arr).unwrap() + Ok(self.data.clone()) }, - err => nop() - }; - Ok(clone.clone()) + err => Err(Error::AttenuatorError) + } + } + + pub fn set_channel_attenuation(&mut self, channel: u8, attenuation: f32) -> Result> { + assert!((channel < 4) && (channel >= 0)); + let mut arr: [f32; 4] = self.get_attenuation(); + arr[channel as usize] = attenuation; + match self.set_attenuation(arr) { + Ok(v) => Ok(v[channel as usize]), + Err(e) => Err(e) + } + } + + pub fn get_channel_attenuation(&mut self, channel: u8) -> f32 { + assert!((channel < 4) && (channel >= 0)); + (self.data[channel as usize] as f32)/8.0 + } + + pub fn get_attenuation(&mut self) -> [f32; 4] { + [ + self.get_channel_attenuation(0), + self.get_channel_attenuation(1), + self.get_channel_attenuation(2), + self.get_channel_attenuation(3), + ] } } diff --git a/src/config_register.rs b/src/config_register.rs new file mode 100644 index 0000000..7a8d962 --- /dev/null +++ b/src/config_register.rs @@ -0,0 +1,29 @@ +use embedded_hal::blocking::spi::Transfer + +/* + * Bit Masks for CFG_Write + */ +const RF_SW :u32 = 0x0000000F; +const LED :u32 = 0x000000F0; +const PROFILE :u32 = 0x00000700; +const IO_UPDATE :u32 = 0x00001000; +const MASK_NU :u32 = 0x0001E000; +const CLK_SEL0 :u32 = 0x00020000; +const SYNC_SEL :u32 = 0x00040000; +const RST :u32 = 0x00080000; +const IO_RST :u32 = 0x00100000; +const CLK_SEL1 :u32 = 0x00200000; +const DIV :u32 = 0x00C00000; + +/* + * Bit Masks for CFG_Read + */ +const SMP_ERR :u32 = 0x000000F0; +const PLL_LOCK :u32 = 0x00000F00; +const IFC_MODE :u32 = 0x0000F000; +const PROTO_DEV :u32 = 0x007F0000; + +pub struct ConfigRegister { + spi: SPI, + data: u32, +} diff --git a/src/lib.rs b/src/lib.rs index 2f93759..32d30e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use cortex_m_semihosting::hprintln; pub mod spi_slave; use crate::spi_slave::Parts; -pub mod generic_spi_device; +//pub mod generic_spi_device; pub mod attenuator; @@ -26,6 +26,7 @@ pub enum Error { SPI(E), CSError, GetRefMutDataError, + AttenuatorError, } /* diff --git a/src/main.rs b/src/main.rs index 606b6c8..97ac942 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,6 @@ use nb::block; use firmware; use firmware::{ CPLD, - generic_spi_device::SPIStub, attenuator::Attenuator, }; @@ -85,13 +84,11 @@ fn main() -> ! { let mut switch = CPLD::new(spi, (cs0, cs1, cs2)); let parts = switch.split(); - - let mut spi_stub = SPIStub::new(parts.spi1); - spi_stub.do_something(); let mut attenuator = Attenuator::new(parts.spi2); let mut attenuation :[f32; 4] = [24.0, -5.0, 32.0, 10.2]; attenuator.set_attenuation(attenuation); + attenuator.set_channel_attenuation(2, 15.3); loop { nop(); diff --git a/src/spi_slave.rs b/src/spi_slave.rs index 811cfb8..c4a1fb6 100644 --- a/src/spi_slave.rs +++ b/src/spi_slave.rs @@ -25,7 +25,6 @@ pub struct Parts<'a, DEV: 'a, SPI, CS0, CS1, CS2> { impl<'a, DEV, SPI, CS0, CS1, CS2> Parts<'a, DEV, SPI, CS0, CS1, CS2> { pub(crate) fn new(cpld: &'a DEV) -> Self { Parts { -// spi0: SPISlave(&cpld, 0, PhantomData), spi1: SPISlave(&cpld, 1, PhantomData), spi2: SPISlave(&cpld, 2, PhantomData), spi3: SPISlave(&cpld, 3, PhantomData), @@ -48,9 +47,9 @@ where type Error = Error; fn transfer<'w>(&mut self, words: &'w mut[u8]) -> Result<&'w [u8], Self::Error> { - self.0.do_on_get_ref_mut_data(|mut dev| { - dev.select_chip(self.1) - })?; - self.0.do_on_get_ref_mut_data(move |mut dev| dev.spi.transfer(words).map_err(Error::SPI)) + self.0.do_on_get_ref_mut_data(move |mut dev| { + dev.select_chip(self.1); + dev.spi.transfer(words).map_err(Error::SPI) + }) } }