From 46480285783390d90f8d99e530a1da28a292dc3c Mon Sep 17 00:00:00 2001 From: liebman Date: Wed, 29 Oct 2025 14:47:06 -0700 Subject: examples: add low-power examples for `stm32wlex` --- examples/stm32wle5/.cargo/config.toml | 9 +++ examples/stm32wle5/Cargo.toml | 38 ++++++++++ examples/stm32wle5/README.md | 45 ++++++++++++ examples/stm32wle5/build.rs | 5 ++ examples/stm32wle5/src/bin/adc.rs | 105 ++++++++++++++++++++++++++++ examples/stm32wle5/src/bin/blinky.rs | 95 +++++++++++++++++++++++++ examples/stm32wle5/src/bin/button_exti.rs | 96 +++++++++++++++++++++++++ examples/stm32wle5/stm32wle5.code-workspace | 13 ++++ 8 files changed, 406 insertions(+) create mode 100644 examples/stm32wle5/.cargo/config.toml create mode 100644 examples/stm32wle5/Cargo.toml create mode 100644 examples/stm32wle5/README.md create mode 100644 examples/stm32wle5/build.rs create mode 100644 examples/stm32wle5/src/bin/adc.rs create mode 100644 examples/stm32wle5/src/bin/blinky.rs create mode 100644 examples/stm32wle5/src/bin/button_exti.rs create mode 100644 examples/stm32wle5/stm32wle5.code-workspace (limited to 'examples') diff --git a/examples/stm32wle5/.cargo/config.toml b/examples/stm32wle5/.cargo/config.toml new file mode 100644 index 000000000..0178d377c --- /dev/null +++ b/examples/stm32wle5/.cargo/config.toml @@ -0,0 +1,9 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace your chip as listed in `probe-rs chip list` +runner = "probe-rs run --chip STM32WLE5JCIx --connect-under-reset" + +[build] +target = "thumbv7em-none-eabi" + +[env] +DEFMT_LOG = "info" diff --git a/examples/stm32wle5/Cargo.toml b/examples/stm32wle5/Cargo.toml new file mode 100644 index 000000000..f2fc4dd3d --- /dev/null +++ b/examples/stm32wle5/Cargo.toml @@ -0,0 +1,38 @@ +[package] +edition = "2024" +name = "embassy-stm32wl-examples" +version = "0.1.0" +license = "MIT OR Apache-2.0" +publish = false + +[dependencies] +# Change stm32wl55jc-cm4 to your chip name, if necessary. +embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = ["defmt", "stm32wle5jc", "time-driver-any", "memory-x", "unstable-pac", "exti", "low-power"] } +embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } +embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt"] } +embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-1_000"] } +embassy-embedded-hal = { version = "0.5.0", path = "../../embassy-embedded-hal" } + +defmt = "1.0.1" +defmt-rtt = { version = "1.1.0", optional = true } +defmt-serial = { version = "0.10.0", optional = true } + +cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } +cortex-m-rt = "0.7.0" +embedded-hal = "1.0.0" +embedded-storage = "0.3.1" +panic-probe = { version = "1.0.0", features = ["print-defmt"] } +static_cell = { version = "2.1.1", default-features = false } + +[profile.release] +debug = 2 + +[package.metadata.embassy] +build = [ + { target = "thumbv7em-none-eabi", artifact-dir = "out/examples/stm32wl" } +] + +[features] +default = ["defmt-serial"] +defmt-rtt = ["dep:defmt-rtt"] +defmt-serial = ["dep:defmt-serial"] diff --git a/examples/stm32wle5/README.md b/examples/stm32wle5/README.md new file mode 100644 index 000000000..7435ed1be --- /dev/null +++ b/examples/stm32wle5/README.md @@ -0,0 +1,45 @@ +# Low Power Examples for STM32WLEx family + +Examples in this repo should work with [LoRa-E5 Dev Board](https://www.st.com/en/partner-products-and-services/lora-e5-development-kit.html) board. + +## Prerequsits + +- Connect a usb serial adapter to LPUart1 (this is where ALL logging will go) +- Optional: Connect an amp meter that ran measure down to 0.1uA to the power test pins + +## Example Notes + +All examples will set all pins to analog mode before configuring pins for the example, if any. This saves about 500uA!!!! + +- the `adc` example will sleep in STOP1 betwen samples and the chip will only draw about 13uA while sleeping +- the `blinky` example will sleep in STOP2 and the chip will only draw 1uA or less while sleeping +- the `button_exti` example will sleep in STOP2 and the chip will only draw 1uA or less while sleeping + +Run individual examples with +``` +cargo flash --chip STM32WLE5JCIx --connect-under-reset --bin +``` +for example +``` +cargo flash --chip STM32WLE5JCIx --connect-under-reset --bin blinky +``` + +You can also run them with with `run`. However in this case expect probe-rs to be disconnected as soon as flashing is done as all IO pins are set to analog input! +``` +cargo run --bin blinky +``` + +## Checklist before running examples +You might need to adjust `.cargo/config.toml`, `Cargo.toml` and possibly update pin numbers or peripherals to match the specific MCU or board you are using. + +* [ ] Update .cargo/config.toml with the correct probe-rs command to use your specific MCU. For example for L432KCU6 it should be `probe-rs run --chip STM32L432KCUx`. (use `probe-rs chip list` to find your chip) +* [ ] Update Cargo.toml to have the correct `embassy-stm32` feature. For example for L432KCU6 it should be `stm32l432kc`. Look in the `Cargo.toml` file of the `embassy-stm32` project to find the correct feature flag for your chip. +* [ ] If your board has a special clock or power configuration, make sure that it is set up appropriately. +* [ ] If your board has different pin mapping, update any pin numbers or peripherals in the given example code to match your schematic + +If you are unsure, please drop by the Embassy Matrix chat for support, and let us know: + +* Which example you are trying to run +* Which chip and board you are using + +Embassy Chat: https://matrix.to/#/#embassy-rs:matrix.org diff --git a/examples/stm32wle5/build.rs b/examples/stm32wle5/build.rs new file mode 100644 index 000000000..8cd32d7ed --- /dev/null +++ b/examples/stm32wle5/build.rs @@ -0,0 +1,5 @@ +fn main() { + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); +} diff --git a/examples/stm32wle5/src/bin/adc.rs b/examples/stm32wle5/src/bin/adc.rs new file mode 100644 index 000000000..fabdb9cb3 --- /dev/null +++ b/examples/stm32wle5/src/bin/adc.rs @@ -0,0 +1,105 @@ +#![no_std] +#![no_main] + +use defmt::*; +#[cfg(feature = "defmt-rtt")] +use defmt_rtt as _; +use embassy_executor::Spawner; +use embassy_stm32::adc::{Adc, SampleTime}; +use embassy_stm32::low_power::Executor; +use embassy_stm32::rtc::{Rtc, RtcConfig}; +use embassy_time::Timer; +use panic_probe as _; +use static_cell::StaticCell; + +#[cortex_m_rt::entry] +fn main() -> ! { + info!("main: Starting!"); + Executor::take().run(|spawner| { + spawner.spawn(unwrap!(async_main(spawner))); + }); +} + +#[embassy_executor::task] +async fn async_main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + // enable HSI clock + config.rcc.hsi = true; + // enable LSI clock for RTC + config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); + config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); + config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; + // enable ADC with HSI clock + config.rcc.mux.adcsel = embassy_stm32::pac::rcc::vals::Adcsel::HSI; + #[cfg(feature = "defmt-serial")] + { + // disable debug during sleep to reduce power consumption since we are + // using defmt-serial on LPUART1. + config.enable_debug_during_sleep = false; + // if we are using defmt-serial on LPUART1, we need to use HSI for the clock + // so that its registers are preserved during STOP modes. + config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; + } + // Initialize STM32WL peripherals (use default config like wio-e5-async example) + let p = embassy_stm32::init(config); + + // start with all GPIOs as analog to reduce power consumption + for r in [ + embassy_stm32::pac::GPIOA, + embassy_stm32::pac::GPIOB, + embassy_stm32::pac::GPIOC, + embassy_stm32::pac::GPIOH, + ] { + r.moder().modify(|w| { + for i in 0..16 { + // don't reset these if probe-rs should stay connected! + #[cfg(feature = "defmt-rtt")] + if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { + continue; + } + w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); + } + }); + } + #[cfg(feature = "defmt-serial")] + { + use embassy_stm32::mode::Blocking; + use embassy_stm32::usart::Uart; + let mut config = embassy_stm32::usart::Config::default(); + config.baudrate = 115200; + config.assume_noise_free = true; + config.detect_previous_overrun = true; + let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); + static SERIAL: StaticCell> = StaticCell::new(); + defmt_serial::defmt_serial(SERIAL.init(uart)); + } + + // give the RTC to the low_power executor... + let rtc_config = RtcConfig::default(); + let rtc = Rtc::new(p.RTC, rtc_config); + static RTC: StaticCell = StaticCell::new(); + let rtc = RTC.init(rtc); + embassy_stm32::low_power::stop_with_rtc(rtc); + + info!("Hello World!"); + + let mut adc = Adc::new(p.ADC1); + adc.set_sample_time(SampleTime::CYCLES79_5); + let mut pin = p.PA10; + + let mut vrefint = adc.enable_vrefint(); + let vrefint_sample = adc.blocking_read(&mut vrefint); + let convert_to_millivolts = |sample| { + // From https://www.st.com/resource/en/datasheet/stm32g031g8.pdf + // 6.3.3 Embedded internal reference voltage + const VREFINT_MV: u32 = 1212; // mV + + (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16 + }; + + loop { + let v = adc.blocking_read(&mut pin); + info!("--> {} - {} mV", v, convert_to_millivolts(v)); + Timer::after_secs(1).await; + } +} diff --git a/examples/stm32wle5/src/bin/blinky.rs b/examples/stm32wle5/src/bin/blinky.rs new file mode 100644 index 000000000..f5ba97025 --- /dev/null +++ b/examples/stm32wle5/src/bin/blinky.rs @@ -0,0 +1,95 @@ +#![no_std] +#![no_main] + +use defmt::*; +#[cfg(feature = "defmt-rtt")] +use defmt_rtt as _; +use embassy_executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::low_power::Executor; +use embassy_stm32::rtc::{Rtc, RtcConfig}; +use embassy_time::Timer; +use panic_probe as _; +use static_cell::StaticCell; + +#[cortex_m_rt::entry] +fn main() -> ! { + info!("main: Starting!"); + Executor::take().run(|spawner| { + spawner.spawn(unwrap!(async_main(spawner))); + }); +} + +#[embassy_executor::task] +async fn async_main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + // enable HSI clock + config.rcc.hsi = true; + // enable LSI clock for RTC + config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); + config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); + config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; + #[cfg(feature = "defmt-serial")] + { + // disable debug during sleep to reduce power consumption since we are + // using defmt-serial on LPUART1. + config.enable_debug_during_sleep = false; + // if we are using defmt-serial on LPUART1, we need to use HSI for the clock + // so that its registers are preserved during STOP modes. + config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; + } + // Initialize STM32WL peripherals (use default config like wio-e5-async example) + let p = embassy_stm32::init(config); + + // start with all GPIOs as analog to reduce power consumption + for r in [ + embassy_stm32::pac::GPIOA, + embassy_stm32::pac::GPIOB, + embassy_stm32::pac::GPIOC, + embassy_stm32::pac::GPIOH, + ] { + r.moder().modify(|w| { + for i in 0..16 { + // don't reset these if probe-rs should stay connected! + #[cfg(feature = "defmt-rtt")] + if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { + continue; + } + w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); + } + }); + } + #[cfg(feature = "defmt-serial")] + { + use embassy_stm32::mode::Blocking; + use embassy_stm32::usart::Uart; + let mut config = embassy_stm32::usart::Config::default(); + config.baudrate = 115200; + config.assume_noise_free = true; + config.detect_previous_overrun = true; + let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); + static SERIAL: StaticCell> = StaticCell::new(); + defmt_serial::defmt_serial(SERIAL.init(uart)); + } + + // give the RTC to the low_power executor... + let rtc_config = RtcConfig::default(); + let rtc = Rtc::new(p.RTC, rtc_config); + static RTC: StaticCell = StaticCell::new(); + let rtc = RTC.init(rtc); + embassy_stm32::low_power::stop_with_rtc(rtc); + + info!("Hello World!"); + + let mut led = Output::new(p.PB5, Level::High, Speed::Low); + + loop { + info!("low"); + led.set_low(); + Timer::after_millis(500).await; + + info!("high"); + led.set_high(); + Timer::after_millis(500).await; + } +} diff --git a/examples/stm32wle5/src/bin/button_exti.rs b/examples/stm32wle5/src/bin/button_exti.rs new file mode 100644 index 000000000..dfa391a81 --- /dev/null +++ b/examples/stm32wle5/src/bin/button_exti.rs @@ -0,0 +1,96 @@ +#![no_std] +#![no_main] + +use defmt::*; +#[cfg(feature = "defmt-rtt")] +use defmt_rtt as _; +use embassy_executor::Spawner; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::gpio::Pull; +use embassy_stm32::low_power::Executor; +use embassy_stm32::rtc::{Rtc, RtcConfig}; +use panic_probe as _; +use static_cell::StaticCell; + +#[cortex_m_rt::entry] +fn main() -> ! { + info!("main: Starting!"); + Executor::take().run(|spawner| { + spawner.spawn(unwrap!(async_main(spawner))); + }); +} + +#[embassy_executor::task] +async fn async_main(_spawner: Spawner) { + let mut config = embassy_stm32::Config::default(); + // enable HSI clock + config.rcc.hsi = true; + // enable LSI clock for RTC + config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); + config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); + config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; + // enable ADC with HSI clock + config.rcc.mux.adcsel = embassy_stm32::pac::rcc::vals::Adcsel::HSI; + #[cfg(feature = "defmt-serial")] + { + // disable debug during sleep to reduce power consumption since we are + // using defmt-serial on LPUART1. + config.enable_debug_during_sleep = false; + // if we are using defmt-serial on LPUART1, we need to use HSI for the clock + // so that its registers are preserved during STOP modes. + config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; + } + // Initialize STM32WL peripherals (use default config like wio-e5-async example) + let p = embassy_stm32::init(config); + + // start with all GPIOs as analog to reduce power consumption + for r in [ + embassy_stm32::pac::GPIOA, + embassy_stm32::pac::GPIOB, + embassy_stm32::pac::GPIOC, + embassy_stm32::pac::GPIOH, + ] { + r.moder().modify(|w| { + for i in 0..16 { + // don't reset these if probe-rs should stay connected! + #[cfg(feature = "defmt-rtt")] + if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { + continue; + } + w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); + } + }); + } + #[cfg(feature = "defmt-serial")] + { + use embassy_stm32::mode::Blocking; + use embassy_stm32::usart::Uart; + let mut config = embassy_stm32::usart::Config::default(); + config.baudrate = 115200; + config.assume_noise_free = true; + config.detect_previous_overrun = true; + let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); + static SERIAL: StaticCell> = StaticCell::new(); + defmt_serial::defmt_serial(SERIAL.init(uart)); + } + + // give the RTC to the low_power executor... + let rtc_config = RtcConfig::default(); + let rtc = Rtc::new(p.RTC, rtc_config); + static RTC: StaticCell = StaticCell::new(); + let rtc = RTC.init(rtc); + embassy_stm32::low_power::stop_with_rtc(rtc); + + info!("Hello World!"); + + let mut button = ExtiInput::new(p.PA0, p.EXTI0, Pull::Up); + + info!("Press the USER button..."); + + loop { + button.wait_for_falling_edge().await; + info!("Pressed!"); + button.wait_for_rising_edge().await; + info!("Released!"); + } +} diff --git a/examples/stm32wle5/stm32wle5.code-workspace b/examples/stm32wle5/stm32wle5.code-workspace new file mode 100644 index 000000000..a7c4a0ebd --- /dev/null +++ b/examples/stm32wle5/stm32wle5.code-workspace @@ -0,0 +1,13 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "rust-analyzer.cargo.target": "thumbv7em-none-eabi", + "rust-analyzer.cargo.allTargets": false, + "rust-analyzer.cargo.targetDir": "target/rust-analyzer", + "rust-analyzer.checkOnSave": true, + } +} -- cgit