pub struct MockFlash<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> {
pub erase_counts: Vec<u32>,
pub write_count: u64,
pub erase_ops: u64,
pub read_ops: u64,
pub read_bytes: u64,
/* private fields */
}std only.Expand description
Simulated NOR flash for testing.
This type simulates a NOR flash chip with configurable page and sector sizes. It enforces the same constraints as real hardware:
- Writes must be page-aligned
- Erased regions must be 0xFF before writing
- Can only clear bits (1 -> 0), not set them
§Power-loss simulation
Use set_power_loss_after to configure
deterministic power-loss injection. After the specified number of writes,
subsequent writes will fail according to the chosen PowerLossMode.
§Type Parameters
PAGE_SIZE: Size of a write page in bytesSECTOR_SIZE: Size of an erase sector in bytes
Fields§
§erase_counts: Vec<u32>Track erase counts per sector for wear analysis.
write_count: u64Total number of write operations.
erase_ops: u64Total number of erase operations, successful or torn.
read_ops: u64Total number of read operations.
read_bytes: u64Total bytes read.
The honest measure of mount cost on a real part: what a Full mount
avoids by reading a sealed sector’s footer is bytes over the bus, not
operations. Counted here so the saving can be asserted rather than
claimed.
Implementations§
Source§impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> MockFlash<PAGE_SIZE, SECTOR_SIZE>
impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> MockFlash<PAGE_SIZE, SECTOR_SIZE>
Sourcepub fn set_power_loss_after(&mut self, n: u64, mode: PowerLossMode)
pub fn set_power_loss_after(&mut self, n: u64, mode: PowerLossMode)
Configure deterministic power-loss injection.
After n successful write operations (counted from creation, not from this call),
all subsequent writes will fail according to the specified mode.
§Arguments
n: Total write count threshold. The n-th write and all following writes will fail.mode: How to simulate the power loss (clean fail vs. partial write).
Sourcepub fn set_erase_loss_after(&mut self, n: u64, bytes: usize)
pub fn set_erase_loss_after(&mut self, n: u64, bytes: usize)
Tear the n-th erase operation, leaving only its first bytes erased.
A real erase is a long, progressive operation: cells go from 0 to 1 over milliseconds, low addresses first. Losing power part-way therefore leaves the head of the sector erased and the tail holding whatever was there before — including a stale sector header body under a freshly blank magic.
The erase returns an error, and the erase counter for the affected sectors is not advanced: the sector is not, in any useful sense, erased.
§Arguments
n: erase index, counted from creation. The n-th erase is the one torn.bytes: how many bytes of the requested range actually reached 0xFF.
Sourcepub fn is_power_lost(&self) -> bool
pub fn is_power_lost(&self) -> bool
Returns true if a simulated power loss has been triggered.
Sourcepub fn clear_power_loss(&mut self)
pub fn clear_power_loss(&mut self)
Clear any power-loss configuration, allowing writes and erases to succeed again.
Sourcepub fn total_erases(&self) -> u64
pub fn total_erases(&self) -> u64
Total erase operations across all sectors.