aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-08-23 20:18:34 -0500
committerxoviat <[email protected]>2023-08-23 20:18:34 -0500
commit83f224e14094488244792dc1bc6d7425d8dcd68f (patch)
tree5efe86ecb85733a04e35f26be095d7e83917dca4
parente987259716c56f9854cc4730620d7b5a8be9962d (diff)
stm32/lp: add refcount
-rw-r--r--embassy-stm32/build.rs4
-rw-r--r--embassy-stm32/src/rcc/mod.rs21
2 files changed, 25 insertions, 0 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index 8a731620f..6c364f7bb 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -356,6 +356,8 @@ fn main() {
356 } 356 }
357 fn enable() { 357 fn enable() {
358 critical_section::with(|_| { 358 critical_section::with(|_| {
359 #[cfg(feature = "low-power")]
360 crate::rcc::clock_refcount_add();
359 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true)); 361 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true));
360 #after_enable 362 #after_enable
361 }) 363 })
@@ -363,6 +365,8 @@ fn main() {
363 fn disable() { 365 fn disable() {
364 critical_section::with(|_| { 366 critical_section::with(|_| {
365 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false)); 367 crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false));
368 #[cfg(feature = "low-power")]
369 crate::rcc::clock_refcount_sub();
366 }) 370 })
367 } 371 }
368 fn reset() { 372 fn reset() {
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs
index ac9ae9c6a..3c75923e5 100644
--- a/embassy-stm32/src/rcc/mod.rs
+++ b/embassy-stm32/src/rcc/mod.rs
@@ -26,6 +26,8 @@ use crate::time::Hertz;
26#[cfg_attr(any(rcc_h5, rcc_h50), path = "h5.rs")] 26#[cfg_attr(any(rcc_h5, rcc_h50), path = "h5.rs")]
27mod _version; 27mod _version;
28pub use _version::*; 28pub use _version::*;
29#[cfg(feature = "low-power")]
30use atomic_polyfill::{AtomicU32, Ordering};
29 31
30#[derive(Clone, Copy, Debug)] 32#[derive(Clone, Copy, Debug)]
31#[cfg_attr(feature = "defmt", derive(defmt::Format))] 33#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -79,6 +81,25 @@ pub struct Clocks {
79 pub rtc: Option<Hertz>, 81 pub rtc: Option<Hertz>,
80} 82}
81 83
84#[cfg(feature = "low-power")]
85static CLOCK_REFCOUNT: AtomicU32 = AtomicU32::new(0);
86
87#[cfg(feature = "low-power")]
88pub fn low_power_ready() -> bool {
89 CLOCK_REFCOUNT.load(Ordering::SeqCst) == 0
90}
91
92#[cfg(feature = "low-power")]
93pub(crate) fn clock_refcount_add() {
94 // We don't check for overflow because constructing more than u32 peripherals is unlikely
95 CLOCK_REFCOUNT.fetch_add(1, Ordering::Relaxed);
96}
97
98#[cfg(feature = "low-power")]
99pub(crate) fn clock_refcount_sub() {
100 assert!(CLOCK_REFCOUNT.fetch_sub(1, Ordering::Relaxed) != 0);
101}
102
82/// Frozen clock frequencies 103/// Frozen clock frequencies
83/// 104///
84/// The existence of this value indicates that the clock configuration can no longer be changed 105/// The existence of this value indicates that the clock configuration can no longer be changed