Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate unique id for device peripherals #857

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased]

- Fix `enumeratedValues` with `isDefault` only
- Generate unique identifier instead of `DEVICE_PERIPHERALS` to solve
the link time issue when multiple devices used

## [v0.33.4] - 2024-06-16

Expand Down
22 changes: 17 additions & 5 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::svd::{array::names, Device, Peripheral};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use regex::Regex;
use syn::Ident;

use log::debug;
use std::fs::File;
Expand Down Expand Up @@ -270,12 +272,22 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
}

// Generate unique identifier to prevent linkage errors when using multiple devices.
let mut id = String::from("TAKEN_");
let re = Regex::new(r"[^A-Za-z0-9_]").unwrap();
let result = re.replace_all(&d.name, "_");
for ch in result.chars() {
id.extend(ch.to_uppercase());
}

let taken: Ident = syn::parse_str(&id)?;

out.extend(quote! {
// NOTE `no_mangle` is used here to prevent linking different minor versions of the device
// crate as that would let you `take` the device peripherals more than once (one per minor
// version)
#[no_mangle]
static mut DEVICE_PERIPHERALS: bool = false;
static mut #taken: bool = false;

/// All the peripherals.
#[allow(non_snake_case)]
Expand All @@ -290,12 +302,12 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
pub fn take() -> Option<Self> {
critical_section::with(|_| {
// SAFETY: We are in a critical section, so we have exclusive access
// to `DEVICE_PERIPHERALS`.
if unsafe { DEVICE_PERIPHERALS } {
// to `#taken`.
if unsafe { #taken } {
return None
}

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

Peripherals {
#exprs
Expand Down
Loading