Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void TwoWire::begin(uint8_t address, bool generalCall)

_i2c.generalCall = (generalCall == true) ? 1 : 0;

recoverBus(); // in case I2C bus (device) is stuck after a reset for example

i2c_custom_init(&_i2c, 100000, I2C_ADDRESSINGMODE_7BIT, ownAddress);

if (_i2c.isMaster == 0) {
Expand Down Expand Up @@ -501,6 +503,28 @@ inline void TwoWire::resetTxBuffer(void)
}
}

// Send clear bus (clock pulse) sequence to recover bus.
// Useful in case of bus stuck after a reset for example
// a mix implementation of Clear Bus from
// https://www.nxp.com/docs/en/user-guide/UM10204.pdf
// https://bits4device.wordpress.com/2017/07/28/i2c-bus-recovery/
void TwoWire::recoverBus(void)
{
pinMode(pinNametoDigitalPin(_i2c.sda), INPUT);

if (digitalReadFast(_i2c.sda) == LOW) {
pinMode(pinNametoDigitalPin(_i2c.scl), OUTPUT);

for (int i = 0; i < 20; i++) {
digitalWriteFast(_i2c.scl, LOW);
delayMicroseconds(10);
digitalWriteFast(_i2c.scl, HIGH);
delayMicroseconds(10);
}
pinMode(pinNametoDigitalPin(_i2c.scl), INPUT);
}
}

// Preinstantiate Objects //////////////////////////////////////////////////////

TwoWire Wire = TwoWire(); //D14-D15
1 change: 1 addition & 0 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TwoWire : public Stream {

void resetRxBuffer(void);
void resetTxBuffer(void);
void recoverBus(void);

public:
TwoWire();
Expand Down