Adding settings support for lockin-external

This commit is contained in:
Ryan Summers 2021-02-17 16:53:53 +01:00
parent b66f80f784
commit 50476f68fd
4 changed files with 73 additions and 35 deletions

View File

@ -1,5 +1,5 @@
use serde::Deserialize;
use miniconf::StringSet; use miniconf::StringSet;
use serde::Deserialize;
use super::{abs, copysign, macc, max, min}; use super::{abs, copysign, macc, max, min};
use core::f32; use core::f32;

View File

@ -14,8 +14,8 @@ use serde::Deserialize;
use dsp::iir; use dsp::iir;
use hardware::{ use hardware::{
Adc0Input, Adc1Input, CycleCounter, Dac0Output, Dac1Output, NetworkStack, Adc0Input, Adc1Input, AfeGain, CycleCounter, Dac0Output, Dac1Output,
AFE0, AFE1, AfeGain, NetworkStack, AFE0, AFE1,
}; };
const SCALE: f32 = i16::MAX as _; const SCALE: f32 = i16::MAX as _;

View File

@ -15,17 +15,52 @@ use stabilizer::{hardware, hardware::design_parameters};
use dsp::{lockin::Lockin, rpll::RPLL, Accu}; use dsp::{lockin::Lockin, rpll::RPLL, Accu};
use hardware::{ use hardware::{
Adc0Input, Adc1Input, Dac0Output, Dac1Output, InputStamper, AFE0, AFE1, Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, InputStamper, AFE0,
AFE1,
}; };
#[derive(Deserialize, StringSet)] #[derive(Debug, Clone, Copy, Deserialize, StringSet)]
pub struct Settings { pub struct DspData {
data: u32, // frequency settling time (log2 counter cycles)
frequency_settling_time: u8,
// phase settling time
phase_settling_time: u8,
// Harmonic index of the LO: -1 to _de_modulate the fundamental (complex conjugate)
harmonic: i32,
// Demodulation LO phase offset
phase_offset: i32,
// Log2 lowpass time constant
time_constant: u8,
} }
impl Settings { impl Default for DspData {
pub fn new() -> Self { fn default() -> Self {
Self { data: 5 } Self {
frequency_settling_time: 21,
phase_settling_time: 21,
harmonic: -1,
phase_offset: 0,
time_constant: 6,
}
}
}
#[derive(Debug, Deserialize, StringSet)]
pub struct Settings {
afe: [AfeGain; 2],
dsp: DspData,
}
impl Default for Settings {
fn default() -> Self {
Self {
afe: [AfeGain::G1, AfeGain::G1],
dsp: DspData::default(),
}
} }
} }
@ -44,7 +79,9 @@ const APP: () = {
timestamper: InputStamper, timestamper: InputStamper,
pll: RPLL, pll: RPLL,
lockin: Lockin, lockin: Lockin,
parameters: DspData,
} }
#[init] #[init]
@ -63,7 +100,7 @@ const APP: () = {
.unwrap() .unwrap()
}; };
MqttInterface::new(mqtt_client, "stabilizer", Settings::new()) MqttInterface::new(mqtt_client, "stabilizer", Settings::default())
.unwrap() .unwrap()
}; };
@ -94,9 +131,9 @@ const APP: () = {
dacs: stabilizer.dacs, dacs: stabilizer.dacs,
timestamper: stabilizer.timestamper, timestamper: stabilizer.timestamper,
clock: stabilizer.cycle_counter, clock: stabilizer.cycle_counter,
pll, pll,
lockin: Lockin::default(), lockin: Lockin::default(),
parameters: DspData::default(),
} }
} }
@ -107,7 +144,7 @@ const APP: () = {
/// This is an implementation of a externally (DI0) referenced PLL lockin on the ADC0 signal. /// This is an implementation of a externally (DI0) referenced PLL lockin on the ADC0 signal.
/// It outputs either I/Q or power/phase on DAC0/DAC1. Data is normalized to full scale. /// It outputs either I/Q or power/phase on DAC0/DAC1. Data is normalized to full scale.
/// PLL bandwidth, filter bandwidth, slope, and x/y or power/phase post-filters are available. /// PLL bandwidth, filter bandwidth, slope, and x/y or power/phase post-filters are available.
#[task(binds=DMA1_STR4, resources=[adcs, dacs, lockin, timestamper, pll], priority=2)] #[task(binds=DMA1_STR4, resources=[adcs, dacs, lockin, timestamper, pll, parameters], 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(),
@ -120,6 +157,7 @@ const APP: () = {
]; ];
let lockin = c.resources.lockin; let lockin = c.resources.lockin;
let params = c.resources.parameters;
let timestamp = c let timestamp = c
.resources .resources
@ -129,34 +167,27 @@ const APP: () = {
.map(|t| t as i32); .map(|t| t as i32);
let (pll_phase, pll_frequency) = c.resources.pll.update( let (pll_phase, pll_frequency) = c.resources.pll.update(
timestamp, timestamp,
21, // frequency settling time (log2 counter cycles), TODO: expose params.frequency_settling_time,
21, // phase settling time, TODO: expose params.phase_settling_time,
); );
// Harmonic index of the LO: -1 to _de_modulate the fundamental (complex conjugate)
let harmonic: i32 = -1; // TODO: expose
// Demodulation LO phase offset
let phase_offset: i32 = 0; // TODO: expose
// Log2 lowpass time constant
let time_constant: u8 = 6; // TODO: expose
let sample_frequency = ((pll_frequency let sample_frequency = ((pll_frequency
// half-up rounding bias // half-up rounding bias
// .wrapping_add(1 << design_parameters::SAMPLE_BUFFER_SIZE_LOG2 - 1) // .wrapping_add(1 << design_parameters::SAMPLE_BUFFER_SIZE_LOG2 - 1)
>> design_parameters::SAMPLE_BUFFER_SIZE_LOG2) >> design_parameters::SAMPLE_BUFFER_SIZE_LOG2)
as i32) as i32)
.wrapping_mul(harmonic); // Harmonic index of the LO: -1 to _de_modulate the fundamental (complex conjugate)
let sample_phase = .wrapping_mul(params.harmonic);
phase_offset.wrapping_add(pll_phase.wrapping_mul(harmonic)); let sample_phase = params
.phase_offset
.wrapping_add(pll_phase.wrapping_mul(params.harmonic));
let output = adc_samples[0] let output = adc_samples[0]
.iter() .iter()
.zip(Accu::new(sample_phase, sample_frequency)) .zip(Accu::new(sample_phase, sample_frequency))
// Convert to signed, MSB align the ADC sample. // Convert to signed, MSB align the ADC sample.
.map(|(&sample, phase)| { .map(|(&sample, phase)| {
lockin.update(sample as i16, phase, time_constant) lockin.update(sample as i16, phase, params.time_constant)
}) })
.last() .last()
.unwrap(); .unwrap();
@ -201,11 +232,16 @@ const APP: () = {
} }
} }
#[task(priority = 1, resources=[mqtt_interface, afes])] #[task(priority = 1, resources=[mqtt_interface, afes, parameters])]
fn settings_update(c: settings_update::Context) { fn settings_update(mut c: settings_update::Context) {
let _settings = &c.resources.mqtt_interface.settings; let settings = &c.resources.mqtt_interface.settings;
//c.resources.iir_ch.lock(|iir| *iir = settings.iir);
// TODO: Update AFEs // Update AFEs
c.resources.afes.0.set_gain(settings.afe[0]);
c.resources.afes.1.set_gain(settings.afe[1]);
// Update DSP parameters.
c.resources.parameters.lock(|params| *params = settings.dsp);
} }
#[task(binds = ETH, priority = 1)] #[task(binds = ETH, priority = 1)]

View File

@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};
use miniconf::StringSet; use miniconf::StringSet;
use serde::{Deserialize, Serialize};
use core::convert::TryFrom; use core::convert::TryFrom;
use enum_iterator::IntoEnumIterator; use enum_iterator::IntoEnumIterator;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, IntoEnumIterator, StringSet)] #[derive(
Copy, Clone, Debug, Serialize, Deserialize, IntoEnumIterator, StringSet,
)]
pub enum Gain { pub enum Gain {
G1 = 0b00, G1 = 0b00,
G2 = 0b01, G2 = 0b01,