Skip to content

Commit 61fe2ac

Browse files
committed
Add statically checked macro for safe struct Peripherals creation
1 parent 6ab6f94 commit 61fe2ac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,24 @@ pub use crate::devices::attiny85;
274274
pub use crate::devices::attiny861;
275275
#[cfg(feature = "attiny88")]
276276
pub use crate::devices::attiny88;
277+
278+
/// Safely retrieve an instance of the device's `struct Peripherals` once.
279+
///
280+
/// This macro can only be used once in the whole program.
281+
/// Multiple uses will result in a linker abort.
282+
#[macro_export]
283+
macro_rules! peripherals {
284+
($device_name: ident) => {
285+
{
286+
{
287+
#[no_mangle]
288+
#[link_section=".__peripherals_usage_restriction__"]
289+
#[export_name="__ERROR__avr_device__peripherals__macro_must_only_be_used_once__"]
290+
static _x: () = ();
291+
}
292+
unsafe {
293+
avr_device::$device_name::Peripherals::steal()
294+
}
295+
}
296+
}
297+
}

0 commit comments

Comments
 (0)