diff --git a/semestralka_2_uart/src/bin/main.rs b/semestralka_2_uart/src/bin/main.rs index 35b77bc..d828586 100644 --- a/semestralka_2_uart/src/bin/main.rs +++ b/semestralka_2_uart/src/bin/main.rs @@ -13,7 +13,8 @@ use dma_gpio::config::BAUD; use dma_gpio::wakeup::iwdg::clear_wakeup_flags; use dma_gpio::wakeup::gpio::gpio_wakeup; use dma_gpio::logic::handler::execute_low_power; -use dma_gpio::hw_uart_pc::init::{init_hw_uart_to_pc, CMD_CH}; +use dma_gpio::hw_uart_pc::init::init_hw_uart_to_pc; +use dma_gpio::logic::uart_cmd::CMD_CH; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] @@ -30,7 +31,7 @@ async fn main(spawner: Spawner) { info!("LED ON (MCU awake)"); // LED BLINK - spawner.spawn(led_blink(led).unwrap()); + // spawner.spawn(led_blink(led).unwrap()); // INIT HW UART init_hw_uart_to_pc(p.USART1, p.PA10, p.PA9, &spawner); diff --git a/semestralka_2_uart/src/hw_uart_pc/init.rs b/semestralka_2_uart/src/hw_uart_pc/init.rs index f148f21..54c4938 100644 --- a/semestralka_2_uart/src/hw_uart_pc/init.rs +++ b/semestralka_2_uart/src/hw_uart_pc/init.rs @@ -1,12 +1,8 @@ // src/hw_uart_pc/init.rs -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -use embassy_sync::channel::Channel; - -// UART HW init use crate::config::{BAUD, PIPE_HW_TX, PIPE_HW_RX}; use crate::hw_uart_pc::driver::uart_task; -use crate::sleep::StopEntry; +use crate::logic::uart_cmd::uart_cmd_task; use static_cell::StaticCell; use embassy_stm32::usart::BufferedInterruptHandler; use embassy_stm32::usart::{BufferedUart, Config as UsartConfig}; @@ -21,104 +17,6 @@ bind_interrupts!(struct Irqs { USART1 => BufferedInterruptHandler; }); -#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] -pub enum LowPowerCmd { - Standby8k, // 1 - StandbyFull, // 2 - Standby, // 3 - Shutdown, // 4 - StopMode(StopModeConfig), - Sleep, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] -pub struct StopModeConfig { - pub mode: StopMode, - pub entry: StopEntry, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] -pub enum StopMode { - Stop0, - Stop1, - Stop2, - Stop3, -} - -pub static CMD_CH: Channel = Channel::new(); - -#[embassy_executor::task] -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(8)).await; - } - let _ = PIPE_HW_TX.write( - 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\ - [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; - } - - print_menu().await; - - let mut b = [0u8; 2]; - loop { - b.fill(0); - let n = PIPE_HW_RX.read(&mut b).await; - if n == 0 { continue; } - - match b[0] { - b'1' => CMD_CH.send(LowPowerCmd::Standby8k).await, - b'2' => CMD_CH.send(LowPowerCmd::StandbyFull).await, - 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 (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 - // pos**temu 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; - if n == 0 { print_menu().await; continue; } - let entry = match b[0] { - b'1' => StopEntry::Wfi, - b'2' => StopEntry::Wfe, - b'3' => StopEntry::WfeNoEventClear, - _ => { 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; } - } - } -} - pub fn init_hw_uart_to_pc( usart1: Peri<'static, USART1>, pa10: Peri<'static, PA10>, diff --git a/semestralka_2_uart/src/hw_uart_pc/mod.rs b/semestralka_2_uart/src/hw_uart_pc/mod.rs index 348a430..89e3a09 100644 --- a/semestralka_2_uart/src/hw_uart_pc/mod.rs +++ b/semestralka_2_uart/src/hw_uart_pc/mod.rs @@ -1,4 +1,5 @@ // src/hw_uart_pc/mod.rs + pub mod driver; pub mod usart1; pub mod safety; diff --git a/semestralka_2_uart/src/logic/handler.rs b/semestralka_2_uart/src/logic/handler.rs index 6d193ea..33e89bf 100644 --- a/semestralka_2_uart/src/logic/handler.rs +++ b/semestralka_2_uart/src/logic/handler.rs @@ -8,7 +8,7 @@ use crate::sleep::stop::{enter_stop0, enter_stop1, enter_stop2, enter_stop3}; use crate::sleep::sleep::{enter_sleep_mode, SleepEntry}; use crate::sleep::{standby, shutdown}; use crate::wakeup::iwdg::init_watchdog_reset; -use crate::hw_uart_pc::init::{LowPowerCmd, StopMode, StopModeConfig}; +use crate::logic::uart_cmd::{LowPowerCmd, StopMode, StopModeConfig}; use defmt::info; diff --git a/semestralka_2_uart/src/logic/mod.rs b/semestralka_2_uart/src/logic/mod.rs index 8a72c6c..2dbfe46 100644 --- a/semestralka_2_uart/src/logic/mod.rs +++ b/semestralka_2_uart/src/logic/mod.rs @@ -1,3 +1,4 @@ // src/logic/mod.rs pub mod handler; +pub mod uart_cmd; diff --git a/semestralka_2_uart/src/logic/uart_cmd.rs b/semestralka_2_uart/src/logic/uart_cmd.rs new file mode 100644 index 0000000..678c85e --- /dev/null +++ b/semestralka_2_uart/src/logic/uart_cmd.rs @@ -0,0 +1,106 @@ +// src/logic/uart_cmd.rs + +use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; +use embassy_sync::channel::Channel; + +// UART HW init +use crate::config::{PIPE_HW_TX, PIPE_HW_RX}; +use crate::sleep::StopEntry; + +pub static CMD_CH: Channel = Channel::new(); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] +pub enum LowPowerCmd { + Standby8k, // 1 + StandbyFull, // 2 + Standby, // 3 + Shutdown, // 4 + StopMode(StopModeConfig), + Sleep, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] +pub struct StopModeConfig { + pub mode: StopMode, + pub entry: StopEntry, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] +pub enum StopMode { + Stop0, + Stop1, + Stop2, + Stop3, +} + +#[embassy_executor::task] +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(8)).await; + } + let _ = PIPE_HW_TX.write( + 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\ + [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; + } + + print_menu().await; + + let mut b = [0u8; 2]; + loop { + b.fill(0); + let n = PIPE_HW_RX.read(&mut b).await; + if n == 0 { continue; } + + match b[0] { + b'1' => CMD_CH.send(LowPowerCmd::Standby8k).await, + b'2' => CMD_CH.send(LowPowerCmd::StandbyFull).await, + 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 (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 + // pos**temu 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; + if n == 0 { print_menu().await; continue; } + let entry = match b[0] { + b'1' => StopEntry::Wfi, + b'2' => StopEntry::Wfe, + b'3' => StopEntry::WfeNoEventClear, + _ => { 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; } + } + } +}