diff --git a/Cargo.lock b/Cargo.lock index de1f22f..11ae3fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,7 @@ dependencies = [ "easybench", "miniconf", "ndarray", - "num", + "num-complex", "rand", "serde", ] @@ -472,19 +472,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bcece43b12349917e096cddfa66107277f123e6c96a5aea78711dc601a47152" -[[package]] -name = "num" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" -dependencies = [ - "num-complex", - "num-integer", - "num-iter", - "num-rational", - "num-traits", -] - [[package]] name = "num-complex" version = "0.4.0" @@ -492,6 +479,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085" dependencies = [ "num-traits", + "serde", ] [[package]] @@ -504,28 +492,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-iter" -version = "0.1.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-rational" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - [[package]] name = "num-traits" version = "0.2.14" diff --git a/dsp/Cargo.toml b/dsp/Cargo.toml index 0f82bd2..e3be45b 100644 --- a/dsp/Cargo.toml +++ b/dsp/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] serde = { version = "1.0", features = ["derive"], default-features = false } -num = { version = "0.4.0", default-features = false } +num-complex = { version = "0.4.0", features = ["serde"], default-features = false } miniconf = "0.1" [dev-dependencies] diff --git a/dsp/src/complex.rs b/dsp/src/complex.rs index 1eb003f..bf3e76d 100644 --- a/dsp/src/complex.rs +++ b/dsp/src/complex.rs @@ -1,4 +1,4 @@ -pub use num::Complex; +pub use num_complex::Complex; use super::{atan2, cossin}; diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index c76ab1b..d511ce1 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -4,19 +4,27 @@ use core::sync::atomic::{fence, Ordering}; -use stabilizer::{flatten_closures, hardware, net}; - -use miniconf::Miniconf; -use serde::Deserialize; - use dsp::iir; -use hardware::{ - Adc0Input, Adc1Input, AdcCode, AfeGain, Dac0Output, Dac1Output, DacCode, - DigitalInput0, DigitalInput1, InputPin, SystemTimer, AFE0, AFE1, +use stabilizer::{ + flatten_closures, + hardware::{ + self, + adc::{Adc0Input, Adc1Input, AdcCode}, + afe::Gain, + dac::{Dac0Output, Dac1Output, DacCode}, + embedded_hal::digital::v2::InputPin, + hal, + system_timer::SystemTimer, + DigitalInput0, DigitalInput1, AFE0, AFE1, + }, + net::{ + miniconf::Miniconf, + serde::Deserialize, + telemetry::{Telemetry, TelemetryBuffer}, + NetworkState, NetworkUsers, + }, }; -use net::{NetworkState, NetworkUsers, Telemetry, TelemetryBuffer}; - const SCALE: f32 = i16::MAX as _; // The number of cascaded IIR biquads per channel. Select 1 or 2! @@ -24,7 +32,7 @@ const IIR_CASCADE_LENGTH: usize = 1; #[derive(Clone, Copy, Debug, Deserialize, Miniconf)] pub struct Settings { - afe: [AfeGain; 2], + afe: [Gain; 2], iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2], allow_hold: bool, force_hold: bool, @@ -35,7 +43,7 @@ impl Default for Settings { fn default() -> Self { Self { // Analog frontend programmable gain amplifier gains (G1, G2, G5, G10) - afe: [AfeGain::G1, AfeGain::G1], + afe: [Gain::G1, Gain::G1], // IIR filter tap gains are an array `[b0, b1, b2, a1, a2]` such that the // new output is computed as `y0 = a1*y1 + a2*y2 + b0*x0 + b1*x1 + b2*x2`. // The array is `iir_state[channel-index][cascade-index][coeff-index]`. @@ -52,7 +60,7 @@ impl Default for Settings { } } -#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = stabilizer::hardware::SystemTimer)] +#[rtic::app(device = stabilizer::hardware::hal::stm32, peripherals = true, monotonic = stabilizer::hardware::system_timer::SystemTimer)] const APP: () = { struct Resources { afes: (AFE0, AFE1), @@ -71,7 +79,8 @@ const APP: () = { #[init(spawn=[telemetry, settings_update])] fn init(c: init::Context) -> init::LateResources { // Configure the microcontroller - let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device); + let (mut stabilizer, _pounder) = + hardware::setup::setup(c.core, c.device); let network = NetworkUsers::new( stabilizer.net.stack, @@ -100,7 +109,7 @@ const APP: () = { dacs: stabilizer.dacs, network, digital_inputs: stabilizer.digital_inputs, - telemetry: net::TelemetryBuffer::default(), + telemetry: TelemetryBuffer::default(), settings: Settings::default(), } } @@ -233,7 +242,7 @@ const APP: () = { #[task(binds = ETH, priority = 1)] fn eth(_: eth::Context) { - unsafe { stm32h7xx_hal::ethernet::interrupt_handler() } + unsafe { hal::ethernet::interrupt_handler() } } #[task(binds = SPI2, priority = 3)] diff --git a/src/bin/lockin.rs b/src/bin/lockin.rs index 6dd7658..46ffdc0 100644 --- a/src/bin/lockin.rs +++ b/src/bin/lockin.rs @@ -4,23 +4,29 @@ use core::sync::atomic::{fence, Ordering}; -use embedded_hal::digital::v2::InputPin; - -use serde::Deserialize; - use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL}; - -use stabilizer::{flatten_closures, hardware, net}; - -use hardware::{ - design_parameters, setup, Adc0Input, Adc1Input, AdcCode, AfeGain, - Dac0Output, Dac1Output, DacCode, DigitalInput0, DigitalInput1, - InputStamper, SystemTimer, AFE0, AFE1, +use stabilizer::{ + flatten_closures, + hardware::{ + self, + adc::{Adc0Input, Adc1Input, AdcCode}, + afe::Gain, + dac::{Dac0Output, Dac1Output, DacCode}, + design_parameters, + embedded_hal::digital::v2::InputPin, + hal, + input_stamper::InputStamper, + system_timer::SystemTimer, + DigitalInput0, DigitalInput1, AFE0, AFE1, + }, + net::{ + miniconf::Miniconf, + serde::Deserialize, + telemetry::{Telemetry, TelemetryBuffer}, + NetworkState, NetworkUsers, + }, }; -use miniconf::Miniconf; -use net::{NetworkState, NetworkUsers, Telemetry, TelemetryBuffer}; - // A constant sinusoid to send on the DAC output. // Full-scale gives a +/- 10.24V amplitude waveform. Scale it down to give +/- 1V. const ONE: i16 = ((1.0 / 10.24) * i16::MAX as f32) as _; @@ -47,7 +53,7 @@ enum LockinMode { #[derive(Copy, Clone, Debug, Deserialize, Miniconf)] pub struct Settings { - afe: [AfeGain; 2], + afe: [Gain; 2], lockin_mode: LockinMode, pll_tc: [u8; 2], @@ -63,7 +69,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - afe: [AfeGain::G1; 2], + afe: [Gain::G1; 2], lockin_mode: LockinMode::External, @@ -80,7 +86,7 @@ impl Default for Settings { } } -#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = stabilizer::hardware::SystemTimer)] +#[rtic::app(device = stabilizer::hardware::hal::stm32, peripherals = true, monotonic = stabilizer::hardware::system_timer::SystemTimer)] const APP: () = { struct Resources { afes: (AFE0, AFE1), @@ -99,7 +105,8 @@ const APP: () = { #[init(spawn=[settings_update, telemetry])] fn init(c: init::Context) -> init::LateResources { // Configure the microcontroller - let (mut stabilizer, _pounder) = setup(c.core, c.device); + let (mut stabilizer, _pounder) = + hardware::setup::setup(c.core, c.device); let network = NetworkUsers::new( stabilizer.net.stack, @@ -142,7 +149,7 @@ const APP: () = { network, digital_inputs: stabilizer.digital_inputs, timestamper: stabilizer.timestamper, - telemetry: net::TelemetryBuffer::default(), + telemetry: TelemetryBuffer::default(), settings, @@ -308,27 +315,7 @@ const APP: () = { #[task(binds = ETH, priority = 1)] fn eth(_: eth::Context) { - unsafe { stm32h7xx_hal::ethernet::interrupt_handler() } - } - - #[task(binds = SPI2, priority = 3)] - fn spi2(_: spi2::Context) { - panic!("ADC0 SPI error"); - } - - #[task(binds = SPI3, priority = 3)] - fn spi3(_: spi3::Context) { - panic!("ADC1 SPI error"); - } - - #[task(binds = SPI4, priority = 3)] - fn spi4(_: spi4::Context) { - panic!("DAC0 SPI error"); - } - - #[task(binds = SPI5, priority = 3)] - fn spi5(_: spi5::Context) { - panic!("DAC1 SPI error"); + unsafe { hal::ethernet::interrupt_handler() } } extern "C" { diff --git a/src/hardware/digital_input_stamper.rs b/src/hardware/input_stamper.rs similarity index 100% rename from src/hardware/digital_input_stamper.rs rename to src/hardware/input_stamper.rs diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs index 98c12fa..422f7b9 100644 --- a/src/hardware/mod.rs +++ b/src/hardware/mod.rs @@ -1,28 +1,19 @@ +pub use embedded_hal; ///! Module for all hardware-specific setup of Stabilizer -use stm32h7xx_hal as hal; +pub use stm32h7xx_hal as hal; -// Re-export for the DigitalInputs below: -pub use embedded_hal::digital::v2::InputPin; - -mod adc; -mod afe; -mod configuration; -mod cycle_counter; -mod dac; +pub mod adc; +pub mod afe; +pub mod cycle_counter; +pub mod dac; pub mod design_parameters; -mod digital_input_stamper; -mod eeprom; +pub mod input_stamper; pub mod pounder; -mod system_timer; -mod timers; +pub mod setup; +pub mod system_timer; -pub use adc::{Adc0Input, Adc1Input, AdcCode}; -pub use afe::Gain as AfeGain; -pub use cycle_counter::CycleCounter; -pub use dac::{Dac0Output, Dac1Output, DacCode}; -pub use digital_input_stamper::InputStamper; -pub use pounder::DdsOutput; -pub use system_timer::SystemTimer; +mod eeprom; +mod timers; // Type alias for the analog front-end (AFE) for ADC0. pub type AFE0 = afe::ProgrammableGainAmplifier< @@ -52,8 +43,6 @@ pub type NetworkStack = smoltcp_nal::NetworkStack< pub type EthernetPhy = hal::ethernet::phy::LAN8742A; -pub use configuration::{setup, PounderDevices, StabilizerDevices}; - #[inline(never)] #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { diff --git a/src/hardware/pounder/mod.rs b/src/hardware/pounder/mod.rs index 3105632..a56446e 100644 --- a/src/hardware/pounder/mod.rs +++ b/src/hardware/pounder/mod.rs @@ -1,22 +1,15 @@ +use super::hal; +use embedded_hal::{adc::OneShot, blocking::spi::Transfer}; use serde::{Deserialize, Serialize}; pub mod attenuators; -mod dds_output; +pub mod dds_output; pub mod hrtimer; -mod rf_power; +pub mod rf_power; #[cfg(feature = "pounder_v1_1")] pub mod timestamp; -pub use dds_output::DdsOutput; - -use super::hal; - -use attenuators::AttenuatorInterface; -use rf_power::PowerMeasurementInterface; - -use embedded_hal::{adc::OneShot, blocking::spi::Transfer}; - pub enum GpioPin { Led4Green = 0, Led5Red = 1, @@ -329,7 +322,7 @@ impl PounderDevices { } } -impl AttenuatorInterface for PounderDevices { +impl attenuators::AttenuatorInterface for PounderDevices { /// Reset all of the attenuators to a power-on default state. fn reset_attenuators(&mut self) -> Result<(), Error> { self.mcp23017 @@ -376,7 +369,7 @@ impl AttenuatorInterface for PounderDevices { } } -impl PowerMeasurementInterface for PounderDevices { +impl rf_power::PowerMeasurementInterface for PounderDevices { /// Sample an ADC channel. /// /// Args: diff --git a/src/hardware/configuration.rs b/src/hardware/setup.rs similarity index 99% rename from src/hardware/configuration.rs rename to src/hardware/setup.rs index 7d8c92b..515dd51 100644 --- a/src/hardware/configuration.rs +++ b/src/hardware/setup.rs @@ -15,9 +15,10 @@ use smoltcp_nal::smoltcp; use embedded_hal::digital::v2::{InputPin, OutputPin}; use super::{ - adc, afe, cycle_counter::CycleCounter, dac, design_parameters, - digital_input_stamper, eeprom, pounder, system_timer, timers, DdsOutput, - DigitalInput0, DigitalInput1, EthernetPhy, NetworkStack, AFE0, AFE1, + adc, afe, cycle_counter::CycleCounter, dac, design_parameters, eeprom, + input_stamper::InputStamper, pounder, pounder::dds_output::DdsOutput, + system_timer, timers, DigitalInput0, DigitalInput1, EthernetPhy, + NetworkStack, AFE0, AFE1, }; pub struct NetStorage { @@ -84,7 +85,7 @@ pub struct StabilizerDevices { pub afes: (AFE0, AFE1), pub adcs: (adc::Adc0Input, adc::Adc1Input), pub dacs: (dac::Dac0Output, dac::Dac1Output), - pub timestamper: digital_input_stamper::InputStamper, + pub timestamper: InputStamper, pub adc_dac_timer: timers::SamplingTimer, pub timestamp_timer: timers::TimestampTimer, pub net: NetworkDevices, @@ -509,10 +510,7 @@ pub fn setup( let input_stamper = { let trigger = gpioa.pa3.into_alternate_af2(); - digital_input_stamper::InputStamper::new( - trigger, - timestamp_timer_channels.ch4, - ) + InputStamper::new(trigger, timestamp_timer_channels.ch4) }; let digital_inputs = { diff --git a/src/net/mod.rs b/src/net/mod.rs index bdb871f..e9a2538 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -5,26 +5,28 @@ ///! telemetry (via MQTT), configuration of run-time settings (via MQTT + Miniconf), and live data ///! streaming over raw UDP/TCP sockets. This module encompasses the main processing routines ///! related to Stabilizer networking operations. +pub use heapless; +pub use miniconf; +pub use serde; + +pub mod messages; +pub mod miniconf_client; +pub mod network_processor; +pub mod shared; +pub mod telemetry; + +use crate::hardware::{cycle_counter::CycleCounter, EthernetPhy, NetworkStack}; +use messages::{MqttMessage, SettingsResponse}; +use miniconf_client::MiniconfClient; +use network_processor::NetworkProcessor; +use shared::NetworkManager; +use telemetry::TelemetryClient; + +use core::fmt::Write; use heapless::String; use miniconf::Miniconf; use serde::Serialize; -use core::fmt::Write; - -mod messages; -mod miniconf_client; -mod network_processor; -mod shared; -mod telemetry; - -use crate::hardware::{CycleCounter, EthernetPhy, NetworkStack}; -use messages::{MqttMessage, SettingsResponse}; - -pub use miniconf_client::MiniconfClient; -pub use network_processor::NetworkProcessor; -pub use shared::NetworkManager; -pub use telemetry::{Telemetry, TelemetryBuffer, TelemetryClient}; - pub type NetworkReference = shared::NetworkStackProxy<'static, NetworkStack>; #[derive(Copy, Clone, PartialEq)] diff --git a/src/net/network_processor.rs b/src/net/network_processor.rs index a64d6e7..a8168d3 100644 --- a/src/net/network_processor.rs +++ b/src/net/network_processor.rs @@ -4,7 +4,7 @@ ///! The network processir is a small taks to regularly process incoming data over ethernet, handle ///! the ethernet PHY state, and reset the network as appropriate. use super::{NetworkReference, UpdateState}; -use crate::hardware::{CycleCounter, EthernetPhy}; +use crate::hardware::{cycle_counter::CycleCounter, EthernetPhy}; /// Processor for managing network hardware. pub struct NetworkProcessor { diff --git a/src/net/telemetry.rs b/src/net/telemetry.rs index d976fee..40a5ce3 100644 --- a/src/net/telemetry.rs +++ b/src/net/telemetry.rs @@ -16,7 +16,7 @@ use serde::Serialize; use super::NetworkReference; use crate::hardware::{ - design_parameters::MQTT_BROKER, AdcCode, AfeGain, DacCode, + adc::AdcCode, afe::Gain, dac::DacCode, design_parameters::MQTT_BROKER, }; /// The telemetry client for reporting telemetry data over MQTT. @@ -73,7 +73,7 @@ impl TelemetryBuffer { /// /// # Returns /// The finalized telemetry structure that can be serialized and reported. - pub fn finalize(self, afe0: AfeGain, afe1: AfeGain) -> Telemetry { + pub fn finalize(self, afe0: Gain, afe1: Gain) -> Telemetry { let in0_volts = Into::::into(self.adcs[0]) / afe0.as_multiplier(); let in1_volts = Into::::into(self.adcs[1]) / afe1.as_multiplier();