@@ -156,22 +156,37 @@ WipperSnapper_Component_I2C::scanAddresses() {
156
156
157
157
// Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of
158
158
// those that respond.
159
+ uint8_t addresses_failed_count = 0 , current_failed_count = 0 ;
159
160
WS_DEBUG_PRINTLN (" EXEC: I2C Scan" );
160
161
for (address = 0x08 ; address < 0x7F ; address++) {
162
+ start_scan:
163
+ current_failed_count = 0 ;
161
164
_i2c->beginTransmission (address);
162
165
endTransmissionRC = _i2c->endTransmission ();
163
166
164
167
#if defined(ARDUINO_ARCH_ESP32)
165
168
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
166
169
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp
167
170
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 (" )" );
169
180
scanResp.bus_response =
170
181
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_ERROR_HANG;
171
182
// NOTE: ESP-IDF appears to handle this "behind the scenes" by
172
183
// 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;
175
190
} else if (endTransmissionRC == 7 ) {
176
191
WS_DEBUG_PRINT (" I2C_ESP_ERR: SDA/SCL shorted, requests queued: " );
177
192
WS_DEBUG_PRINTLN (endTransmissionRC);
0 commit comments