3 Commits

Author SHA1 Message Date
Priec
bbddc7cf9c detection of the stop0-2 2025-12-14 12:01:19 +01:00
Priec
49cc8dcc71 stop0 working 2025-12-14 11:56:09 +01:00
Priec
58561ec392 dumb changes didnt do anything 2025-12-13 22:31:49 +01:00
7 changed files with 60 additions and 6 deletions

View File

@@ -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"))

View File

@@ -4,7 +4,7 @@
use defmt::*;
use embassy_stm32::pac;
use embassy_executor::Spawner;
use embassy_executor::{Spawner, task};
use embassy_stm32::Config;
use embassy_stm32::gpio::{Output, Level, Speed};
use embassy_time::{Duration, Timer};
@@ -28,6 +28,9 @@ async fn main(spawner: Spawner) {
led.set_high();
info!("LED ON (MCU awake)");
// LED BLINK
spawner.spawn(led_blink(led).unwrap());
// INIT HW UART
init_hw_uart_to_pc(p.USART1, p.PA10, p.PA9, &spawner);
@@ -67,3 +70,11 @@ async fn main(spawner: Spawner) {
execute_low_power(cmd, &mut iwdg).await;
}
}
#[task]
async fn led_blink(mut led: Output<'static>) {
loop {
led.toggle();
Timer::after(Duration::from_millis(300)).await;
}
}

View File

@@ -0,0 +1,8 @@
#include "stm32u5xx_ll_pwr.h"
void rust_LL_PWR_SetPowerMode(unsigned int mode)
{
// Configure STOP0
LL_PWR_SetPowerMode(mode);
}

View File

@@ -33,7 +33,7 @@ pub async fn uart_task(
}
// Outgoing data waiting in TX pipe
Either::Second(n) => {
unwrap!(uart.write(&tx_buf[..n]).await);
unwrap!(uart.write_all(&tx_buf[..n]).await);
}
}
yield_now().await;

View File

@@ -40,6 +40,7 @@ pub struct StopModeConfig {
#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
pub enum StopMode {
Stop0,
Stop1,
Stop2,
Stop3,
@@ -51,11 +52,10 @@ pub static CMD_CH: Channel<CriticalSectionRawMutex, LowPowerCmd, 1> = Channel::n
pub async fn uart_cmd_task() {
async fn print_menu() {
while PIPE_HW_TX.len() > 0 {
embassy_time::Timer::after(embassy_time::Duration::from_millis(2)).await;
embassy_time::Timer::after(embassy_time::Duration::from_millis(8)).await;
}
let _ = PIPE_HW_TX.write(b"\x1B[2J\x1B[H").await;
let _ = PIPE_HW_TX.write(
b"\r\n\
b"\x1B[2J\x1B[H\r\n\
Modes:\r\n\
[1] Standby + 8 KB SRAM2 retention\r\n\
[2] Standby + full SRAM2 retention\r\n\
@@ -64,6 +64,7 @@ pub async fn uart_cmd_task() {
[5] Stop mode (0-3)\r\n\
"
).await;
embassy_time::Timer::after(embassy_time::Duration::from_millis(8)).await;
}
print_menu().await;
@@ -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;

View File

@@ -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),

View File

@@ -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 (STOP0STOP3)
#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)]
pub enum StopEntry {
@@ -18,6 +22,24 @@ impl From<StopEntry> 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);