Electronic – I2C bus occupied

i2cstm32f10x

I have a problem with an I2C communication and want you to ask whether there is a better solution than mine.

I am programming an STM32F103 at the moment. There is one sensor (MPU6050) which is connected via I2C to the STM32. I recognized that if I reset the STM32 a few times, the STM32 gets stuck (or got an timeout error) during the initialization of the I2C. The timeout error was during the wait of the busy flag. So the I2C communication was "occupied". The MPU6050 is the only other I2C device (slave).

In my opinion there is an error, because I was reading a register from the MPU6050 when I reset the STM32. So the MPU6050 never gets the stop condition.

I do not know how to reset the I2C connection, when the bus is "occupied" (by the MPU6050). So my solution would be to do a hardware reset with a transmission gate of the MPU6050. Is there maybe a simpler software solution?

Thanks for the help.

Best Answer

One general solution to unsticking an I2C bus is to just clock the SCL line for 18 or more cycles. This will clock the rest of the current byte out of the slave, let it see a NACK and then clock out one more byte. However, if the slave is very confused, this may not work. Also, most hardware I2C modules cannot do this so you would need to take control of the I2C lines as GPIOs and clock it manually.

Externally driving a reset line to the slave is the best solution. Also, some slaves can be configured to timeout after a relatively long time (e.g. 250 ms).