Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double I2C read in one transaction skips a clock pulse (#5528) #6654

Merged
merged 9 commits into from
Oct 27, 2019
33 changes: 25 additions & 8 deletions cores/esp8266/core_esp8266_si2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class Twi
}
}

// 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);
Expand Down Expand Up @@ -386,13 +388,17 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned
{
write_stop();
}
else
{
twi_scl_valley();
WAIT_CLOCK_STRETCH();
// 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;
Expand Down Expand Up @@ -422,13 +428,17 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
{
write_stop();
}
else
{
twi_scl_valley();
WAIT_CLOCK_STRETCH();
// 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();
earlephilhower marked this conversation as resolved.
Show resolved Hide resolved
twi_scl_valley();
busywait(twi_dcount);
}
return 0;
Expand Down Expand Up @@ -649,6 +659,13 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
}
}

void Twi::twi_scl_valley(void)
{
SCL_LOW();
busywait(twi_dcount);
SCL_HIGH();
}

void ICACHE_RAM_ATTR Twi::onTimer(void *unused)
{
(void)unused;
Expand Down