esp8266 / esp8266/Arduino

ESP8266 TWI/I2C Master write: No delay between SCL going Low and SDA change.

Open
#8,964 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

### Basic Infos

- [x ] This issue complies with the [issue POLICY doc](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [ x] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
- [ x] I have tested that the issue is present in current master branch (aka latest git).
- [ x] I have searched the issue tracker for a similar issue.
- [ -] If there is a stack dump, I have decoded it.
- [ x] I have filled out all fields below.

#### Platform

- Hardware: LOLIN(WEMOS) D1 R2 & mini(esp8266_d1_mini), Platform=esp8266, Package=esp8266
- Core Version: 3.1.2
- Development Env: VisualStudio+VisualMicro
- Operating System: Windows10

### Settings in IDE

- Module: LOLIN(WEMOS) D1 R2 & mini
- Flash Mode: qio
- Flash Size: 4MB
- lwip Variant: v2 Lower Memory
- Reset Method: nodemcu
- Flash Frequency: 40Mhz
- CPU Frequency: 80Mhz
- Upload Using: SERIAL
- Upload Speed: 921600

### Problem Description

There is no delay between SCL going Low and SDA change in core_esp8266_si2c.cpp line 311.
The result is a transition in only 125ns which may result in wrong data read or START/STOP conditions.
It happens often with slow devices and long lines with high capacitance.
To fix it, we only need an additional busywait (twi_dcount); after setting SCL low.

```c++
bool Twi::write_bit(bool bit)
{
SCL_LOW(twi_scl);
if (bit)
{
SDA_HIGH(twi_sda);
}
else
{
SDA_LOW(twi_sda);
}
busywait(twi_dcount + 1);
SCL_HIGH(twi_scl);
WAIT_CLOCK_STRETCH();
busywait(twi_dcount);
return true;
}
```

but should be

```c++
bool Twi::write_bit(bool bit)
{
SCL_LOW(twi_scl);
busywait (twi_dcount);
if (bit)
{
SDA_HIGH(twi_sda);
}
else
{
SDA_LOW(twi_sda);
}
busywait(twi_dcount + 1);
SCL_HIGH(twi_scl);
WAIT_CLOCK_STRETCH();
busywait(twi_dcount);
return true;
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.