Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadNorFlash::read_if_not_empty #65

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/nor_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl core::fmt::Display for NorFlashErrorKind {

/// Read only NOR flash trait.
pub trait ReadNorFlash: ErrorType {
/// The minumum number of bytes the storage peripheral can read
/// The minimum number of bytes the storage peripheral can read
const READ_SIZE: usize;

/// Read a slice of data from the storage peripheral, starting the read
Expand All @@ -68,6 +68,24 @@ pub trait ReadNorFlash: ErrorType {
/// can use the [`check_read`] helper function.
fn read(&mut self, offset: u32, bytes: &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.
///
/// The function returns `Ok(true)` if the data is considered to be non-empty.
/// The value of `bytes` should not be considered reliable if the function returns `Ok(false)`.
///
/// This function is useful for devices that offer transparent flash encryption,
/// and for devices where the erased byte value is not `0xFF`.
///
/// # Errors
///
/// Returns an error if the arguments are not aligned or out of bounds. The implementation
/// can use the [`check_read`] helper function.
fn read_if_not_empty(&mut self, offset: u32, bytes: &mut [u8]) -> Result<bool, Self::Error> {
self.read(offset, bytes)?;
Ok(!bytes.iter().all(|&b| b == 0xFF))
}

/// The capacity of the peripheral in bytes.
fn capacity(&self) -> usize;
}
Expand All @@ -83,10 +101,10 @@ pub fn check_read<T: ReadNorFlash>(

/// NOR flash trait.
pub trait NorFlash: ReadNorFlash {
/// The minumum number of bytes the storage peripheral can write
/// The minimum number of bytes the storage peripheral can write
const WRITE_SIZE: usize;

/// The minumum number of bytes the storage peripheral can erase
/// The minimum number of bytes the storage peripheral can erase
const ERASE_SIZE: usize;

/// Erase the given storage range, clearing all data within `[from..to]`.
Expand Down