Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Add `--skip-peripherals-struct` flag to skip generating the `Peripherals`
struct, its `take`/`steal` impl and the `DEVICE_PERIPHERALS` static

## [v0.37.1] - 2025-10-17

- Change `feature(doc_auto_cfg)` to `feature(doc_cfg)` to allow nightly docs to build.
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Config {
pub generic_mod: bool,
pub make_mod: bool,
pub skip_crate_attributes: bool,
pub skip_peripherals_struct: bool,
pub ignore_groups: bool,
pub keep_list: bool,
pub strict: bool,
Expand Down
106 changes: 54 additions & 52 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,64 +295,66 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
}

let nomangle = if config.edition >= RustEdition::E2024 {
quote!(#[unsafe(no_mangle)])
} else {
quote!(#[no_mangle])
};

let set_device_peripherals_true = if config.edition >= RustEdition::E2024 {
quote!(unsafe { DEVICE_PERIPHERALS = true })
} else {
quote!(DEVICE_PERIPHERALS = true;)
};

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)
#nomangle
static mut DEVICE_PERIPHERALS: bool = false;

/// All the peripherals.
#[allow(non_snake_case)]
pub struct Peripherals {
#fields
}
if !config.skip_peripherals_struct {
let nomangle = if config.edition >= RustEdition::E2024 {
quote!(#[unsafe(no_mangle)])
} else {
quote!(#[no_mangle])
};

impl Peripherals {
/// Returns all the peripherals *once*.
#[cfg(feature = "critical-section")]
#[inline]
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 } {
return None
}
let set_device_peripherals_true = if config.edition >= RustEdition::E2024 {
quote!(unsafe { DEVICE_PERIPHERALS = true })
} else {
quote!(DEVICE_PERIPHERALS = true;)
};

// SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`,
// ensuring the peripherals can only be returned once.
Some(unsafe { Peripherals::steal() })
})
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)
#nomangle
static mut DEVICE_PERIPHERALS: bool = false;

/// All the peripherals.
#[allow(non_snake_case)]
pub struct Peripherals {
#fields
}

/// Unchecked version of `Peripherals::take`.
///
/// # Safety
///
/// Each of the returned peripherals must be used at most once.
#[inline]
pub unsafe fn steal() -> Self {
#set_device_peripherals_true

Peripherals {
#exprs
impl Peripherals {
/// Returns all the peripherals *once*.
#[cfg(feature = "critical-section")]
#[inline]
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 } {
return None
}

// SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`,
// ensuring the peripherals can only be returned once.
Some(unsafe { Peripherals::steal() })
})
}

/// Unchecked version of `Peripherals::take`.
///
/// # Safety
///
/// Each of the returned peripherals must be used at most once.
#[inline]
pub unsafe fn steal() -> Self {
#set_device_peripherals_true

Peripherals {
#exprs
}
}
}
}
});
});
}

Ok(out)
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake`
.action(ArgAction::SetTrue)
.help("Do not generate crate attributes"),
)
.arg(
Arg::new("skip_peripherals_struct")
.long("skip-peripherals-struct")
.alias("skip_peripherals_struct")
.action(ArgAction::SetTrue)
.help("Do not generate the `Peripherals` struct, its `take`/`steal` impl, and the `DEVICE_PERIPHERALS` static"),
)
.arg(
Arg::new("strict")
.long("strict")
Expand Down
Loading