Merge branch 'rs/issue-276/unique-identifiers' into feature/telemetry
This commit is contained in:
commit
1c9f30b4d5
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -437,6 +437,12 @@ dependencies = [
|
|||||||
"heapless 0.6.1",
|
"heapless 0.6.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nanorand"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac1378b66f7c93a1c0f8464a19bf47df8795083842e5090f4b7305973d5a22d0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nb"
|
name = "nb"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
@ -710,10 +716,11 @@ 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=8468f11#8468f11abacd7aba82454e6904df19c1d1ab91bb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"embedded-nal",
|
"embedded-nal",
|
||||||
"heapless 0.6.1",
|
"heapless 0.6.1",
|
||||||
|
"nanorand",
|
||||||
"smoltcp",
|
"smoltcp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -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 = "8468f11"
|
||||||
|
|
||||||
[patch.crates-io.minimq]
|
[patch.crates-io.minimq]
|
||||||
git = "https://github.com/quartiq/minimq.git"
|
git = "https://github.com/quartiq/minimq.git"
|
||||||
|
@ -17,6 +17,9 @@ python3 -m pip install -r requirements.txt
|
|||||||
|
|
||||||
cargo flash --elf target/thumbv7em-none-eabihf/release/dual-iir --chip STM32H743ZITx
|
cargo flash --elf target/thumbv7em-none-eabihf/release/dual-iir --chip STM32H743ZITx
|
||||||
|
|
||||||
|
# Before attempting to ping the device, sleep to allow Stabilizer to boot.
|
||||||
|
sleep 30
|
||||||
|
|
||||||
# Test pinging Stabilizer. This exercises that:
|
# Test pinging Stabilizer. This exercises that:
|
||||||
# * DHCP is functional and an IP has been acquired
|
# * DHCP is functional and an IP has been acquired
|
||||||
# * Stabilizer's network is functioning as intended
|
# * Stabilizer's network is functioning as intended
|
||||||
@ -24,6 +27,6 @@ cargo flash --elf target/thumbv7em-none-eabihf/release/dual-iir --chip STM32H743
|
|||||||
ping -c 5 -w 20 stabilizer-hitl
|
ping -c 5 -w 20 stabilizer-hitl
|
||||||
|
|
||||||
# Test the MQTT interface.
|
# Test the MQTT interface.
|
||||||
python3 miniconf.py dt/sinara/stabilizer afe/0='"G2"'
|
python3 miniconf.py dt/sinara/dual-iir/04-91-62-d9-7e-5f afe/0='"G2"'
|
||||||
python3 miniconf.py dt/sinara/stabilizer afe/0='"G1"' iir_ch/0/0=\
|
python3 miniconf.py dt/sinara/dual-iir/04-91-62-d9-7e-5f afe/0='"G1"' iir_ch/0/0=\
|
||||||
'{"y_min": -32767, "y_max": 32767, "y_offset": 0, "ba": [1.0, 0, 0, 0, 0]}'
|
'{"y_min": -32767, "y_max": 32767, "y_offset": 0, "ba": [1.0, 0, 0, 0, 0]}'
|
||||||
|
@ -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::{minimq, Miniconf};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use dsp::iir;
|
use dsp::iir;
|
||||||
use hardware::{
|
use hardware::{
|
||||||
Adc0Input, Adc1Input, AfeGain, CycleCounter, Dac0Output, Dac1Output,
|
Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, DigitalInput0,
|
||||||
DigitalInput0, DigitalInput1, SystemTimer, InputPin, NetworkStack, AFE0, AFE1,
|
DigitalInput1, InputPin, SystemTimer, 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!
|
||||||
@ -63,11 +65,9 @@ const APP: () = {
|
|||||||
digital_inputs: (DigitalInput0, DigitalInput1),
|
digital_inputs: (DigitalInput0, 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>,
|
|
||||||
telemetry: Telemetry,
|
telemetry: Telemetry,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
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])]
|
||||||
@ -79,23 +79,16 @@ 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 = {
|
stabilizer.net.stack,
|
||||||
minimq::MqttClient::new(
|
"",
|
||||||
hardware::design_parameters::MQTT_BROKER.into(),
|
&net::get_device_prefix(
|
||||||
"",
|
env!("CARGO_BIN_NAME"),
|
||||||
stabilizer.net.stack,
|
stabilizer.net.mac_address,
|
||||||
)
|
),
|
||||||
.unwrap()
|
stabilizer.net.phy,
|
||||||
};
|
stabilizer.cycle_counter,
|
||||||
|
);
|
||||||
MqttInterface::new(
|
|
||||||
mqtt_client,
|
|
||||||
"dt/sinara/stabilizer",
|
|
||||||
Settings::default(),
|
|
||||||
)
|
|
||||||
.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();
|
||||||
@ -111,13 +104,12 @@ 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,
|
|
||||||
telemetry: Telemetry::default(),
|
telemetry: Telemetry::default(),
|
||||||
digital_inputs: stabilizer.digital_inputs,
|
digital_inputs: stabilizer.digital_inputs,
|
||||||
|
mqtt_config,
|
||||||
settings: Settings::default(),
|
settings: Settings::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,44 +178,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);
|
||||||
@ -233,9 +207,10 @@ const APP: () = {
|
|||||||
c.resources.afes.1.set_gain(settings.afe[1]);
|
c.resources.afes.1.set_gain(settings.afe[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(priority = 1, resources=[mqtt_interface, settings, telemetry], schedule=[telemetry])]
|
#[task(priority = 1, resources=[mqtt_config, settings, telemetry], schedule=[telemetry])]
|
||||||
fn telemetry(mut c: telemetry::Context) {
|
fn telemetry(mut c: telemetry::Context) {
|
||||||
let telemetry = c.resources.telemetry.lock(|telemetry| telemetry.clone());
|
let telemetry =
|
||||||
|
c.resources.telemetry.lock(|telemetry| telemetry.clone());
|
||||||
|
|
||||||
// Serialize telemetry outside of a critical section to prevent blocking the processing
|
// Serialize telemetry outside of a critical section to prevent blocking the processing
|
||||||
// task.
|
// task.
|
||||||
@ -245,15 +220,29 @@ const APP: () = {
|
|||||||
>(&telemetry)
|
>(&telemetry)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
c.resources.mqtt_interface.client(|client| {
|
c.resources.mqtt_config.mqtt.client(|client| {
|
||||||
// TODO: Incorporate current MQTT prefix instead of hard-coded value.
|
// TODO: Incorporate current MQTT prefix instead of hard-coded value.
|
||||||
client.publish("dt/sinara/dual-iir/telemetry", telemetry.as_bytes(), minimq::QoS::AtMostOnce, &[]).ok()
|
client
|
||||||
|
.publish(
|
||||||
|
"dt/sinara/dual-iir/telemetry",
|
||||||
|
telemetry.as_bytes(),
|
||||||
|
minimq::QoS::AtMostOnce,
|
||||||
|
&[],
|
||||||
|
)
|
||||||
|
.ok()
|
||||||
});
|
});
|
||||||
|
|
||||||
let telemetry_period = c.resources.settings.lock(|settings| settings.telemetry_period_secs);
|
let telemetry_period = c
|
||||||
|
.resources
|
||||||
|
.settings
|
||||||
|
.lock(|settings| settings.telemetry_period_secs);
|
||||||
|
|
||||||
// Schedule the telemetry task in the future.
|
// Schedule the telemetry task in the future.
|
||||||
c.schedule.telemetry( c.scheduled + SystemTimer::ticks_from_secs(telemetry_period as u32))
|
c.schedule
|
||||||
|
.telemetry(
|
||||||
|
c.scheduled
|
||||||
|
+ SystemTimer::ticks_from_secs(telemetry_period as u32),
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,16 +4,20 @@
|
|||||||
|
|
||||||
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::net;
|
||||||
|
|
||||||
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 +60,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 +73,16 @@ 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(
|
stabilizer.net.stack,
|
||||||
design_parameters::MQTT_BROKER.into(),
|
"",
|
||||||
"",
|
&net::get_device_prefix(
|
||||||
stabilizer.net.stack,
|
env!("CARGO_BIN_NAME"),
|
||||||
)
|
stabilizer.net.mac_address,
|
||||||
.unwrap();
|
),
|
||||||
|
stabilizer.net.phy,
|
||||||
MqttInterface::new(
|
stabilizer.cycle_counter,
|
||||||
mqtt_client,
|
);
|
||||||
"dt/sinara/lockin",
|
|
||||||
Settings::default(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
let settings = Settings::default();
|
let settings = Settings::default();
|
||||||
|
|
||||||
@ -118,10 +113,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 +195,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]);
|
||||||
|
@ -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, system_timer, DdsOutput, DigitalInput0,
|
digital_input_stamper, eeprom, pounder, system_timer, timers, DdsOutput,
|
||||||
DigitalInput1, NetworkStack, AFE0, AFE1,
|
DigitalInput0, DigitalInput1, EthernetPhy, NetworkStack, AFE0, AFE1,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct NetStorage {
|
pub struct NetStorage {
|
||||||
@ -56,7 +56,8 @@ 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,
|
||||||
|
pub mac_address: smoltcp::wire::EthernetAddress,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The available hardware interfaces on Stabilizer.
|
/// The available hardware interfaces on Stabilizer.
|
||||||
@ -608,14 +609,27 @@ pub fn setup(
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let random_seed = {
|
||||||
|
let mut rng =
|
||||||
|
device.RNG.constrain(ccdr.peripheral.RNG, &ccdr.clocks);
|
||||||
|
let mut data = [0u8; 4];
|
||||||
|
rng.fill(&mut data).unwrap();
|
||||||
|
data
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut stack = smoltcp_nal::NetworkStack::new(
|
||||||
|
interface,
|
||||||
|
sockets,
|
||||||
|
&handles,
|
||||||
|
Some(dhcp_client),
|
||||||
|
);
|
||||||
|
|
||||||
|
stack.seed_random_port(&random_seed);
|
||||||
|
|
||||||
NetworkDevices {
|
NetworkDevices {
|
||||||
stack: smoltcp_nal::NetworkStack::new(
|
stack,
|
||||||
interface,
|
|
||||||
sockets,
|
|
||||||
&handles,
|
|
||||||
Some(dhcp_client),
|
|
||||||
),
|
|
||||||
phy: lan8742a,
|
phy: lan8742a,
|
||||||
|
mac_address: mac_addr,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -53,6 +53,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;
|
||||||
|
148
src/net/mod.rs
Normal file
148
src/net/mod.rs
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
use crate::hardware::{
|
||||||
|
design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack,
|
||||||
|
};
|
||||||
|
|
||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
|
use heapless::{consts, String};
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the MQTT prefix of a device.
|
||||||
|
///
|
||||||
|
/// # Args
|
||||||
|
/// * `app` - The name of the application that is executing.
|
||||||
|
/// * `mac` - The ethernet MAC address of the device.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// The MQTT prefix used for this device.
|
||||||
|
pub fn get_device_prefix(
|
||||||
|
app: &str,
|
||||||
|
mac: smoltcp_nal::smoltcp::wire::EthernetAddress,
|
||||||
|
) -> String<consts::U128> {
|
||||||
|
let mac_string = {
|
||||||
|
let mut mac_string: String<consts::U32> = String::new();
|
||||||
|
let mac = mac.as_bytes();
|
||||||
|
|
||||||
|
// Note(unwrap): 32-bytes is guaranteed to be valid for any mac address, as the address has
|
||||||
|
// a fixed length.
|
||||||
|
write!(
|
||||||
|
&mut mac_string,
|
||||||
|
"{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}",
|
||||||
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
mac_string
|
||||||
|
};
|
||||||
|
|
||||||
|
// Note(unwrap): The mac address + binary name must be short enough to fit into this string. If
|
||||||
|
// they are defined too long, this will panic and the device will fail to boot.
|
||||||
|
let mut prefix: String<consts::U128> = String::new();
|
||||||
|
write!(&mut prefix, "dt/sinara/{}/{}", app, mac_string).unwrap();
|
||||||
|
|
||||||
|
prefix
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user