From 0895ce4b99041e12ea95c79841c20f3d4d8e509c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Sat, 16 Nov 2024 15:25:15 +0100 Subject: [PATCH 1/2] Add ReadNorFlash::read_if_not_empty --- src/nor_flash.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/nor_flash.rs b/src/nor_flash.rs index c20ddba..b57fc9d 100644 --- a/src/nor_flash.rs +++ b/src/nor_flash.rs @@ -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 { + self.read(offset, bytes)?; + Ok(!bytes.iter().all(|&b| b == 0xFF)) + } + /// The capacity of the peripheral in bytes. fn capacity(&self) -> usize; } From 9913ae332b9a19f5a673b7d75db473c420a66f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Sat, 16 Nov 2024 15:33:06 +0100 Subject: [PATCH 2/2] Bonus typo --- src/nor_flash.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nor_flash.rs b/src/nor_flash.rs index b57fc9d..d04c17b 100644 --- a/src/nor_flash.rs +++ b/src/nor_flash.rs @@ -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 @@ -101,10 +101,10 @@ pub fn check_read( /// 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]`.