Skip to content

Commit ed21f8f

Browse files
committed
Generate unique id for device peripherals
1 parent f864c3e commit ed21f8f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/generate/device.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::svd::{array::names, Device, Peripheral};
22
use proc_macro2::{Span, TokenStream};
33
use quote::{quote, ToTokens};
4+
use syn::Ident;
45

56
use log::debug;
67
use std::fs::File;
@@ -270,12 +271,24 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
270271
}
271272
}
272273

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+
273286
out.extend(quote! {
274287
// NOTE `no_mangle` is used here to prevent linking different minor versions of the device
275288
// crate as that would let you `take` the device peripherals more than once (one per minor
276289
// version)
277290
#[no_mangle]
278-
static mut DEVICE_PERIPHERALS: bool = false;
291+
static mut #taken: bool = false;
279292

280293
/// All the peripherals.
281294
#[allow(non_snake_case)]
@@ -290,12 +303,12 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
290303
pub fn take() -> Option<Self> {
291304
critical_section::with(|_| {
292305
// 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 } {
295308
return None
296309
}
297310

298-
// SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`,
311+
// SAFETY: `#taken` is set to `true` by `Peripherals::steal`,
299312
// ensuring the peripherals can only be returned once.
300313
Some(unsafe { Peripherals::steal() })
301314
})
@@ -308,7 +321,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
308321
/// Each of the returned peripherals must be used at most once.
309322
#[inline]
310323
pub unsafe fn steal() -> Self {
311-
DEVICE_PERIPHERALS = true;
324+
#taken = true;
312325

313326
Peripherals {
314327
#exprs

0 commit comments

Comments
 (0)