Adding prototype PHY reset detection

This commit is contained in:
Ryan Summers 2021-03-17 20:16:13 +01:00
parent 4797f0e97d
commit a717630c53
8 changed files with 141 additions and 117 deletions

2
Cargo.lock generated
View File

@ -707,7 +707,7 @@ dependencies = [
[[package]] [[package]]
name = "smoltcp-nal" name = "smoltcp-nal"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/quartiq/smoltcp-nal.git?branch=main#56519012d7c6a382eaa0d7ecb26f2701771d9ce8" source = "git+https://github.com/quartiq/smoltcp-nal.git?branch=feature/reset-support#a6db6579100987502563578fb386109ad08758a7"
dependencies = [ dependencies = [
"embedded-nal", "embedded-nal",
"heapless 0.6.1", "heapless 0.6.1",

View File

@ -56,7 +56,7 @@ git = "https://github.com/quartiq/minimq.git"
[patch.crates-io.smoltcp-nal] [patch.crates-io.smoltcp-nal]
git = "https://github.com/quartiq/smoltcp-nal.git" git = "https://github.com/quartiq/smoltcp-nal.git"
branch = "main" branch = "feature/reset-support"
[patch.crates-io.serde-json-core] [patch.crates-io.serde-json-core]
git = "https://github.com/rust-embedded-community/serde-json-core.git" git = "https://github.com/rust-embedded-community/serde-json-core.git"

View File

@ -4,17 +4,18 @@
use stm32h7xx_hal as hal; use stm32h7xx_hal as hal;
use stabilizer::hardware; use stabilizer::{hardware, net};
use miniconf::{minimq, Miniconf, MqttInterface}; use miniconf::Miniconf;
use serde::Deserialize; use serde::Deserialize;
use dsp::iir; use dsp::iir;
use hardware::{ use hardware::{
Adc0Input, Adc1Input, AfeGain, CycleCounter, Dac0Output, Dac1Output, Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, AFE0, AFE1,
NetworkStack, AFE0, AFE1,
}; };
use net::{Action, MqttSettings};
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!
@ -41,9 +42,7 @@ const APP: () = {
afes: (AFE0, AFE1), afes: (AFE0, AFE1),
adcs: (Adc0Input, Adc1Input), adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output), dacs: (Dac0Output, Dac1Output),
mqtt_interface: mqtt_settings: MqttSettings<Settings>,
MqttInterface<Settings, NetworkStack, minimq::consts::U256>,
clock: CycleCounter,
// 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])]
@ -57,23 +56,13 @@ const APP: () = {
// 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_interface = { let mqtt_settings = MqttSettings::new(
let mqtt_client = { stabilizer.net.stack,
minimq::MqttClient::new( "",
hardware::design_parameters::MQTT_BROKER.into(), "dt/sinara/stabilizer",
"", stabilizer.net.phy,
stabilizer.net.stack, stabilizer.cycle_counter,
) );
.unwrap()
};
MqttInterface::new(
mqtt_client,
"dt/sinara/stabilizer",
Settings::default(),
)
.unwrap()
};
// Enable ADC/DAC events // Enable ADC/DAC events
stabilizer.adcs.0.start(); stabilizer.adcs.0.start();
@ -85,11 +74,10 @@ const APP: () = {
stabilizer.adc_dac_timer.start(); stabilizer.adc_dac_timer.start();
init::LateResources { init::LateResources {
mqtt_interface,
afes: stabilizer.afes, afes: stabilizer.afes,
adcs: stabilizer.adcs, adcs: stabilizer.adcs,
dacs: stabilizer.dacs, dacs: stabilizer.dacs,
clock: stabilizer.cycle_counter, mqtt_settings,
} }
} }
@ -138,44 +126,22 @@ const APP: () = {
} }
} }
#[idle(resources=[mqtt_interface, clock], spawn=[settings_update])] #[idle(resources=[mqtt_settings], spawn=[settings_update])]
fn idle(mut c: idle::Context) -> ! { fn idle(mut c: idle::Context) -> ! {
let clock = c.resources.clock;
loop { loop {
let sleep = c.resources.mqtt_interface.lock(|interface| { match c.resources.mqtt_settings.lock(|settings| settings.update()) {
match interface.network_stack().poll(clock.current_ms()) { Some(Action::Sleep) => cortex_m::asm::wfi(),
Ok(updated) => !updated, Some(Action::UpdateSettings) => {
Err(err) => { c.spawn.settings_update().unwrap()
log::info!("Network error: {:?}", err);
false
}
} }
}); _ => {}
match c
.resources
.mqtt_interface
.lock(|interface| interface.update())
{
Ok(update) => {
if update {
c.spawn.settings_update().unwrap();
} else if sleep {
cortex_m::asm::wfi();
}
}
Err(miniconf::MqttError::Network(
smoltcp_nal::NetworkError::NoIpAddress,
)) => {}
Err(error) => log::info!("Unexpected error: {:?}", error),
} }
} }
} }
#[task(priority = 1, resources=[mqtt_interface, afes, iir_ch])] #[task(priority = 1, resources=[mqtt_settings, afes, iir_ch])]
fn settings_update(mut c: settings_update::Context) { fn settings_update(mut c: settings_update::Context) {
let settings = &c.resources.mqtt_interface.settings; let settings = &c.resources.mqtt_settings.mqtt_interface.settings;
// Update the IIR channels. // Update the IIR channels.
c.resources.iir_ch.lock(|iir| *iir = settings.iir_ch); c.resources.iir_ch.lock(|iir| *iir = settings.iir_ch);

View File

@ -4,16 +4,18 @@
use generic_array::typenum::U4; use generic_array::typenum::U4;
use miniconf::{minimq, Miniconf, MqttInterface};
use serde::Deserialize; use serde::Deserialize;
use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL}; use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL};
use stabilizer::hardware::{ use stabilizer::hardware::{
design_parameters, setup, Adc0Input, Adc1Input, AfeGain, CycleCounter, design_parameters, setup, Adc0Input, Adc1Input, AfeGain, Dac0Output,
Dac0Output, Dac1Output, InputStamper, NetworkStack, AFE0, AFE1, Dac1Output, InputStamper, AFE0, AFE1,
}; };
use miniconf::Miniconf;
use stabilizer::net::{Action, MqttSettings};
#[derive(Copy, Clone, Debug, Deserialize, Miniconf)] #[derive(Copy, Clone, Debug, Deserialize, Miniconf)]
enum Conf { enum Conf {
PowerPhase, PowerPhase,
@ -56,11 +58,7 @@ const APP: () = {
afes: (AFE0, AFE1), afes: (AFE0, AFE1),
adcs: (Adc0Input, Adc1Input), adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output), dacs: (Dac0Output, Dac1Output),
clock: CycleCounter, mqtt_settings: MqttSettings<Settings>,
mqtt_interface:
MqttInterface<Settings, NetworkStack, minimq::consts::U256>,
settings: Settings, settings: Settings,
timestamper: InputStamper, timestamper: InputStamper,
@ -73,21 +71,13 @@ 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_interface = { let mqtt_settings = MqttSettings::new(
let mqtt_client = minimq::MqttClient::new( stabilizer.net.stack,
design_parameters::MQTT_BROKER.into(), "",
"", "dt/sinara/lockin",
stabilizer.net.stack, stabilizer.net.phy,
) stabilizer.cycle_counter,
.unwrap(); );
MqttInterface::new(
mqtt_client,
"dt/sinara/lockin",
Settings::default(),
)
.unwrap()
};
let settings = Settings::default(); let settings = Settings::default();
@ -118,10 +108,8 @@ const APP: () = {
afes: stabilizer.afes, afes: stabilizer.afes,
adcs: stabilizer.adcs, adcs: stabilizer.adcs,
dacs: stabilizer.dacs, dacs: stabilizer.dacs,
mqtt_settings,
timestamper: stabilizer.timestamper, timestamper: stabilizer.timestamper,
clock: stabilizer.cycle_counter,
mqtt_interface,
settings, settings,
@ -202,44 +190,22 @@ const APP: () = {
} }
} }
#[idle(resources=[mqtt_interface, clock], spawn=[settings_update])] #[idle(resources=[mqtt_settings], spawn=[settings_update])]
fn idle(mut c: idle::Context) -> ! { fn idle(mut c: idle::Context) -> ! {
let clock = c.resources.clock;
loop { loop {
let sleep = c.resources.mqtt_interface.lock(|interface| { match c.resources.mqtt_settings.lock(|settings| settings.update()) {
match interface.network_stack().poll(clock.current_ms()) { Some(Action::Sleep) => cortex_m::asm::wfi(),
Ok(updated) => !updated, Some(Action::UpdateSettings) => {
Err(err) => { c.spawn.settings_update().unwrap()
log::info!("Network error: {:?}", err);
false
}
} }
}); _ => {}
match c
.resources
.mqtt_interface
.lock(|interface| interface.update())
{
Ok(update) => {
if update {
c.spawn.settings_update().unwrap();
} else if sleep {
cortex_m::asm::wfi();
}
}
Err(miniconf::MqttError::Network(
smoltcp_nal::NetworkError::NoIpAddress,
)) => {}
Err(error) => log::info!("Unexpected error: {:?}", error),
} }
} }
} }
#[task(priority = 1, resources=[mqtt_interface, settings, afes])] #[task(priority = 1, resources=[mqtt_settings, settings, afes])]
fn settings_update(mut c: settings_update::Context) { fn settings_update(mut c: settings_update::Context) {
let settings = &c.resources.mqtt_interface.settings; let settings = &c.resources.mqtt_settings.mqtt_interface.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, NetworkStack, digital_input_stamper, eeprom, pounder, timers, DdsOutput, EthernetPhy,
AFE0, AFE1, NetworkStack, AFE0, AFE1,
}; };
pub struct NetStorage { pub struct NetStorage {
@ -56,7 +56,7 @@ impl NetStorage {
/// The available networking devices on Stabilizer. /// The available networking devices on Stabilizer.
pub struct NetworkDevices { pub struct NetworkDevices {
pub stack: NetworkStack, pub stack: NetworkStack,
pub phy: ethernet::phy::LAN8742A<ethernet::EthernetMAC>, pub phy: EthernetPhy,
} }
/// The available hardware interfaces on Stabilizer. /// The available hardware interfaces on Stabilizer.

View File

@ -40,6 +40,8 @@ pub type NetworkStack = smoltcp_nal::NetworkStack<
hal::ethernet::EthernetDMA<'static>, hal::ethernet::EthernetDMA<'static>,
>; >;
pub type EthernetPhy = hal::ethernet::phy::LAN8742A<hal::ethernet::EthernetMAC>;
pub use configuration::{setup, PounderDevices, StabilizerDevices}; pub use configuration::{setup, PounderDevices, StabilizerDevices};
#[inline(never)] #[inline(never)]

View File

@ -5,3 +5,4 @@
extern crate log; extern crate log;
pub mod hardware; pub mod hardware;
pub mod net;

89
src/net/mod.rs Normal file
View File

@ -0,0 +1,89 @@
use crate::hardware::{
design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack,
};
use miniconf::{minimq, MqttInterface};
pub enum Action {
Sleep,
UpdateSettings,
}
pub struct MqttSettings<S>
where
S: miniconf::Miniconf + Default,
{
pub mqtt_interface: MqttInterface<S, NetworkStack, minimq::consts::U256>,
clock: CycleCounter,
phy: EthernetPhy,
network_was_reset: bool,
}
impl<S> MqttSettings<S>
where
S: miniconf::Miniconf + Default,
{
pub fn new(
stack: NetworkStack,
client_id: &str,
prefix: &str,
phy: EthernetPhy,
clock: CycleCounter,
) -> Self {
let mqtt_interface = {
let mqtt_client = {
minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack)
.unwrap()
};
MqttInterface::new(mqtt_client, prefix, S::default()).unwrap()
};
Self {
mqtt_interface,
clock,
phy,
network_was_reset: false,
}
}
pub fn update(&mut self) -> Option<Action> {
let now = self.clock.current_ms();
let sleep = match self.mqtt_interface.network_stack().poll(now) {
Ok(updated) => !updated,
Err(err) => {
log::info!("Network error: {:?}", err);
false
}
};
// If the PHY indicates there's no more ethernet link, reset the network stack and close all
// sockets.
if self.phy.poll_link() == false {
// Only reset the network stack once per link reconnection. This prevents us from
// sending an excessive number of DHCP requests.
if !self.network_was_reset {
self.network_was_reset = true;
self.mqtt_interface.network_stack().reset();
}
} else {
self.network_was_reset = false;
}
match self.mqtt_interface.update() {
Ok(true) => Some(Action::UpdateSettings),
Ok(false) if sleep => Some(Action::Sleep),
Ok(_) => None,
Err(miniconf::MqttError::Network(
smoltcp_nal::NetworkError::NoIpAddress,
)) => None,
Err(error) => {
log::info!("Unexpected error: {:?}", error);
None
}
}
}
}