Skip to content

Commit

Permalink
Add statically checked macro for safe struct Peripherals creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuesch committed Aug 7, 2022
1 parent 6ab6f94 commit 61fe2ac
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,24 @@ pub use crate::devices::attiny85;
pub use crate::devices::attiny861;
#[cfg(feature = "attiny88")]
pub use crate::devices::attiny88;

/// Safely retrieve an instance of the device's `struct Peripherals` once.
///
/// This macro can only be used once in the whole program.
/// Multiple uses will result in a linker abort.
#[macro_export]
macro_rules! peripherals {
($device_name: ident) => {
{
{
#[no_mangle]
#[link_section=".__peripherals_usage_restriction__"]
#[export_name="__ERROR__avr_device__peripherals__macro_must_only_be_used_once__"]
static _x: () = ();
}
unsafe {
avr_device::$device_name::Peripherals::steal()
}
}
}
}

0 comments on commit 61fe2ac

Please sign in to comment.