Skip to content

Commit adafae9

Browse files
committed
I2C Scans for all! (S2 users having ESP_ERR_TIMEOUT: I2C Bus Busy)
1 parent 712e434 commit adafae9

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,37 @@ WipperSnapper_Component_I2C::scanAddresses() {
156156

157157
// Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of
158158
// those that respond.
159+
uint8_t addresses_failed_count = 0, current_failed_count = 0;
159160
WS_DEBUG_PRINTLN("EXEC: I2C Scan");
160161
for (address = 0x08; address < 0x7F; address++) {
162+
start_scan:
163+
current_failed_count = 0;
161164
_i2c->beginTransmission(address);
162165
endTransmissionRC = _i2c->endTransmission();
163166

164167
#if defined(ARDUINO_ARCH_ESP32)
165168
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
166169
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp
167170
if (endTransmissionRC == 5) {
168-
WS_DEBUG_PRINTLN("ESP_ERR_TIMEOUT: I2C Bus Busy");
171+
addresses_failed_count++;
172+
current_failed_count++;
173+
WS_DEBUG_PRINT("ESP_ERR_TIMEOUT: I2C Bus Busy - (Address: 0x");
174+
WS_DEBUG_PRINT(address, HEX);
175+
WS_DEBUG_PRINT(" - Error Count: ");
176+
WS_DEBUG_PRINT(current_failed_count);
177+
WS_DEBUG_PRINT(" of ");
178+
WS_DEBUG_PRINT(addresses_failed_count);
179+
WS_DEBUG_PRINT(")");
169180
scanResp.bus_response =
170181
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_ERROR_HANG;
171182
// NOTE: ESP-IDF appears to handle this "behind the scenes" by
172183
// resetting/clearing the bus. The user should be prompted to
173-
// perform a bus scan again.
174-
break;
184+
// perform a bus scan again. This is a workaround for the ESP32s2 in v3.1.x
185+
if (current_failed_count > 5 || addresses_failed_count > 100) {
186+
WS_DEBUG_PRINTLN("ESP_ERR_TIMEOUT: Too many bus hangs");
187+
break;
188+
}
189+
goto start_scan;
175190
} else if (endTransmissionRC == 7) {
176191
WS_DEBUG_PRINT("I2C_ESP_ERR: SDA/SCL shorted, requests queued: ");
177192
WS_DEBUG_PRINTLN(endTransmissionRC);

0 commit comments

Comments
 (0)