diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 7200f6315..281b19680 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -128,6 +128,8 @@ private: } } + // Generate a clock "valley" (at the end of a segment, just before a repeated start) + void twi_scl_valley(void); public: void setClock(unsigned int freq); @@ -386,13 +388,16 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned { write_stop(); } + else + { + twi_scl_valley(); + // TD-er: Also busywait(twi_dcount) here? + // busywait(twi_dcount); + } i = 0; while (!SDA_READ() && (i++) < 10) { - SCL_LOW(); - busywait(twi_dcount); - SCL_HIGH(); - WAIT_CLOCK_STRETCH(); + twi_scl_valley(); busywait(twi_dcount); } return 0; @@ -422,13 +427,16 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned { write_stop(); } + else + { + twi_scl_valley(); + // TD-er: Also busywait(twi_dcount) here? + // busywait(twi_dcount); + } i = 0; while (!SDA_READ() && (i++) < 10) { - SCL_LOW(); - busywait(twi_dcount); - SCL_HIGH(); - WAIT_CLOCK_STRETCH(); + twi_scl_valley(); busywait(twi_dcount); } return 0; @@ -649,6 +657,14 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) } } +void Twi::twi_scl_valley(void) +{ + SCL_LOW(); + busywait(twi_dcount); + SCL_HIGH(); + WAIT_CLOCK_STRETCH(); +} + void ICACHE_RAM_ATTR Twi::onTimer(void *unused) { (void)unused;