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