Skip to content

Commit

Permalink
esp32c3: during scan retry only once on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
l0ud committed Jul 28, 2023
1 parent 5096fd1 commit 8792023
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/i2cscan/i2cscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace I2CSCAN
{

uint8_t pickDevice(uint8_t addr1, uint8_t addr2, bool scanIfNotFound) {
if(I2CSCAN::isI2CExist(addr1)) {
if(I2CSCAN::hasDevOnBus(addr1)) {
return addr1;
}
if(I2CSCAN::isI2CExist(addr2)) {
if(I2CSCAN::hasDevOnBus(addr2)) {
return addr2;
}
if (scanIfNotFound) {
Expand Down Expand Up @@ -104,23 +104,25 @@ namespace I2CSCAN
}
else if (error == 4)
{
Serial.printf("[ERR] I2C (@ %s(%d) : %s(%d)): Unknow error at address 0x%02x\n",
Serial.printf("[ERR] I2C (@ %s(%d) : %s(%d)): Unknown error at address 0x%02x\n",
portMap[i].c_str(), portArray[i], portMap[j].c_str(), portArray[j], address);
}
}
return found;
}

bool isI2CExist(uint8_t addr) {
bool hasDevOnBus(uint8_t addr) {
byte error;
#if ESP32C3
int retries = 1;
do {
#endif
Wire.beginTransmission(addr);
error = Wire.endTransmission();
Wire.clearWriteError();
#if ESP32C3
}
while (error == 5);
while (error == 5 && retries--);
#endif
if(error == 0)
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/i2cscan/i2cscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace I2CSCAN {
void scani2cports();
bool checkI2C(uint8_t i, uint8_t j);
bool isI2CExist(uint8_t addr);
bool hasDevOnBus(uint8_t addr);
uint8_t pickDevice(uint8_t addr1, uint8_t addr2, bool scanIfNotFound);
int clearBus(uint8_t SDA, uint8_t SCL);
boolean inArray(uint8_t value, uint8_t* arr, size_t arrSize);
Expand Down

0 comments on commit 8792023

Please sign in to comment.