@@ -85,10 +85,14 @@ pub enum Function {
85
85
#[ macro_export]
86
86
macro_rules! algorithm {
87
87
( $type: ty, {
88
+ device_name: $device_name: expr,
89
+ device_type: $device_type: expr,
88
90
flash_address: $flash_address: expr,
89
91
flash_size: $flash_size: expr,
90
92
page_size: $page_size: expr,
91
93
empty_value: $empty_value: expr,
94
+ program_time_out: $program_time_out: expr,
95
+ erase_time_out: $erase_time_out: expr,
92
96
sectors: [ $( {
93
97
size: $size: expr,
94
98
address: $address: expr,
@@ -108,9 +112,9 @@ macro_rules! algorithm {
108
112
1 => $crate:: Function :: Erase ,
109
113
2 => $crate:: Function :: Program ,
110
114
3 => $crate:: Function :: Verify ,
111
- _ => panic!( "This branch can only be reached if the host library sent an unknown function code." )
115
+ _ => core :: panic!( "This branch can only be reached if the host library sent an unknown function code." )
112
116
} ;
113
- match <$type as FlashAlgorithm >:: new( addr, clock, function) {
117
+ match <$type as $crate :: FlashAlgorithm >:: new( addr, clock, function) {
114
118
Ok ( inst) => {
115
119
_ALGO_INSTANCE. as_mut_ptr( ) . write( inst) ;
116
120
_IS_INIT = true ;
@@ -136,7 +140,7 @@ macro_rules! algorithm {
136
140
return 1 ;
137
141
}
138
142
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
139
- match <$type as FlashAlgorithm >:: erase_sector( this, addr) {
143
+ match <$type as $crate :: FlashAlgorithm >:: erase_sector( this, addr) {
140
144
Ok ( ( ) ) => 0 ,
141
145
Err ( e) => e. get( ) ,
142
146
}
@@ -149,7 +153,7 @@ macro_rules! algorithm {
149
153
}
150
154
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
151
155
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) {
156
+ match <$type as $crate :: FlashAlgorithm >:: program_page( this, addr, data_slice) {
153
157
Ok ( ( ) ) => 0 ,
154
158
Err ( e) => e. get( ) ,
155
159
}
@@ -163,24 +167,24 @@ macro_rules! algorithm {
163
167
#[ link_section = "DeviceData" ]
164
168
pub static FlashDevice : FlashDeviceDescription = FlashDeviceDescription {
165
169
// The version is never read by probe-rs and can be fixed.
166
- vers: 0x0 ,
170
+ vers: 0x1 ,
167
171
// The device name here can be customized but it really has no real use
168
172
// appart from identifying the device the ELF is intended for which we have
169
173
// in our YAML.
170
- dev_name: [ 0u8 ; 128 ] ,
174
+ dev_name: $crate :: arrayify_string ( $device_name ) ,
171
175
// The specification does not specify the values that can go here,
172
176
// but this value means internal flash device.
173
- dev_type: 5 ,
177
+ dev_type: $device_type ,
174
178
dev_addr: $flash_address,
175
179
device_size: $flash_size,
176
180
page_size: $page_size,
177
181
_reserved: 0 ,
178
182
// The empty state of a byte in flash.
179
183
empty: $empty_value,
180
184
// This value can be used to estimate the amount of time the flashing procedure takes worst case.
181
- program_time_out: 1000 ,
185
+ program_time_out: $program_time_out ,
182
186
// This value can be used to estimate the amount of time the erasing procedure takes worst case.
183
- erase_time_out: 2000 ,
187
+ erase_time_out: $erase_time_out ,
184
188
flash_sectors: [
185
189
$(
186
190
FlashSector {
@@ -200,7 +204,7 @@ macro_rules! algorithm {
200
204
pub struct FlashDeviceDescription {
201
205
vers: u16 ,
202
206
dev_name: [ u8 ; 128 ] ,
203
- dev_type: u16 ,
207
+ dev_type: DeviceType ,
204
208
dev_addr: u32 ,
205
209
device_size: u32 ,
206
210
page_size: u32 ,
@@ -218,6 +222,17 @@ macro_rules! algorithm {
218
222
size: u32 ,
219
223
address: u32 ,
220
224
}
225
+
226
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
227
+ #[ repr( u16 ) ]
228
+ pub enum DeviceType {
229
+ Unknown = 0 ,
230
+ Onchip = 1 ,
231
+ Ext8Bit = 2 ,
232
+ Ext16Bit = 3 ,
233
+ Ext32Bit = 4 ,
234
+ ExtSpi = 5 ,
235
+ }
221
236
} ;
222
237
}
223
238
@@ -239,7 +254,7 @@ macro_rules! erase_chip {
239
254
return 1 ;
240
255
}
241
256
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
242
- match <$type as FlashAlgorithm >:: erase_all( this) {
257
+ match <$type as $crate :: FlashAlgorithm >:: erase_all( this) {
243
258
Ok ( ( ) ) => 0 ,
244
259
Err ( e) => e. get( ) ,
245
260
}
@@ -267,13 +282,14 @@ macro_rules! verify {
267
282
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
268
283
269
284
if data. is_null( ) {
270
- match <$type as FlashAlgorithm >:: verify( this, addr, size, None ) {
285
+ match <$type as $crate :: FlashAlgorithm >:: verify( this, addr, size, None ) {
271
286
Ok ( ( ) ) => 0 ,
272
287
Err ( e) => e. get( ) ,
273
288
}
274
289
} else {
275
290
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) ) {
291
+ match <$type as $crate:: FlashAlgorithm >:: verify( this, addr, size, Some ( data_slice) )
292
+ {
277
293
Ok ( ( ) ) => 0 ,
278
294
Err ( e) => e. get( ) ,
279
295
}
@@ -286,5 +302,18 @@ macro_rules! verify {
286
302
#[ macro_export]
287
303
macro_rules! count {
288
304
( ) => ( 0usize ) ;
289
- ( $x: tt $( $xs: tt) * ) => ( 1usize + count!( $( $xs) * ) ) ;
305
+ ( $x: tt $( $xs: tt) * ) => ( 1usize + $crate:: count!( $( $xs) * ) ) ;
306
+ }
307
+
308
+ pub const fn arrayify_string < const N : usize > ( msg : & ' static str ) -> [ u8 ; N ] {
309
+ let mut arr = [ 0u8 ; N ] ;
310
+ let mut idx = 0 ;
311
+ let msg_bytes = msg. as_bytes ( ) ;
312
+
313
+ while ( idx < msg_bytes. len ( ) ) && ( idx < N ) {
314
+ arr[ idx] = msg_bytes[ idx] ;
315
+ idx += 1 ;
316
+ }
317
+
318
+ arr
290
319
}
0 commit comments