diff options
| author | Christian Enderle <[email protected]> | 2024-01-02 23:05:58 +0100 |
|---|---|---|
| committer | Christian Enderle <[email protected]> | 2024-01-02 23:05:58 +0100 |
| commit | 38303009907fbf9ebd4919e85166c8bebb6e0ba4 (patch) | |
| tree | 4401fba4e2d40c4b82e029e09b51647237c4618f /examples | |
| parent | f1c077ed2e420b1b4f8c1835fe05a1279a742267 (diff) | |
stm32l5: add low-power stop example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l5/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/stop.rs | 61 |
2 files changed, 66 insertions, 1 deletions
diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml index 329e7b98d..c50314587 100644 --- a/examples/stm32l5/Cargo.toml +++ b/examples/stm32l5/Cargo.toml | |||
| @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | # Change stm32l552ze to your chip name, if necessary. | 8 | # Change stm32l552ze to your chip name, if necessary. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "memory-x"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "memory-x", "low-power"] } |
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.4.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.4.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| @@ -30,3 +30,7 @@ static_cell = "2" | |||
| 30 | 30 | ||
| 31 | [profile.release] | 31 | [profile.release] |
| 32 | debug = 2 | 32 | debug = 2 |
| 33 | |||
| 34 | [[bin]] | ||
| 35 | name = "stop" | ||
| 36 | default-features = ["embassy-stm32/low-power"] | ||
diff --git a/examples/stm32l5/src/bin/stop.rs b/examples/stm32l5/src/bin/stop.rs new file mode 100644 index 000000000..21b30a266 --- /dev/null +++ b/examples/stm32l5/src/bin/stop.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::gpio::{Level, Output, Speed, AnyPin}; | ||
| 7 | use embassy_stm32::low_power::Executor; | ||
| 8 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 9 | use embassy_stm32::Config; | ||
| 10 | use embassy_stm32::rcc::LsConfig; | ||
| 11 | use embassy_time::Timer; | ||
| 12 | use static_cell::StaticCell; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | #[cortex_m_rt::entry] | ||
| 16 | fn main() -> ! { | ||
| 17 | Executor::take().run(|spawner| { | ||
| 18 | unwrap!(spawner.spawn(async_main(spawner))); | ||
| 19 | }) | ||
| 20 | } | ||
| 21 | |||
| 22 | #[embassy_executor::task] | ||
| 23 | async fn async_main(spawner: Spawner) { | ||
| 24 | let mut config = Config::default(); | ||
| 25 | config.rcc.ls = LsConfig::default_lsi(); | ||
| 26 | // when enabled the power-consumption is much higher during stop, but debugging and RTT is working | ||
| 27 | // if you wan't to measure the power-consumption, or for production: uncomment this line | ||
| 28 | // config.enable_debug_during_sleep = false; | ||
| 29 | let p = embassy_stm32::init(config); | ||
| 30 | |||
| 31 | // give the RTC to the executor... | ||
| 32 | let rtc = Rtc::new(p.RTC, RtcConfig::default()); | ||
| 33 | static RTC: StaticCell<Rtc> = StaticCell::new(); | ||
| 34 | let rtc = RTC.init(rtc); | ||
| 35 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 36 | |||
| 37 | unwrap!(spawner.spawn(blinky(p.PC7.into()))); | ||
| 38 | unwrap!(spawner.spawn(timeout())); | ||
| 39 | } | ||
| 40 | |||
| 41 | #[embassy_executor::task] | ||
| 42 | async fn blinky(led: AnyPin) -> ! { | ||
| 43 | let mut led = Output::new(led, Level::Low, Speed::Low); | ||
| 44 | loop { | ||
| 45 | info!("high"); | ||
| 46 | led.set_high(); | ||
| 47 | Timer::after_millis(300).await; | ||
| 48 | |||
| 49 | info!("low"); | ||
| 50 | led.set_low(); | ||
| 51 | Timer::after_millis(300).await; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | // when enable_debug_during_sleep is false, it is more difficult to reprogram the MCU | ||
| 56 | // therefore we block the MCU after 30s to be able to reprogram it easily | ||
| 57 | #[embassy_executor::task] | ||
| 58 | async fn timeout() -> ! { | ||
| 59 | Timer::after_secs(30).await; | ||
| 60 | loop {} | ||
| 61 | } | ||
