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 30, 2023
1 parent 8e1f2c7 commit c6a90cc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
11 changes: 6 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,24 @@ 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();
#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
2 changes: 1 addition & 1 deletion src/batterymonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void BatteryMonitor::Setup()
#if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021
for (uint8_t i = 0x48; i < 0x4F; i++)
{
if (I2CSCAN::isI2CExist(i))
if (I2CSCAN::hasDevOnBus(i))
{
address = i;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/SensorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace SlimeVR
I2CSCAN::clearBus(sdaPin, sclPin);
swapI2C(sclPin, sdaPin);

if (I2CSCAN::isI2CExist(address)) {
if (I2CSCAN::hasDevOnBus(address)) {
m_Logger.trace("IMU %d found at address 0x%02X", sensorID, address);
} else {
sensor = new ErroneousSensor(sensorID, imuType);
Expand Down

0 comments on commit c6a90cc

Please sign in to comment.