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/stm32l5/src/bin/stop.rs | |
| parent | f1c077ed2e420b1b4f8c1835fe05a1279a742267 (diff) | |
stm32l5: add low-power stop example
Diffstat (limited to 'examples/stm32l5/src/bin/stop.rs')
| -rw-r--r-- | examples/stm32l5/src/bin/stop.rs | 61 |
1 files changed, 61 insertions, 0 deletions
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 | } | ||
