From 3ebbd97760c816ab88fadf493e4dbe9b7e880965 Mon Sep 17 00:00:00 2001 From: Priec Date: Wed, 3 Dec 2025 20:20:34 +0100 Subject: [PATCH] improvements to semestralka 2 --- external_led_blinky/src/bin/main.rs | 4 ++-- semestralka_2/src/bin/main.rs | 6 ++++-- semestralka_2/src/sleep/standby.rs | 28 ++++++++++++++++++++++------ semestralka_2/src/wakeup/iwdg.rs | 2 ++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/external_led_blinky/src/bin/main.rs b/external_led_blinky/src/bin/main.rs index a04dc34..bb9f880 100644 --- a/external_led_blinky/src/bin/main.rs +++ b/external_led_blinky/src/bin/main.rs @@ -13,12 +13,12 @@ async fn main(_spawner: Spawner) { let p = init(Config::default()); let mut output_pin = Output::new(p.PA3, Level::Low, Speed::Low); - let mut artificial_ground = Output::new(p.PB0, Level::Low, Speed::Low); + let mut _artificial_ground = Output::new(p.PB0, Level::Low, Speed::Low); loop { output_pin.set_high(); Timer::after(Duration::from_millis(500)).await; - output_pin.set_low(); + // output_pin.set_low(); Timer::after(Duration::from_millis(500)).await; } } diff --git a/semestralka_2/src/bin/main.rs b/semestralka_2/src/bin/main.rs index 8d7ae4a..020cbf1 100644 --- a/semestralka_2/src/bin/main.rs +++ b/semestralka_2/src/bin/main.rs @@ -20,7 +20,7 @@ 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 dma_gpio::sleep::standby::enter_standby_with_sram2_retention; +use dma_gpio::sleep::standby; use {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { @@ -70,7 +70,9 @@ async fn main(spawner: Spawner) { loop { info!("entering shutdown"); - enter_standby_with_sram2_retention(); + // enter_shutdown(); + info!("shutdown"); + standby::enter_standby_with_sram2_full(); cortex_m::asm::wfi(); yield_now().await; } diff --git a/semestralka_2/src/sleep/standby.rs b/semestralka_2/src/sleep/standby.rs index 3c4ad48..b07b1fe 100644 --- a/semestralka_2/src/sleep/standby.rs +++ b/semestralka_2/src/sleep/standby.rs @@ -12,12 +12,17 @@ pub fn enter_standby() -> ! { cortex_m::asm::udf(); // never happen marker } -pub fn enter_standby_with_sram2_retention() -> ! { - sram2_retention(); +pub fn enter_standby_with_sram2_8kb() -> ! { + sram2_8kb_retention(); enter_standby(); } -pub fn sram2_retention() { +pub fn enter_standby_with_sram2_full() -> ! { + sram2_full_retention(); + enter_standby(); +} + +pub fn sram2_full_retention() { unsafe extern "C" { fn HAL_PWREx_EnableSRAM2ContentStandbyRetention(sram2_pages: u32); } @@ -25,18 +30,29 @@ pub fn sram2_retention() { unsafe { // 0x60 = PWR_SRAM2_FULL_STANDBY = PWR_CR1_RRSB1 | PWR_CR1_RRSB2 // See: STM32U5xx HAL: stm32u5xx_hal_pwr_ex.h line 227 - // CMSIS: PWR_CR1_RRSB1=0x20, PWR_CR1_RRSB2=0x40 + // PWR_CR1_RRSB1=0x20, PWR_CR1_RRSB2=0x40 HAL_PWREx_EnableSRAM2ContentStandbyRetention(0x60); } } -pub fn disable_sram2_retention() { +pub fn sram2_8kb_retention() { + unsafe extern "C" { + fn HAL_PWREx_EnableSRAM2ContentStandbyRetention(sram2_pages: u32); + } + + unsafe { + // 0x40 = PWR_SRAM2_PAGE1_STANDBY = PWR_CR1_RRSB2 + // 8KB retention only + HAL_PWREx_EnableSRAM2ContentStandbyRetention(0x40); + } +} + +pub fn disable_sram2_full_retention() { unsafe extern "C" { fn HAL_PWREx_DisableSRAM2ContentStandbyRetention(sram2_pages: u32); } unsafe { - // Disable full SRAM2 retention HAL_PWREx_DisableSRAM2ContentStandbyRetention(0x60); } } diff --git a/semestralka_2/src/wakeup/iwdg.rs b/semestralka_2/src/wakeup/iwdg.rs index b9ccb33..bffdc3c 100644 --- a/semestralka_2/src/wakeup/iwdg.rs +++ b/semestralka_2/src/wakeup/iwdg.rs @@ -6,6 +6,7 @@ use embassy_stm32::wdg::IndependentWatchdog; use embassy_stm32::Peri; use embassy_time::{Duration, Timer}; use embassy_stm32::pac; +use crate::sleep::standby; use crate::config::WATCHDOG_TIMEOUT_US; @@ -20,6 +21,7 @@ use crate::config::WATCHDOG_TIMEOUT_US; /// - `PWR_SR` — Power Control / Status pub fn clear_wakeup_flags() { info!("Clearing wakeup flags..."); + standby::disable_sram2_full_retention(); // Clear reset flags // let rcc = unsafe { &*pac::RCC::ptr() };