Skip to content

Commit

Permalink
Merge pull request #811 from quartiq/feature/sequential-storage
Browse files Browse the repository at this point in the history
Adding usage of sequential storage
  • Loading branch information
jordens authored Dec 4, 2023
2 parents 0c9df95 + 8bc4d84 commit accf844
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 112 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ default-target = "thumbv7em-none-eabihf"
members = ["ad9959", "serial-settings"]

[dependencies]
sequential-storage = "0.6"
embedded-io = "0.6"
embedded-storage = "0.3"
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
Expand Down
113 changes: 4 additions & 109 deletions src/hardware/flash.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,13 @@
use crate::hardware::platform;
use core::fmt::Write;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use stm32h7xx_hal::flash::LockedFlashBank;

#[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 serial_settings::Settings for Settings {
fn reset(&mut self) {
*self = Self::new(self.mac)
}
}

impl Settings {
pub fn reload(&mut self, storage: &mut Flash) {
let mut buffer = [0u8; 512];
storage.read(0, &mut buffer).unwrap();
let Ok(mut settings) = postcard::from_bytes::<Self>(&buffer) else {
return;
};

settings.mac = self.mac;
*self = settings;
}

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

Self {
broker: "mqtt".into(),
id,
mac,
}
impl Flash {
pub fn range(&self) -> core::ops::Range<u32> {
0..(self.0.len() as u32)
}
}

pub struct Flash(pub LockedFlashBank);

impl embedded_storage::nor_flash::ErrorType for Flash {
type Error =
<LockedFlashBank as embedded_storage::nor_flash::ErrorType>::Error;
Expand Down Expand Up @@ -81,72 +45,3 @@ impl embedded_storage::nor_flash::NorFlash for Flash {
bank.write(offset, bytes)
}
}

#[derive(Debug)]
pub enum Error<F> {
Postcard(postcard::Error),
Flash(F),
}

impl<F> From<postcard::Error> for Error<F> {
fn from(e: postcard::Error) -> Self {
Self::Postcard(e)
}
}

pub struct SerialSettingsPlatform {
/// The interface to read/write data to/from serially (via text) to the user.
pub interface: serial_settings::BestEffortInterface<
usbd_serial::SerialPort<'static, super::UsbBus>,
>,
/// The Settings structure.
pub settings: Settings,
/// The storage mechanism used to persist settings to between boots.
pub storage: Flash,
}

impl serial_settings::Platform for SerialSettingsPlatform {
type Interface = serial_settings::BestEffortInterface<
usbd_serial::SerialPort<'static, super::UsbBus>,
>;
type Settings = Settings;
type Error =
Error<<Flash as embedded_storage::nor_flash::ErrorType>::Error>;

fn save(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
let serialized = postcard::to_slice(self.settings(), buf)?;
self.storage
.erase(0, serialized.len() as u32)
.map_err(Self::Error::Flash)?;
self.storage
.write(0, serialized)
.map_err(Self::Error::Flash)?;
Ok(())
}

fn cmd(&mut self, cmd: &str) {
match cmd {
"reboot" => cortex_m::peripheral::SCB::sys_reset(),
"dfu" => platform::start_dfu_reboot(),
_ => {
writeln!(
self.interface_mut(),
"Invalid platform command: `{cmd}` not in [`dfu`, `reboot`]"
)
.ok();
}
}
}

fn settings(&self) -> &Self::Settings {
&self.settings
}

fn settings_mut(&mut self) -> &mut Self::Settings {
&mut self.settings
}

fn interface_mut(&mut self) -> &mut Self::Interface {
&mut self.interface
}
}
2 changes: 1 addition & 1 deletion src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub type I2c1Proxy =
shared_bus::I2cProxy<'static, shared_bus::AtomicCheckMutex<I2c1>>;

pub type SerialTerminal =
serial_settings::Runner<'static, flash::SerialSettingsPlatform>;
serial_settings::Runner<'static, crate::settings::SerialSettingsPlatform>;

#[inline(never)]
#[panic_handler]
Expand Down
4 changes: 2 additions & 2 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,11 +1088,11 @@ pub fn setup(

let mut storage = super::flash::Flash(flash_bank2.unwrap());
let mut settings =
super::flash::Settings::new(network_devices.mac_address);
crate::settings::Settings::new(network_devices.mac_address);
settings.reload(&mut storage);

serial_settings::Runner::new(
super::flash::SerialSettingsPlatform {
crate::settings::SerialSettingsPlatform {
interface: serial_settings::BestEffortInterface::new(
usb_serial,
),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

pub mod hardware;
pub mod net;
pub mod settings;
Loading

0 comments on commit accf844

Please sign in to comment.