aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authoreZio Pan <[email protected]>2024-04-27 21:37:58 +0800
committereZio Pan <[email protected]>2024-04-28 00:33:02 +0800
commitd9e59e8e42ea009e365bb893b91d81fe47fe927d (patch)
treec6aa9f34ef6cecc3e9bad121aea4f04d7258bdb8 /examples
parent34074e6eb0741e084653b3ef71163393741f558b (diff)
low power for h5
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h5/Cargo.toml2
-rw-r--r--examples/stm32h5/src/bin/stop.rs71
2 files changed, 72 insertions, 1 deletions
diff --git a/examples/stm32h5/Cargo.toml b/examples/stm32h5/Cargo.toml
index 4527f4bcb..82760db64 100644
--- a/examples/stm32h5/Cargo.toml
+++ b/examples/stm32h5/Cargo.toml
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32h563zi to your chip name, if necessary. 8# Change stm32h563zi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h563zi", "memory-x", "time-driver-any", "exti", "unstable-pac"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h563zi", "memory-x", "time-driver-any", "exti", "unstable-pac", "low-power"] }
10embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
diff --git a/examples/stm32h5/src/bin/stop.rs b/examples/stm32h5/src/bin/stop.rs
new file mode 100644
index 000000000..0d14c0668
--- /dev/null
+++ b/examples/stm32h5/src/bin/stop.rs
@@ -0,0 +1,71 @@
1// Notice:
2// the MCU might need an extra reset to make the code actually running
3
4#![no_std]
5#![no_main]
6
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_stm32::gpio::{AnyPin, Level, Output, Speed};
10use embassy_stm32::low_power::Executor;
11use embassy_stm32::rcc::{HSIPrescaler, LsConfig};
12use embassy_stm32::rtc::{Rtc, RtcConfig};
13use embassy_stm32::Config;
14use embassy_time::Timer;
15use static_cell::StaticCell;
16use {defmt_rtt as _, panic_probe as _};
17
18#[cortex_m_rt::entry]
19fn main() -> ! {
20 Executor::take().run(|spawner| {
21 unwrap!(spawner.spawn(async_main(spawner)));
22 })
23}
24
25#[embassy_executor::task]
26async fn async_main(spawner: Spawner) {
27 defmt::info!("Program Start");
28
29 let mut config = Config::default();
30
31 // System Clock seems need to be equal or lower than 16 MHz
32 config.rcc.hsi = Some(HSIPrescaler::DIV4);
33
34 config.rcc.ls = LsConfig::default_lsi();
35 // when enabled the power-consumption is much higher during stop, but debugging and RTT is working
36 // if you wan't to measure the power-consumption, or for production: uncomment this line
37 // config.enable_debug_during_sleep = false;
38 let p = embassy_stm32::init(config);
39
40 // give the RTC to the executor...
41 let rtc = Rtc::new(p.RTC, RtcConfig::default());
42 static RTC: StaticCell<Rtc> = StaticCell::new();
43 let rtc = RTC.init(rtc);
44 embassy_stm32::low_power::stop_with_rtc(rtc);
45
46 unwrap!(spawner.spawn(blinky(p.PB4.into())));
47 unwrap!(spawner.spawn(timeout()));
48}
49
50#[embassy_executor::task]
51async fn blinky(led: AnyPin) {
52 let mut led = Output::new(led, Level::Low, Speed::Low);
53 loop {
54 info!("high");
55 led.set_high();
56 Timer::after_millis(300).await;
57
58 info!("low");
59 led.set_low();
60 Timer::after_millis(300).await;
61 }
62}
63
64// when enable_debug_during_sleep is false, it is more difficult to reprogram the MCU
65// therefore we block the MCU after 30s to be able to reprogram it easily
66#[embassy_executor::task]
67async fn timeout() -> ! {
68 Timer::after_secs(30).await;
69 #[allow(clippy::empty_loop)]
70 loop {}
71}