embassy/embassy-extras/src/macros.rs

76 lines
2.1 KiB
Rust
Raw Normal View History

#[macro_export]
macro_rules! peripherals {
($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => {
2021-03-21 21:58:59 +01:00
pub mod peripherals {
$(
$(#[$cfg])?
#[allow(non_camel_case_types)]
pub struct $type { _private: () }
impl embassy::util::Steal for $type {
#[inline]
unsafe fn steal() -> Self {
Self{ _private: ()}
}
}
2021-03-21 21:58:59 +01:00
$(#[$cfg])?
impl embassy::util::PeripheralBorrow for $type {
type Target = $type;
#[inline]
unsafe fn unborrow(self) -> $type {
self
}
}
2021-03-21 21:58:59 +01:00
$(#[$cfg])?
impl embassy::util::PeripheralBorrow for &mut $type {
type Target = $type;
#[inline]
unsafe fn unborrow(self) -> $type {
::core::ptr::read(self)
}
}
)*
}
pub struct Peripherals {
$(
$(#[$cfg])?
2021-03-21 21:58:59 +01:00
pub $name: peripherals::$type,
)*
}
impl Peripherals {
2021-03-21 21:58:59 +01:00
///Returns all the peripherals *once*
#[inline]
pub fn take() -> Option<Self> {
#[no_mangle]
static mut _EMBASSY_DEVICE_PERIPHERALS: bool = false;
cortex_m::interrupt::free(|_| {
if unsafe { _EMBASSY_DEVICE_PERIPHERALS } {
None
} else {
Some(unsafe { <Self as embassy::util::Steal>::steal() })
}
})
}
}
impl embassy::util::Steal for Peripherals {
#[inline]
unsafe fn steal() -> Self {
Self {
$(
$(#[$cfg])?
2021-03-21 21:58:59 +01:00
$name: <peripherals::$type as embassy::util::Steal>::steal(),
)*
}
}
}
};
}