Skip to content

Commit

Permalink
Merge pull request #884 from quartiq/fix/861/menu-clear
Browse files Browse the repository at this point in the history
Updating sequential-storage, adding flash erase support
  • Loading branch information
ryan-summers authored Jun 19, 2024
2 parents 58bc7da + c670ea0 commit 3ef00cb
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 63 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Booster
* Panic information is now persisted after reboot and available via telemetry and the USB serial
console.
* Device operational settings can now be modified, stored and cleared from device flash using the
USB serial console. Run-time settings are unique to each application, but network settings are
unified for all applications (i.e. lockin, dual-iir, etc.)

### Changed
* Broker and static IP/DHCP are no longer configured at compile time,
Expand Down
23 changes: 20 additions & 3 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ members = ["ad9959", "serial-settings"]

[dependencies]
panic-persist = { version = "0.3", features = ["utf8", "custom-panic-handler"] }
sequential-storage = "0.6"
sequential-storage = "2"
embedded-io = "0.6"
embedded-storage = "0.3"
embedded-storage-async = "0.4"
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 @@ -72,6 +73,7 @@ tca9539 = "0.2"
smoltcp-nal = { version = "0.5", features = ["shared-stack"] }
postcard = "1"
bit_field = "0.10.2"
embassy-futures = { version = "0.1", default-features = false }

[build-dependencies]
built = { version = "0.7", features = ["git2"], default-features = false }
Expand Down
21 changes: 12 additions & 9 deletions serial-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ pub trait Platform<const Y: usize>: Sized {
/// Execute a platform specific command.
fn cmd(&mut self, cmd: &str);

/// Handle clearing a settings key.
///
/// # Note
/// The run-time setting will have already been updated when this is called. This is intended
/// to remove settings from permanent storage if necessary.
///
/// # Arguments
/// * `buffer` The element serialization buffer.
/// * `key` The name of the setting to be cleared. If `None`, all settings are cleared.
fn clear(&mut self, buffer: &mut [u8], key: Option<&str>);

/// Return a mutable reference to the `Interface`.
fn interface_mut(&mut self) -> &mut Self::Interface;
}
Expand Down Expand Up @@ -225,15 +236,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
writeln!(interface, "All settings cleared").unwrap();
}

match interface.platform.save(interface.buffer, maybe_key, settings) {
Ok(_) => {
writeln!(interface, "Settings saved. You may need to reboot for the settings to be applied")
}
Err(e) => {
writeln!(interface, "Failed to clear settings: {e:?}")
}
}
.unwrap();
interface.platform.clear(interface.buffer, maybe_key);
}

fn handle_get(
Expand Down
47 changes: 43 additions & 4 deletions src/hardware/flash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
use stm32h7xx_hal::flash::LockedFlashBank;

use stm32h7xx_hal::flash::Error as FlashError;

pub struct Flash(pub LockedFlashBank);

impl Flash {
Expand All @@ -8,9 +11,8 @@ impl Flash {
}
}

impl embedded_storage::nor_flash::ErrorType for Flash {
type Error =
<LockedFlashBank as embedded_storage::nor_flash::ErrorType>::Error;
impl ErrorType for Flash {
type Error = FlashError;
}

impl embedded_storage::nor_flash::ReadNorFlash for Flash {
Expand All @@ -20,7 +22,23 @@ impl embedded_storage::nor_flash::ReadNorFlash for Flash {
&mut self,
offset: u32,
bytes: &mut [u8],
) -> Result<(), Self::Error> {
) -> Result<(), FlashError> {
self.0.read(offset, bytes)
}

fn capacity(&self) -> usize {
self.0.capacity()
}
}

impl embedded_storage_async::nor_flash::ReadNorFlash for Flash {
const READ_SIZE: usize = LockedFlashBank::READ_SIZE;

async fn read(
&mut self,
offset: u32,
bytes: &mut [u8],
) -> Result<(), FlashError> {
self.0.read(offset, bytes)
}

Expand All @@ -45,3 +63,24 @@ impl embedded_storage::nor_flash::NorFlash for Flash {
bank.write(offset, bytes)
}
}

impl embedded_storage_async::nor_flash::NorFlash for Flash {
const WRITE_SIZE: usize =
stm32h7xx_hal::flash::UnlockedFlashBank::WRITE_SIZE;
const ERASE_SIZE: usize =
stm32h7xx_hal::flash::UnlockedFlashBank::ERASE_SIZE;

async fn erase(&mut self, from: u32, to: u32) -> Result<(), FlashError> {
let mut bank = self.0.unlocked();
bank.erase(from, to)
}

async fn write(
&mut self,
offset: u32,
bytes: &[u8],
) -> Result<(), FlashError> {
let mut bank = self.0.unlocked();
bank.write(offset, bytes)
}
}
Loading

0 comments on commit 3ef00cb

Please sign in to comment.