aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32g0/src
diff options
context:
space:
mode:
authorFelipe Balbi <[email protected]>2024-05-16 12:14:05 -0700
committerFelipe Balbi <[email protected]>2024-05-16 12:15:43 -0700
commit57d9bfd343c51c9823b3e17e8a48260f0b67939f (patch)
treea4d9f284d94ff2cf3c5f795c9d102cfef1507fff /examples/stm32g0/src
parentd45638be1b1b120b0edb1cce6ff701b3ab515d53 (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.rs48
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
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::i2c::{self, I2c};
7use embassy_stm32::time::Hertz;
8use embassy_stm32::{bind_interrupts, peripherals};
9use embassy_time::{Duration, Timer};
10use {defmt_rtt as _, panic_probe as _};
11
12bind_interrupts!(struct Irqs {
13 I2C1 => i2c::EventInterruptHandler<peripherals::I2C1>, i2c::ErrorInterruptHandler<peripherals::I2C1>;
14});
15
16const TMP117_ADDR: u8 = 0x48;
17const TMP117_TEMP_RESULT: u8 = 0x00;
18
19#[embassy_executor::main]
20async 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}