diff options
| author | Zach <[email protected]> | 2024-02-18 16:23:01 -0600 |
|---|---|---|
| committer | Zach <[email protected]> | 2024-02-18 16:26:44 -0600 |
| commit | f12bba8a6db167582b072b007927b1a9920e86ba (patch) | |
| tree | 008146cbdb959e4ca6093306d3d37bc4c0367aa9 /examples/stm32u5/src/bin | |
| parent | 377e58e408f830f79171a470ba602b7d8bc525e4 (diff) | |
Add simple i2c example for u5
Diffstat (limited to 'examples/stm32u5/src/bin')
| -rw-r--r-- | examples/stm32u5/src/bin/i2c.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/i2c.rs b/examples/stm32u5/src/bin/i2c.rs new file mode 100644 index 000000000..e376c6bc8 --- /dev/null +++ b/examples/stm32u5/src/bin/i2c.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{info, unwrap}; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::dma::NoDma; | ||
| 7 | use embassy_stm32::i2c::I2c; | ||
| 8 | use embassy_stm32::time::Hertz; | ||
| 9 | use embassy_stm32::{bind_interrupts, i2c, peripherals}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | const HTS221_ADDRESS: u8 = 0x5F; | ||
| 13 | const WHOAMI: u8 = 0x0F; | ||
| 14 | |||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>; | ||
| 17 | I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>; | ||
| 18 | }); | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | let p = embassy_stm32::init(Default::default()); | ||
| 23 | let mut i2c = I2c::new( | ||
| 24 | p.I2C2, | ||
| 25 | p.PH4, | ||
| 26 | p.PH5, | ||
| 27 | Irqs, | ||
| 28 | NoDma, | ||
| 29 | NoDma, | ||
| 30 | Hertz(100_000), | ||
| 31 | Default::default(), | ||
| 32 | ); | ||
| 33 | |||
| 34 | let mut data = [0u8; 1]; | ||
| 35 | unwrap!(i2c.blocking_write_read(HTS221_ADDRESS, &[WHOAMI], &mut data)); | ||
| 36 | |||
| 37 | // HTS221 data sheet is here: https://www.st.com/resource/en/datasheet/hts221.pdf | ||
| 38 | // 7.1 WHO_AM_I command is x0F which expected response xBC. | ||
| 39 | info!("Whoami: 0x{:02x}", data[0]); | ||
| 40 | assert_eq!(0xBC, data[0]); | ||
| 41 | } | ||
