Skip to content

Commit 6a73d56

Browse files
committed
Add statically checked macro for safe struct Peripherals creation
1 parent bfccae0 commit 6a73d56

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/lib.rs

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

0 commit comments

Comments
 (0)