diff options
| author | Andres Oliva <[email protected]> | 2023-10-10 18:10:53 +0200 |
|---|---|---|
| committer | Andres Oliva <[email protected]> | 2023-10-10 18:20:46 +0200 |
| commit | cd12c9cbceb0ba90a493194d8200b1d8e98bba1b (patch) | |
| tree | ad6f45f8dbe7de2b6e93e9678fdebef4993bcb46 /examples/stm32f4/src | |
| parent | eff73d6dfa4c5920a55a5ee2bf5c0b2ef68fbae1 (diff) | |
stm32: add timeout to I2C driver
Diffstat (limited to 'examples/stm32f4/src')
| -rw-r--r-- | examples/stm32f4/src/bin/i2c.rs | 8 |
1 files changed, 2 insertions, 6 deletions
diff --git a/examples/stm32f4/src/bin/i2c.rs b/examples/stm32f4/src/bin/i2c.rs index a92957325..10ca2bdc7 100644 --- a/examples/stm32f4/src/bin/i2c.rs +++ b/examples/stm32f4/src/bin/i2c.rs | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dma::NoDma; | 7 | use embassy_stm32::dma::NoDma; |
| 8 | use embassy_stm32::i2c::{Error, I2c, TimeoutI2c}; | 8 | use embassy_stm32::i2c::{Error, I2c}; |
| 9 | use embassy_stm32::time::Hertz; | 9 | use embassy_stm32::time::Hertz; |
| 10 | use embassy_stm32::{bind_interrupts, i2c, peripherals}; | 10 | use embassy_stm32::{bind_interrupts, i2c, peripherals}; |
| 11 | use embassy_time::Duration; | 11 | use embassy_time::Duration; |
| @@ -34,13 +34,9 @@ async fn main(_spawner: Spawner) { | |||
| 34 | Default::default(), | 34 | Default::default(), |
| 35 | ); | 35 | ); |
| 36 | 36 | ||
| 37 | // I2C bus can freeze if SCL line is shorted or due to a broken device that clock stretches for too long. | ||
| 38 | // TimeoutI2c allows recovering from such errors by throwing `Error::Timeout` after a given delay. | ||
| 39 | let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000)); | ||
| 40 | |||
| 41 | let mut data = [0u8; 1]; | 37 | let mut data = [0u8; 1]; |
| 42 | 38 | ||
| 43 | match timeout_i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) { | 39 | match i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) { |
| 44 | Ok(()) => info!("Whoami: {}", data[0]), | 40 | Ok(()) => info!("Whoami: {}", data[0]), |
| 45 | Err(Error::Timeout) => error!("Operation timed out"), | 41 | Err(Error::Timeout) => error!("Operation timed out"), |
| 46 | Err(e) => error!("I2c Error: {:?}", e), | 42 | Err(e) => error!("I2c Error: {:?}", e), |
