From 49cc8dcc71ae5fbf4f92cdd636f7843693a656ee Mon Sep 17 00:00:00 2001 From: Priec Date: Sun, 14 Dec 2025 11:56:09 +0100 Subject: [PATCH] stop0 working --- semestralka_2_uart/build.rs | 2 ++ semestralka_2_uart/src/c_ll/stop0_ll.c | 8 ++++++++ semestralka_2_uart/src/hw_uart_pc/init.rs | 12 +++++++++++- semestralka_2_uart/src/sleep/handler.rs | 1 + semestralka_2_uart/src/sleep/stop.rs | 22 ++++++++++++++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 semestralka_2_uart/src/c_ll/stop0_ll.c diff --git a/semestralka_2_uart/build.rs b/semestralka_2_uart/build.rs index bc6641f..1635f11 100644 --- a/semestralka_2_uart/build.rs +++ b/semestralka_2_uart/build.rs @@ -37,6 +37,8 @@ fn main() { build.file(hal_driver.join("Src").join(src)); } + build.file("src/c_ll/stop0_ll.c"); + build .include(hal_driver.join("Inc")) .include(device.join("Include")) diff --git a/semestralka_2_uart/src/c_ll/stop0_ll.c b/semestralka_2_uart/src/c_ll/stop0_ll.c new file mode 100644 index 0000000..876f22a --- /dev/null +++ b/semestralka_2_uart/src/c_ll/stop0_ll.c @@ -0,0 +1,8 @@ +#include "stm32u5xx_ll_pwr.h" + +void rust_LL_PWR_SetPowerMode(unsigned int mode) +{ + // Configure STOP0 + LL_PWR_SetPowerMode(mode); + +} diff --git a/semestralka_2_uart/src/hw_uart_pc/init.rs b/semestralka_2_uart/src/hw_uart_pc/init.rs index b841c95..4571ca8 100644 --- a/semestralka_2_uart/src/hw_uart_pc/init.rs +++ b/semestralka_2_uart/src/hw_uart_pc/init.rs @@ -40,6 +40,7 @@ pub struct StopModeConfig { #[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] pub enum StopMode { + Stop0, Stop1, Stop2, Stop3, @@ -80,17 +81,26 @@ pub async fn uart_cmd_task() { b'3' => CMD_CH.send(LowPowerCmd::Standby).await, b'4' => CMD_CH.send(LowPowerCmd::Shutdown).await, b'5' => { - let _ = PIPE_HW_TX.write(b"Enter Stop mode number (1-3): ").await; + let _ = PIPE_HW_TX.write(b"Enter Stop mode number (0-3): ").await; b.fill(0); let n = PIPE_HW_RX.read(&mut b).await; if n == 0 { print_menu().await; continue; } let stop_mode = match b[0] { + b'0' => StopMode::Stop0, b'1' => StopMode::Stop1, b'2' => StopMode::Stop2, b'3' => StopMode::Stop3, _ => { print_menu().await; continue; } }; + if matches!(stop_mode, StopMode::Stop0) { + 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 + })).await; + continue; + } let _ = PIPE_HW_TX.write(b"Enter entry method (1=WFI,2=WFE,3=WFE no clear): ").await; b.fill(0); let n = PIPE_HW_RX.read(&mut b).await; diff --git a/semestralka_2_uart/src/sleep/handler.rs b/semestralka_2_uart/src/sleep/handler.rs index f955435..aaf8ce1 100644 --- a/semestralka_2_uart/src/sleep/handler.rs +++ b/semestralka_2_uart/src/sleep/handler.rs @@ -49,6 +49,7 @@ pub async fn execute_low_power( 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), diff --git a/semestralka_2_uart/src/sleep/stop.rs b/semestralka_2_uart/src/sleep/stop.rs index 8b8ce62..da89436 100644 --- a/semestralka_2_uart/src/sleep/stop.rs +++ b/semestralka_2_uart/src/sleep/stop.rs @@ -1,5 +1,9 @@ // src/sleep/stop.rs +use cortex_m::peripheral::SCB; +use cortex_m::Peripherals; +use cortex_m::asm; + /// How to enter STOPx mode (STOP0–STOP3) #[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] pub enum StopEntry { @@ -18,6 +22,24 @@ impl From for u8 { } } +pub fn enter_stop0() -> ! { + unsafe extern "C" { + fn rust_LL_PWR_SetPowerMode(mode: u32); + } + + const LL_PWR_STOP0_MODE: u32 = 0; //stm32u5xx_ll_pwr.h + + unsafe { + rust_LL_PWR_SetPowerMode(LL_PWR_STOP0_MODE); + + let mut core = Peripherals::steal(); + core.SCB.set_sleepdeep(); + asm::wfi(); + } + + cortex_m::asm::udf(); +} + pub fn enter_stop1(entry: StopEntry) -> ! { unsafe extern "C" { fn HAL_PWREx_EnterSTOP1Mode(entry: u8);