diff options
| author | liebman <[email protected]> | 2025-10-30 13:07:07 -0700 |
|---|---|---|
| committer | liebman <[email protected]> | 2025-11-03 12:50:41 -0800 |
| commit | 9131cbd9f2b28ff10be64162a3d55d288f27190a (patch) | |
| tree | 3d16a5587d01cb8194c2bd7981af768f771c22f6 /examples | |
| parent | 5b70da2256747853ac4f866e60493241ac34bcd3 (diff) | |
examples: : stm32wlex: add i2c example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32wle5/README.md | 1 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/adc.rs | 5 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/blinky.rs | 5 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/button_exti.rs | 5 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/i2c.rs | 112 |
5 files changed, 116 insertions, 12 deletions
diff --git a/examples/stm32wle5/README.md b/examples/stm32wle5/README.md index 63507f490..18c3b5071 100644 --- a/examples/stm32wle5/README.md +++ b/examples/stm32wle5/README.md | |||
| @@ -15,6 +15,7 @@ All examples will set all pins to analog mode before configuring pins for the ex | |||
| 15 | - the `adc` example will sleep in STOP1 betwen samples and the chip will only draw about 13uA while sleeping | 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 | 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 | 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 | ||
| 18 | 19 | ||
| 19 | For each example you will need to start `defmt-print` with the example binary and the correct serial port in a seperate terminal. Example: | 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: |
| 20 | ``` | 21 | ``` |
diff --git a/examples/stm32wle5/src/bin/adc.rs b/examples/stm32wle5/src/bin/adc.rs index fabdb9cb3..b4af656ed 100644 --- a/examples/stm32wle5/src/bin/adc.rs +++ b/examples/stm32wle5/src/bin/adc.rs | |||
| @@ -65,10 +65,7 @@ async fn async_main(_spawner: Spawner) { | |||
| 65 | { | 65 | { |
| 66 | use embassy_stm32::mode::Blocking; | 66 | use embassy_stm32::mode::Blocking; |
| 67 | use embassy_stm32::usart::Uart; | 67 | use embassy_stm32::usart::Uart; |
| 68 | let mut config = embassy_stm32::usart::Config::default(); | 68 | let 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!"); | 69 | 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(); | 70 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); |
| 74 | defmt_serial::defmt_serial(SERIAL.init(uart)); | 71 | defmt_serial::defmt_serial(SERIAL.init(uart)); |
diff --git a/examples/stm32wle5/src/bin/blinky.rs b/examples/stm32wle5/src/bin/blinky.rs index f5ba97025..a7a571cb1 100644 --- a/examples/stm32wle5/src/bin/blinky.rs +++ b/examples/stm32wle5/src/bin/blinky.rs | |||
| @@ -63,10 +63,7 @@ async fn async_main(_spawner: Spawner) { | |||
| 63 | { | 63 | { |
| 64 | use embassy_stm32::mode::Blocking; | 64 | use embassy_stm32::mode::Blocking; |
| 65 | use embassy_stm32::usart::Uart; | 65 | use embassy_stm32::usart::Uart; |
| 66 | let mut config = embassy_stm32::usart::Config::default(); | 66 | let config = embassy_stm32::usart::Config::default(); |
| 67 | config.baudrate = 115200; | ||
| 68 | config.assume_noise_free = true; | ||
| 69 | config.detect_previous_overrun = true; | ||
| 70 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | 67 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); |
| 71 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | 68 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); |
| 72 | defmt_serial::defmt_serial(SERIAL.init(uart)); | 69 | defmt_serial::defmt_serial(SERIAL.init(uart)); |
diff --git a/examples/stm32wle5/src/bin/button_exti.rs b/examples/stm32wle5/src/bin/button_exti.rs index dfa391a81..c8083a240 100644 --- a/examples/stm32wle5/src/bin/button_exti.rs +++ b/examples/stm32wle5/src/bin/button_exti.rs | |||
| @@ -65,10 +65,7 @@ async fn async_main(_spawner: Spawner) { | |||
| 65 | { | 65 | { |
| 66 | use embassy_stm32::mode::Blocking; | 66 | use embassy_stm32::mode::Blocking; |
| 67 | use embassy_stm32::usart::Uart; | 67 | use embassy_stm32::usart::Uart; |
| 68 | let mut config = embassy_stm32::usart::Config::default(); | 68 | let 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!"); | 69 | 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(); | 70 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); |
| 74 | defmt_serial::defmt_serial(SERIAL.init(uart)); | 71 | defmt_serial::defmt_serial(SERIAL.init(uart)); |
diff --git a/examples/stm32wle5/src/bin/i2c.rs b/examples/stm32wle5/src/bin/i2c.rs new file mode 100644 index 000000000..4a56773e9 --- /dev/null +++ b/examples/stm32wle5/src/bin/i2c.rs | |||
| @@ -0,0 +1,112 @@ | |||
| 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 | static RTC: StaticCell<Rtc> = StaticCell::new(); | ||
| 85 | let rtc = RTC.init(rtc); | ||
| 86 | embassy_stm32::low_power::stop_with_rtc(rtc); | ||
| 87 | |||
| 88 | info!("Hello World!"); | ||
| 89 | let en3v3 = embassy_stm32::gpio::Output::new( | ||
| 90 | p.PA9, | ||
| 91 | embassy_stm32::gpio::Level::High, | ||
| 92 | embassy_stm32::gpio::Speed::High, | ||
| 93 | ); | ||
| 94 | core::mem::forget(en3v3); // keep the output pin enabled | ||
| 95 | |||
| 96 | let mut i2c = I2c::new(p.I2C2, p.PB15, p.PA15, IrqsI2C, p.DMA1_CH6, p.DMA1_CH7, { | ||
| 97 | let mut config = i2c::Config::default(); | ||
| 98 | config.frequency = Hertz::khz(100); | ||
| 99 | config.timeout = Duration::from_millis(500); | ||
| 100 | config | ||
| 101 | }); | ||
| 102 | |||
| 103 | loop { | ||
| 104 | let mut buffer = [0; 2]; | ||
| 105 | // read the temperature register of the onboard lm75 | ||
| 106 | match i2c.read(0x48, &mut buffer).await { | ||
| 107 | Ok(_) => info!("--> {:?}", buffer), | ||
| 108 | Err(e) => info!("--> Error: {:?}", e), | ||
| 109 | } | ||
| 110 | Timer::after_secs(5).await; | ||
| 111 | } | ||
| 112 | } | ||
