Skip to content

Commit 31eed58

Browse files
authored
Merge pull request #712 from adafruit/fix-i2c-scan-issue
Fix: I2C Scan on ESP32-S2
2 parents 0218e06 + 322c72e commit 31eed58

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ WipperSnapper_Component_I2C::WipperSnapper_Component_I2C(
8282
} else {
8383
_isInit = true; // if the peripheral was configured incorrectly
8484
}
85-
_i2c->setClock(50000);
8685
#elif defined(ARDUINO_ARCH_ESP8266)
8786
_i2c = new TwoWire();
8887
_i2c->begin(msgInitRequest->i2c_pin_sda, msgInitRequest->i2c_pin_scl);
@@ -143,8 +142,6 @@ wippersnapper_i2c_v1_BusResponse WipperSnapper_Component_I2C::getBusStatus() {
143142
/************************************************************************/
144143
wippersnapper_i2c_v1_I2CBusScanResponse
145144
WipperSnapper_Component_I2C::scanAddresses() {
146-
uint8_t endTransmissionRC;
147-
uint16_t address;
148145
wippersnapper_i2c_v1_I2CBusScanResponse scanResp =
149146
wippersnapper_i2c_v1_I2CBusScanResponse_init_zero;
150147

@@ -156,37 +153,45 @@ WipperSnapper_Component_I2C::scanAddresses() {
156153

157154
// Scan all I2C addresses between 0x08 and 0x7F inclusive and return a list of
158155
// those that respond.
159-
WS_DEBUG_PRINTLN("EXEC: I2C Scan");
160-
for (address = 0x08; address < 0x7F; address++) {
156+
WS_DEBUG_PRINTLN("[i2c]: Scanning I2C Bus for Devices...");
157+
for (uint8_t address = 1; address < 127; ++address) {
158+
WS_DEBUG_PRINT("[i2c] Scanning Address: 0x");
159+
WS_DEBUG_PRINTLN(address, HEX);
161160
_i2c->beginTransmission(address);
162-
endTransmissionRC = _i2c->endTransmission();
161+
uint8_t endTransmissionRC = _i2c->endTransmission();
163162

164-
#if defined(ARDUINO_ARCH_ESP32)
165-
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
166-
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp
167-
if (endTransmissionRC == 5) {
168-
WS_DEBUG_PRINTLN("ESP_ERR_TIMEOUT: I2C Bus Busy");
169-
scanResp.bus_response =
170-
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_ERROR_HANG;
171-
// NOTE: ESP-IDF appears to handle this "behind the scenes" by
172-
// resetting/clearing the bus. The user should be prompted to
173-
// perform a bus scan again.
174-
break;
175-
} else if (endTransmissionRC == 7) {
176-
WS_DEBUG_PRINT("I2C_ESP_ERR: SDA/SCL shorted, requests queued: ");
177-
WS_DEBUG_PRINTLN(endTransmissionRC);
178-
break;
179-
}
180-
#endif
181-
182-
// Found device!
183163
if (endTransmissionRC == 0) {
184-
WS_DEBUG_PRINT("Found I2C Device at 0x");
185-
WS_DEBUG_PRINTLN(address);
164+
WS_DEBUG_PRINTLN("[i2c] Found Device!");
186165
scanResp.addresses_found[scanResp.addresses_found_count] =
187166
(uint32_t)address;
188167
scanResp.addresses_found_count++;
189168
}
169+
#if defined(ARDUINO_ARCH_ESP32)
170+
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
171+
else if (endTransmissionRC == 3) {
172+
WS_DEBUG_PRINTLN("[i2c] Did not find device: NACK on transmit of data!");
173+
continue;
174+
} else if (endTransmissionRC == 2) {
175+
WS_DEBUG_PRINTLN(
176+
"[i2c] Did not find device: NACK on transmit of address!");
177+
continue;
178+
} else if (endTransmissionRC == 1) {
179+
WS_DEBUG_PRINTLN(
180+
"[i2c] Did not find device: data too long to fit in xmit buffer!");
181+
continue;
182+
} else if (endTransmissionRC == 4) {
183+
WS_DEBUG_PRINTLN(
184+
"[i2c] Did not find device: Unspecified bus error occured!");
185+
continue;
186+
} else if (endTransmissionRC == 5) {
187+
WS_DEBUG_PRINTLN("[i2c] Did not find device: Bus timed out!");
188+
continue;
189+
} else {
190+
WS_DEBUG_PRINTLN(
191+
"[i2c] Did not find device: Unknown bus error has occured!");
192+
continue;
193+
}
194+
#endif
190195
}
191196

192197
#ifndef ARDUINO_ARCH_ESP32
@@ -195,8 +200,9 @@ WipperSnapper_Component_I2C::scanAddresses() {
195200
WS.feedWDT();
196201
#endif
197202

198-
WS_DEBUG_PRINT("I2C Devices Found: ")
199-
WS_DEBUG_PRINTLN(scanResp.addresses_found_count);
203+
WS_DEBUG_PRINT("[i2c] Scan Complete! Found: ")
204+
WS_DEBUG_PRINT(scanResp.addresses_found_count);
205+
WS_DEBUG_PRINTLN(" Devices on bus.");
200206

201207
scanResp.bus_response = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_SUCCESS;
202208
return scanResp;

0 commit comments

Comments
 (0)