Skip to content

Commit c17b4bd

Browse files
authored
No glitches, handles Bus Faults
Reinit i2c hardware when bus fault (busy) is detected
1 parent aac21fe commit c17b4bd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

libraries/Wire/src/Wire.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
110110
pinMode(scl,PULLUP|OPEN_DRAIN|OUTPUT|INPUT);
111111

112112
if(!digitalRead(sda)||!digitalRead(scl)){ // bus in busy state
113-
log_d("invalid state sda=%d, scl=%d",digitalRead(sda),digitalRead(scl));
113+
// Serial.printf("invalid state sda=%d, scl=%d\n",digitalRead(sda),digitalRead(scl));
114114
digitalWrite(sda,HIGH);
115115
digitalWrite(scl,HIGH);
116116
delayMicroseconds(5);
@@ -124,17 +124,23 @@ bool TwoWire::initHardware(int sdaPin, int sclPin, uint32_t frequency){
124124
delayMicroseconds(5);
125125
digitalWrite(sda,HIGH);
126126
}
127-
128127
i2cAttachSDA(i2c, sda);
129128
i2cAttachSCL(i2c, scl);
129+
130+
if(!digitalRead(sda)||!digitalRead(scl)){ // bus in busy state
131+
// Serial.println("Bus Invalid State, TwoWire() Can't init");
132+
return false; // bus is busy
133+
}
134+
130135
return true;
131136
}
132137

133138
i2c_err_t TwoWire::processQueue(uint32_t * readCount){
134139
last_error=i2cProcQueue(i2c,readCount,_timeOutMillis);
135140
if(last_error==I2C_ERROR_BUSY){ // try to clear the bus
136-
if(initHardware(sda,scl,getClock()))
141+
if(initHardware(sda,scl,getClock())){
137142
last_error=i2cProcQueue(i2c,readCount,_timeOutMillis);
143+
}
138144
}
139145

140146
rxIndex = 0;
@@ -143,7 +149,7 @@ i2c_err_t TwoWire::processQueue(uint32_t * readCount){
143149
txQueued = 0; // the SendStop=true will restart all Queueing
144150
if(_dump){
145151
i2cDumpI2c(i2c);
146-
i2cDumpInts();
152+
i2cDumpInts(num);
147153
}
148154
i2cFreeQueue(i2c);
149155
return last_error;
@@ -230,7 +236,7 @@ return last_error;
230236
/*stickbreaker Dump i2c Interrupt buffer, i2c isr Debugging
231237
*/
232238
void TwoWire::dumpInts(){
233-
i2cDumpInts();
239+
i2cDumpInts(num);
234240
}
235241

236242
/*stickbreaker i2c isr Debugging

0 commit comments

Comments
 (0)