Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve I2C bus recovery of Interrupted READ cycle #1767

Merged
merged 1 commit into from
Aug 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions cores/esp32/esp32-hal-i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,33 +1350,32 @@ static void i2cReleaseISR(i2c_t * i2c)
}

static bool i2cCheckLineState(int8_t sda, int8_t scl){
if(sda < 0 || scl < 0){
return true;//return true since there is nothing to do
if(sda < 0 || scl < 0){
return false;//return false since there is nothing to do
}
// if the bus is not 'clear' try the recommended recovery sequence, START, 9 Clocks, STOP
// if the bus is not 'clear' try the cycling SCL until SDA goes High or 9 cycles
digitalWrite(sda, HIGH);
digitalWrite(scl, HIGH);
pinMode(sda, PULLUP|OPEN_DRAIN|OUTPUT|INPUT);
pinMode(scl, PULLUP|OPEN_DRAIN|OUTPUT|INPUT);
pinMode(sda, PULLUP|OPEN_DRAIN|INPUT);
pinMode(scl, PULLUP|OPEN_DRAIN|OUTPUT);

if(!digitalRead(sda) || !digitalRead(scl)) { // bus in busy state
log_w("invalid state sda=%d, scl=%d\n", digitalRead(sda), digitalRead(scl));
digitalWrite(sda, HIGH);
log_w("invalid state sda(%d)=%d, scl(%d)=%d", sda, digitalRead(sda), scl, digitalRead(scl));
digitalWrite(scl, HIGH);
delayMicroseconds(5);
digitalWrite(sda, LOW);
for(uint8_t a=0; a<9; a++) {
delayMicroseconds(5);
digitalWrite(scl, LOW);
delayMicroseconds(5);
digitalWrite(scl, HIGH);
if(digitalRead(sda)){ // bus recovered
log_d("Recovered after %d Cycles",a+1);
break;
}
}
delayMicroseconds(5);
digitalWrite(sda, HIGH);
}

if(!digitalRead(sda) || !digitalRead(scl)) { // bus in busy state
log_e("Bus Invalid State, TwoWire() Can't init");
log_e("Bus Invalid State, TwoWire() Can't init sda=%d, scl=%d",digitalRead(sda),digitalRead(scl));
return false; // bus is busy
}
return true;
Expand Down