Merge pull request #2950 from felipebalbi/add-stm32g0-i2c-async-example

stm32g0: add i2c_async example
This commit is contained in:
Dario Nieuwenhuis 2024-05-16 19:17:48 +00:00 committed by GitHub
commit 128575aebe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,48 @@
#![no_std]
#![no_main]
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::i2c::{self, I2c};
use embassy_stm32::time::Hertz;
use embassy_stm32::{bind_interrupts, peripherals};
use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
I2C1 => i2c::EventInterruptHandler<peripherals::I2C1>, i2c::ErrorInterruptHandler<peripherals::I2C1>;
});
const TMP117_ADDR: u8 = 0x48;
const TMP117_TEMP_RESULT: u8 = 0x00;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
info!("Hello world");
let p = embassy_stm32::init(Default::default());
let mut data = [0u8; 2];
let mut i2c = I2c::new(
p.I2C1,
p.PB8,
p.PB9,
Irqs,
p.DMA1_CH1,
p.DMA1_CH2,
Hertz(100_000),
Default::default(),
);
loop {
match i2c.write_read(TMP117_ADDR, &[TMP117_TEMP_RESULT], &mut data).await {
Ok(()) => {
let temp = f32::from(i16::from_be_bytes(data)) * 7.8125 / 1000.0;
info!("Temperature {}", temp);
}
Err(_) => error!("I2C Error"),
}
Timer::after(Duration::from_millis(1000)).await;
}
}