@@ -85,10 +85,14 @@ pub enum Function {
8585#[ macro_export]
8686macro_rules! algorithm {
8787 ( $type: ty, {
88+ device_name: $device_name: expr,
89+ device_type: $device_type: expr,
8890 flash_address: $flash_address: expr,
8991 flash_size: $flash_size: expr,
9092 page_size: $page_size: expr,
9193 empty_value: $empty_value: expr,
94+ program_time_out: $program_time_out: expr,
95+ erase_time_out: $erase_time_out: expr,
9296 sectors: [ $( {
9397 size: $size: expr,
9498 address: $address: expr,
@@ -97,6 +101,8 @@ macro_rules! algorithm {
97101 static mut _IS_INIT: bool = false ;
98102 static mut _ALGO_INSTANCE: core:: mem:: MaybeUninit <$type> = core:: mem:: MaybeUninit :: uninit( ) ;
99103
104+ core:: arch:: global_asm!( ".section .PrgData, \" aw\" " ) ;
105+
100106 #[ no_mangle]
101107 #[ link_section = ".entry" ]
102108 pub unsafe extern "C" fn Init ( addr: u32 , clock: u32 , function: u32 ) -> u32 {
@@ -108,9 +114,9 @@ macro_rules! algorithm {
108114 1 => $crate:: Function :: Erase ,
109115 2 => $crate:: Function :: Program ,
110116 3 => $crate:: Function :: Verify ,
111- _ => panic!( "This branch can only be reached if the host library sent an unknown function code." )
117+ _ => core :: panic!( "This branch can only be reached if the host library sent an unknown function code." )
112118 } ;
113- match <$type as FlashAlgorithm >:: new( addr, clock, function) {
119+ match <$type as $crate :: FlashAlgorithm >:: new( addr, clock, function) {
114120 Ok ( inst) => {
115121 _ALGO_INSTANCE. as_mut_ptr( ) . write( inst) ;
116122 _IS_INIT = true ;
@@ -136,7 +142,7 @@ macro_rules! algorithm {
136142 return 1 ;
137143 }
138144 let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
139- match <$type as FlashAlgorithm >:: erase_sector( this, addr) {
145+ match <$type as $crate :: FlashAlgorithm >:: erase_sector( this, addr) {
140146 Ok ( ( ) ) => 0 ,
141147 Err ( e) => e. get( ) ,
142148 }
@@ -149,7 +155,7 @@ macro_rules! algorithm {
149155 }
150156 let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
151157 let data_slice: & [ u8 ] = unsafe { core:: slice:: from_raw_parts( data, size as usize ) } ;
152- match <$type as FlashAlgorithm >:: program_page( this, addr, data_slice) {
158+ match <$type as $crate :: FlashAlgorithm >:: program_page( this, addr, data_slice) {
153159 Ok ( ( ) ) => 0 ,
154160 Err ( e) => e. get( ) ,
155161 }
@@ -163,24 +169,24 @@ macro_rules! algorithm {
163169 #[ link_section = "DeviceData" ]
164170 pub static FlashDevice : FlashDeviceDescription = FlashDeviceDescription {
165171 // The version is never read by probe-rs and can be fixed.
166- vers: 0x0 ,
172+ vers: 0x1 ,
167173 // The device name here can be customized but it really has no real use
168174 // appart from identifying the device the ELF is intended for which we have
169175 // in our YAML.
170- dev_name: [ 0u8 ; 128 ] ,
176+ dev_name: $crate :: arrayify_string ( $device_name ) ,
171177 // The specification does not specify the values that can go here,
172178 // but this value means internal flash device.
173- dev_type: 5 ,
179+ dev_type: $device_type ,
174180 dev_addr: $flash_address,
175181 device_size: $flash_size,
176182 page_size: $page_size,
177183 _reserved: 0 ,
178184 // The empty state of a byte in flash.
179185 empty: $empty_value,
180186 // This value can be used to estimate the amount of time the flashing procedure takes worst case.
181- program_time_out: 1000 ,
187+ program_time_out: $program_time_out ,
182188 // This value can be used to estimate the amount of time the erasing procedure takes worst case.
183- erase_time_out: 2000 ,
189+ erase_time_out: $erase_time_out ,
184190 flash_sectors: [
185191 $(
186192 FlashSector {
@@ -200,7 +206,7 @@ macro_rules! algorithm {
200206 pub struct FlashDeviceDescription {
201207 vers: u16 ,
202208 dev_name: [ u8 ; 128 ] ,
203- dev_type: u16 ,
209+ dev_type: DeviceType ,
204210 dev_addr: u32 ,
205211 device_size: u32 ,
206212 page_size: u32 ,
@@ -218,6 +224,17 @@ macro_rules! algorithm {
218224 size: u32 ,
219225 address: u32 ,
220226 }
227+
228+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
229+ #[ repr( u16 ) ]
230+ pub enum DeviceType {
231+ Unknown = 0 ,
232+ Onchip = 1 ,
233+ Ext8Bit = 2 ,
234+ Ext16Bit = 3 ,
235+ Ext32Bit = 4 ,
236+ ExtSpi = 5 ,
237+ }
221238 } ;
222239}
223240
@@ -239,7 +256,7 @@ macro_rules! erase_chip {
239256 return 1 ;
240257 }
241258 let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
242- match <$type as FlashAlgorithm >:: erase_all( this) {
259+ match <$type as $crate :: FlashAlgorithm >:: erase_all( this) {
243260 Ok ( ( ) ) => 0 ,
244261 Err ( e) => e. get( ) ,
245262 }
@@ -267,13 +284,14 @@ macro_rules! verify {
267284 let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
268285
269286 if data. is_null( ) {
270- match <$type as FlashAlgorithm >:: verify( this, addr, size, None ) {
287+ match <$type as $crate :: FlashAlgorithm >:: verify( this, addr, size, None ) {
271288 Ok ( ( ) ) => 0 ,
272289 Err ( e) => e. get( ) ,
273290 }
274291 } else {
275292 let data_slice: & [ u8 ] = unsafe { core:: slice:: from_raw_parts( data, size as usize ) } ;
276- match <$type as FlashAlgorithm >:: verify( this, addr, size, Some ( data_slice) ) {
293+ match <$type as $crate:: FlashAlgorithm >:: verify( this, addr, size, Some ( data_slice) )
294+ {
277295 Ok ( ( ) ) => 0 ,
278296 Err ( e) => e. get( ) ,
279297 }
@@ -286,5 +304,18 @@ macro_rules! verify {
286304#[ macro_export]
287305macro_rules! count {
288306 ( ) => ( 0usize ) ;
289- ( $x: tt $( $xs: tt) * ) => ( 1usize + count!( $( $xs) * ) ) ;
307+ ( $x: tt $( $xs: tt) * ) => ( 1usize + $crate:: count!( $( $xs) * ) ) ;
308+ }
309+
310+ pub const fn arrayify_string < const N : usize > ( msg : & ' static str ) -> [ u8 ; N ] {
311+ let mut arr = [ 0u8 ; N ] ;
312+ let mut idx = 0 ;
313+ let msg_bytes = msg. as_bytes ( ) ;
314+
315+ while ( idx < msg_bytes. len ( ) ) && ( idx < N ) {
316+ arr[ idx] = msg_bytes[ idx] ;
317+ idx += 1 ;
318+ }
319+
320+ arr
290321}
0 commit comments