#[macro_export] macro_rules! peripherals { ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { pub mod peripherals { $( $(#[$cfg])? #[allow(non_camel_case_types)] pub struct $name { _private: () } $(#[$cfg])? impl $name { /// Unsafely create an instance of this peripheral out of thin air. /// /// # Safety /// /// You must ensure that you're only using one instance of this type at a time. #[inline] pub unsafe fn steal() -> Self { Self{ _private: ()} } } $(#[$cfg])? $crate::unsafe_impl_unborrow!($name); )* } #[allow(non_snake_case)] pub struct Peripherals { $( $(#[$cfg])? pub $name: peripherals::$name, )* } impl Peripherals { ///Returns all the peripherals *once* #[inline] pub(crate) fn take() -> Self { #[no_mangle] static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false; critical_section::with(|_| unsafe { if _EMBASSY_DEVICE_PERIPHERALS { panic!("init called more than once!") } _EMBASSY_DEVICE_PERIPHERALS = true; Self::steal() }) } } impl Peripherals { /// Unsafely create an instance of this peripheral out of thin air. /// /// # Safety /// /// You must ensure that you're only using one instance of this type at a time. #[inline] pub unsafe fn steal() -> Self { Self { $( $(#[$cfg])? $name: peripherals::$name::steal(), )* } } } }; } #[macro_export] macro_rules! unborrow { ($($name:ident),*) => { $( let mut $name = $name.unborrow(); )* } } #[macro_export] macro_rules! unsafe_impl_unborrow { ($type:ident) => { unsafe impl $crate::Unborrow for $type { type Target = $type; #[inline] fn unborrow<'a>(self) -> $crate::Unborrowed<'a, Self::Target> where Self: 'a, { $crate::Unborrowed::new(self) } } }; }