Skip to content

Commit 4dadf27

Browse files
Expand NVRAM trait with integrity validation helpers (#11)
Related to OpenDevicePartnership/embassy-imxrt#538. This PR adds two helper functions to the NVRAM trait which makes it easy for a user to obtain the contents of an NVRAM table for validation and then clear them if found to be invalid.
1 parent 1903e77 commit 4dadf27

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

embedded-mcu-hal/src/nvram.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ pub trait NvramStorage<'a, T>: Send {
1414
pub trait Nvram<'a, StorageType, StoredType, const CELL_COUNT: usize>: Send
1515
where
1616
StorageType: NvramStorage<'a, StoredType>,
17+
StoredType: Copy,
1718
{
1819
/// Returns an array of mutable storage cells.
20+
///
21+
/// This method borrows the NVRAM for its entire lifetime to prevent double-borrowing
22+
/// of the underlying hardware. It can effectively only be called once per instance.
1923
fn storage(&'a mut self) -> &'a mut [StorageType; CELL_COUNT];
24+
25+
/// Dumps the contents of all storage cells into an array.
26+
/// This is for integrity validation purposes and not an alternative to `storage`.
27+
fn dump_storage(&self) -> [StoredType; CELL_COUNT];
28+
29+
/// Clears all storage cells to an implementation-defined cleared state.
30+
///
31+
/// StoredType implementations should document their clearing behavior. For numeric
32+
/// types like u32, this is commonly zero.
33+
/// Best practice is to use this API, if needed, during device initialization to ensure
34+
/// a known state prior to calling `storage()` and utilizing the NVRAM cells.
35+
fn clear_storage(&mut self);
2036
}

0 commit comments

Comments
 (0)