Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding initial support for configuring parameters via USB #805

Merged
merged 18 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ default-target = "thumbv7em-none-eabihf"
members = ["ad9959"]

[dependencies]
embedded-storage = "0.3"
menu = "0.3"
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
cortex-m-rt = { version = "0.7", features = ["device"] }
log = { version = "0.4", features = ["max_level_trace", "release_max_level_info"] }
Expand Down Expand Up @@ -67,6 +69,7 @@ usbd-serial = "0.1.1"
miniconf = "0.9.0"
smoltcp-nal = { version = "0.4.1", features = ["shared-stack"]}
bbqueue = "0.5"
postcard = "1"

[dependencies.stm32h7xx-hal]
version = "0.15.1"
Expand Down
38 changes: 24 additions & 14 deletions book/src/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ Stabilizer requires an MQTT broker that supports MQTTv5.
The MQTT broker is used to distribute and exchange elemetry data and to view/change application settings.
The broker must be reachable by both the host-side applications used to
interact with the application on Stabilizer and by the application running on Stabilizer.
Determine the IPv4 address of the broker as seen from the network Stabilizer is
connected to. The broker IP address must be stable. It will be used later
during firmware build.
The broker must be reachable on port 1883 on that IP address.
The broker must be reachable on port 1883 on that IP address - it may either be an IP address or a
fully qualified domain name.
Firewalls between Stabilizer and the broker may need to be configured to
allow connections from Stabilizer to that port and IP address.

Expand Down Expand Up @@ -83,23 +81,21 @@ docker run -p 1883:1883 --name mosquitto -v ${pwd}/mosquitto.conf:/mosquitto/con
git clone https://github.com/quartiq/stabilizer
cd stabilizer
```
5. Build firmware specifying the MQTT broker IP. Replace `10.34.16.1` by the
stable and reachable broker IPv4 address determined above.
5. Build firmware
```bash
# Bash
BROKER="10.34.16.1" cargo build --release
cargo build --release

# Powershell
# Note: This sets the broker for all future builds as well.
$env:BROKER='10.34.16.1'; cargo build --release
cargo build --release
```
6. Extract the application binary (substitute `dual-iir` below with the desired application name)
```bash
# Bash
BROKER="10.34.16.1" cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin
cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin

# Powershell
$env:BROKER='10.34.16.1'; cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin
cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin
```

## Flashing
Expand Down Expand Up @@ -163,16 +159,29 @@ described [above](#st-link-virtual-mass-storage).
2. Build and run firmware on the device
```bash
# Bash
BROKER="10.34.16.1" cargo run --release --bin dual-iir
cargo run --release --bin dual-iir

# Powershell
$Env:BROKER='10.34.16.1'; cargo run --release --bin dual-iir
cargo run --release --bin dual-iir
```

When using debug (non `--release`) mode, decrease the sampling frequency significantly.
The added error checking code and missing optimizations may lead to the application
missing timer deadlines and panicing.

## Set the MQTT broker

The MQTT broker can be configured via the USB port on Stabilizer's front. Connect a USB cable and
open up the serial port in a serial terminal of your choice. `pyserial` provides a simple,
easy-to-use terminal emulator:
```sh
python -m serial <port>
```

Once you have opened the port, you can use the provided menu to update the MQTT broker address. The
address can be an IP address or a domain name. Once the broker has been updated, power cycle
stabilizer to have the new broker address take effect.

## Verify MQTT connection

Once your MQTT broker and Stabilizer are both running, verify that the application
Expand All @@ -190,7 +199,8 @@ Broker.

![MQTT Explorer Configuration](assets/mqtt-explorer.png)

> **Note:** In MQTT explorer, use the same broker address that you used when building the firmware.
> **Note:** In MQTT explorer, use the same broker address that you set in the Stabilizer serial
> terminal.

In addition to the `alive` status, telemetry messages are published at regular intervals
when Stabilizer has connected to the broker. Once you observe incoming telemetry,
Expand Down
7 changes: 4 additions & 3 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,22 @@ mod app {
let clock = SystemTimer::new(|| monotonics::now().ticks() as u32);

// Configure the microcontroller
let (stabilizer, _pounder) = hardware::setup::setup(
let (mut stabilizer, _pounder) = hardware::setup::setup(
c.core,
c.device,
clock,
BATCH_SIZE,
SAMPLE_TICKS,
);

let flash = stabilizer.usb_serial.flash();
let mut network = NetworkUsers::new(
stabilizer.net.stack,
stabilizer.net.phy,
clock,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
option_env!("BROKER").unwrap_or("mqtt"),
&flash.settings.broker,
&flash.settings.id,
);

let generator = network.configure_streaming(StreamFormat::AdcDacData);
Expand Down
5 changes: 3 additions & 2 deletions src/bin/lockin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ mod app {
SAMPLE_TICKS,
);

let flash = stabilizer.usb_serial.flash();
let mut network = NetworkUsers::new(
stabilizer.net.stack,
stabilizer.net.phy,
clock,
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
option_env!("BROKER").unwrap_or("mqtt"),
&flash.settings.broker,
&flash.settings.id,
);

let generator = network.configure_streaming(StreamFormat::AdcDacData);
Expand Down
65 changes: 65 additions & 0 deletions src/hardware/flash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use core::fmt::Write;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use stm32h7xx_hal::flash::{LockedFlashBank, UnlockedFlashBank};

#[derive(Clone, serde::Serialize, serde::Deserialize, miniconf::Tree)]
pub struct Settings {
pub broker: heapless::String<255>,
pub id: heapless::String<23>,
#[serde(skip)]
#[tree(skip)]
pub mac: smoltcp_nal::smoltcp::wire::EthernetAddress,
}

impl Settings {
fn new(mac: smoltcp_nal::smoltcp::wire::EthernetAddress) -> Self {
let mut id = heapless::String::new();
write!(&mut id, "{mac}").unwrap();

Self {
broker: "mqtt".into(),
id,
mac,
}
}

pub fn reset(&mut self) {
*self = Self::new(self.mac)
}
}

pub struct FlashSettings {
flash: LockedFlashBank,
pub settings: Settings,
}

impl FlashSettings {
pub fn new(
mut flash: LockedFlashBank,
mac: smoltcp_nal::smoltcp::wire::EthernetAddress,
) -> Self {
let mut buffer = [0u8; 512];
flash.read(0, &mut buffer[..]).unwrap();

let settings = match postcard::from_bytes(&buffer) {
Ok(settings) => settings,
Err(_) => {
log::warn!(
"Failed to load settings from flash. Using defaults"
);
Settings::new(mac)
}
};

Self { flash, settings }
}

pub fn save(&mut self) {
let mut bank = self.flash.unlocked();

let mut data = [0; 512];
let serialized = postcard::to_slice(&self.settings, &mut data).unwrap();
bank.erase(0, UnlockedFlashBank::ERASE_SIZE as u32).unwrap();
bank.write(0, serialized).unwrap();
}
}
1 change: 1 addition & 0 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod cpu_temp_sensor;
pub mod dac;
pub mod delay;
pub mod design_parameters;
pub mod flash;
pub mod input_stamper;
pub mod pounder;
pub mod serial_terminal;
Expand Down
Loading
Loading