embassy/embassy-extras/src/macros.rs

148 lines
4.0 KiB
Rust
Raw Normal View History

#[macro_export]
macro_rules! peripherals {
2021-03-27 03:12:58 +01:00
($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
2021-03-21 21:58:59 +01:00
pub mod peripherals {
$(
$(#[$cfg])?
#[allow(non_camel_case_types)]
2021-03-27 03:12:58 +01:00
pub struct $name { _private: () }
2021-03-21 21:58:59 +01:00
$(#[$cfg])?
2021-03-27 03:12:58 +01:00
impl embassy::util::Steal for $name {
2021-03-21 21:58:59 +01:00
#[inline]
unsafe fn steal() -> Self {
Self{ _private: ()}
}
}
2021-03-21 21:58:59 +01:00
$(#[$cfg])?
2021-04-14 19:59:52 +02:00
impl embassy::util::Unborrow for $name {
2021-03-27 03:12:58 +01:00
type Target = $name;
2021-03-21 21:58:59 +01:00
#[inline]
2021-03-27 03:12:58 +01:00
unsafe fn unborrow(self) -> $name {
2021-03-21 21:58:59 +01:00
self
}
}
2021-03-21 21:58:59 +01:00
$(#[$cfg])?
2021-04-14 19:59:52 +02:00
impl embassy::util::Unborrow for &mut $name {
2021-03-27 03:12:58 +01:00
type Target = $name;
2021-03-21 21:58:59 +01:00
#[inline]
2021-03-27 03:12:58 +01:00
unsafe fn unborrow(self) -> $name {
2021-03-21 21:58:59 +01:00
::core::ptr::read(self)
}
}
)*
}
2021-03-27 03:12:58 +01:00
#[allow(non_snake_case)]
pub struct Peripherals {
$(
$(#[$cfg])?
2021-03-27 03:12:58 +01:00
pub $name: peripherals::$name,
)*
}
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;
2021-05-11 00:57:52 +02:00
critical_section::with(|_| {
2021-03-21 21:58:59 +01:00
if unsafe { _EMBASSY_DEVICE_PERIPHERALS } {
None
} else {
2021-04-01 23:29:58 +02:00
unsafe { _EMBASSY_DEVICE_PERIPHERALS = true };
2021-03-21 21:58:59 +01:00
Some(unsafe { <Self as embassy::util::Steal>::steal() })
}
})
}
}
impl embassy::util::Steal for Peripherals {
#[inline]
unsafe fn steal() -> Self {
Self {
$(
$(#[$cfg])?
2021-03-27 03:12:58 +01:00
$name: <peripherals::$name as embassy::util::Steal>::steal(),
)*
}
}
}
};
}
2021-03-21 22:09:06 +01:00
#[macro_export]
macro_rules! unborrow {
($($name:ident),*) => {
$(
2021-03-22 02:10:59 +01:00
let mut $name = unsafe { $name.unborrow() };
2021-03-21 22:09:06 +01:00
)*
}
}
2021-03-27 03:33:32 +01:00
#[macro_export]
macro_rules! impl_unborrow {
($type:ident) => {
2021-04-14 19:59:52 +02:00
impl ::embassy::util::Unborrow for $type {
2021-03-27 03:33:32 +01:00
type Target = $type;
#[inline]
unsafe fn unborrow(self) -> Self::Target {
self
}
}
2021-04-14 19:59:52 +02:00
impl<'a> ::embassy::util::Unborrow for &'a mut $type {
2021-03-27 03:33:32 +01:00
type Target = $type;
#[inline]
unsafe fn unborrow(self) -> Self::Target {
unsafe { ::core::ptr::read(self) }
}
}
};
}
#[macro_export]
macro_rules! std_peripherals {
($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
#[doc = r"All the peripherals"]
#[allow(non_snake_case)]
pub struct Peripherals {
$(
$(#[$cfg])?
pub $name: pac::$name,
)+
}
static mut GLOBAL_CLOCKS: Option<Clocks> = None;
impl Peripherals {
pub fn take() -> Option<(Peripherals, Clocks)> {
2021-03-29 16:02:23 +02:00
match unsafe {GLOBAL_CLOCKS.take()} {
Some(clocks) => {
let dp = unsafe { pac::Peripherals::steal() };
let peripherals = Peripherals {
$(
$(#[$cfg])?
$name: dp.$name,
)+
};
Some((peripherals, clocks))
},
None => None,
}
}
pub unsafe fn set_peripherals(clocks: Clocks) {
GLOBAL_CLOCKS.replace(clocks);
}
}
};
}