aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2021-03-29 09:00:23 -0500
committerGitHub <[email protected]>2021-03-29 09:00:23 -0500
commit494c47808a7a83457bcf0cc59dad9c27317f12f3 (patch)
tree3ed5cad7132a89f1aa75e6591a9699234bdc700e
parent86f59d144470c2611a8babd9a49f528502752181 (diff)
parent50ecb7d42b74d6bd0af91310d6d02c1a4a6531dd (diff)
Merge pull request #122 from xoviat/cleanup
cleanup and consolidate peripherals macro
-rw-r--r--embassy-extras/src/macros.rs39
-rw-r--r--embassy-stm32/src/lib.rs35
2 files changed, 41 insertions, 33 deletions
diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs
index 9110f2251..e57b89ec7 100644
--- a/embassy-extras/src/macros.rs
+++ b/embassy-extras/src/macros.rs
@@ -105,3 +105,42 @@ macro_rules! impl_unborrow {
105 } 105 }
106 }; 106 };
107} 107}
108
109#[macro_export]
110macro_rules! std_peripherals {
111 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
112 #[doc = r"All the peripherals"]
113 #[allow(non_snake_case)]
114 pub struct Peripherals {
115 $(
116 $(#[$cfg])?
117 pub $name: pac::$name,
118 )+
119 }
120
121 static mut GLOBAL_CLOCKS: Option<Clocks> = None;
122
123 impl Peripherals {
124 pub fn take() -> Option<(Peripherals, Clocks)> {
125 match unsafe {GLOBAL_CLOCKS} {
126 Some(clocks) => {
127 let dp = unsafe { pac::Peripherals::steal() };
128 let peripherals = Peripherals {
129 $(
130 $(#[$cfg])?
131 $name: dp.$name,
132 )+
133 };
134
135 Some((peripherals, clocks))
136 },
137 None => None,
138 }
139 }
140
141 pub unsafe fn set_peripherals(clocks: Clocks) {
142 GLOBAL_CLOCKS.replace(clocks);
143 }
144 }
145 };
146}
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index 40e6093d5..3cef287c3 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -100,39 +100,8 @@ use core::option::Option;
100use hal::prelude::*; 100use hal::prelude::*;
101use hal::rcc::Clocks; 101use hal::rcc::Clocks;
102 102
103macro_rules! peripherals {
104 ($($PER:ident,)+) => {
105 #[doc = r"All the peripherals"]
106 #[allow(non_snake_case)]
107 pub struct Peripherals {
108 $(
109 pub $PER: pac::$PER,
110 )+
111 }
112
113 static mut GLOBAL_PERIPHERALS: Option<(Peripherals, Clocks)> = None;
114
115 impl Peripherals {
116 pub fn take() -> Option<(Peripherals, Clocks)> {
117 unsafe { GLOBAL_PERIPHERALS.take() }
118 }
119
120 pub unsafe fn set_peripherals(clocks: Clocks) {
121 let dp = pac::Peripherals::steal();
122 let peripherals = Peripherals {
123 $(
124 $PER: dp.$PER,
125 )+
126 };
127
128 GLOBAL_PERIPHERALS.replace((peripherals, clocks));
129 }
130 }
131 };
132}
133
134#[cfg(feature = "stm32f446")] 103#[cfg(feature = "stm32f446")]
135peripherals! { 104embassy_extras::std_peripherals! {
136 DCMI, 105 DCMI,
137 FMC, 106 FMC,
138 DBGMCU, 107 DBGMCU,
@@ -211,7 +180,7 @@ peripherals! {
211} 180}
212 181
213#[cfg(feature = "stm32f405")] 182#[cfg(feature = "stm32f405")]
214peripherals! { 183embassy_extras::std_peripherals! {
215 RNG, 184 RNG,
216 DCMI, 185 DCMI,
217 FSMC, 186 FSMC,