diff options
| author | xoviat <[email protected]> | 2025-11-03 18:55:48 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-03 18:55:48 -0600 |
| commit | 953f2f861838465fe7ddf048e36ac3970ad31849 (patch) | |
| tree | bfbf7ccac590d099191005da16d0dc75db0fc200 /examples | |
| parent | a93d4886f19620e7a417e86a2d22d405026712d5 (diff) | |
| parent | 345efc383fa9afabaf23b629f0937855ea4c754f (diff) | |
Merge branch 'main' into adc
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32wle5/.cargo/config.toml | 9 | ||||
| -rw-r--r-- | examples/stm32wle5/Cargo.toml | 38 | ||||
| -rw-r--r-- | examples/stm32wle5/README.md | 52 | ||||
| -rw-r--r-- | examples/stm32wle5/build.rs | 5 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/adc.rs | 100 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/blinky.rs | 90 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/button_exti.rs | 91 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/i2c.rs | 110 | ||||
| -rw-r--r-- | examples/stm32wle5/stm32wle5.code-workspace | 13 |
9 files changed, 508 insertions, 0 deletions
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 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-rs chip list` | ||
| 3 | runner = "probe-rs run --chip STM32WLE5JCIx --connect-under-reset" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabi" | ||
| 7 | |||
| 8 | [env] | ||
| 9 | 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 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2024" | ||
| 3 | name = "embassy-stm32wl-examples" | ||
| 4 | version = "0.1.0" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | publish = false | ||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | # Change stm32wl55jc-cm4 to your chip name, if necessary. | ||
| 10 | embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = ["defmt", "stm32wle5jc", "time-driver-any", "memory-x", "unstable-pac", "exti", "low-power"] } | ||
| 11 | embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } | ||
| 12 | embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt"] } | ||
| 13 | embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-1_000"] } | ||
| 14 | embassy-embedded-hal = { version = "0.5.0", path = "../../embassy-embedded-hal" } | ||
| 15 | |||
| 16 | defmt = "1.0.1" | ||
| 17 | defmt-rtt = { version = "1.1.0", optional = true } | ||
| 18 | defmt-serial = { version = "0.10.0", optional = true } | ||
| 19 | |||
| 20 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | ||
| 21 | cortex-m-rt = "0.7.0" | ||
| 22 | embedded-hal = "1.0.0" | ||
| 23 | embedded-storage = "0.3.1" | ||
| 24 | panic-probe = { version = "1.0.0", features = ["print-defmt"] } | ||
| 25 | static_cell = { version = "2.1.1", default-features = false } | ||
| 26 | |||
| 27 | [profile.release] | ||
| 28 | debug = 2 | ||
| 29 | |||
| 30 | [package.metadata.embassy] | ||
| 31 | build = [ | ||
| 32 | { target = "thumbv7em-none-eabi", artifact-dir = "out/examples/stm32wl" } | ||
| 33 | ] | ||
| 34 | |||
| 35 | [features] | ||
| 36 | default = ["defmt-serial"] | ||
| 37 | defmt-rtt = ["dep:defmt-rtt"] | ||
| 38 | defmt-serial = ["dep:defmt-serial"] | ||
diff --git a/examples/stm32wle5/README.md b/examples/stm32wle5/README.md new file mode 100644 index 000000000..18c3b5071 --- /dev/null +++ b/examples/stm32wle5/README.md | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | # Low Power Examples for STM32WLEx family | ||
| 2 | |||
| 3 | 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. | ||
| 4 | |||
| 5 | ## Prerequsits | ||
| 6 | |||
| 7 | - Connect a usb serial adapter to LPUart1 (this is where ALL logging will go) | ||
| 8 | - Optional: Connect an amp meter that ran measure down to 0.1uA to the power test pins | ||
| 9 | - `cargo install defmt-print` so you can print log messahes from LPUart1 | ||
| 10 | |||
| 11 | ## Example Notes | ||
| 12 | |||
| 13 | All examples will set all pins to analog mode before configuring pins for the example, if any. This saves about 500uA!!!! | ||
| 14 | |||
| 15 | - the `adc` example will sleep in STOP1 betwen samples and the chip will only draw about 13uA while sleeping | ||
| 16 | - the `blinky` example will sleep in STOP2 and the chip will only draw 1uA or less while sleeping | ||
| 17 | - the `button_exti` example will sleep in STOP2 and the chip will only draw 1uA or less while sleeping | ||
| 18 | - the `i2c` examples will sleep in STOP1 between reads and the chip only draw about 10uA while sleeping | ||
| 19 | |||
| 20 | For each example you will need to start `defmt-print` with the example binary and the correct serial port in a seperate terminal. Example: | ||
| 21 | ``` | ||
| 22 | defmt-print -w -v -e target/thumbv7em-none-eabi/debug/<module-name> serial --path /dev/cu.usbserial-00000000 --baud 115200 | ||
| 23 | ``` | ||
| 24 | |||
| 25 | Run individual examples with | ||
| 26 | ``` | ||
| 27 | cargo flash --chip STM32WLE5JCIx --connect-under-reset --bin <module-name> | ||
| 28 | ``` | ||
| 29 | for example | ||
| 30 | ``` | ||
| 31 | cargo flash --chip STM32WLE5JCIx --connect-under-reset --bin blinky | ||
| 32 | ``` | ||
| 33 | |||
| 34 | 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! | ||
| 35 | ``` | ||
| 36 | cargo run --bin blinky | ||
| 37 | ``` | ||
| 38 | |||
| 39 | ## Checklist before running examples | ||
| 40 | 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. | ||
| 41 | |||
| 42 | * [ ] 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) | ||
| 43 | * [ ] 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. | ||
| 44 | * [ ] If your board has a special clock or power configuration, make sure that it is set up appropriately. | ||
| 45 | * [ ] If your board has different pin mapping, update any pin numbers or peripherals in the given example code to match your schematic | ||
| 46 | |||
| 47 | If you are unsure, please drop by the Embassy Matrix chat for support, and let us know: | ||
| 48 | |||
| 49 | * Which example you are trying to run | ||
| 50 | * Which chip and board you are using | ||
| 51 | |||
| 52 | 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 @@ | |||
| 1 | fn main() { | ||
| 2 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 3 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | ||
| 4 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 5 | } | ||
diff --git a/examples/stm32wle5/src/bin/adc.rs b/examples/stm32wle5/src/bin/adc.rs new file mode 100644 index 000000000..ff1a5fa16 --- /dev/null +++ b/examples/stm32wle5/src/bin/adc.rs | |||
| @@ -0,0 +1,100 @@ | |||
| 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::adc::{Adc, SampleTime}; | ||
| 9 | use embassy_stm32::low_power::Executor; | ||
| 10 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 11 | use embassy_time::Timer; | ||
| 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 config = embassy_stm32::usart::Config::default(); | ||
| 69 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 70 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 71 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 72 | } | ||
| 73 | |||
| 74 | // give the RTC to the low_power executor... | ||
| 75 | let rtc_config = RtcConfig::default(); | ||
| 76 | let rtc = Rtc::new(p.RTC, rtc_config); | ||
| 77 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 78 | |||
| 79 | info!("Hello World!"); | ||
| 80 | |||
| 81 | let mut adc = Adc::new(p.ADC1); | ||
| 82 | adc.set_sample_time(SampleTime::CYCLES79_5); | ||
| 83 | let mut pin = p.PA10; | ||
| 84 | |||
| 85 | let mut vrefint = adc.enable_vrefint(); | ||
| 86 | let vrefint_sample = adc.blocking_read(&mut vrefint); | ||
| 87 | let convert_to_millivolts = |sample| { | ||
| 88 | // From https://www.st.com/resource/en/datasheet/stm32g031g8.pdf | ||
| 89 | // 6.3.3 Embedded internal reference voltage | ||
| 90 | const VREFINT_MV: u32 = 1212; // mV | ||
| 91 | |||
| 92 | (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16 | ||
| 93 | }; | ||
| 94 | |||
| 95 | loop { | ||
| 96 | let v = adc.blocking_read(&mut pin); | ||
| 97 | info!("--> {} - {} mV", v, convert_to_millivolts(v)); | ||
| 98 | Timer::after_secs(1).await; | ||
| 99 | } | ||
| 100 | } | ||
diff --git a/examples/stm32wle5/src/bin/blinky.rs b/examples/stm32wle5/src/bin/blinky.rs new file mode 100644 index 000000000..1191a1157 --- /dev/null +++ b/examples/stm32wle5/src/bin/blinky.rs | |||
| @@ -0,0 +1,90 @@ | |||
| 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::gpio::{Level, Output, Speed}; | ||
| 9 | use embassy_stm32::low_power::Executor; | ||
| 10 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 11 | use embassy_time::Timer; | ||
| 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 | #[cfg(feature = "defmt-serial")] | ||
| 33 | { | ||
| 34 | // disable debug during sleep to reduce power consumption since we are | ||
| 35 | // using defmt-serial on LPUART1. | ||
| 36 | config.enable_debug_during_sleep = false; | ||
| 37 | // if we are using defmt-serial on LPUART1, we need to use HSI for the clock | ||
| 38 | // so that its registers are preserved during STOP modes. | ||
| 39 | config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; | ||
| 40 | } | ||
| 41 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | ||
| 42 | let p = embassy_stm32::init(config); | ||
| 43 | |||
| 44 | // start with all GPIOs as analog to reduce power consumption | ||
| 45 | for r in [ | ||
| 46 | embassy_stm32::pac::GPIOA, | ||
| 47 | embassy_stm32::pac::GPIOB, | ||
| 48 | embassy_stm32::pac::GPIOC, | ||
| 49 | embassy_stm32::pac::GPIOH, | ||
| 50 | ] { | ||
| 51 | r.moder().modify(|w| { | ||
| 52 | for i in 0..16 { | ||
| 53 | // don't reset these if probe-rs should stay connected! | ||
| 54 | #[cfg(feature = "defmt-rtt")] | ||
| 55 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 56 | continue; | ||
| 57 | } | ||
| 58 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 59 | } | ||
| 60 | }); | ||
| 61 | } | ||
| 62 | #[cfg(feature = "defmt-serial")] | ||
| 63 | { | ||
| 64 | use embassy_stm32::mode::Blocking; | ||
| 65 | use embassy_stm32::usart::Uart; | ||
| 66 | let config = embassy_stm32::usart::Config::default(); | ||
| 67 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 68 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 69 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 70 | } | ||
| 71 | |||
| 72 | // give the RTC to the low_power executor... | ||
| 73 | let rtc_config = RtcConfig::default(); | ||
| 74 | let rtc = Rtc::new(p.RTC, rtc_config); | ||
| 75 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 76 | |||
| 77 | info!("Hello World!"); | ||
| 78 | |||
| 79 | let mut led = Output::new(p.PB5, Level::High, Speed::Low); | ||
| 80 | |||
| 81 | loop { | ||
| 82 | info!("low"); | ||
| 83 | led.set_low(); | ||
| 84 | Timer::after_millis(500).await; | ||
| 85 | |||
| 86 | info!("high"); | ||
| 87 | led.set_high(); | ||
| 88 | Timer::after_millis(500).await; | ||
| 89 | } | ||
| 90 | } | ||
diff --git a/examples/stm32wle5/src/bin/button_exti.rs b/examples/stm32wle5/src/bin/button_exti.rs new file mode 100644 index 000000000..f07f9724d --- /dev/null +++ b/examples/stm32wle5/src/bin/button_exti.rs | |||
| @@ -0,0 +1,91 @@ | |||
| 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 config = embassy_stm32::usart::Config::default(); | ||
| 69 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 70 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 71 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 72 | } | ||
| 73 | |||
| 74 | // give the RTC to the low_power executor... | ||
| 75 | let rtc_config = RtcConfig::default(); | ||
| 76 | let rtc = Rtc::new(p.RTC, rtc_config); | ||
| 77 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 78 | |||
| 79 | info!("Hello World!"); | ||
| 80 | |||
| 81 | let mut button = ExtiInput::new(p.PA0, p.EXTI0, Pull::Up); | ||
| 82 | |||
| 83 | info!("Press the USER button..."); | ||
| 84 | |||
| 85 | loop { | ||
| 86 | button.wait_for_falling_edge().await; | ||
| 87 | info!("Pressed!"); | ||
| 88 | button.wait_for_rising_edge().await; | ||
| 89 | info!("Released!"); | ||
| 90 | } | ||
| 91 | } | ||
diff --git a/examples/stm32wle5/src/bin/i2c.rs b/examples/stm32wle5/src/bin/i2c.rs new file mode 100644 index 000000000..af07f911e --- /dev/null +++ b/examples/stm32wle5/src/bin/i2c.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 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::i2c::I2c; | ||
| 9 | use embassy_stm32::low_power::Executor; | ||
| 10 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 11 | use embassy_stm32::time::Hertz; | ||
| 12 | use embassy_stm32::{bind_interrupts, i2c, peripherals}; | ||
| 13 | use embassy_time::{Duration, Timer}; | ||
| 14 | use panic_probe as _; | ||
| 15 | use static_cell::StaticCell; | ||
| 16 | |||
| 17 | bind_interrupts!(struct IrqsI2C{ | ||
| 18 | I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>; | ||
| 19 | I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>; | ||
| 20 | }); | ||
| 21 | |||
| 22 | #[cortex_m_rt::entry] | ||
| 23 | fn main() -> ! { | ||
| 24 | info!("main: Starting!"); | ||
| 25 | Executor::take().run(|spawner| { | ||
| 26 | spawner.spawn(unwrap!(async_main(spawner))); | ||
| 27 | }); | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn async_main(_spawner: Spawner) { | ||
| 32 | let mut config = embassy_stm32::Config::default(); | ||
| 33 | // enable HSI clock | ||
| 34 | config.rcc.hsi = true; | ||
| 35 | // enable LSI clock for RTC | ||
| 36 | config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); | ||
| 37 | config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); | ||
| 38 | config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; | ||
| 39 | // enable ADC with HSI clock | ||
| 40 | config.rcc.mux.i2c2sel = embassy_stm32::pac::rcc::vals::I2c2sel::HSI; | ||
| 41 | #[cfg(feature = "defmt-serial")] | ||
| 42 | { | ||
| 43 | // disable debug during sleep to reduce power consumption since we are | ||
| 44 | // using defmt-serial on LPUART1. | ||
| 45 | config.enable_debug_during_sleep = false; | ||
| 46 | // if we are using defmt-serial on LPUART1, we need to use HSI for the clock | ||
| 47 | // so that its registers are preserved during STOP modes. | ||
| 48 | config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; | ||
| 49 | } | ||
| 50 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | ||
| 51 | let p = embassy_stm32::init(config); | ||
| 52 | |||
| 53 | // start with all GPIOs as analog to reduce power consumption | ||
| 54 | for r in [ | ||
| 55 | embassy_stm32::pac::GPIOA, | ||
| 56 | embassy_stm32::pac::GPIOB, | ||
| 57 | embassy_stm32::pac::GPIOC, | ||
| 58 | embassy_stm32::pac::GPIOH, | ||
| 59 | ] { | ||
| 60 | r.moder().modify(|w| { | ||
| 61 | for i in 0..16 { | ||
| 62 | // don't reset these if probe-rs should stay connected! | ||
| 63 | #[cfg(feature = "defmt-rtt")] | ||
| 64 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 65 | continue; | ||
| 66 | } | ||
| 67 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 68 | } | ||
| 69 | }); | ||
| 70 | } | ||
| 71 | #[cfg(feature = "defmt-serial")] | ||
| 72 | { | ||
| 73 | use embassy_stm32::mode::Blocking; | ||
| 74 | use embassy_stm32::usart::Uart; | ||
| 75 | let config = embassy_stm32::usart::Config::default(); | ||
| 76 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 77 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 78 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 79 | } | ||
| 80 | |||
| 81 | // give the RTC to the low_power executor... | ||
| 82 | let rtc_config = RtcConfig::default(); | ||
| 83 | let rtc = Rtc::new(p.RTC, rtc_config); | ||
| 84 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 85 | |||
| 86 | info!("Hello World!"); | ||
| 87 | let en3v3 = embassy_stm32::gpio::Output::new( | ||
| 88 | p.PA9, | ||
| 89 | embassy_stm32::gpio::Level::High, | ||
| 90 | embassy_stm32::gpio::Speed::High, | ||
| 91 | ); | ||
| 92 | core::mem::forget(en3v3); // keep the output pin enabled | ||
| 93 | |||
| 94 | let mut i2c = I2c::new(p.I2C2, p.PB15, p.PA15, IrqsI2C, p.DMA1_CH6, p.DMA1_CH7, { | ||
| 95 | let mut config = i2c::Config::default(); | ||
| 96 | config.frequency = Hertz::khz(100); | ||
| 97 | config.timeout = Duration::from_millis(500); | ||
| 98 | config | ||
| 99 | }); | ||
| 100 | |||
| 101 | loop { | ||
| 102 | let mut buffer = [0; 2]; | ||
| 103 | // read the temperature register of the onboard lm75 | ||
| 104 | match i2c.read(0x48, &mut buffer).await { | ||
| 105 | Ok(_) => info!("--> {:?}", buffer), | ||
| 106 | Err(e) => info!("--> Error: {:?}", e), | ||
| 107 | } | ||
| 108 | Timer::after_secs(5).await; | ||
| 109 | } | ||
| 110 | } | ||
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 @@ | |||
| 1 | { | ||
| 2 | "folders": [ | ||
| 3 | { | ||
| 4 | "path": "." | ||
| 5 | } | ||
| 6 | ], | ||
| 7 | "settings": { | ||
| 8 | "rust-analyzer.cargo.target": "thumbv7em-none-eabi", | ||
| 9 | "rust-analyzer.cargo.allTargets": false, | ||
| 10 | "rust-analyzer.cargo.targetDir": "target/rust-analyzer", | ||
| 11 | "rust-analyzer.checkOnSave": true, | ||
| 12 | } | ||
| 13 | } | ||
