2021-03-21 19:57:49 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! peripherals {
|
|
|
|
($($(#[$cfg:meta])? $name:ident: $type:ident),*$(,)?) => {
|
2021-03-21 20:58:59 +00:00
|
|
|
pub mod peripherals {
|
|
|
|
$(
|
|
|
|
$(#[$cfg])?
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub struct $type { _private: () }
|
|
|
|
|
2021-03-22 13:55:27 +00:00
|
|
|
$(#[$cfg])?
|
2021-03-21 20:58:59 +00:00
|
|
|
impl embassy::util::Steal for $type {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn steal() -> Self {
|
|
|
|
Self{ _private: ()}
|
|
|
|
}
|
2021-03-21 19:57:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 20:58:59 +00:00
|
|
|
$(#[$cfg])?
|
|
|
|
impl embassy::util::PeripheralBorrow for $type {
|
|
|
|
type Target = $type;
|
|
|
|
#[inline]
|
|
|
|
unsafe fn unborrow(self) -> $type {
|
|
|
|
self
|
|
|
|
}
|
2021-03-21 19:57:49 +00:00
|
|
|
}
|
2021-03-21 20:58:59 +00:00
|
|
|
|
|
|
|
$(#[$cfg])?
|
|
|
|
impl embassy::util::PeripheralBorrow for &mut $type {
|
|
|
|
type Target = $type;
|
|
|
|
#[inline]
|
|
|
|
unsafe fn unborrow(self) -> $type {
|
|
|
|
::core::ptr::read(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
2021-03-21 19:57:49 +00:00
|
|
|
|
|
|
|
pub struct Peripherals {
|
|
|
|
$(
|
|
|
|
$(#[$cfg])?
|
2021-03-21 20:58:59 +00:00
|
|
|
pub $name: peripherals::$type,
|
2021-03-21 19:57:49 +00:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Peripherals {
|
2021-03-21 20:58:59 +00: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 {
|
2021-03-21 19:57:49 +00:00
|
|
|
Self {
|
|
|
|
$(
|
|
|
|
$(#[$cfg])?
|
2021-03-21 20:58:59 +00:00
|
|
|
$name: <peripherals::$type as embassy::util::Steal>::steal(),
|
2021-03-21 19:57:49 +00:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
2021-03-21 21:09:06 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! unborrow {
|
|
|
|
($($name:ident),*) => {
|
|
|
|
$(
|
2021-03-22 01:10:59 +00:00
|
|
|
let mut $name = unsafe { $name.unborrow() };
|
2021-03-21 21:09:06 +00:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|