Skip to content

Commit 3ef00cb

Browse files
authored
Merge pull request #884 from quartiq/fix/861/menu-clear
Updating sequential-storage, adding flash erase support
2 parents 58bc7da + c670ea0 commit 3ef00cb

6 files changed

Lines changed: 229 additions & 63 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
Booster
1515
* Panic information is now persisted after reboot and available via telemetry and the USB serial
1616
console.
17+
* Device operational settings can now be modified, stored and cleared from device flash using the
18+
USB serial console. Run-time settings are unique to each application, but network settings are
19+
unified for all applications (i.e. lockin, dual-iir, etc.)
1720

1821
### Changed
1922
* Broker and static IP/DHCP are no longer configured at compile time,

Cargo.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ members = ["ad9959", "serial-settings"]
3333

3434
[dependencies]
3535
panic-persist = { version = "0.3", features = ["utf8", "custom-panic-handler"] }
36-
sequential-storage = "0.6"
36+
sequential-storage = "2"
3737
embedded-io = "0.6"
3838
embedded-storage = "0.3"
39+
embedded-storage-async = "0.4"
3940
cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
4041
cortex-m-rt = { version = "0.7", features = ["device"] }
4142
log = { version = "0.4", features = ["max_level_trace", "release_max_level_info"] }
@@ -72,6 +73,7 @@ tca9539 = "0.2"
7273
smoltcp-nal = { version = "0.5", features = ["shared-stack"] }
7374
postcard = "1"
7475
bit_field = "0.10.2"
76+
embassy-futures = { version = "0.1", default-features = false }
7577

7678
[build-dependencies]
7779
built = { version = "0.7", features = ["git2"], default-features = false }

serial-settings/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ pub trait Platform<const Y: usize>: Sized {
9191
/// Execute a platform specific command.
9292
fn cmd(&mut self, cmd: &str);
9393

94+
/// Handle clearing a settings key.
95+
///
96+
/// # Note
97+
/// The run-time setting will have already been updated when this is called. This is intended
98+
/// to remove settings from permanent storage if necessary.
99+
///
100+
/// # Arguments
101+
/// * `buffer` The element serialization buffer.
102+
/// * `key` The name of the setting to be cleared. If `None`, all settings are cleared.
103+
fn clear(&mut self, buffer: &mut [u8], key: Option<&str>);
104+
94105
/// Return a mutable reference to the `Interface`.
95106
fn interface_mut(&mut self) -> &mut Self::Interface;
96107
}
@@ -225,15 +236,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
225236
writeln!(interface, "All settings cleared").unwrap();
226237
}
227238

228-
match interface.platform.save(interface.buffer, maybe_key, settings) {
229-
Ok(_) => {
230-
writeln!(interface, "Settings saved. You may need to reboot for the settings to be applied")
231-
}
232-
Err(e) => {
233-
writeln!(interface, "Failed to clear settings: {e:?}")
234-
}
235-
}
236-
.unwrap();
239+
interface.platform.clear(interface.buffer, maybe_key);
237240
}
238241

239242
fn handle_get(

src/hardware/flash.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
12
use stm32h7xx_hal::flash::LockedFlashBank;
23

4+
use stm32h7xx_hal::flash::Error as FlashError;
5+
36
pub struct Flash(pub LockedFlashBank);
47

58
impl Flash {
@@ -8,9 +11,8 @@ impl Flash {
811
}
912
}
1013

11-
impl embedded_storage::nor_flash::ErrorType for Flash {
12-
type Error =
13-
<LockedFlashBank as embedded_storage::nor_flash::ErrorType>::Error;
14+
impl ErrorType for Flash {
15+
type Error = FlashError;
1416
}
1517

1618
impl embedded_storage::nor_flash::ReadNorFlash for Flash {
@@ -20,7 +22,23 @@ impl embedded_storage::nor_flash::ReadNorFlash for Flash {
2022
&mut self,
2123
offset: u32,
2224
bytes: &mut [u8],
23-
) -> Result<(), Self::Error> {
25+
) -> Result<(), FlashError> {
26+
self.0.read(offset, bytes)
27+
}
28+
29+
fn capacity(&self) -> usize {
30+
self.0.capacity()
31+
}
32+
}
33+
34+
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash {
35+
const READ_SIZE: usize = LockedFlashBank::READ_SIZE;
36+
37+
async fn read(
38+
&mut self,
39+
offset: u32,
40+
bytes: &mut [u8],
41+
) -> Result<(), FlashError> {
2442
self.0.read(offset, bytes)
2543
}
2644

@@ -45,3 +63,24 @@ impl embedded_storage::nor_flash::NorFlash for Flash {
4563
bank.write(offset, bytes)
4664
}
4765
}
66+
67+
impl embedded_storage_async::nor_flash::NorFlash for Flash {
68+
const WRITE_SIZE: usize =
69+
stm32h7xx_hal::flash::UnlockedFlashBank::WRITE_SIZE;
70+
const ERASE_SIZE: usize =
71+
stm32h7xx_hal::flash::UnlockedFlashBank::ERASE_SIZE;
72+
73+
async fn erase(&mut self, from: u32, to: u32) -> Result<(), FlashError> {
74+
let mut bank = self.0.unlocked();
75+
bank.erase(from, to)
76+
}
77+
78+
async fn write(
79+
&mut self,
80+
offset: u32,
81+
bytes: &[u8],
82+
) -> Result<(), FlashError> {
83+
let mut bank = self.0.unlocked();
84+
bank.write(offset, bytes)
85+
}
86+
}

0 commit comments

Comments
 (0)