Merge pull request #314 from quartiq/feature/phy-reset
Adding support for PHY reset detection
This commit is contained in:
commit
686a15cfca
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -710,7 +710,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?rev=56519012d7#56519012d7c6a382eaa0d7ecb26f2701771d9ce8"
|
source = "git+https://github.com/quartiq/smoltcp-nal.git?rev=7572183#757218316620e074405bc38ff5502ef17a8a3499"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"embedded-nal",
|
"embedded-nal",
|
||||||
"heapless 0.6.1",
|
"heapless 0.6.1",
|
||||||
|
@ -60,7 +60,7 @@ rev = "314fa5587d"
|
|||||||
|
|
||||||
[dependencies.smoltcp-nal]
|
[dependencies.smoltcp-nal]
|
||||||
git = "https://github.com/quartiq/smoltcp-nal.git"
|
git = "https://github.com/quartiq/smoltcp-nal.git"
|
||||||
rev = "56519012d7"
|
rev = "7572183"
|
||||||
|
|
||||||
[patch.crates-io.minimq]
|
[patch.crates-io.minimq]
|
||||||
git = "https://github.com/quartiq/minimq.git"
|
git = "https://github.com/quartiq/minimq.git"
|
||||||
|
@ -2,17 +2,19 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
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, DigitalInput1,
|
||||||
DigitalInput1, InputPin, NetworkStack, AFE0, AFE1,
|
InputPin, AFE0, AFE1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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!
|
||||||
@ -44,9 +46,7 @@ const APP: () = {
|
|||||||
digital_input1: DigitalInput1,
|
digital_input1: DigitalInput1,
|
||||||
adcs: (Adc0Input, Adc1Input),
|
adcs: (Adc0Input, Adc1Input),
|
||||||
dacs: (Dac0Output, Dac1Output),
|
dacs: (Dac0Output, Dac1Output),
|
||||||
mqtt_interface:
|
mqtt_config: MiniconfInterface<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])]
|
||||||
@ -59,23 +59,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_config = MiniconfInterface::new(
|
||||||
let mqtt_client = {
|
|
||||||
minimq::MqttClient::new(
|
|
||||||
hardware::design_parameters::MQTT_BROKER.into(),
|
|
||||||
"",
|
|
||||||
stabilizer.net.stack,
|
stabilizer.net.stack,
|
||||||
)
|
"",
|
||||||
.unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
MqttInterface::new(
|
|
||||||
mqtt_client,
|
|
||||||
"dt/sinara/stabilizer",
|
"dt/sinara/stabilizer",
|
||||||
Settings::default(),
|
stabilizer.net.phy,
|
||||||
)
|
stabilizer.cycle_counter,
|
||||||
.unwrap()
|
);
|
||||||
};
|
|
||||||
|
|
||||||
// Spawn a settings update for default settings.
|
// Spawn a settings update for default settings.
|
||||||
c.spawn.settings_update().unwrap();
|
c.spawn.settings_update().unwrap();
|
||||||
@ -90,11 +80,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_config,
|
||||||
digital_input1: stabilizer.digital_inputs.1,
|
digital_input1: stabilizer.digital_inputs.1,
|
||||||
settings: Settings::default(),
|
settings: Settings::default(),
|
||||||
}
|
}
|
||||||
@ -151,44 +140,26 @@ const APP: () = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle(resources=[mqtt_interface, clock], spawn=[settings_update])]
|
#[idle(resources=[mqtt_config], 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 interface.network_stack().poll(clock.current_ms()) {
|
|
||||||
Ok(updated) => !updated,
|
|
||||||
Err(err) => {
|
|
||||||
log::info!("Network error: {:?}", err);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
match c
|
match c
|
||||||
.resources
|
.resources
|
||||||
.mqtt_interface
|
.mqtt_config
|
||||||
.lock(|interface| interface.update())
|
.lock(|config_interface| config_interface.update())
|
||||||
{
|
{
|
||||||
Ok(update) => {
|
Some(Action::Sleep) => cortex_m::asm::wfi(),
|
||||||
if update {
|
Some(Action::UpdateSettings) => {
|
||||||
c.spawn.settings_update().unwrap();
|
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, settings])]
|
#[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_interface.settings;
|
let settings = &c.resources.mqtt_config.mqtt.settings;
|
||||||
|
|
||||||
// Update the IIR channels.
|
// Update the IIR channels.
|
||||||
c.resources.settings.lock(|current| *current = *settings);
|
c.resources.settings.lock(|current| *current = *settings);
|
||||||
|
@ -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, MiniconfInterface};
|
||||||
|
|
||||||
#[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_config: MiniconfInterface<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_config = MiniconfInterface::new(
|
||||||
let mqtt_client = minimq::MqttClient::new(
|
|
||||||
design_parameters::MQTT_BROKER.into(),
|
|
||||||
"",
|
|
||||||
stabilizer.net.stack,
|
stabilizer.net.stack,
|
||||||
)
|
"",
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
MqttInterface::new(
|
|
||||||
mqtt_client,
|
|
||||||
"dt/sinara/lockin",
|
"dt/sinara/lockin",
|
||||||
Settings::default(),
|
stabilizer.net.phy,
|
||||||
)
|
stabilizer.cycle_counter,
|
||||||
.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_config,
|
||||||
timestamper: stabilizer.timestamper,
|
timestamper: stabilizer.timestamper,
|
||||||
clock: stabilizer.cycle_counter,
|
|
||||||
|
|
||||||
mqtt_interface,
|
|
||||||
|
|
||||||
settings,
|
settings,
|
||||||
|
|
||||||
@ -202,44 +190,26 @@ const APP: () = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle(resources=[mqtt_interface, clock], spawn=[settings_update])]
|
#[idle(resources=[mqtt_config], 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 interface.network_stack().poll(clock.current_ms()) {
|
|
||||||
Ok(updated) => !updated,
|
|
||||||
Err(err) => {
|
|
||||||
log::info!("Network error: {:?}", err);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
match c
|
match c
|
||||||
.resources
|
.resources
|
||||||
.mqtt_interface
|
.mqtt_config
|
||||||
.lock(|interface| interface.update())
|
.lock(|config_interface| config_interface.update())
|
||||||
{
|
{
|
||||||
Ok(update) => {
|
Some(Action::Sleep) => cortex_m::asm::wfi(),
|
||||||
if update {
|
Some(Action::UpdateSettings) => {
|
||||||
c.spawn.settings_update().unwrap();
|
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_config, 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_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]);
|
||||||
|
@ -14,7 +14,7 @@ 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, DigitalInput0,
|
digital_input_stamper, eeprom, pounder, timers, DdsOutput, DigitalInput0,
|
||||||
DigitalInput1, NetworkStack, AFE0, AFE1,
|
DigitalInput1, EthernetPhy, 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.
|
||||||
|
@ -51,6 +51,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)]
|
||||||
|
@ -5,3 +5,4 @@
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
pub mod hardware;
|
pub mod hardware;
|
||||||
|
pub mod net;
|
||||||
|
109
src/net/mod.rs
Normal file
109
src/net/mod.rs
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
use crate::hardware::{
|
||||||
|
design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack,
|
||||||
|
};
|
||||||
|
|
||||||
|
use miniconf::minimq;
|
||||||
|
|
||||||
|
/// Potential actions for firmware to take.
|
||||||
|
pub enum Action {
|
||||||
|
/// Indicates that firmware can sleep for the next event.
|
||||||
|
Sleep,
|
||||||
|
|
||||||
|
/// Indicates that settings have updated and firmware needs to propogate changes.
|
||||||
|
UpdateSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// MQTT settings interface.
|
||||||
|
pub struct MiniconfInterface<S>
|
||||||
|
where
|
||||||
|
S: miniconf::Miniconf + Default,
|
||||||
|
{
|
||||||
|
pub mqtt: miniconf::MqttInterface<S, NetworkStack, minimq::consts::U256>,
|
||||||
|
clock: CycleCounter,
|
||||||
|
phy: EthernetPhy,
|
||||||
|
network_was_reset: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> MiniconfInterface<S>
|
||||||
|
where
|
||||||
|
S: miniconf::Miniconf + Default,
|
||||||
|
{
|
||||||
|
/// Construct a new MQTT settings interface.
|
||||||
|
///
|
||||||
|
/// # Args
|
||||||
|
/// * `stack` - The network stack to use for communication.
|
||||||
|
/// * `client_id` - The ID of the MQTT client. May be an empty string for auto-assigning.
|
||||||
|
/// * `prefix` - The MQTT device prefix to use for this device.
|
||||||
|
/// * `phy` - The PHY driver for querying the link state.
|
||||||
|
/// * `clock` - The clock to utilize for querying the current system time.
|
||||||
|
pub fn new(
|
||||||
|
stack: NetworkStack,
|
||||||
|
client_id: &str,
|
||||||
|
prefix: &str,
|
||||||
|
phy: EthernetPhy,
|
||||||
|
clock: CycleCounter,
|
||||||
|
) -> Self {
|
||||||
|
let mqtt = {
|
||||||
|
let mqtt_client = {
|
||||||
|
minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
miniconf::MqttInterface::new(mqtt_client, prefix, S::default())
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
mqtt,
|
||||||
|
clock,
|
||||||
|
phy,
|
||||||
|
network_was_reset: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the MQTT interface and service the network
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// An option containing an action that should be completed as a result of network servicing.
|
||||||
|
pub fn update(&mut self) -> Option<Action> {
|
||||||
|
let now = self.clock.current_ms();
|
||||||
|
|
||||||
|
// First, service the network stack to process and inbound and outbound traffic.
|
||||||
|
let sleep = match self.mqtt.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 DHCP server in the network
|
||||||
|
// stack.
|
||||||
|
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.network_stack().handle_link_reset();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.network_was_reset = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, service the MQTT interface and handle any necessary messages.
|
||||||
|
match self.mqtt.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user