2 Commits

Author SHA1 Message Date
Priec
20dfdbc335 wake up pin working for the wake up 2025-12-14 14:33:01 +01:00
Priec
f4ca3071f0 exti gpio wake up 2025-12-14 12:50:21 +01:00
8 changed files with 44 additions and 27 deletions

View File

@@ -16,7 +16,7 @@ embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch =
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["tick-hz-32_768"] }
embassy-hal-internal = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-usb = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["unstable-pac", "stm32u575zi", "time-driver-tim2", "memory-x", "defmt"] }
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["unstable-pac", "stm32u575zi", "time-driver-tim2", "memory-x", "defmt", "exti"] }
embedded-hal = "1.0.0"
embedded-graphics = "0.8.1"

View File

@@ -11,6 +11,7 @@ use embassy_time::{Duration, Timer};
use dma_gpio::config::BAUD;
use dma_gpio::wakeup::iwdg::{clear_wakeup_flags, init_watchdog_reset};
use dma_gpio::wakeup::gpio::gpio_wakeup;
use dma_gpio::sleep::handler::execute_low_power;
use dma_gpio::hw_uart_pc::init::{init_hw_uart_to_pc, LowPowerCmd, CMD_CH};
use {defmt_rtt as _, panic_probe as _};
@@ -65,6 +66,7 @@ async fn main(spawner: Spawner) {
info!("ready for uart");
let mut iwdg = Some(p.IWDG);
spawner.spawn(gpio_wakeup(p.PA0, p.EXTI0).unwrap()); // exti wake up
loop {
let cmd = CMD_CH.receive().await;
execute_low_power(cmd, &mut iwdg).await;

View File

@@ -97,7 +97,7 @@ pub async fn uart_cmd_task() {
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
// pos**temu halu co si nevie urobit konzistentnost
})).await;
continue;
}

View File

@@ -14,7 +14,7 @@ use defmt::info;
pub async fn execute_low_power(
cmd: LowPowerCmd,
iwdg: &mut Option<Peri<'static, peripherals::IWDG>>
) -> ! {
) {
Timer::after(Duration::from_millis(10)).await;
match cmd {
@@ -45,14 +45,16 @@ pub async fn execute_low_power(
}
LowPowerCmd::StopMode(StopModeConfig { mode, entry }) => {
info!("Entering {:?} with {:?}", mode, entry);
if let Some(wdg) = iwdg.take() {
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),
StopMode::Stop3 => {
if let Some(wdg) = iwdg.take() {
init_watchdog_reset(wdg).await;
}
enter_stop3(entry);
}
}
}
}

View File

@@ -22,7 +22,7 @@ impl From<StopEntry> for u8 {
}
}
pub fn enter_stop0() -> ! {
pub fn enter_stop0() {
unsafe extern "C" {
fn rust_LL_PWR_SetPowerMode(mode: u32);
}
@@ -35,12 +35,11 @@ pub fn enter_stop0() -> ! {
let mut core = Peripherals::steal();
core.SCB.set_sleepdeep();
asm::wfi();
core.SCB.clear_sleepdeep();
}
cortex_m::asm::udf();
}
pub fn enter_stop1(entry: StopEntry) -> ! {
pub fn enter_stop1(entry: StopEntry) {
unsafe extern "C" {
fn HAL_PWREx_EnterSTOP1Mode(entry: u8);
}
@@ -48,11 +47,9 @@ pub fn enter_stop1(entry: StopEntry) -> ! {
unsafe {
HAL_PWREx_EnterSTOP1Mode(entry.into());
}
cortex_m::asm::udf()
}
pub fn enter_stop2(entry: StopEntry) -> ! {
pub fn enter_stop2(entry: StopEntry) {
unsafe extern "C" {
fn HAL_PWREx_EnterSTOP2Mode(entry: u8);
}
@@ -60,11 +57,9 @@ pub fn enter_stop2(entry: StopEntry) -> ! {
unsafe {
HAL_PWREx_EnterSTOP2Mode(entry.into());
}
cortex_m::asm::udf()
}
pub fn enter_stop3(entry: StopEntry) -> ! {
pub fn enter_stop3(entry: StopEntry) {
unsafe extern "C" {
fn HAL_PWREx_EnterSTOP3Mode(entry: u8);
}
@@ -72,6 +67,4 @@ pub fn enter_stop3(entry: StopEntry) -> ! {
unsafe {
HAL_PWREx_EnterSTOP3Mode(entry.into());
}
cortex_m::asm::udf()
}

View File

@@ -0,0 +1,25 @@
// src/wakeup/gpio.rs
use defmt::info;
use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::Pull;
use embassy_stm32::{peripherals, interrupt};
use embassy_time::{Duration, Timer};
use embassy_hal_internal::Peri;
/// GPIO pin as an EXTI wakeup source.
#[embassy_executor::task]
pub async fn gpio_wakeup(
pin: Peri<'static, peripherals::PA0>,
ch: Peri<'static, peripherals::EXTI0>
) {
info!("Configuring EXTI wake input on PA0 (rising edge)");
let mut btn = ExtiInput::new(pin, ch, Pull::Up);
loop {
info!("Waiting for rising edge on PA0");
btn.wait_for_falling_edge().await;
info!("GPIO wakeup: event detected!");
Timer::after(Duration::from_millis(50)).await;
}
}

View File

@@ -11,10 +11,7 @@ use crate::sleep::standby;
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.
/// Call early in startup
///
/// # Registers
/// - `RCC_CSR` — Reset and Clock Control / Status
@@ -38,10 +35,7 @@ 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
/// Timeout value is configured in `WATCHDOG_TIMEOUT_US` from config.rs
pub async fn init_watchdog_reset(iwdg: Peri<'static, peripherals::IWDG>) {
info!("Initializing watchdog after watchdog wake...");
let mut watchdog = IndependentWatchdog::new(iwdg, WATCHDOG_TIMEOUT_US);

View File

@@ -1,3 +1,4 @@
// src/wakeup/mod.rs
pub mod iwdg;
pub mod gpio;