From 8954c94a20a6a306cb51b9442b321de085d3fae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 14 Apr 2021 15:53:52 +0200 Subject: [PATCH 1/5] hardware: add digital input support * The inputs of the buffer are not pulled up/down. That might make them unusable if left floating. --- dsp/src/iir.rs | 8 ++++++-- src/bin/dual-iir.rs | 20 ++++++++++++++------ src/hardware/configuration.rs | 14 +++++++++++--- src/hardware/mod.rs | 8 ++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index ee9639e..a6af191 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -117,7 +117,7 @@ impl IIR { /// # Arguments /// * `xy` - Current filter state. /// * `x0` - New input. - pub fn update(&self, xy: &mut Vec5, x0: f32) -> f32 { + pub fn update(&self, xy: &mut Vec5, x0: f32, hold: bool) -> f32 { let n = self.ba.len(); debug_assert!(xy.len() == n); // `xy` contains x0 x1 y0 y1 y2 @@ -128,7 +128,11 @@ impl IIR { // Store x0 x0 x1 x2 y1 y2 xy[0] = x0; // Compute y0 by multiply-accumulate - let y0 = macc(self.y_offset, xy, &self.ba); + let y0 = if hold { + xy[n / 2 + 1] + } else { + macc(self.y_offset, xy, &self.ba) + }; // Limit y0 let y0 = max(self.y_min, min(self.y_max, y0)); // Store y0 x0 x1 y0 y1 y2 diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 4a27d48..520d5ec 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -4,6 +4,8 @@ use stm32h7xx_hal as hal; +pub use embedded_hal::digital::v2::InputPin; + use stabilizer::hardware; use miniconf::{minimq, Miniconf, MqttInterface}; @@ -12,7 +14,7 @@ use serde::Deserialize; use dsp::iir; use hardware::{ Adc0Input, Adc1Input, AfeGain, CycleCounter, Dac0Output, Dac1Output, - NetworkStack, AFE0, AFE1, + DigitalInput1, NetworkStack, AFE0, AFE1, }; const SCALE: f32 = i16::MAX as _; @@ -39,6 +41,7 @@ impl Default for Settings { const APP: () = { struct Resources { afes: (AFE0, AFE1), + di1: DigitalInput1, adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), mqtt_interface: @@ -90,6 +93,7 @@ const APP: () = { adcs: stabilizer.adcs, dacs: stabilizer.dacs, clock: stabilizer.cycle_counter, + di1: stabilizer.digital_inputs.1, } } @@ -109,7 +113,7 @@ const APP: () = { /// /// Because the ADC and DAC operate at the same rate, these two constraints actually implement /// the same time bounds, meeting one also means the other is also met. - #[task(binds=DMA1_STR4, resources=[adcs, dacs, iir_state, iir_ch], priority=2)] + #[task(binds=DMA1_STR4, resources=[adcs, di1, dacs, iir_state, iir_ch], priority=2)] fn process(c: process::Context) { let adc_samples = [ c.resources.adcs.0.acquire_buffer(), @@ -121,13 +125,17 @@ const APP: () = { c.resources.dacs.1.acquire_buffer(), ]; + let hold = c.resources.di1.is_high().unwrap(); + for channel in 0..adc_samples.len() { for sample in 0..adc_samples[0].len() { - let x = f32::from(adc_samples[channel][sample] as i16); - let mut y = x; + let mut y = f32::from(adc_samples[channel][sample] as i16); for i in 0..c.resources.iir_state[channel].len() { - y = c.resources.iir_ch[channel][i] - .update(&mut c.resources.iir_state[channel][i], y); + y = c.resources.iir_ch[channel][i].update( + &mut c.resources.iir_state[channel][i], + y, + hold, + ); } // Note(unsafe): The filter limits ensure that the value is in range. // The truncation introduces 1/2 LSB distortion. diff --git a/src/hardware/configuration.rs b/src/hardware/configuration.rs index 0351db9..31fb16d 100644 --- a/src/hardware/configuration.rs +++ b/src/hardware/configuration.rs @@ -9,12 +9,12 @@ use stm32h7xx_hal::{ use smoltcp_nal::smoltcp; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +pub use embedded_hal::digital::v2::{InputPin, OutputPin}; use super::{ adc, afe, cycle_counter::CycleCounter, dac, design_parameters, - digital_input_stamper, eeprom, pounder, timers, DdsOutput, NetworkStack, - AFE0, AFE1, + digital_input_stamper, eeprom, pounder, timers, DdsOutput, DigitalInput0, + DigitalInput1, NetworkStack, AFE0, AFE1, }; pub struct NetStorage { @@ -69,6 +69,7 @@ pub struct StabilizerDevices { pub timestamp_timer: timers::TimestampTimer, pub net: NetworkDevices, pub cycle_counter: CycleCounter, + pub digital_inputs: (DigitalInput0, DigitalInput1), } /// The available Pounder-specific hardware interfaces. @@ -439,6 +440,12 @@ pub fn setup( ) }; + let digital_inputs = { + let di0 = gpiog.pg9.into_floating_input(); + let di1 = gpioc.pc15.into_floating_input(); + (di0, di1) + }; + let mut eeprom_i2c = { let sda = gpiof.pf0.into_alternate_af4().set_open_drain(); let scl = gpiof.pf1.into_alternate_af4().set_open_drain(); @@ -872,6 +879,7 @@ pub fn setup( adc_dac_timer: sampling_timer, timestamp_timer, cycle_counter, + digital_inputs, }; // info!("Version {} {}", build_info::PKG_VERSION, build_info::GIT_VERSION.unwrap()); diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs index cc7a34a..34f092f 100644 --- a/src/hardware/mod.rs +++ b/src/hardware/mod.rs @@ -34,6 +34,14 @@ pub type AFE1 = afe::ProgrammableGainAmplifier< hal::gpio::gpiod::PD15>, >; +// Type alias for digital input 0 (DI0). +pub type DigitalInput0 = + hal::gpio::gpiog::PG9>; + +// Type alias for digital input 1 (DI1). +pub type DigitalInput1 = + hal::gpio::gpioc::PC15>; + pub type NetworkStack = smoltcp_nal::NetworkStack< 'static, 'static, From bf5f6afbc10f6c4f96fd4658e464da6529177ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 14 Apr 2021 16:02:54 +0200 Subject: [PATCH 2/5] fix bench --- dsp/benches/micro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsp/benches/micro.rs b/dsp/benches/micro.rs index 0902a4c..22a2540 100644 --- a/dsp/benches/micro.rs +++ b/dsp/benches/micro.rs @@ -67,7 +67,7 @@ fn iir_bench() { let mut xy = iir::Vec5::default(); println!( "int::IIR::update(s, x): {}", - bench_env(0.32241, |x| dut.update(&mut xy, *x)) + bench_env(0.32241, |x| dut.update(&mut xy, *x, true)) ); } From 2bfed7b66971211bdb90220345a0b7fdb895f05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 14 Apr 2021 16:09:54 +0200 Subject: [PATCH 3/5] dual-iir: add enable_hold, force_hold * Also use Settings as resource directly and copy it bulk (like lockin-external) --- src/bin/dual-iir.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 520d5ec..0dc9818 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -22,10 +22,12 @@ const SCALE: f32 = i16::MAX as _; // The number of cascaded IIR biquads per channel. Select 1 or 2! const IIR_CASCADE_LENGTH: usize = 1; -#[derive(Debug, Deserialize, Miniconf)] +#[derive(Clone, Copy, Debug, Deserialize, Miniconf)] pub struct Settings { afe: [AfeGain; 2], iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2], + allow_hold: bool, + force_hold: bool, } impl Default for Settings { @@ -33,6 +35,8 @@ impl Default for Settings { Self { afe: [AfeGain::G1, AfeGain::G1], iir_ch: [[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2], + allow_hold: false, + force_hold: false, } } } @@ -51,11 +55,10 @@ const APP: () = { // Format: iir_state[ch][cascade-no][coeff] #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] iir_state: [[iir::Vec5; IIR_CASCADE_LENGTH]; 2], - #[init([[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2])] - iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2], + settings: Settings, } - #[init] + #[init(spawn=[settings_update])] fn init(c: init::Context) -> init::LateResources { // Configure the microcontroller let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device); @@ -78,6 +81,9 @@ const APP: () = { .unwrap() }; + // Spawn a settings update for default settings. + c.spawn.settings_update().unwrap(); + // Enable ADC/DAC events stabilizer.adcs.0.start(); stabilizer.adcs.1.start(); @@ -94,6 +100,7 @@ const APP: () = { dacs: stabilizer.dacs, clock: stabilizer.cycle_counter, di1: stabilizer.digital_inputs.1, + settings: Settings::default(), } } @@ -113,7 +120,7 @@ const APP: () = { /// /// Because the ADC and DAC operate at the same rate, these two constraints actually implement /// the same time bounds, meeting one also means the other is also met. - #[task(binds=DMA1_STR4, resources=[adcs, di1, dacs, iir_state, iir_ch], priority=2)] + #[task(binds=DMA1_STR4, resources=[adcs, di1, dacs, iir_state, settings], priority=2)] fn process(c: process::Context) { let adc_samples = [ c.resources.adcs.0.acquire_buffer(), @@ -125,13 +132,15 @@ const APP: () = { c.resources.dacs.1.acquire_buffer(), ]; - let hold = c.resources.di1.is_high().unwrap(); + let hold = c.resources.settings.force_hold + || (c.resources.di1.is_high().unwrap() + && c.resources.settings.allow_hold); for channel in 0..adc_samples.len() { for sample in 0..adc_samples[0].len() { let mut y = f32::from(adc_samples[channel][sample] as i16); for i in 0..c.resources.iir_state[channel].len() { - y = c.resources.iir_ch[channel][i].update( + y = c.resources.settings.iir_ch[channel][i].update( &mut c.resources.iir_state[channel][i], y, hold, @@ -181,12 +190,12 @@ const APP: () = { } } - #[task(priority = 1, resources=[mqtt_interface, afes, iir_ch])] + #[task(priority = 1, resources=[mqtt_interface, afes, settings])] fn settings_update(mut c: settings_update::Context) { let settings = &c.resources.mqtt_interface.settings; // Update the IIR channels. - c.resources.iir_ch.lock(|iir| *iir = settings.iir_ch); + c.resources.settings.lock(|current| *current = *settings); // Update AFEs c.resources.afes.0.set_gain(settings.afe[0]); From 8cc06d39c4e65fd3ceb867598b98f08c31a1b9ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 15 Apr 2021 13:47:10 +0200 Subject: [PATCH 4/5] dual-iir: use InputPin re-export, rename digital_input --- src/bin/dual-iir.rs | 16 ++++++---------- src/hardware/configuration.rs | 2 +- src/hardware/mod.rs | 3 +++ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 0dc9818..d51ca56 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -2,10 +2,6 @@ #![no_std] #![no_main] -use stm32h7xx_hal as hal; - -pub use embedded_hal::digital::v2::InputPin; - use stabilizer::hardware; use miniconf::{minimq, Miniconf, MqttInterface}; @@ -14,7 +10,7 @@ use serde::Deserialize; use dsp::iir; use hardware::{ Adc0Input, Adc1Input, AfeGain, CycleCounter, Dac0Output, Dac1Output, - DigitalInput1, NetworkStack, AFE0, AFE1, + DigitalInput1, InputPin, NetworkStack, AFE0, AFE1, }; const SCALE: f32 = i16::MAX as _; @@ -45,7 +41,7 @@ impl Default for Settings { const APP: () = { struct Resources { afes: (AFE0, AFE1), - di1: DigitalInput1, + digital_input1: DigitalInput1, adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), mqtt_interface: @@ -99,7 +95,7 @@ const APP: () = { adcs: stabilizer.adcs, dacs: stabilizer.dacs, clock: stabilizer.cycle_counter, - di1: stabilizer.digital_inputs.1, + digital_input1: stabilizer.digital_inputs.1, settings: Settings::default(), } } @@ -120,7 +116,7 @@ const APP: () = { /// /// Because the ADC and DAC operate at the same rate, these two constraints actually implement /// the same time bounds, meeting one also means the other is also met. - #[task(binds=DMA1_STR4, resources=[adcs, di1, dacs, iir_state, settings], priority=2)] + #[task(binds=DMA1_STR4, resources=[adcs, digital_input1, dacs, iir_state, settings], priority=2)] fn process(c: process::Context) { let adc_samples = [ c.resources.adcs.0.acquire_buffer(), @@ -133,7 +129,7 @@ const APP: () = { ]; let hold = c.resources.settings.force_hold - || (c.resources.di1.is_high().unwrap() + || (c.resources.digital_input1.is_high().unwrap() && c.resources.settings.allow_hold); for channel in 0..adc_samples.len() { @@ -204,7 +200,7 @@ const APP: () = { #[task(binds = ETH, priority = 1)] fn eth(_: eth::Context) { - unsafe { hal::ethernet::interrupt_handler() } + unsafe { stm32h7xx_hal::ethernet::interrupt_handler() } } #[task(binds = SPI2, priority = 3)] diff --git a/src/hardware/configuration.rs b/src/hardware/configuration.rs index 31fb16d..bff3fe6 100644 --- a/src/hardware/configuration.rs +++ b/src/hardware/configuration.rs @@ -9,7 +9,7 @@ use stm32h7xx_hal::{ use smoltcp_nal::smoltcp; -pub use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::digital::v2::{InputPin, OutputPin}; use super::{ adc, afe, cycle_counter::CycleCounter, dac, design_parameters, diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs index 34f092f..26800fc 100644 --- a/src/hardware/mod.rs +++ b/src/hardware/mod.rs @@ -1,6 +1,9 @@ ///! Module for all hardware-specific setup of Stabilizer use stm32h7xx_hal as hal; +// Re-export for the DigitalInputs below: +pub use embedded_hal::digital::v2::InputPin; + #[cfg(feature = "semihosting")] use panic_semihosting as _; From 1a08634dcb90634141c5f2bb966174b7905423fe Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 19 Apr 2021 12:17:41 +0200 Subject: [PATCH 5/5] Renaming interface to avoid confusion --- src/bin/dual-iir.rs | 20 ++++++++++++-------- src/bin/lockin-external.rs | 20 ++++++++++++-------- src/net/mod.rs | 11 ++++++----- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index cac6f45..8f07cf1 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -13,7 +13,7 @@ use hardware::{ InputPin, AFE0, AFE1, }; -use net::{Action, MqttSettings}; +use net::{Action, MiniconfInterface}; const SCALE: f32 = i16::MAX as _; @@ -46,7 +46,7 @@ const APP: () = { digital_input1: DigitalInput1, adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), - mqtt_settings: MqttSettings, + mqtt_config: MiniconfInterface, // Format: iir_state[ch][cascade-no][coeff] #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] @@ -59,7 +59,7 @@ const APP: () = { // Configure the microcontroller let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device); - let mqtt_settings = MqttSettings::new( + let mqtt_config = MiniconfInterface::new( stabilizer.net.stack, "", "dt/sinara/stabilizer", @@ -83,7 +83,7 @@ const APP: () = { afes: stabilizer.afes, adcs: stabilizer.adcs, dacs: stabilizer.dacs, - mqtt_settings, + mqtt_config, digital_input1: stabilizer.digital_inputs.1, settings: Settings::default(), } @@ -140,10 +140,14 @@ const APP: () = { } } - #[idle(resources=[mqtt_settings], spawn=[settings_update])] + #[idle(resources=[mqtt_config], spawn=[settings_update])] fn idle(mut c: idle::Context) -> ! { loop { - match c.resources.mqtt_settings.lock(|settings| settings.update()) { + match c + .resources + .mqtt_config + .lock(|config_interface| config_interface.update()) + { Some(Action::Sleep) => cortex_m::asm::wfi(), Some(Action::UpdateSettings) => { c.spawn.settings_update().unwrap() @@ -153,9 +157,9 @@ const APP: () = { } } - #[task(priority = 1, resources=[mqtt_settings, afes, settings])] + #[task(priority = 1, resources=[mqtt_config, afes, settings])] fn settings_update(mut c: settings_update::Context) { - let settings = &c.resources.mqtt_settings.mqtt.settings; + let settings = &c.resources.mqtt_config.mqtt.settings; // Update the IIR channels. c.resources.settings.lock(|current| *current = *settings); diff --git a/src/bin/lockin-external.rs b/src/bin/lockin-external.rs index b62ea42..36bd882 100644 --- a/src/bin/lockin-external.rs +++ b/src/bin/lockin-external.rs @@ -14,7 +14,7 @@ use stabilizer::hardware::{ }; use miniconf::Miniconf; -use stabilizer::net::{Action, MqttSettings}; +use stabilizer::net::{Action, MiniconfInterface}; #[derive(Copy, Clone, Debug, Deserialize, Miniconf)] enum Conf { @@ -58,7 +58,7 @@ const APP: () = { afes: (AFE0, AFE1), adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), - mqtt_settings: MqttSettings, + mqtt_config: MiniconfInterface, settings: Settings, timestamper: InputStamper, @@ -71,7 +71,7 @@ const APP: () = { // Configure the microcontroller let (mut stabilizer, _pounder) = setup(c.core, c.device); - let mqtt_settings = MqttSettings::new( + let mqtt_config = MiniconfInterface::new( stabilizer.net.stack, "", "dt/sinara/lockin", @@ -108,7 +108,7 @@ const APP: () = { afes: stabilizer.afes, adcs: stabilizer.adcs, dacs: stabilizer.dacs, - mqtt_settings, + mqtt_config, timestamper: stabilizer.timestamper, settings, @@ -190,10 +190,14 @@ const APP: () = { } } - #[idle(resources=[mqtt_settings], spawn=[settings_update])] + #[idle(resources=[mqtt_config], spawn=[settings_update])] fn idle(mut c: idle::Context) -> ! { loop { - match c.resources.mqtt_settings.lock(|settings| settings.update()) { + match c + .resources + .mqtt_config + .lock(|config_interface| config_interface.update()) + { Some(Action::Sleep) => cortex_m::asm::wfi(), Some(Action::UpdateSettings) => { c.spawn.settings_update().unwrap() @@ -203,9 +207,9 @@ const APP: () = { } } - #[task(priority = 1, resources=[mqtt_settings, settings, afes])] + #[task(priority = 1, resources=[mqtt_config, settings, afes])] fn settings_update(mut c: settings_update::Context) { - let settings = &c.resources.mqtt_settings.mqtt.settings; + let settings = &c.resources.mqtt_config.mqtt.settings; c.resources.afes.0.set_gain(settings.afe[0]); c.resources.afes.1.set_gain(settings.afe[1]); diff --git a/src/net/mod.rs b/src/net/mod.rs index f38f3d9..23d712d 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -2,7 +2,7 @@ use crate::hardware::{ design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, }; -use miniconf::{minimq, MqttInterface}; +use miniconf::minimq; /// Potential actions for firmware to take. pub enum Action { @@ -14,17 +14,17 @@ pub enum Action { } /// MQTT settings interface. -pub struct MqttSettings +pub struct MiniconfInterface where S: miniconf::Miniconf + Default, { - pub mqtt: MqttInterface, + pub mqtt: miniconf::MqttInterface, clock: CycleCounter, phy: EthernetPhy, network_was_reset: bool, } -impl MqttSettings +impl MiniconfInterface where S: miniconf::Miniconf + Default, { @@ -49,7 +49,8 @@ where .unwrap() }; - MqttInterface::new(mqtt_client, prefix, S::default()).unwrap() + miniconf::MqttInterface::new(mqtt_client, prefix, S::default()) + .unwrap() }; Self {