-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #805 from quartiq/feature/flash-storage
Adding initial support for configuring parameters via USB
- Loading branch information
Showing
10 changed files
with
341 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.