Skip to content

Commit ff8c00c

Browse files
committed
Add wrapper for NOR flash storage to collect usage statistics
1 parent 64a32f9 commit ff8c00c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/nor_flash.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,104 @@ where
390390
}
391391
}
392392

393+
/// A wrapper for NOR flash storage to collect usage statistics
394+
#[derive(Clone, Copy, Debug)]
395+
pub struct NorFlashStats<S> {
396+
storage: S,
397+
/// Number of read operations
398+
pub reads: usize,
399+
/// Amount read chunks
400+
pub read: usize,
401+
/// Number of write operations
402+
pub writes: usize,
403+
/// Amount written chunks
404+
pub written: usize,
405+
/// Number of erase operations
406+
pub erases: usize,
407+
/// Amount of erased sectors
408+
pub erased: usize,
409+
}
410+
411+
impl<S> From<S> for NorFlashStats<S> {
412+
fn from(storage: S) -> Self {
413+
Self {
414+
storage,
415+
reads: 0,
416+
read: 0,
417+
writes: 0,
418+
written: 0,
419+
erases: 0,
420+
erased: 0,
421+
}
422+
}
423+
}
424+
425+
impl<S> NorFlashStats<S> {
426+
/// Unwrap to get wrapped storage instance
427+
pub fn into_inner(self) -> S {
428+
self.storage
429+
}
430+
}
431+
432+
impl<S> core::ops::Deref for NorFlashStats<S> {
433+
type Target = S;
434+
435+
fn deref(&self) -> &Self::Target {
436+
&self.storage
437+
}
438+
}
439+
440+
impl<S> core::ops::DerefMut for NorFlashStats<S> {
441+
fn deref_mut(&mut self) -> &mut Self::Target {
442+
&mut self.storage
443+
}
444+
}
445+
446+
impl<S: ErrorType> ErrorType for NorFlashStats<S> {
447+
type Error = S::Error;
448+
}
449+
450+
impl<S: ReadNorFlash> ReadNorFlash for NorFlashStats<S> {
451+
const READ_SIZE: usize = S::READ_SIZE;
452+
453+
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), S::Error> {
454+
let res = self.storage.read(offset, bytes);
455+
if res.is_ok() {
456+
self.reads += 1;
457+
self.read += bytes.len() / S::READ_SIZE;
458+
}
459+
res
460+
}
461+
462+
fn capacity(&self) -> usize {
463+
self.storage.capacity()
464+
}
465+
}
466+
467+
impl<S: NorFlash> NorFlash for NorFlashStats<S> {
468+
const WRITE_SIZE: usize = S::WRITE_SIZE;
469+
const ERASE_SIZE: usize = S::ERASE_SIZE;
470+
const ERASE_BYTE: u8 = S::ERASE_BYTE;
471+
472+
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), S::Error> {
473+
let res = self.storage.write(offset, bytes);
474+
if res.is_ok() {
475+
self.writes += 1;
476+
self.written += bytes.len() / S::WRITE_SIZE;
477+
}
478+
res
479+
}
480+
481+
fn erase(&mut self, from: u32, to: u32) -> Result<(), S::Error> {
482+
let res = self.storage.erase(from, to);
483+
if res.is_ok() {
484+
self.erases += 1;
485+
self.erased += (to - from) as usize / S::ERASE_SIZE;
486+
}
487+
res
488+
}
489+
}
490+
393491
/// Simple RAM-backed flash storage implementation for tests
394492
#[derive(Clone, Copy, Debug)]
395493
pub struct MockFlash<

0 commit comments

Comments
 (0)