diff options
| author | chemicstry <[email protected]> | 2022-10-24 12:33:17 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-10-24 12:33:17 +0300 |
| commit | 1bed02296cf15013e0149b36c3ddedb3278e9b88 (patch) | |
| tree | d2cbc692ea1aab186f8994c24aab97fb4c877faf /examples | |
| parent | d99841fea90caccfd95c2dad8f233ab3198f7371 (diff) | |
i2cv2 timeouts
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/i2c.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/stm32h7/src/bin/i2c.rs b/examples/stm32h7/src/bin/i2c.rs new file mode 100644 index 000000000..7a314b996 --- /dev/null +++ b/examples/stm32h7/src/bin/i2c.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::i2c::{Error, I2c, TimeoutI2c}; | ||
| 8 | use embassy_stm32::interrupt; | ||
| 9 | use embassy_stm32::time::Hertz; | ||
| 10 | use embassy_time::Duration; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | const ADDRESS: u8 = 0x5F; | ||
| 14 | const WHOAMI: u8 = 0x0F; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) -> ! { | ||
| 18 | info!("Hello world!"); | ||
| 19 | let p = embassy_stm32::init(Default::default()); | ||
| 20 | |||
| 21 | let irq = interrupt::take!(I2C2_EV); | ||
| 22 | let mut i2c = I2c::new( | ||
| 23 | p.I2C2, | ||
| 24 | p.PB10, | ||
| 25 | p.PB11, | ||
| 26 | irq, | ||
| 27 | p.DMA1_CH4, | ||
| 28 | p.DMA1_CH5, | ||
| 29 | Hertz(100_000), | ||
| 30 | Default::default(), | ||
| 31 | ); | ||
| 32 | let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000)); | ||
| 33 | |||
| 34 | let mut data = [0u8; 1]; | ||
| 35 | |||
| 36 | match timeout_i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) { | ||
| 37 | Ok(()) => info!("Whoami: {}", data[0]), | ||
| 38 | Err(Error::Timeout) => error!("Operation timed out"), | ||
| 39 | Err(e) => error!("I2c Error: {:?}", e), | ||
| 40 | } | ||
| 41 | } | ||
