From 2a4890165d460324966a718fde9b712b06e3cc38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Kr=C3=B6ger?= <timokroeger93@gmail.com>
Date: Thu, 29 Jul 2021 15:24:42 +0200
Subject: [PATCH] stm32f0: Enable debug access in low power modes

---
 embassy-stm32/src/rcc/f0/mod.rs        | 10 +++-------
 examples/stm32f0/src/bin/hello.rs      |  2 +-
 examples/stm32f0/src/example_common.rs |  8 ++++++++
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/embassy-stm32/src/rcc/f0/mod.rs b/embassy-stm32/src/rcc/f0/mod.rs
index 870fe42cf..6600a1e28 100644
--- a/embassy-stm32/src/rcc/f0/mod.rs
+++ b/embassy-stm32/src/rcc/f0/mod.rs
@@ -2,7 +2,8 @@ use core::marker::PhantomData;
 
 use embassy::util::Unborrow;
 
-use crate::pac::{DBGMCU, FLASH, RCC};
+use crate::dbgmcu::Dbgmcu;
+use crate::pac::{FLASH, RCC};
 use crate::peripherals;
 use crate::time::Hertz;
 
@@ -193,12 +194,7 @@ impl<'d> Rcc<'d> {
             if self.config.enable_debug_wfe {
                 RCC.ahbenr().modify(|w| w.set_dmaen(true));
 
-                critical_section::with(|_| {
-                    DBGMCU.cr().modify(|w| {
-                        w.set_dbg_standby(true);
-                        w.set_dbg_stop(true);
-                    });
-                });
+                critical_section::with(|_| Dbgmcu::enable_all());
             }
         }
 
diff --git a/examples/stm32f0/src/bin/hello.rs b/examples/stm32f0/src/bin/hello.rs
index a78b9892f..6ebb5cd4d 100644
--- a/examples/stm32f0/src/bin/hello.rs
+++ b/examples/stm32f0/src/bin/hello.rs
@@ -14,7 +14,7 @@ use embassy_stm32::Peripherals;
 #[path = "../example_common.rs"]
 mod example_common;
 
-#[embassy::main]
+#[embassy::main(config = "example_common::config()")]
 async fn main(_spawner: Spawner, _p: Peripherals) -> ! {
     loop {
         Timer::after(Duration::from_secs(1)).await;
diff --git a/examples/stm32f0/src/example_common.rs b/examples/stm32f0/src/example_common.rs
index 54d633837..e0ba4cd01 100644
--- a/examples/stm32f0/src/example_common.rs
+++ b/examples/stm32f0/src/example_common.rs
@@ -6,6 +6,14 @@ use panic_probe as _;
 pub use defmt::*;
 
 use core::sync::atomic::{AtomicUsize, Ordering};
+use embassy_stm32::rcc;
+use embassy_stm32::Config;
+
+pub fn config() -> Config {
+    let mut rcc_config = rcc::Config::default();
+    rcc_config.enable_debug_wfe = true;
+    Config::default().rcc(rcc_config)
+}
 
 defmt::timestamp! {"{=u64}", {
         static COUNT: AtomicUsize = AtomicUsize::new(0);