Service
|
Let Digole Serial Display work with ESP8266 at I2CPosted at:2017-01-12 13:54:56 Edited at:2020-01-09 01:59:57
|
The ESP8266 doesn't have hardware I2C port, the lib for Arduino used software I2C to accomlish the function, this is a limitation (not a bug) in the lib when reading data from slave: Some user give a solution by using: static bool twi_read_bit(void) { twi_delay(twi_dcount+2);
SCL_HIGH();
while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching
bool bit = SDA_READ();
twi_delay(twi_dcount);
return bit;
}
You can see the code: "while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);", it just wait a limited time to slave to prepare the data, so we need to change it wait until the slave ready, and doesn't matter how long, as the following code:
while (SCL_READ() == 0) yield(); After this changing, the ESP8266 will work with any kind of "slow" I2C slave then!
The new function should like this: static bool twi_read_bit(void) {
uint32_t i = 0;
SCL_LOW();
SDA_HIGH();
twi_delay(twi_dcount+2);
SCL_HIGH();
// while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching
while (SCL_READ() == 0) yield();
bool bit = SDA_READ();
twi_delay(twi_dcount);
return bit;
}
If you think this solution help you any way, please follow up with a "Thanks!" : )
UPDATED: 2020/01/08 For new version of Arduino, seach for "core_esp8266_si2c.cpp"---on Mac OS, it is at a HIDEN folder name "Library", if you can't see the folder, Google "show hiden file on Mac". The full path: ~/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.2/cores/esp8266 Search function of "void WAIT_CLOCK_STRETCH" in file, and change it to:
inline void WAIT_CLOCK_STRETCH()
{
while(!SCL_READ())
yield();
// for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++)
// {
// }
}
This change will let the read data function waiting unlimited time with slow I2V slave device. |