Skip to content

Commit

Permalink
Re-add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kounocom committed Jan 19, 2025
1 parent 20520c0 commit ffca264
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/i2cscan/i2cscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ namespace I2CSCAN
#if ESP32
Wire.end();
#endif
// Skip excluded SDA ports
while (inArray(portArray[currentSDA], portExclude, sizeof(portExclude))) {
currentSDA++;
if (currentSDA >= sizeof(portArray)) {
scanState = ScanState::DONE;
return;
}
}
// Skip excluded SCL ports
while (inArray(portArray[currentSCL], portExclude, sizeof(portExclude))) {
currentSCL++;
if (currentSCL >= sizeof(portArray)) {
Expand Down Expand Up @@ -95,11 +97,13 @@ namespace I2CSCAN
if (currentAddress >= 127) {
currentAddress = 1;
currentSCL++;
// Skip excluded SCL ports
while (inArray(portArray[currentSCL], portExclude, sizeof(portExclude))) {
currentSCL++;
if (currentSCL >= sizeof(portArray)) {
currentSCL = 0;
currentSDA++;
// Skip excluded SDA ports
while (inArray(portArray[currentSDA], portExclude, sizeof(portExclude))) {
currentSDA++;
if (currentSDA >= sizeof(portArray)) {
Expand Down Expand Up @@ -139,6 +143,7 @@ namespace I2CSCAN
do {
#endif
Wire.beginTransmission(addr);
// The return value of endTransmission is used to determine if a device is present
error = Wire.endTransmission();
#if ESP32C3
}
Expand All @@ -149,21 +154,37 @@ namespace I2CSCAN
return false;
}

/**
* This routine turns off the I2C bus and clears it
* on return SCA and SCL pins are tri-state inputs.
* You need to call Wire.begin() after this to re-enable I2C
* This routine does NOT use the Wire library at all.
*
* returns 0 if bus cleared
* 1 if SCL held low.
* 2 if SDA held low by slave clock stretch for > 2sec
* 3 if SDA held low after 20 clocks.
* From: http://www.forward.com.au/pfod/ArduinoProgramming/I2C_ClearBus/index.html
* (c)2014 Forward Computing and Control Pty. Ltd.
* NSW Australia, www.forward.com.au
* This code may be freely used for both private and commerical use
*/

int clearBus(uint8_t SDA, uint8_t SCL) {
#if defined(TWCR) && defined(TWEN)
TWCR &= ~(_BV(TWEN));
TWCR &= ~(_BV(TWEN)); // Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directly
#endif

pinMode(SDA, INPUT_PULLUP);
pinMode(SCL, INPUT_PULLUP);

boolean SCL_LOW = (digitalRead(SCL) == LOW);
if (SCL_LOW) {
return 1;
return 1; // I2C bus error. Could not clear SCL, clock line held low.
}

boolean SDA_LOW = (digitalRead(SDA) == LOW);
int clockCount = 20;
int clockCount = 20; // > 2x9 clock

while (SDA_LOW && (clockCount > 0)) {
clockCount--;
Expand Down

0 comments on commit ffca264

Please sign in to comment.