Skip to content

Commit b73b0fc

Browse files
committed
taskI2C: reset I2C bus if cannot connect to MPU1 and MPU2 for too long
There are still certain corner cases which can cause the Arduino I2C bus to become "stuck" -- it will not be able to make any successful I2C communication after that regardless of whether the MPUs are connected correctly or not. Example of one such case: connect a wire to SDA (with the MPUs connected as well). Rapidly attach and detach the other end of the wire to ground. taskI2C will also rapidly report the MPUs as disconnected and connected, and then finally stop at "the MPUs are disconnected", unable to reconnect again. Take the hard approach in fixing this: when we detect that both MPUs are disconnected after 1 second, hard reset the Arduino's I2C bus, wait 1 second, and then re-enable it again. Some quick testing shows that this works.
1 parent 8f9e533 commit b73b0fc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

arduino/taskI2C.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ taskI2C_setup()
8585
void
8686
taskI2C(void *pvParameters)
8787
{
88+
unsigned int inactiveCounter = 0;
8889
taskComm_Command cmd = {};
8990
cmd.type = TASKCOMM_COMMAND_SEND_SAMPLE;
9091

@@ -111,6 +112,20 @@ taskI2C(void *pvParameters)
111112
if (mpu2Active && isMpu2Zeroes(&cmd.sendSample.sample))
112113
mpu2Active = false;
113114

115+
if (mpu1Active || mpu2Active) {
116+
inactiveCounter = 0;
117+
} else {
118+
inactiveCounter++;
119+
if (inactiveCounter >= 44) {
120+
taskENTER_CRITICAL();
121+
Fastwire::reset();
122+
delay(1000);
123+
Fastwire::setup(100, true);
124+
taskEXIT_CRITICAL();
125+
inactiveCounter = 0;
126+
}
127+
}
128+
114129
if (!mpu1Active) {
115130
zeroMpu1(&cmd.sendSample.sample);
116131
mpu1Active = setupMpu(&mpu1);

0 commit comments

Comments
 (0)