diff --git a/semestralka_2/src/bin/main.rs b/semestralka_2/src/bin/main.rs index 93e831e..8d7ae4a 100644 --- a/semestralka_2/src/bin/main.rs +++ b/semestralka_2/src/bin/main.rs @@ -20,6 +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 {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { @@ -64,12 +65,12 @@ async fn main(spawner: Spawner) { // MAIN LOOP Timer::after(Duration::from_millis(500)).await; - // init_watchdog(p.IWDG).await; + init_watchdog(p.IWDG).await; Timer::after(Duration::from_millis(10)).await; loop { info!("entering shutdown"); - enter_shutdown(); + enter_standby_with_sram2_retention(); cortex_m::asm::wfi(); yield_now().await; } diff --git a/semestralka_2/src/sleep/standby.rs b/semestralka_2/src/sleep/standby.rs index f1374f8..3c4ad48 100644 --- a/semestralka_2/src/sleep/standby.rs +++ b/semestralka_2/src/sleep/standby.rs @@ -11,3 +11,32 @@ pub fn enter_standby() -> ! { cortex_m::asm::udf(); // never happen marker } + +pub fn enter_standby_with_sram2_retention() -> ! { + sram2_retention(); + enter_standby(); +} + +pub fn sram2_retention() { + unsafe extern "C" { + fn HAL_PWREx_EnableSRAM2ContentStandbyRetention(sram2_pages: u32); + } + + 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 + HAL_PWREx_EnableSRAM2ContentStandbyRetention(0x60); + } +} + +pub fn disable_sram2_retention() { + unsafe extern "C" { + fn HAL_PWREx_DisableSRAM2ContentStandbyRetention(sram2_pages: u32); + } + + unsafe { + // Disable full SRAM2 retention + HAL_PWREx_DisableSRAM2ContentStandbyRetention(0x60); + } +}