diff --git a/src/main/drivers/bus_i2c_hal.c b/src/main/drivers/bus_i2c_hal.c index 8e96efd438..e434d8c272 100644 --- a/src/main/drivers/bus_i2c_hal.c +++ b/src/main/drivers/bus_i2c_hal.c @@ -282,7 +282,6 @@ uint16_t i2cGetErrorCounter(void) static void i2cUnstick(IO_t scl, IO_t sda) { int i; - int timeout = 100; IOHi(scl); IOHi(sda); @@ -290,27 +289,31 @@ static void i2cUnstick(IO_t scl, IO_t sda) IOConfigGPIO(scl, IOCFG_OUT_OD); IOConfigGPIO(sda, IOCFG_OUT_OD); - for (i = 0; i < 8; i++) { + // Analog Devices AN-686 + // We need 9 clock pulses + STOP condition + for (i = 0; i < 9; i++) { // Wait for any clock stretching to finish + int timeout = 100; while (!IORead(scl) && timeout) { - delayMicroseconds(10); + delayMicroseconds(5); timeout--; } // Pull low IOLo(scl); // Set bus low - delayMicroseconds(10); + delayMicroseconds(5); IOHi(scl); // Set bus high - delayMicroseconds(10); + delayMicroseconds(5); } - // Generate a start then stop condition - IOLo(sda); // Set bus data low - delayMicroseconds(10); - IOLo(scl); // Set bus scl low - delayMicroseconds(10); + // Generate a stop condition in case there was none + IOLo(scl); + delayMicroseconds(5); + IOLo(sda); + delayMicroseconds(5); + IOHi(scl); // Set bus scl high - delayMicroseconds(10); + delayMicroseconds(5); IOHi(sda); // Set bus sda high }