@@ -75,6 +75,15 @@ pub trait FlashAlgorithm: Sized + 'static {
7575 /// * `data` - The data to compare with.
7676 #[ cfg( feature = "verify" ) ]
7777 fn verify ( & mut self , address : u32 , size : u32 , data : Option < & [ u8 ] > ) -> Result < ( ) , ErrorCode > ;
78+
79+ /// Read flash.
80+ ///
81+ /// # Arguments
82+ ///
83+ /// * `address` - The start address of the flash to read.
84+ /// * `data` - The data.
85+ #[ cfg( feature = "read-flash" ) ]
86+ fn read_flash ( & mut self , address : u32 , data : & mut [ u8 ] ) -> Result < ( ) , ErrorCode > ;
7887}
7988
8089#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
@@ -167,6 +176,7 @@ macro_rules! algorithm {
167176 }
168177 }
169178 $crate:: erase_chip!( $type) ;
179+ $crate:: read_flash!( $type) ;
170180 $crate:: verify!( $type) ;
171181
172182 #[ allow( non_upper_case_globals) ]
@@ -270,6 +280,33 @@ macro_rules! erase_chip {
270280 } ;
271281}
272282
283+ #[ doc( hidden) ]
284+ #[ macro_export]
285+ #[ cfg( not( feature = "read-flash" ) ) ]
286+ macro_rules! read_flash {
287+ ( $type: ty) => { } ;
288+ }
289+ #[ doc( hidden) ]
290+ #[ macro_export]
291+ #[ cfg( feature = "read-flash" ) ]
292+ macro_rules! read_flash {
293+ ( $type: ty) => {
294+ #[ no_mangle]
295+ #[ link_section = ".entry" ]
296+ pub unsafe extern "C" fn ReadFlash ( addr: u32 , size: u32 , data: * mut u8 ) -> u32 {
297+ if !_IS_INIT {
298+ return 1 ;
299+ }
300+ let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
301+ let data_slice: & mut [ u8 ] = unsafe { core:: slice:: from_raw_parts_mut( data, size as usize ) } ;
302+ match <$type as $crate:: FlashAlgorithm >:: read_flash( this, addr, data_slice) {
303+ Ok ( ( ) ) => 0 ,
304+ Err ( e) => e. get( ) ,
305+ }
306+ }
307+ } ;
308+ }
309+
273310#[ doc( hidden) ]
274311#[ macro_export]
275312#[ cfg( not( feature = "verify" ) ) ]
0 commit comments