Merge pull request #122 from xoviat/cleanup

cleanup and consolidate peripherals macro
This commit is contained in:
xoviat 2021-03-29 09:00:23 -05:00 committed by GitHub
commit 494c47808a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 33 deletions

View file

@ -105,3 +105,42 @@ macro_rules! impl_unborrow {
}
};
}
#[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)> {
match unsafe {GLOBAL_CLOCKS} {
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);
}
}
};
}

View file

@ -100,39 +100,8 @@ use core::option::Option;
use hal::prelude::*;
use hal::rcc::Clocks;
macro_rules! peripherals {
($($PER:ident,)+) => {
#[doc = r"All the peripherals"]
#[allow(non_snake_case)]
pub struct Peripherals {
$(
pub $PER: pac::$PER,
)+
}
static mut GLOBAL_PERIPHERALS: Option<(Peripherals, Clocks)> = None;
impl Peripherals {
pub fn take() -> Option<(Peripherals, Clocks)> {
unsafe { GLOBAL_PERIPHERALS.take() }
}
pub unsafe fn set_peripherals(clocks: Clocks) {
let dp = pac::Peripherals::steal();
let peripherals = Peripherals {
$(
$PER: dp.$PER,
)+
};
GLOBAL_PERIPHERALS.replace((peripherals, clocks));
}
}
};
}
#[cfg(feature = "stm32f446")]
peripherals! {
embassy_extras::std_peripherals! {
DCMI,
FMC,
DBGMCU,
@ -211,7 +180,7 @@ peripherals! {
}
#[cfg(feature = "stm32f405")]
peripherals! {
embassy_extras::std_peripherals! {
RNG,
DCMI,
FSMC,