From 50ecb7d42b74d6bd0af91310d6d02c1a4a6531dd Mon Sep 17 00:00:00 2001 From: xoviat Date: Mon, 29 Mar 2021 08:57:40 -0500 Subject: [PATCH] cleanup and consolidate peripherals macro --- embassy-extras/src/macros.rs | 39 ++++++++++++++++++++++++++++++++++++ embassy-stm32/src/lib.rs | 35 ++------------------------------ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index 9110f225..e57b89ec 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -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 = 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); + } + } + }; +} diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 40e6093d..3cef287c 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -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,