Merge branch 'feature/phy-reset' into feature/tcp-randomizer

This commit is contained in:
Ryan Summers 2021-04-19 12:19:24 +02:00
commit 6be642d3d7
7 changed files with 86 additions and 40 deletions

View File

@ -67,7 +67,7 @@ fn iir_bench() {
let mut xy = iir::Vec5::default(); let mut xy = iir::Vec5::default();
println!( println!(
"int::IIR::update(s, x): {}", "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))
); );
} }

View File

@ -117,7 +117,7 @@ impl IIR {
/// # Arguments /// # Arguments
/// * `xy` - Current filter state. /// * `xy` - Current filter state.
/// * `x0` - New input. /// * `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(); let n = self.ba.len();
debug_assert!(xy.len() == n); debug_assert!(xy.len() == n);
// `xy` contains x0 x1 y0 y1 y2 // `xy` contains x0 x1 y0 y1 y2
@ -128,7 +128,11 @@ impl IIR {
// Store x0 x0 x1 x2 y1 y2 // Store x0 x0 x1 x2 y1 y2
xy[0] = x0; xy[0] = x0;
// Compute y0 by multiply-accumulate // 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 // Limit y0
let y0 = max(self.y_min, min(self.y_max, y0)); let y0 = max(self.y_min, min(self.y_max, y0));
// Store y0 x0 x1 y0 y1 y2 // Store y0 x0 x1 y0 y1 y2

View File

@ -2,8 +2,6 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use stm32h7xx_hal as hal;
use stabilizer::{hardware, net}; use stabilizer::{hardware, net};
use miniconf::Miniconf; use miniconf::Miniconf;
@ -11,20 +9,23 @@ use serde::Deserialize;
use dsp::iir; use dsp::iir;
use hardware::{ use hardware::{
Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, AFE0, AFE1, Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, DigitalInput1,
InputPin, AFE0, AFE1,
}; };
use net::{Action, MqttSettings}; use net::{Action, MiniconfInterface};
const SCALE: f32 = i16::MAX as _; const SCALE: f32 = i16::MAX as _;
// The number of cascaded IIR biquads per channel. Select 1 or 2! // The number of cascaded IIR biquads per channel. Select 1 or 2!
const IIR_CASCADE_LENGTH: usize = 1; const IIR_CASCADE_LENGTH: usize = 1;
#[derive(Debug, Deserialize, Miniconf)] #[derive(Clone, Copy, Debug, Deserialize, Miniconf)]
pub struct Settings { pub struct Settings {
afe: [AfeGain; 2], afe: [AfeGain; 2],
iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2], iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2],
allow_hold: bool,
force_hold: bool,
} }
impl Default for Settings { impl Default for Settings {
@ -32,6 +33,8 @@ impl Default for Settings {
Self { Self {
afe: [AfeGain::G1, AfeGain::G1], afe: [AfeGain::G1, AfeGain::G1],
iir_ch: [[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2], iir_ch: [[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2],
allow_hold: false,
force_hold: false,
} }
} }
} }
@ -40,23 +43,23 @@ impl Default for Settings {
const APP: () = { const APP: () = {
struct Resources { struct Resources {
afes: (AFE0, AFE1), afes: (AFE0, AFE1),
digital_input1: DigitalInput1,
adcs: (Adc0Input, Adc1Input), adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output), dacs: (Dac0Output, Dac1Output),
mqtt_settings: MqttSettings<Settings>, mqtt_config: MiniconfInterface<Settings>,
// Format: iir_state[ch][cascade-no][coeff] // Format: iir_state[ch][cascade-no][coeff]
#[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])]
iir_state: [[iir::Vec5; IIR_CASCADE_LENGTH]; 2], iir_state: [[iir::Vec5; IIR_CASCADE_LENGTH]; 2],
#[init([[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2])] settings: Settings,
iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2],
} }
#[init] #[init(spawn=[settings_update])]
fn init(c: init::Context) -> init::LateResources { fn init(c: init::Context) -> init::LateResources {
// Configure the microcontroller // Configure the microcontroller
let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device); let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device);
let mqtt_settings = MqttSettings::new( let mqtt_config = MiniconfInterface::new(
stabilizer.net.stack, stabilizer.net.stack,
"", "",
"dt/sinara/stabilizer", "dt/sinara/stabilizer",
@ -64,6 +67,9 @@ const APP: () = {
stabilizer.cycle_counter, stabilizer.cycle_counter,
); );
// Spawn a settings update for default settings.
c.spawn.settings_update().unwrap();
// Enable ADC/DAC events // Enable ADC/DAC events
stabilizer.adcs.0.start(); stabilizer.adcs.0.start();
stabilizer.adcs.1.start(); stabilizer.adcs.1.start();
@ -77,7 +83,9 @@ const APP: () = {
afes: stabilizer.afes, afes: stabilizer.afes,
adcs: stabilizer.adcs, adcs: stabilizer.adcs,
dacs: stabilizer.dacs, dacs: stabilizer.dacs,
mqtt_settings, mqtt_config,
digital_input1: stabilizer.digital_inputs.1,
settings: Settings::default(),
} }
} }
@ -97,7 +105,7 @@ const APP: () = {
/// ///
/// Because the ADC and DAC operate at the same rate, these two constraints actually implement /// 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. /// 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, digital_input1, dacs, iir_state, settings], priority=2)]
fn process(c: process::Context) { fn process(c: process::Context) {
let adc_samples = [ let adc_samples = [
c.resources.adcs.0.acquire_buffer(), c.resources.adcs.0.acquire_buffer(),
@ -109,13 +117,19 @@ const APP: () = {
c.resources.dacs.1.acquire_buffer(), c.resources.dacs.1.acquire_buffer(),
]; ];
let hold = c.resources.settings.force_hold
|| (c.resources.digital_input1.is_high().unwrap()
&& c.resources.settings.allow_hold);
for channel in 0..adc_samples.len() { for channel in 0..adc_samples.len() {
for sample in 0..adc_samples[0].len() { for sample in 0..adc_samples[0].len() {
let x = f32::from(adc_samples[channel][sample] as i16); let mut y = f32::from(adc_samples[channel][sample] as i16);
let mut y = x;
for i in 0..c.resources.iir_state[channel].len() { for i in 0..c.resources.iir_state[channel].len() {
y = c.resources.iir_ch[channel][i] y = c.resources.settings.iir_ch[channel][i].update(
.update(&mut c.resources.iir_state[channel][i], y); &mut c.resources.iir_state[channel][i],
y,
hold,
);
} }
// Note(unsafe): The filter limits ensure that the value is in range. // Note(unsafe): The filter limits ensure that the value is in range.
// The truncation introduces 1/2 LSB distortion. // The truncation introduces 1/2 LSB distortion.
@ -126,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) -> ! { fn idle(mut c: idle::Context) -> ! {
loop { 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::Sleep) => cortex_m::asm::wfi(),
Some(Action::UpdateSettings) => { Some(Action::UpdateSettings) => {
c.spawn.settings_update().unwrap() c.spawn.settings_update().unwrap()
@ -139,12 +157,12 @@ const APP: () = {
} }
} }
#[task(priority = 1, resources=[mqtt_settings, afes, iir_ch])] #[task(priority = 1, resources=[mqtt_config, afes, settings])]
fn settings_update(mut c: settings_update::Context) { 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. // Update the IIR channels.
c.resources.iir_ch.lock(|iir| *iir = settings.iir_ch); c.resources.settings.lock(|current| *current = *settings);
// Update AFEs // Update AFEs
c.resources.afes.0.set_gain(settings.afe[0]); c.resources.afes.0.set_gain(settings.afe[0]);
@ -153,7 +171,7 @@ const APP: () = {
#[task(binds = ETH, priority = 1)] #[task(binds = ETH, priority = 1)]
fn eth(_: eth::Context) { fn eth(_: eth::Context) {
unsafe { hal::ethernet::interrupt_handler() } unsafe { stm32h7xx_hal::ethernet::interrupt_handler() }
} }
#[task(binds = SPI2, priority = 3)] #[task(binds = SPI2, priority = 3)]

View File

@ -14,7 +14,7 @@ use stabilizer::hardware::{
}; };
use miniconf::Miniconf; use miniconf::Miniconf;
use stabilizer::net::{Action, MqttSettings}; use stabilizer::net::{Action, MiniconfInterface};
#[derive(Copy, Clone, Debug, Deserialize, Miniconf)] #[derive(Copy, Clone, Debug, Deserialize, Miniconf)]
enum Conf { enum Conf {
@ -58,7 +58,7 @@ const APP: () = {
afes: (AFE0, AFE1), afes: (AFE0, AFE1),
adcs: (Adc0Input, Adc1Input), adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output), dacs: (Dac0Output, Dac1Output),
mqtt_settings: MqttSettings<Settings>, mqtt_config: MiniconfInterface<Settings>,
settings: Settings, settings: Settings,
timestamper: InputStamper, timestamper: InputStamper,
@ -71,7 +71,7 @@ const APP: () = {
// Configure the microcontroller // Configure the microcontroller
let (mut stabilizer, _pounder) = setup(c.core, c.device); let (mut stabilizer, _pounder) = setup(c.core, c.device);
let mqtt_settings = MqttSettings::new( let mqtt_config = MiniconfInterface::new(
stabilizer.net.stack, stabilizer.net.stack,
"", "",
"dt/sinara/lockin", "dt/sinara/lockin",
@ -108,7 +108,7 @@ const APP: () = {
afes: stabilizer.afes, afes: stabilizer.afes,
adcs: stabilizer.adcs, adcs: stabilizer.adcs,
dacs: stabilizer.dacs, dacs: stabilizer.dacs,
mqtt_settings, mqtt_config,
timestamper: stabilizer.timestamper, timestamper: stabilizer.timestamper,
settings, 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) -> ! { fn idle(mut c: idle::Context) -> ! {
loop { 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::Sleep) => cortex_m::asm::wfi(),
Some(Action::UpdateSettings) => { Some(Action::UpdateSettings) => {
c.spawn.settings_update().unwrap() 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) { 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.0.set_gain(settings.afe[0]);
c.resources.afes.1.set_gain(settings.afe[1]); c.resources.afes.1.set_gain(settings.afe[1]);

View File

@ -13,8 +13,8 @@ use embedded_hal::digital::v2::{InputPin, OutputPin};
use super::{ use super::{
adc, afe, cycle_counter::CycleCounter, dac, design_parameters, adc, afe, cycle_counter::CycleCounter, dac, design_parameters,
digital_input_stamper, eeprom, pounder, timers, DdsOutput, EthernetPhy, digital_input_stamper, eeprom, pounder, timers, DdsOutput, DigitalInput0,
NetworkStack, AFE0, AFE1, DigitalInput1, EthernetPhy, NetworkStack, AFE0, AFE1,
}; };
pub struct NetStorage { pub struct NetStorage {
@ -69,6 +69,7 @@ pub struct StabilizerDevices {
pub timestamp_timer: timers::TimestampTimer, pub timestamp_timer: timers::TimestampTimer,
pub net: NetworkDevices, pub net: NetworkDevices,
pub cycle_counter: CycleCounter, pub cycle_counter: CycleCounter,
pub digital_inputs: (DigitalInput0, DigitalInput1),
} }
/// The available Pounder-specific hardware interfaces. /// 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 mut eeprom_i2c = {
let sda = gpiof.pf0.into_alternate_af4().set_open_drain(); let sda = gpiof.pf0.into_alternate_af4().set_open_drain();
let scl = gpiof.pf1.into_alternate_af4().set_open_drain(); let scl = gpiof.pf1.into_alternate_af4().set_open_drain();
@ -884,6 +891,7 @@ pub fn setup(
adc_dac_timer: sampling_timer, adc_dac_timer: sampling_timer,
timestamp_timer, timestamp_timer,
cycle_counter, cycle_counter,
digital_inputs,
}; };
// info!("Version {} {}", build_info::PKG_VERSION, build_info::GIT_VERSION.unwrap()); // info!("Version {} {}", build_info::PKG_VERSION, build_info::GIT_VERSION.unwrap());

View File

@ -1,6 +1,9 @@
///! Module for all hardware-specific setup of Stabilizer ///! Module for all hardware-specific setup of Stabilizer
use stm32h7xx_hal as hal; use stm32h7xx_hal as hal;
// Re-export for the DigitalInputs below:
pub use embedded_hal::digital::v2::InputPin;
#[cfg(feature = "semihosting")] #[cfg(feature = "semihosting")]
use panic_semihosting as _; use panic_semihosting as _;
@ -34,6 +37,14 @@ pub type AFE1 = afe::ProgrammableGainAmplifier<
hal::gpio::gpiod::PD15<hal::gpio::Output<hal::gpio::PushPull>>, hal::gpio::gpiod::PD15<hal::gpio::Output<hal::gpio::PushPull>>,
>; >;
// Type alias for digital input 0 (DI0).
pub type DigitalInput0 =
hal::gpio::gpiog::PG9<hal::gpio::Input<hal::gpio::Floating>>;
// Type alias for digital input 1 (DI1).
pub type DigitalInput1 =
hal::gpio::gpioc::PC15<hal::gpio::Input<hal::gpio::Floating>>;
pub type NetworkStack = smoltcp_nal::NetworkStack< pub type NetworkStack = smoltcp_nal::NetworkStack<
'static, 'static,
'static, 'static,

View File

@ -2,7 +2,7 @@ use crate::hardware::{
design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack,
}; };
use miniconf::{minimq, MqttInterface}; use miniconf::minimq;
/// Potential actions for firmware to take. /// Potential actions for firmware to take.
pub enum Action { pub enum Action {
@ -14,17 +14,17 @@ pub enum Action {
} }
/// MQTT settings interface. /// MQTT settings interface.
pub struct MqttSettings<S> pub struct MiniconfInterface<S>
where where
S: miniconf::Miniconf + Default, S: miniconf::Miniconf + Default,
{ {
pub mqtt: MqttInterface<S, NetworkStack, minimq::consts::U256>, pub mqtt: miniconf::MqttInterface<S, NetworkStack, minimq::consts::U256>,
clock: CycleCounter, clock: CycleCounter,
phy: EthernetPhy, phy: EthernetPhy,
network_was_reset: bool, network_was_reset: bool,
} }
impl<S> MqttSettings<S> impl<S> MiniconfInterface<S>
where where
S: miniconf::Miniconf + Default, S: miniconf::Miniconf + Default,
{ {
@ -49,7 +49,8 @@ where
.unwrap() .unwrap()
}; };
MqttInterface::new(mqtt_client, prefix, S::default()).unwrap() miniconf::MqttInterface::new(mqtt_client, prefix, S::default())
.unwrap()
}; };
Self { Self {