Skip to main content

MockFlash

Struct MockFlash 

Source
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 */
}
Available on crate feature 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 bytes
  • SECTOR_SIZE: Size of an erase sector in bytes

Fields§

§erase_counts: Vec<u32>

Track erase counts per sector for wear analysis.

§write_count: u64

Total number of write operations.

§erase_ops: u64

Total number of erase operations, successful or torn.

§read_ops: u64

Total number of read operations.

§read_bytes: u64

Total 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>

Source

pub fn new(size: usize) -> Self

Create a new mock flash of size bytes.

§Panics

Panics if size is not a multiple of SECTOR_SIZE.

Source

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).
Source

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.
Source

pub fn is_power_lost(&self) -> bool

Returns true if a simulated power loss has been triggered.

Source

pub fn clear_power_loss(&mut self)

Clear any power-loss configuration, allowing writes and erases to succeed again.

Source

pub fn total_erases(&self) -> u64

Total erase operations across all sectors.

Source

pub fn ops(&self) -> FlashOps

Snapshot the operation counters.

Take one before an operation and one after, and subtract: see FlashOps.

Source

pub fn hex_dump(&self, offset: usize, len: usize) -> Vec<u8> ⓘ

Dump a range of flash as bytes (for debugging).

Trait Implementations§

Source§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> Clone for MockFlash<PAGE_SIZE, SECTOR_SIZE>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> ErrorType for MockFlash<PAGE_SIZE, SECTOR_SIZE>

Source§

type Error = MockFlashError

Errors returned by this NOR flash.
Source§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> NorFlash for MockFlash<PAGE_SIZE, SECTOR_SIZE>

Source§

const WRITE_SIZE: usize = PAGE_SIZE

The minumum number of bytes the storage peripheral can write
Source§

const ERASE_SIZE: usize = SECTOR_SIZE

The minumum number of bytes the storage peripheral can erase
Source§

fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error>

Erase the given storage range, clearing all data within [from..to]. The given range will contain all 1s afterwards. Read more
Source§

fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>

If power is lost during write, the contents of the written words are undefined, but the rest of the page is guaranteed to be unchanged. It is not allowed to write to the same word twice. Read more
Source§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> ReadNorFlash for MockFlash<PAGE_SIZE, SECTOR_SIZE>

Source§

const READ_SIZE: usize = 1

The minumum number of bytes the storage peripheral can read
Source§

fn read(&mut self, offset: u32, buf: &mut [u8]) -> Result<(), Self::Error>

Read a slice of data from the storage peripheral, starting the read operation at the given address offset, and reading bytes.len() bytes. Read more
Source§

fn capacity(&self) -> usize

The capacity of the peripheral in bytes.

Auto Trait Implementations§

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> Freeze for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> RefUnwindSafe for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> Send for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> Sync for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> Unpin for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> UnsafeUnpin for MockFlash<PAGE_SIZE, SECTOR_SIZE>

§

impl<const PAGE_SIZE: usize, const SECTOR_SIZE: usize> UnwindSafe for MockFlash<PAGE_SIZE, SECTOR_SIZE>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.