diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-01-02 22:32:42 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-01-02 22:32:42 +0000 |
| commit | a769ac188f2a9623db65acdcc27ffd26a95e3540 (patch) | |
| tree | 3da7c91b73f1b7e149e34dfa2352497ff3edfb6a | |
| parent | 73c85f5981f2348691e713b6f630a9d4465d1344 (diff) | |
| parent | 7f00d7aa0c0d9bd142209667bfdc224049cf6f90 (diff) | |
Merge pull request #2387 from chrenderle/low-power
low-power: add stop support for stm32l5
| -rw-r--r-- | embassy-stm32/Cargo.toml | 6 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/low_power.rs | 21 | ||||
| -rw-r--r-- | examples/stm32l5/Cargo.toml | 6 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/stop.rs | 61 |
5 files changed, 90 insertions, 8 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 16d1cca4c..571fb8ac0 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -24,7 +24,7 @@ flavors = [ | |||
| 24 | { regex_feature = "stm32l0.*", target = "thumbv6m-none-eabi", features = ["low-power"] }, | 24 | { regex_feature = "stm32l0.*", target = "thumbv6m-none-eabi", features = ["low-power"] }, |
| 25 | { regex_feature = "stm32l1.*", target = "thumbv7m-none-eabi" }, | 25 | { regex_feature = "stm32l1.*", target = "thumbv7m-none-eabi" }, |
| 26 | { regex_feature = "stm32l4.*", target = "thumbv7em-none-eabi" }, | 26 | { regex_feature = "stm32l4.*", target = "thumbv7em-none-eabi" }, |
| 27 | { regex_feature = "stm32l5.*", target = "thumbv8m.main-none-eabihf" }, | 27 | { regex_feature = "stm32l5.*", target = "thumbv8m.main-none-eabihf", features = ["low-power"] }, |
| 28 | { regex_feature = "stm32u5.*", target = "thumbv8m.main-none-eabihf" }, | 28 | { regex_feature = "stm32u5.*", target = "thumbv8m.main-none-eabihf" }, |
| 29 | { regex_feature = "stm32wb.*", target = "thumbv7em-none-eabi" }, | 29 | { regex_feature = "stm32wb.*", target = "thumbv7em-none-eabi" }, |
| 30 | { regex_feature = "stm32wba.*", target = "thumbv8m.main-none-eabihf" }, | 30 | { regex_feature = "stm32wba.*", target = "thumbv8m.main-none-eabihf" }, |
| @@ -57,7 +57,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 57 | rand_core = "0.6.3" | 57 | rand_core = "0.6.3" |
| 58 | sdio-host = "0.5.0" | 58 | sdio-host = "0.5.0" |
| 59 | critical-section = "1.1" | 59 | critical-section = "1.1" |
| 60 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-8caf2f0bda28baf4393899dc67ba57f058087f5a" } | 60 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-4d06d091219322837b7cef21748a180d09d7a34f" } |
| 61 | vcell = "0.1.3" | 61 | vcell = "0.1.3" |
| 62 | bxcan = "0.7.0" | 62 | bxcan = "0.7.0" |
| 63 | nb = "1.0.0" | 63 | nb = "1.0.0" |
| @@ -75,7 +75,7 @@ critical-section = { version = "1.1", features = ["std"] } | |||
| 75 | [build-dependencies] | 75 | [build-dependencies] |
| 76 | proc-macro2 = "1.0.36" | 76 | proc-macro2 = "1.0.36" |
| 77 | quote = "1.0.15" | 77 | quote = "1.0.15" |
| 78 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-8caf2f0bda28baf4393899dc67ba57f058087f5a", default-features = false, features = ["metadata"]} | 78 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-4d06d091219322837b7cef21748a180d09d7a34f", default-features = false, features = ["metadata"]} |
| 79 | 79 | ||
| 80 | 80 | ||
| 81 | [features] | 81 | [features] |
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index f10e9d50d..45a2ab63e 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -157,7 +157,7 @@ pub struct Config { | |||
| 157 | /// RCC config. | 157 | /// RCC config. |
| 158 | pub rcc: rcc::Config, | 158 | pub rcc: rcc::Config, |
| 159 | 159 | ||
| 160 | /// Enable debug during sleep. | 160 | /// Enable debug during sleep and stop. |
| 161 | /// | 161 | /// |
| 162 | /// May incrase power consumption. Defaults to true. | 162 | /// May incrase power consumption. Defaults to true. |
| 163 | #[cfg(dbgmcu)] | 163 | #[cfg(dbgmcu)] |
| @@ -209,7 +209,7 @@ pub fn init(config: Config) -> Peripherals { | |||
| 209 | 209 | ||
| 210 | #[cfg(dbgmcu)] | 210 | #[cfg(dbgmcu)] |
| 211 | crate::pac::DBGMCU.cr().modify(|cr| { | 211 | crate::pac::DBGMCU.cr().modify(|cr| { |
| 212 | #[cfg(any(dbgmcu_f0, dbgmcu_c0, dbgmcu_g0, dbgmcu_u5, dbgmcu_wba))] | 212 | #[cfg(any(dbgmcu_f0, dbgmcu_c0, dbgmcu_g0, dbgmcu_u5, dbgmcu_wba, dbgmcu_l5))] |
| 213 | { | 213 | { |
| 214 | cr.set_dbg_stop(config.enable_debug_during_sleep); | 214 | cr.set_dbg_stop(config.enable_debug_during_sleep); |
| 215 | cr.set_dbg_standby(config.enable_debug_during_sleep); | 215 | cr.set_dbg_standby(config.enable_debug_during_sleep); |
diff --git a/embassy-stm32/src/low_power.rs b/embassy-stm32/src/low_power.rs index 4fab8dae4..4c3d288fd 100644 --- a/embassy-stm32/src/low_power.rs +++ b/embassy-stm32/src/low_power.rs | |||
| @@ -37,6 +37,8 @@ | |||
| 37 | //! async fn async_main(spawner: Spawner) { | 37 | //! async fn async_main(spawner: Spawner) { |
| 38 | //! // initialize the platform... | 38 | //! // initialize the platform... |
| 39 | //! let mut config = embassy_stm32::Config::default(); | 39 | //! let mut config = embassy_stm32::Config::default(); |
| 40 | //! // when enabled the power-consumption is much higher during stop, but debugging and RTT is working | ||
| 41 | //! config.enable_debug_during_sleep = false; | ||
| 40 | //! let p = embassy_stm32::init(config); | 42 | //! let p = embassy_stm32::init(config); |
| 41 | //! | 43 | //! |
| 42 | //! // give the RTC to the executor... | 44 | //! // give the RTC to the executor... |
| @@ -107,6 +109,19 @@ pub enum StopMode { | |||
| 107 | Stop2, | 109 | Stop2, |
| 108 | } | 110 | } |
| 109 | 111 | ||
| 112 | #[cfg(stm32l5)] | ||
| 113 | use stm32_metapac::pwr::vals::Lpms; | ||
| 114 | |||
| 115 | #[cfg(stm32l5)] | ||
| 116 | impl Into<Lpms> for StopMode { | ||
| 117 | fn into(self) -> Lpms { | ||
| 118 | match self { | ||
| 119 | StopMode::Stop1 => Lpms::STOP1, | ||
| 120 | StopMode::Stop2 => Lpms::STOP2, | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 110 | /// Thread mode executor, using WFE/SEV. | 125 | /// Thread mode executor, using WFE/SEV. |
| 111 | /// | 126 | /// |
| 112 | /// This is the simplest and most common kind of executor. It runs on | 127 | /// This is the simplest and most common kind of executor. It runs on |
| @@ -164,8 +179,10 @@ impl Executor { | |||
| 164 | } | 179 | } |
| 165 | } | 180 | } |
| 166 | 181 | ||
| 167 | fn configure_stop(&mut self, _stop_mode: StopMode) { | 182 | #[allow(unused_variables)] |
| 168 | // TODO: configure chip-specific settings for stop | 183 | fn configure_stop(&mut self, stop_mode: StopMode) { |
| 184 | #[cfg(stm32l5)] | ||
| 185 | crate::pac::PWR.cr1().modify(|m| m.set_lpms(stop_mode.into())); | ||
| 169 | } | 186 | } |
| 170 | 187 | ||
| 171 | fn configure_pwr(&mut self) { | 188 | fn configure_pwr(&mut self) { |
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..32a736de8 --- /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::{AnyPin, Level, Output, Speed}; | ||
| 7 | use embassy_stm32::low_power::Executor; | ||
| 8 | use embassy_stm32::rcc::LsConfig; | ||
| 9 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 10 | use embassy_stm32::Config; | ||
| 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 | } | ||
