diff --git a/semestralka_2_uart/src/hw_uart_pc/init.rs b/semestralka_2_uart/src/hw_uart_pc/init.rs index fdd77f6..986451b 100644 --- a/semestralka_2_uart/src/hw_uart_pc/init.rs +++ b/semestralka_2_uart/src/hw_uart_pc/init.rs @@ -30,6 +30,7 @@ pub enum LowPowerCmd { Standby, // 3 Shutdown, // 4 StopMode(StopModeConfig), + Sleep, } #[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] @@ -62,6 +63,7 @@ pub async fn uart_cmd_task() { [3] Standby minimal\r\n\ [4] Shutdown\r\n\ [5] Stop mode (0-3)\r\n\ + [6] Sleep mode (WFI or WFE)\r\n\ " ).await; embassy_time::Timer::after(embassy_time::Duration::from_millis(8)).await; @@ -112,7 +114,8 @@ pub async fn uart_cmd_task() { _ => { print_menu().await; continue; } }; CMD_CH.send(LowPowerCmd::StopMode(StopModeConfig { mode: stop_mode, entry })).await; - } + }, + b'6' => CMD_CH.send(LowPowerCmd::Sleep).await, _ => { print_menu().await; continue; } } } diff --git a/semestralka_2_uart/src/sleep/handler.rs b/semestralka_2_uart/src/sleep/handler.rs index 9ca4d2a..bc30236 100644 --- a/semestralka_2_uart/src/sleep/handler.rs +++ b/semestralka_2_uart/src/sleep/handler.rs @@ -57,5 +57,11 @@ pub async fn execute_low_power( } } } + LowPowerCmd::Sleep => { + info!("Entering Sleep mode (WFI)..."); + use crate::sleep::sleep::{enter_sleep_mode, SleepEntry}; + enter_sleep_mode(SleepEntry::Wfi); + info!("Woke up from Sleep mode."); + } } } diff --git a/semestralka_2_uart/src/sleep/mod.rs b/semestralka_2_uart/src/sleep/mod.rs index a2d253e..0ae2f7f 100644 --- a/semestralka_2_uart/src/sleep/mod.rs +++ b/semestralka_2_uart/src/sleep/mod.rs @@ -4,5 +4,6 @@ pub mod standby; pub mod shutdown; pub mod stop; pub mod handler; +pub mod sleep; pub use stop::StopEntry; diff --git a/semestralka_2_uart/src/sleep/sleep.rs b/semestralka_2_uart/src/sleep/sleep.rs new file mode 100644 index 0000000..8d132c3 --- /dev/null +++ b/semestralka_2_uart/src/sleep/sleep.rs @@ -0,0 +1,19 @@ +// src/sleep/sleep.rs + +use cortex_m::Peripherals; + +#[derive(Clone, Copy, Debug, defmt::Format)] +pub enum SleepEntry { + Wfi, + Wfe, +} + +pub fn enter_sleep_mode(entry: SleepEntry) { + let mut core = unsafe { Peripherals::steal() }; + core.SCB.clear_sleepdeep(); + + match entry { + SleepEntry::Wfi => cortex_m::asm::wfi(), + SleepEntry::Wfe => cortex_m::asm::wfe(), + } +}