diff options
| author | liebman <[email protected]> | 2025-10-29 14:47:06 -0700 |
|---|---|---|
| committer | liebman <[email protected]> | 2025-11-03 12:50:41 -0800 |
| commit | 46480285783390d90f8d99e530a1da28a292dc3c (patch) | |
| tree | daff2dd4ed4f42709e53d2c410073292f1120551 /examples/stm32wle5/src/bin/button_exti.rs | |
| parent | ccce1478c161662521a77284222f5710b1ee34d9 (diff) | |
examples: add low-power examples for `stm32wlex`
Diffstat (limited to 'examples/stm32wle5/src/bin/button_exti.rs')
| -rw-r--r-- | examples/stm32wle5/src/bin/button_exti.rs | 96 |
1 files changed, 96 insertions, 0 deletions
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 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | #[cfg(feature = "defmt-rtt")] | ||
| 6 | use defmt_rtt as _; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::exti::ExtiInput; | ||
| 9 | use embassy_stm32::gpio::Pull; | ||
| 10 | use embassy_stm32::low_power::Executor; | ||
| 11 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 12 | use panic_probe as _; | ||
| 13 | use static_cell::StaticCell; | ||
| 14 | |||
| 15 | #[cortex_m_rt::entry] | ||
| 16 | fn main() -> ! { | ||
| 17 | info!("main: Starting!"); | ||
| 18 | Executor::take().run(|spawner| { | ||
| 19 | spawner.spawn(unwrap!(async_main(spawner))); | ||
| 20 | }); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[embassy_executor::task] | ||
| 24 | async fn async_main(_spawner: Spawner) { | ||
| 25 | let mut config = embassy_stm32::Config::default(); | ||
| 26 | // enable HSI clock | ||
| 27 | config.rcc.hsi = true; | ||
| 28 | // enable LSI clock for RTC | ||
| 29 | config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); | ||
| 30 | config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); | ||
| 31 | config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; | ||
| 32 | // enable ADC with HSI clock | ||
| 33 | config.rcc.mux.adcsel = embassy_stm32::pac::rcc::vals::Adcsel::HSI; | ||
| 34 | #[cfg(feature = "defmt-serial")] | ||
| 35 | { | ||
| 36 | // disable debug during sleep to reduce power consumption since we are | ||
| 37 | // using defmt-serial on LPUART1. | ||
| 38 | config.enable_debug_during_sleep = false; | ||
| 39 | // if we are using defmt-serial on LPUART1, we need to use HSI for the clock | ||
| 40 | // so that its registers are preserved during STOP modes. | ||
| 41 | config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; | ||
| 42 | } | ||
| 43 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | ||
| 44 | let p = embassy_stm32::init(config); | ||
| 45 | |||
| 46 | // start with all GPIOs as analog to reduce power consumption | ||
| 47 | for r in [ | ||
| 48 | embassy_stm32::pac::GPIOA, | ||
| 49 | embassy_stm32::pac::GPIOB, | ||
| 50 | embassy_stm32::pac::GPIOC, | ||
| 51 | embassy_stm32::pac::GPIOH, | ||
| 52 | ] { | ||
| 53 | r.moder().modify(|w| { | ||
| 54 | for i in 0..16 { | ||
| 55 | // don't reset these if probe-rs should stay connected! | ||
| 56 | #[cfg(feature = "defmt-rtt")] | ||
| 57 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 58 | continue; | ||
| 59 | } | ||
| 60 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 61 | } | ||
| 62 | }); | ||
| 63 | } | ||
| 64 | #[cfg(feature = "defmt-serial")] | ||
| 65 | { | ||
| 66 | use embassy_stm32::mode::Blocking; | ||
| 67 | use embassy_stm32::usart::Uart; | ||
| 68 | let mut config = embassy_stm32::usart::Config::default(); | ||
| 69 | config.baudrate = 115200; | ||
| 70 | config.assume_noise_free = true; | ||
| 71 | config.detect_previous_overrun = true; | ||
| 72 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 73 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 74 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 75 | } | ||
| 76 | |||
| 77 | // give the RTC to the low_power executor... | ||
| 78 | let rtc_config = RtcConfig::default(); | ||
| 79 | let rtc = Rtc::new(p.RTC, rtc_config); | ||
| 80 | static RTC: StaticCell<Rtc> = StaticCell::new(); | ||
| 81 | let rtc = RTC.init(rtc); | ||
| 82 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 83 | |||
| 84 | info!("Hello World!"); | ||
| 85 | |||
| 86 | let mut button = ExtiInput::new(p.PA0, p.EXTI0, Pull::Up); | ||
| 87 | |||
| 88 | info!("Press the USER button..."); | ||
| 89 | |||
| 90 | loop { | ||
| 91 | button.wait_for_falling_edge().await; | ||
| 92 | info!("Pressed!"); | ||
| 93 | button.wait_for_rising_edge().await; | ||
| 94 | info!("Released!"); | ||
| 95 | } | ||
| 96 | } | ||
