diff options
Diffstat (limited to 'examples/stm32wle5/src/bin/i2c.rs')
| -rw-r--r-- | examples/stm32wle5/src/bin/i2c.rs | 77 |
1 files changed, 77 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..8e7a6e2d8 --- /dev/null +++ b/examples/stm32wle5/src/bin/i2c.rs | |||
| @@ -0,0 +1,77 @@ | |||
| 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::time::Hertz; | ||
| 10 | use embassy_stm32::{bind_interrupts, i2c, low_power, peripherals}; | ||
| 11 | use embassy_time::{Duration, Timer}; | ||
| 12 | use panic_probe as _; | ||
| 13 | use static_cell::StaticCell; | ||
| 14 | |||
| 15 | bind_interrupts!(struct IrqsI2C{ | ||
| 16 | I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>; | ||
| 17 | I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>; | ||
| 18 | }); | ||
| 19 | |||
| 20 | #[embassy_executor::main(executor = "low_power::Executor")] | ||
| 21 | async fn async_main(_spawner: Spawner) { | ||
| 22 | let mut config = embassy_stm32::Config::default(); | ||
| 23 | // enable HSI clock | ||
| 24 | config.rcc.hsi = true; | ||
| 25 | // enable LSI clock for RTC | ||
| 26 | config.rcc.ls = embassy_stm32::rcc::LsConfig::default_lsi(); | ||
| 27 | config.rcc.msi = Some(embassy_stm32::rcc::MSIRange::RANGE4M); | ||
| 28 | config.rcc.sys = embassy_stm32::rcc::Sysclk::MSI; | ||
| 29 | // enable ADC with HSI clock | ||
| 30 | config.rcc.mux.i2c2sel = embassy_stm32::pac::rcc::vals::I2c2sel::HSI; | ||
| 31 | #[cfg(feature = "defmt-serial")] | ||
| 32 | { | ||
| 33 | // disable debug during sleep to reduce power consumption since we are | ||
| 34 | // using defmt-serial on LPUART1. | ||
| 35 | config.enable_debug_during_sleep = false; | ||
| 36 | // if we are using defmt-serial on LPUART1, we need to use HSI for the clock | ||
| 37 | // so that its registers are preserved during STOP modes. | ||
| 38 | config.rcc.mux.lpuart1sel = embassy_stm32::pac::rcc::vals::Lpuart1sel::HSI; | ||
| 39 | } | ||
| 40 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | ||
| 41 | let p = embassy_stm32::init(config); | ||
| 42 | |||
| 43 | #[cfg(feature = "defmt-serial")] | ||
| 44 | { | ||
| 45 | use embassy_stm32::mode::Blocking; | ||
| 46 | use embassy_stm32::usart::Uart; | ||
| 47 | let config = embassy_stm32::usart::Config::default(); | ||
| 48 | let uart = Uart::new_blocking(p.LPUART1, p.PC0, p.PC1, config).expect("failed to configure UART!"); | ||
| 49 | static SERIAL: StaticCell<Uart<'static, Blocking>> = StaticCell::new(); | ||
| 50 | defmt_serial::defmt_serial(SERIAL.init(uart)); | ||
| 51 | } | ||
| 52 | |||
| 53 | info!("Hello World!"); | ||
| 54 | let en3v3 = embassy_stm32::gpio::Output::new( | ||
| 55 | p.PA9, | ||
| 56 | embassy_stm32::gpio::Level::High, | ||
| 57 | embassy_stm32::gpio::Speed::High, | ||
| 58 | ); | ||
| 59 | core::mem::forget(en3v3); // keep the output pin enabled | ||
| 60 | |||
| 61 | let mut i2c = I2c::new(p.I2C2, p.PB15, p.PA15, IrqsI2C, p.DMA1_CH6, p.DMA1_CH7, { | ||
| 62 | let mut config = i2c::Config::default(); | ||
| 63 | config.frequency = Hertz::khz(100); | ||
| 64 | config.timeout = Duration::from_millis(1000); | ||
| 65 | config | ||
| 66 | }); | ||
| 67 | |||
| 68 | loop { | ||
| 69 | let mut buffer = [0; 2]; | ||
| 70 | // read the temperature register of the onboard lm75 | ||
| 71 | match i2c.read(0x48, &mut buffer).await { | ||
| 72 | Ok(_) => info!("--> {:?}", buffer), | ||
| 73 | Err(e) => info!("--> Error: {:?}", e), | ||
| 74 | } | ||
| 75 | Timer::after_secs(5).await; | ||
| 76 | } | ||
| 77 | } | ||
