Reserved flash to store some non-volatile state
/ audit (push) Successful in 19s Details
/ build-firmware (push) Successful in 24s Details
/ build-linux (push) Successful in 38s Details
/ build-appimage (push) Successful in 3m33s Details
/ build-windows (push) Successful in 1m42s Details

This commit is contained in:
Sebastian 2024-01-16 18:17:10 +01:00
parent ae54fdfee9
commit 6048d4826c
4 changed files with 52 additions and 1 deletions

View File

@ -26,3 +26,4 @@ cheapsdo-protocol = { path = "../protocol" }
postcard = {version = "1.0.8", features = ["use-defmt"]}
heapless = {version = "0.8.0", features = ["defmt-03"]}
num = {version = "0.4.1", default-features = false, features = ["libm"]}
serde = { version = "1.0.195", default-features = false }

38
firmware/src/config.rs Normal file
View File

@ -0,0 +1,38 @@
use postcard::{from_bytes_cobs, to_slice_cobs};
use serde::{Deserialize, Serialize};
use stm32f1xx_hal::flash::FlashWriter;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Config {
pub pwm: u16,
}
impl Default for Config {
fn default() -> Self {
Self { pwm: 1500u16 }
}
}
const PAGE_SIZE: usize = 1024;
const CONFIG_OFFSET: u32 = 63 * PAGE_SIZE as u32;
pub fn load(flash: &mut FlashWriter) -> Config {
let tmp = flash.read(CONFIG_OFFSET, PAGE_SIZE).unwrap();
let mut config_page = [0u8; PAGE_SIZE];
config_page.copy_from_slice(tmp);
match from_bytes_cobs::<Config>(&mut config_page) {
Ok(config) => config,
Err(_) => Config::default(),
}
}
impl Config {
pub fn save(&self, flash: &mut FlashWriter) {
let mut page = [0u8; PAGE_SIZE];
let used = to_slice_cobs(self, &mut page).unwrap();
flash.write(CONFIG_OFFSET, &used).unwrap();
}
}

View File

@ -6,6 +6,7 @@ use defmt_rtt as _; // global logger
use panic_probe as _;
use stm32f1xx_hal as _;
mod config;
mod si5153;
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
@ -41,6 +42,7 @@ mod app {
use cheapsdo_protocol::{DeviceMessage, HostMessage, StatusMessage};
use crate::config;
use crate::si5153;
const USB_BUFFER_SIZE: usize = 64;
@ -229,6 +231,15 @@ mod app {
si_pll.set_ms_freq(&mut i2c1, si5153::Multisynth::MS0, 100_000_000);
si_pll.enable_ms_output(&mut i2c1, si5153::Multisynth::MS0);
let mut writer = flash.writer(
stm32f1xx_hal::flash::SectorSize::Sz1K,
stm32f1xx_hal::flash::FlashSize::Sz64K,
);
let config = config::load(&mut writer);
defmt::info!("read config from flash");
update_pwm::spawn().unwrap();
(

View File

@ -1,6 +1,7 @@
/* Linker script for the STM32F103C8T6 */
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
FLASH : ORIGIN = 0x08000000, LENGTH = 63K
CONFIG : ORIGIN = FLASH + 63K, LENGTH = 1K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}