+// try i2c bus recovery at 100kHz = 5uS high, 5uS low
+static void I2C_recovery(void) {
+
+ DbpString("Performing i2c bus recovery");
+
+ // reset I2C
+ SDA_H; SCL_H;
+
+ //9nth cycle acts as NACK
+ for (int i = 0; i < 10; i++) {
+ SCL_H; WaitUS(5);
+ SCL_L; WaitUS(5);
+ }
+
+ //a STOP signal (SDA from low to high while CLK is high)
+ SDA_L; WaitUS(5);
+ SCL_H; WaitUS(2);
+ SDA_H; WaitUS(2);
+
+ bool isok = (SCL_read && SDA_read);
+ if (!SDA_read)
+ DbpString("I2C bus recovery error: SDA still LOW");
+ if (!SCL_read)
+ DbpString("I2C bus recovery error: SCL still LOW");
+ if (isok)
+ DbpString("I2C bus recovery complete");
+}
+