1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

added clock stretch yield, [issue 2162] fixed twi::status (#6860)

This commit is contained in:
StanJ 2019-11-30 22:21:54 -06:00 committed by Develo
parent 22b2687a25
commit cc6d346aa5

View File

@ -22,6 +22,7 @@
#include "twi.h" #include "twi.h"
#include "pins_arduino.h" #include "pins_arduino.h"
#include "wiring_private.h" #include "wiring_private.h"
#include "PolledTimeout.h"
@ -122,9 +123,12 @@ private:
// Handle the case where a slave needs to stretch the clock with a time-limited busy wait // Handle the case where a slave needs to stretch the clock with a time-limited busy wait
inline void WAIT_CLOCK_STRETCH() inline void WAIT_CLOCK_STRETCH()
{ {
for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) esp8266::polledTimeout::oneShotFastUs timeout(twi_clockStretchLimit);
esp8266::polledTimeout::periodicFastUs yieldTimeout(5000);
while(!timeout && !SCL_READ()) // outer loop is stretch duration up to stretch limit
{ {
/* noop */ if (yieldTimeout) // inner loop yields every 5ms
yield();
} }
} }
@ -245,7 +249,7 @@ void Twi::init(unsigned char sda, unsigned char scl)
pinMode(twi_sda, INPUT_PULLUP); pinMode(twi_sda, INPUT_PULLUP);
pinMode(twi_scl, INPUT_PULLUP); pinMode(twi_scl, INPUT_PULLUP);
twi_setClock(preferred_si2c_clock); twi_setClock(preferred_si2c_clock);
twi_setClockStretchLimit(230); // default value is 230 uS twi_setClockStretchLimit(150000L); // default value is 150 mS
} }
void Twi::setAddress(uint8_t address) void Twi::setAddress(uint8_t address)
@ -444,6 +448,7 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
uint8_t Twi::status() uint8_t Twi::status()
{ {
WAIT_CLOCK_STRETCH(); // wait for a slow slave to finish
if (!SCL_READ()) if (!SCL_READ())
{ {
return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover
@ -463,11 +468,6 @@ uint8_t Twi::status()
return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits.
} }
if (!write_start())
{
return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master?
}
return I2C_OK; return I2C_OK;
} }