From 434e2b3d212cf1886b8d8d337022726e2eda0c03 Mon Sep 17 00:00:00 2001 From: Priec Date: Wed, 3 Dec 2025 17:57:10 +0100 Subject: [PATCH] shutdown added --- semestralka_2/src/bin/main.rs | 15 ++++----------- semestralka_2/src/sleep/mod.rs | 1 + semestralka_2/src/sleep/shutdown.rs | 13 +++++++++++++ semestralka_2/src/wakeup/iwdg.rs | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 semestralka_2/src/sleep/shutdown.rs diff --git a/semestralka_2/src/bin/main.rs b/semestralka_2/src/bin/main.rs index eb6fdd9..93e831e 100644 --- a/semestralka_2/src/bin/main.rs +++ b/semestralka_2/src/bin/main.rs @@ -19,16 +19,13 @@ use dma_gpio::config::{ }; use dma_gpio::hw_uart_pc::{driver::uart_task, usart1}; use dma_gpio::wakeup::iwdg::{clear_wakeup_flags, init_watchdog}; +use dma_gpio::sleep::shutdown::enter_shutdown; use {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { USART1 => BufferedInterruptHandler; }); -unsafe extern "C" { - fn HAL_PWR_EnterSTANDBYMode(); -} - #[embassy_executor::main] async fn main(spawner: Spawner) { info!("boot"); @@ -65,18 +62,14 @@ async fn main(spawner: Spawner) { info!("DBGMCU CR: dbg_stop={}, dbg_standby={}", cr.dbg_stop(), cr.dbg_standby()); // MAIN LOOP - info!("Preparing for standby..."); Timer::after(Duration::from_millis(500)).await; - init_watchdog(p.IWDG).await; + // init_watchdog(p.IWDG).await; Timer::after(Duration::from_millis(10)).await; - - unsafe { - info!("Standby..."); - HAL_PWR_EnterSTANDBYMode(); - } loop { + info!("entering shutdown"); + enter_shutdown(); cortex_m::asm::wfi(); yield_now().await; } diff --git a/semestralka_2/src/sleep/mod.rs b/semestralka_2/src/sleep/mod.rs index d57c64f..eec9227 100644 --- a/semestralka_2/src/sleep/mod.rs +++ b/semestralka_2/src/sleep/mod.rs @@ -1,3 +1,4 @@ // src/sleep/mod.rs pub mod standby; +pub mod shutdown; diff --git a/semestralka_2/src/sleep/shutdown.rs b/semestralka_2/src/sleep/shutdown.rs new file mode 100644 index 0000000..44bc0b3 --- /dev/null +++ b/semestralka_2/src/sleep/shutdown.rs @@ -0,0 +1,13 @@ +// src/sleep/standby.rs + +pub fn enter_shutdown() -> ! { + unsafe extern "C" { + fn HAL_PWREx_EnterSHUTDOWNMode(); + } + + unsafe { + HAL_PWREx_EnterSHUTDOWNMode(); + } + + cortex_m::asm::udf(); //panic +} diff --git a/semestralka_2/src/wakeup/iwdg.rs b/semestralka_2/src/wakeup/iwdg.rs index 977d7af..b9ccb33 100644 --- a/semestralka_2/src/wakeup/iwdg.rs +++ b/semestralka_2/src/wakeup/iwdg.rs @@ -9,6 +9,15 @@ use embassy_stm32::pac; use crate::config::WATCHDOG_TIMEOUT_US; +/// Clears system reset and standby flags after wakeup. +/// +/// Call early in startup to: +/// - Clear reset flags by setting `RMVF` in `RCC_CSR`. +/// - If `SBF` in `PWR_SR` is set (woke from Standby), clear it. +/// +/// # Registers +/// - `RCC_CSR` — Reset and Clock Control / Status +/// - `PWR_SR` — Power Control / Status pub fn clear_wakeup_flags() { info!("Clearing wakeup flags..."); @@ -26,6 +35,11 @@ pub fn clear_wakeup_flags() { } } +/// Initializes the Independent Watchdog (IWDG) timer. +/// Wakeup source: Can wake the system from Standby mode on timeout +/// +/// # Timing +/// - Timeout value is configured in `WATCHDOG_TIMEOUT_US` from config.rs pub async fn init_watchdog(iwdg: Peri<'_, peripherals::IWDG>) { info!("Initializing watchdog after watchdog wake..."); let mut watchdog = IndependentWatchdog::new(iwdg, WATCHDOG_TIMEOUT_US);