From 20dfdbc33529504b28f996214c483eb7bc82672a Mon Sep 17 00:00:00 2001 From: Priec Date: Sun, 14 Dec 2025 14:33:01 +0100 Subject: [PATCH] wake up pin working for the wake up --- semestralka_2_uart/src/bin/main.rs | 4 +++- semestralka_2_uart/src/hw_uart_pc/init.rs | 2 +- semestralka_2_uart/src/sleep/handler.rs | 12 +++++++----- semestralka_2_uart/src/sleep/stop.rs | 17 +++++------------ semestralka_2_uart/src/wakeup/gpio.rs | 8 +++++--- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/semestralka_2_uart/src/bin/main.rs b/semestralka_2_uart/src/bin/main.rs index adfafc7..df63101 100644 --- a/semestralka_2_uart/src/bin/main.rs +++ b/semestralka_2_uart/src/bin/main.rs @@ -11,6 +11,7 @@ use embassy_time::{Duration, Timer}; use dma_gpio::config::BAUD; use dma_gpio::wakeup::iwdg::{clear_wakeup_flags, init_watchdog_reset}; +use dma_gpio::wakeup::gpio::gpio_wakeup; use dma_gpio::sleep::handler::execute_low_power; use dma_gpio::hw_uart_pc::init::{init_hw_uart_to_pc, LowPowerCmd, CMD_CH}; use {defmt_rtt as _, panic_probe as _}; @@ -29,7 +30,7 @@ async fn main(spawner: Spawner) { info!("LED ON (MCU awake)"); // LED BLINK - // spawner.spawn(led_blink(led).unwrap()); + spawner.spawn(led_blink(led).unwrap()); // INIT HW UART init_hw_uart_to_pc(p.USART1, p.PA10, p.PA9, &spawner); @@ -65,6 +66,7 @@ async fn main(spawner: Spawner) { info!("ready for uart"); let mut iwdg = Some(p.IWDG); + spawner.spawn(gpio_wakeup(p.PA0, p.EXTI0).unwrap()); // exti wake up loop { let cmd = CMD_CH.receive().await; execute_low_power(cmd, &mut iwdg).await; diff --git a/semestralka_2_uart/src/hw_uart_pc/init.rs b/semestralka_2_uart/src/hw_uart_pc/init.rs index 4571ca8..fdd77f6 100644 --- a/semestralka_2_uart/src/hw_uart_pc/init.rs +++ b/semestralka_2_uart/src/hw_uart_pc/init.rs @@ -97,7 +97,7 @@ pub async fn uart_cmd_task() { CMD_CH.send(LowPowerCmd::StopMode(StopModeConfig { mode: stop_mode, entry: StopEntry::Wfi, // to tu je, lebo nejdem prepisovat kod, kvoli - // posratemu halu co si nevie urobit konzistentnost + // pos**temu halu co si nevie urobit konzistentnost })).await; continue; } diff --git a/semestralka_2_uart/src/sleep/handler.rs b/semestralka_2_uart/src/sleep/handler.rs index aaf8ce1..9ca4d2a 100644 --- a/semestralka_2_uart/src/sleep/handler.rs +++ b/semestralka_2_uart/src/sleep/handler.rs @@ -14,7 +14,7 @@ use defmt::info; pub async fn execute_low_power( cmd: LowPowerCmd, iwdg: &mut Option> -) -> ! { +) { Timer::after(Duration::from_millis(10)).await; match cmd { @@ -45,14 +45,16 @@ pub async fn execute_low_power( } LowPowerCmd::StopMode(StopModeConfig { mode, entry }) => { info!("Entering {:?} with {:?}", mode, entry); - if let Some(wdg) = iwdg.take() { - init_watchdog_reset(wdg).await; - } match mode { StopMode::Stop0 => enter_stop0(), StopMode::Stop1 => enter_stop1(entry), StopMode::Stop2 => enter_stop2(entry), - StopMode::Stop3 => enter_stop3(entry), + StopMode::Stop3 => { + if let Some(wdg) = iwdg.take() { + init_watchdog_reset(wdg).await; + } + enter_stop3(entry); + } } } } diff --git a/semestralka_2_uart/src/sleep/stop.rs b/semestralka_2_uart/src/sleep/stop.rs index da89436..fd05201 100644 --- a/semestralka_2_uart/src/sleep/stop.rs +++ b/semestralka_2_uart/src/sleep/stop.rs @@ -22,7 +22,7 @@ impl From for u8 { } } -pub fn enter_stop0() -> ! { +pub fn enter_stop0() { unsafe extern "C" { fn rust_LL_PWR_SetPowerMode(mode: u32); } @@ -35,12 +35,11 @@ pub fn enter_stop0() -> ! { let mut core = Peripherals::steal(); core.SCB.set_sleepdeep(); asm::wfi(); + core.SCB.clear_sleepdeep(); } - - cortex_m::asm::udf(); } -pub fn enter_stop1(entry: StopEntry) -> ! { +pub fn enter_stop1(entry: StopEntry) { unsafe extern "C" { fn HAL_PWREx_EnterSTOP1Mode(entry: u8); } @@ -48,11 +47,9 @@ pub fn enter_stop1(entry: StopEntry) -> ! { unsafe { HAL_PWREx_EnterSTOP1Mode(entry.into()); } - - cortex_m::asm::udf() } -pub fn enter_stop2(entry: StopEntry) -> ! { +pub fn enter_stop2(entry: StopEntry) { unsafe extern "C" { fn HAL_PWREx_EnterSTOP2Mode(entry: u8); } @@ -60,11 +57,9 @@ pub fn enter_stop2(entry: StopEntry) -> ! { unsafe { HAL_PWREx_EnterSTOP2Mode(entry.into()); } - - cortex_m::asm::udf() } -pub fn enter_stop3(entry: StopEntry) -> ! { +pub fn enter_stop3(entry: StopEntry) { unsafe extern "C" { fn HAL_PWREx_EnterSTOP3Mode(entry: u8); } @@ -72,6 +67,4 @@ pub fn enter_stop3(entry: StopEntry) -> ! { unsafe { HAL_PWREx_EnterSTOP3Mode(entry.into()); } - - cortex_m::asm::udf() } diff --git a/semestralka_2_uart/src/wakeup/gpio.rs b/semestralka_2_uart/src/wakeup/gpio.rs index 7761e78..8447da6 100644 --- a/semestralka_2_uart/src/wakeup/gpio.rs +++ b/semestralka_2_uart/src/wakeup/gpio.rs @@ -4,6 +4,7 @@ use defmt::info; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::Pull; use embassy_stm32::{peripherals, interrupt}; +use embassy_time::{Duration, Timer}; use embassy_hal_internal::Peri; /// GPIO pin as an EXTI wake‑up source. @@ -13,11 +14,12 @@ pub async fn gpio_wakeup( ch: Peri<'static, peripherals::EXTI0> ) { info!("Configuring EXTI wake input on PA0 (rising edge)"); - let mut btn = ExtiInput::new(pin, ch, Pull::Down); + let mut btn = ExtiInput::new(pin, ch, Pull::Up); loop { - info!("→ Waiting for rising edge on PA0…"); - btn.wait_for_rising_edge().await; + info!("Waiting for rising edge on PA0"); + btn.wait_for_falling_edge().await; info!("GPIO wake‑up: event detected!"); + Timer::after(Duration::from_millis(50)).await; } }