diff options
| author | Felipe Balbi <[email protected]> | 2024-05-16 12:14:05 -0700 |
|---|---|---|
| committer | Felipe Balbi <[email protected]> | 2024-05-16 12:15:43 -0700 |
| commit | 57d9bfd343c51c9823b3e17e8a48260f0b67939f (patch) | |
| tree | a4d9f284d94ff2cf3c5f795c9d102cfef1507fff /examples/stm32g0/src | |
| parent | d45638be1b1b120b0edb1cce6ff701b3ab515d53 (diff) | |
stm32g0: add i2c_async example
This example will help those having difficulties understanding how to
bind interrupts on stm32g0 devices.
Diffstat (limited to 'examples/stm32g0/src')
| -rw-r--r-- | examples/stm32g0/src/bin/i2c_async.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/i2c_async.rs b/examples/stm32g0/src/bin/i2c_async.rs new file mode 100644 index 000000000..7e3189b05 --- /dev/null +++ b/examples/stm32g0/src/bin/i2c_async.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::i2c::{self, I2c}; | ||
| 7 | use embassy_stm32::time::Hertz; | ||
| 8 | use embassy_stm32::{bind_interrupts, peripherals}; | ||
| 9 | use embassy_time::{Duration, Timer}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | bind_interrupts!(struct Irqs { | ||
| 13 | I2C1 => i2c::EventInterruptHandler<peripherals::I2C1>, i2c::ErrorInterruptHandler<peripherals::I2C1>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | const TMP117_ADDR: u8 = 0x48; | ||
| 17 | const TMP117_TEMP_RESULT: u8 = 0x00; | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) { | ||
| 21 | info!("Hello world"); | ||
| 22 | |||
| 23 | let p = embassy_stm32::init(Default::default()); | ||
| 24 | |||
| 25 | let mut data = [0u8; 2]; | ||
| 26 | let mut i2c = I2c::new( | ||
| 27 | p.I2C1, | ||
| 28 | p.PB8, | ||
| 29 | p.PB9, | ||
| 30 | Irqs, | ||
| 31 | p.DMA1_CH1, | ||
| 32 | p.DMA1_CH2, | ||
| 33 | Hertz(100_000), | ||
| 34 | Default::default(), | ||
| 35 | ); | ||
| 36 | |||
| 37 | loop { | ||
| 38 | match i2c.write_read(TMP117_ADDR, &[TMP117_TEMP_RESULT], &mut data).await { | ||
| 39 | Ok(()) => { | ||
| 40 | let temp = f32::from(i16::from_be_bytes(data)) * 7.8125 / 1000.0; | ||
| 41 | info!("Temperature {}", temp); | ||
| 42 | } | ||
| 43 | Err(_) => error!("I2C Error"), | ||
| 44 | } | ||
| 45 | |||
| 46 | Timer::after(Duration::from_millis(1000)).await; | ||
| 47 | } | ||
| 48 | } | ||
