1
1
use crate :: svd:: { array:: names, Device , Peripheral } ;
2
2
use proc_macro2:: { Span , TokenStream } ;
3
3
use quote:: { quote, ToTokens } ;
4
+ use syn:: Ident ;
4
5
5
6
use log:: debug;
6
7
use std:: fs:: File ;
@@ -270,12 +271,24 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
270
271
}
271
272
}
272
273
274
+ // Generate unique identifier to prevent linkage errors when using multiple devices.
275
+ let mut id = String :: from ( "TAKEN_" ) ;
276
+ for ch in d. name . chars ( ) {
277
+ if ch == ' ' || ch == '-' {
278
+ id. push ( '_' ) ;
279
+ } else {
280
+ id. extend ( ch. to_uppercase ( ) ) ;
281
+ }
282
+ }
283
+
284
+ let taken: Ident = syn:: parse_str ( & id) ?;
285
+
273
286
out. extend ( quote ! {
274
287
// NOTE `no_mangle` is used here to prevent linking different minor versions of the device
275
288
// crate as that would let you `take` the device peripherals more than once (one per minor
276
289
// version)
277
290
#[ no_mangle]
278
- static mut DEVICE_PERIPHERALS : bool = false ;
291
+ static mut #taken : bool = false ;
279
292
280
293
/// All the peripherals.
281
294
#[ allow( non_snake_case) ]
@@ -290,12 +303,12 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
290
303
pub fn take( ) -> Option <Self > {
291
304
critical_section:: with( |_| {
292
305
// SAFETY: We are in a critical section, so we have exclusive access
293
- // to `DEVICE_PERIPHERALS `.
294
- if unsafe { DEVICE_PERIPHERALS } {
306
+ // to `#taken `.
307
+ if unsafe { #taken } {
295
308
return None
296
309
}
297
310
298
- // SAFETY: `DEVICE_PERIPHERALS ` is set to `true` by `Peripherals::steal`,
311
+ // SAFETY: `#taken ` is set to `true` by `Peripherals::steal`,
299
312
// ensuring the peripherals can only be returned once.
300
313
Some ( unsafe { Peripherals :: steal( ) } )
301
314
} )
@@ -308,7 +321,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
308
321
/// Each of the returned peripherals must be used at most once.
309
322
#[ inline]
310
323
pub unsafe fn steal( ) -> Self {
311
- DEVICE_PERIPHERALS = true ;
324
+ #taken = true ;
312
325
313
326
Peripherals {
314
327
#exprs
0 commit comments