From 00938bcb239dcce7a10dd59cb67068ff7da9f9e5 Mon Sep 17 00:00:00 2001 From: occheung Date: Tue, 11 Aug 2020 00:07:07 +0800 Subject: [PATCH] attenuator: init --- Cargo.toml | 3 +-- migen/fpga_config.py | 2 +- src/attenuator.rs | 57 +++++++++++++++++++++++++++++++++++++++ src/generic_spi_device.rs | 27 +++++++++++++++++++ src/lib.rs | 25 +++++++---------- src/main.rs | 32 ++++++++++++++-------- src/spi_slave.rs | 15 +++-------- 7 files changed, 119 insertions(+), 42 deletions(-) create mode 100644 src/attenuator.rs create mode 100644 src/generic_spi_device.rs diff --git a/Cargo.toml b/Cargo.toml index 7382304..5ae1074 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,11 +10,10 @@ cortex-m-semihosting = "0.3.3" panic-halt = "0.2.0" cortex-m = "0.6.2" cortex-m-rt = "0.6.12" +embedded-hal = "0.2.4" stm32h7xx-hal = {version = "0.6.0", features = [ "stm32h743v", "rt", "unproven" ] } stm32h7-ethernet = { version = "0.2.0", features = [ "phy_lan8742a", "stm32h743v" ] } smoltcp = { version = "0.6.0", default-features = false, features = [ "ethernet", "proto-ipv4", "proto-ipv6", "socket-raw" ] } -xca9548a = "0.2.0" -lm75 = "0.1.1" nb = "1.0.0" # Logging and Panicking diff --git a/migen/fpga_config.py b/migen/fpga_config.py index ba08bc5..c48f7cf 100644 --- a/migen/fpga_config.py +++ b/migen/fpga_config.py @@ -29,7 +29,7 @@ class UrukulConnector(Module): ] # Debug purposes: Tie EEM MISO to EEM MOSI - # self.comb += eem.p[2].eq(eem.n[1]) + self.comb += eem.p[2].eq(eem.n[1]) if __name__ == "__main__": diff --git a/src/attenuator.rs b/src/attenuator.rs new file mode 100644 index 0000000..f8e278b --- /dev/null +++ b/src/attenuator.rs @@ -0,0 +1,57 @@ +use embedded_hal::blocking::spi::Transfer; +use cortex_m::asm::nop; +use cortex_m_semihosting::hprintln; +//use core::clone; + +use crate::Error; + +pub struct Attenuator { + spi: SPI, + data: [u8; 4], +} + +impl Attenuator +where + SPI: Transfer +{ + pub fn new(spi: SPI) -> Self { + Attenuator { + spi, + data: [0, 0, 0, 0], + } + } + + pub fn set_attenuation(&mut self, att: [f32; 4]) -> Result<[u8; 4], Error> { + for i in 0..4 { + let mut atten = att[i]; + if att[i] > 31.5 { + atten = 31.5; + } + if att[i] < 0.0 { + atten = 0.0; + } + self.data[i] = (atten * 2.0) as u8; + 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() + }, + err => nop() + }; + Ok(clone.clone()) + } +} + +impl Transfer for Attenuator +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) + } +} diff --git a/src/generic_spi_device.rs b/src/generic_spi_device.rs new file mode 100644 index 0000000..e28dffb --- /dev/null +++ b/src/generic_spi_device.rs @@ -0,0 +1,27 @@ +use embedded_hal::blocking::spi::Transfer; +use cortex_m::asm::nop; +use cortex_m_semihosting::hprintln; +use crate::Error; + +pub struct SPIStub { + spi: SPI +} + +impl SPIStub +where + SPI: Transfer +{ + pub fn new(spi: SPI) -> Self { + SPIStub{ + spi + } + } + + pub fn do_something(&mut self) { + let mut buffer :[u8; 4]= [0xDE, 0xAD, 0xBE, 0xEF]; + match self.spi.transfer(&mut buffer).map_err(Error::SPI) { + Ok(arr) => hprintln!("{:?}", arr).unwrap(), + Err(_) => nop(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index dd52938..2f93759 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,8 @@ #![no_std] - -use stm32h7xx_hal::{ - hal::{ - digital::v2::{ - InputPin, - OutputPin, - }, - blocking::spi::Transfer, - spi::FullDuplex, - }, - pac, - prelude::*, - spi, +extern crate embedded_hal; +use embedded_hal::{ + digital::v2::OutputPin, + blocking::spi::Transfer, }; use core::cell; @@ -20,11 +11,12 @@ use cortex_m; use cortex_m::asm::nop; use cortex_m_semihosting::hprintln; -use nb::block; - pub mod spi_slave; -use crate::spi_slave::{SPISlave, Parts}; +use crate::spi_slave::Parts; +pub mod generic_spi_device; + +pub mod attenuator; /* * Enum for structuring error @@ -64,6 +56,7 @@ where { type Error = Error; fn select_chip(&mut self, chip: u8) -> Result<(), Self::Error> { + hprintln!("Select chip: {}", chip).unwrap(); match chip & (1 << 0) { 0 => self.chip_select.0.set_low(), _ => self.chip_select.0.set_high(), diff --git a/src/main.rs b/src/main.rs index 5eab81d..606b6c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,12 @@ use cortex_m_semihosting::hprintln; use core::ptr; use nb::block; -use firmware::CPLD; +use firmware; +use firmware::{ + CPLD, + generic_spi_device::SPIStub, + attenuator::Attenuator, +}; #[entry] fn main() -> ! { @@ -63,6 +68,12 @@ fn main() -> ! { let mosi = gpiob.pb5.into_alternate_af5(); let miso = gpioa.pa6.into_alternate_af5(); + let (cs0, cs1, cs2) = ( + gpiob.pb12.into_push_pull_output(), + gpioa.pa15.into_push_pull_output(), + gpioc.pc7.into_push_pull_output(), + ); + let mut spi = dp.SPI1.spi( (sclk, miso, mosi), spi::MODE_0, @@ -71,19 +82,18 @@ fn main() -> ! { &ccdr.clocks, ); - let mut data :u8 = 0xAD; + let mut switch = CPLD::new(spi, (cs0, cs1, cs2)); - let mut switch = CPLD::new(spi, ( - gpiob.pb12.into_push_pull_output(), - gpioa.pa15.into_push_pull_output(), - gpioc.pc7.into_push_pull_output(), - )); + 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); loop { -// hprintln!("Sent {}", data).unwrap(); -// block!(spi.send(data)).unwrap(); -// data = block!(spi.read()).unwrap(); -// hprintln!("Read {}", data).unwrap(); nop(); } } diff --git a/src/spi_slave.rs b/src/spi_slave.rs index 48e5d78..811cfb8 100644 --- a/src/spi_slave.rs +++ b/src/spi_slave.rs @@ -1,16 +1,7 @@ -use stm32h7xx_hal::{ - hal::{ - digital::v2::{ - InputPin, - OutputPin, - }, - blocking::spi::Transfer, - }, - pac, - prelude::*, - spi, +use embedded_hal::{ + blocking::spi::Transfer, + digital::v2::OutputPin, }; - use core::marker::PhantomData; use crate::{DoOnGetRefMutData, Error, SelectChip};