HAL_I2C bug?

gerardy
Posts: 3
Joined: Wed Nov 15, 2017 7:49 pm

HAL_I2C bug?

Postby gerardy » Wed Nov 15, 2017 8:06 pm

Using arduino environment with default I2C pin and an I2C LCD display and i2c library LiquidCrystal_I2C_ID1574

The write sequence for 1 byte to the display is
- write first nibble with lCD "enable"off
- write first nibble with lCD "enable"on
- write first nibble with lCD "enable"off

Then same sequence for second nibble.

The sequence of 3 writes for each nibble is quite fast


When using the standard liquid cristal, the I2C hardware fails
The code for standard library is as follows:
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
Wire.beginTransmission(_Addr);
printIIC((int)(_data) | _backlightval);
Wire.endTransmission();
}

if modifyng the liquid crystal lib by adding a delay, the I2C works fine
Modified lib:
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
Wire.beginTransmission(_Addr);
printIIC((int)(_data) | _backlightval);
Wire.endTransmission();
// GY endtransmission/I2C seem to return before completion
delay(1);
//gy
}


It looks like the i2cWrite from the esp32 Hal i2c library completes prematurily.

Rgds
Gerard

StimpyYourCat
Posts: 1
Joined: Mon Nov 13, 2017 5:24 pm

Re: HAL_I2C bug?

Postby StimpyYourCat » Mon Nov 20, 2017 4:57 am

Hello Gerard,

It may indeed be a software bug but before we can conclude that I should ask: Have you soldered external pullup resistors between the ESP32 SDA pin and 3v3 bus, and another resistor between SCL pin and 3v3 bus? On my ESP32 dev board these pins are defined in Arduino Wire library as SDA --> GPIO21 and SCL --> GPIO22.

The I2C specification is that SDA and SCL pins are open drain, so they are activated by being pulled low to ground. In the steady state which is high, they need pullup resistors to Vcc which in our case is 3.3 Volts for the ESP32. Lack of pullup resistors and sometimes reliance on a microcontroller's internal pullup resistors alone, means the signals are floating and this can lead to timing problems and also outright data glitches in I2C. Whereas with the external pullup resistors on the 2 signals, I2C is very reliable protocol.

Per the I2C spec you should use 4K7 ohms as starting values for the pullup resistors. Texas Instruments has a white paper that details when to change to other values of resistor but 4K7 works most of the time for most loads. Some of the pins in ESP32 can be configured to use internal pullup resistors but I do not know if these 2 particular pins do, or if the software is configuring them as pullup resistors. Anyway I always hard wire external pullup resistors when using I2C. Mine I am using the DS3231 realtime clock and also a HTU-21D temperatures & humidity sensor on the same I2C bus with those pins. I was relying on internal pullup just to write the code knowing that you are supposed to use external pullups. It would work for a while but then get bad reads on both devices. After installing the 2 pullup resistors as I usually do, everything is stable.

So I would verify the hardward solution first, just a suggestion. Best of luck to you.

gerardy
Posts: 3
Joined: Wed Nov 15, 2017 7:49 pm

Re: HAL_I2C bug?

Postby gerardy » Mon Nov 20, 2017 4:08 pm

Thank you for your interest in my problem.
Yes I do have pull up resistors to 3.3V on both SDA and SCK. Looking at SDA and SCK signals with a logic analizer, the signals level looks good. I did not had a chance to look at them with an oscilloscope.

Regards
Gerard

Who is online

Users browsing this forum: No registered users and 83 guests