@@ -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,
@@ -97,6 +101,8 @@ macro_rules! algorithm {
97
101
static mut _IS_INIT: bool = false ;
98
102
static mut _ALGO_INSTANCE: core:: mem:: MaybeUninit <$type> = core:: mem:: MaybeUninit :: uninit( ) ;
99
103
104
+ core:: arch:: global_asm!( ".section .PrgData, \" aw\" " ) ;
105
+
100
106
#[ no_mangle]
101
107
#[ link_section = ".entry" ]
102
108
pub unsafe extern "C" fn Init ( addr: u32 , clock: u32 , function: u32 ) -> u32 {
@@ -108,9 +114,9 @@ macro_rules! algorithm {
108
114
1 => $crate:: Function :: Erase ,
109
115
2 => $crate:: Function :: Program ,
110
116
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." )
112
118
} ;
113
- match <$type as FlashAlgorithm >:: new( addr, clock, function) {
119
+ match <$type as $crate :: FlashAlgorithm >:: new( addr, clock, function) {
114
120
Ok ( inst) => {
115
121
_ALGO_INSTANCE. as_mut_ptr( ) . write( inst) ;
116
122
_IS_INIT = true ;
@@ -136,7 +142,7 @@ macro_rules! algorithm {
136
142
return 1 ;
137
143
}
138
144
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) {
140
146
Ok ( ( ) ) => 0 ,
141
147
Err ( e) => e. get( ) ,
142
148
}
@@ -149,7 +155,7 @@ macro_rules! algorithm {
149
155
}
150
156
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
151
157
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) {
153
159
Ok ( ( ) ) => 0 ,
154
160
Err ( e) => e. get( ) ,
155
161
}
@@ -163,24 +169,24 @@ macro_rules! algorithm {
163
169
#[ link_section = "DeviceData" ]
164
170
pub static FlashDevice : FlashDeviceDescription = FlashDeviceDescription {
165
171
// The version is never read by probe-rs and can be fixed.
166
- vers: 0x0 ,
172
+ vers: 0x1 ,
167
173
// The device name here can be customized but it really has no real use
168
174
// appart from identifying the device the ELF is intended for which we have
169
175
// in our YAML.
170
- dev_name: [ 0u8 ; 128 ] ,
176
+ dev_name: $crate :: arrayify_string ( $device_name ) ,
171
177
// The specification does not specify the values that can go here,
172
178
// but this value means internal flash device.
173
- dev_type: 5 ,
179
+ dev_type: $device_type ,
174
180
dev_addr: $flash_address,
175
181
device_size: $flash_size,
176
182
page_size: $page_size,
177
183
_reserved: 0 ,
178
184
// The empty state of a byte in flash.
179
185
empty: $empty_value,
180
186
// 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 ,
182
188
// 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 ,
184
190
flash_sectors: [
185
191
$(
186
192
FlashSector {
@@ -200,7 +206,7 @@ macro_rules! algorithm {
200
206
pub struct FlashDeviceDescription {
201
207
vers: u16 ,
202
208
dev_name: [ u8 ; 128 ] ,
203
- dev_type: u16 ,
209
+ dev_type: DeviceType ,
204
210
dev_addr: u32 ,
205
211
device_size: u32 ,
206
212
page_size: u32 ,
@@ -218,6 +224,17 @@ macro_rules! algorithm {
218
224
size: u32 ,
219
225
address: u32 ,
220
226
}
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
+ }
221
238
} ;
222
239
}
223
240
@@ -239,7 +256,7 @@ macro_rules! erase_chip {
239
256
return 1 ;
240
257
}
241
258
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) {
243
260
Ok ( ( ) ) => 0 ,
244
261
Err ( e) => e. get( ) ,
245
262
}
@@ -267,13 +284,14 @@ macro_rules! verify {
267
284
let this = & mut * _ALGO_INSTANCE. as_mut_ptr( ) ;
268
285
269
286
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 ) {
271
288
Ok ( ( ) ) => 0 ,
272
289
Err ( e) => e. get( ) ,
273
290
}
274
291
} else {
275
292
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
+ {
277
295
Ok ( ( ) ) => 0 ,
278
296
Err ( e) => e. get( ) ,
279
297
}
@@ -286,5 +304,18 @@ macro_rules! verify {
286
304
#[ macro_export]
287
305
macro_rules! count {
288
306
( ) => ( 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
290
321
}
0 commit comments