aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32wle5/src/bin/i2c.rs
diff options
context:
space:
mode:
authorliebman <[email protected]>2025-10-30 13:07:07 -0700
committerliebman <[email protected]>2025-11-03 12:50:41 -0800
commit9131cbd9f2b28ff10be64162a3d55d288f27190a (patch)
tree3d16a5587d01cb8194c2bd7981af768f771c22f6 /examples/stm32wle5/src/bin/i2c.rs
parent5b70da2256747853ac4f866e60493241ac34bcd3 (diff)
examples: : stm32wlex: add i2c example
Diffstat (limited to 'examples/stm32wle5/src/bin/i2c.rs')
-rw-r--r--examples/stm32wle5/src/bin/i2c.rs112
1 files changed, 112 insertions, 0 deletions
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
4use defmt::*;
5#[cfg(feature = "defmt-rtt")]
6use defmt_rtt as _;
7use embassy_executor::Spawner;
8use embassy_stm32::i2c::I2c;
9use embassy_stm32::low_power::Executor;
10use embassy_stm32::rtc::{Rtc, RtcConfig};
11use embassy_stm32::time::Hertz;
12use embassy_stm32::{bind_interrupts, i2c, peripherals};
13use embassy_time::{Duration, Timer};
14use panic_probe as _;
15use static_cell::StaticCell;
16
17bind_interrupts!(struct IrqsI2C{
18 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
19 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
20});
21
22#[cortex_m_rt::entry]
23fn main() -> ! {
24 info!("main: Starting!");
25 Executor::take().run(|spawner| {
26 spawner.spawn(unwrap!(async_main(spawner)));
27 });
28}
29
30#[embassy_executor::task]
31async 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}