diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-02-18 22:35:18 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-18 22:35:18 +0000 |
| commit | 69bfcaad42e560b3d52c0e03a761dcedf34cc09f (patch) | |
| tree | 40c859e7de6ee53356773728bcd373a42b8ef476 /examples/stm32u5/src | |
| parent | 63f955ce35502129698ab11996a673b7ff6af145 (diff) | |
| parent | f12bba8a6db167582b072b007927b1a9920e86ba (diff) | |
Merge pull request #2594 from exzachlyvv/zvv/u5-i2c
Add simple i2c example for u5
Diffstat (limited to 'examples/stm32u5/src')
| -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 | } | ||
