Post

#24: ESP32 I2C Slave Issues

If you need to use the ESP32 as an I2C slave device please read this post first. It will save you a lot of pain and troubleshooting.

If you only use the ESP32 as an I2C master, this article doesn’t apply. This is ONLY for scenarios where the ESP32 is configured as an I2C slave.

Backstory

A while back a good friend of mine, whose team is working on an ESP32 project, was experiencing some very strange I2C behavior so they logged an issue on the IDF project on Github.

It didn’t get much attention, until recently, when a community member did a deep dive and found several disturbing implementation issues. He ultimately rewrote an I2C driver from scratch which will hopefully be open-sourced.

What Are the Issues?

I definitely recommend you read the entire issue history found here:

https://github.com/espressif/esp-idf/issues/13377

A TL;DR summary of the problems when the ESP32 is an I2C slave.

  • If the ESP32 has no data for the master, a master read of the device will return random bytes.
  • Clock stretching is not properly implemented
  • Buffering of data is not properly implemented

These issues are not present when the ESP32 is the master device on an I2C bus.

Production Pointers

  • At this point the native I2C driver should not be relied upon when used in a slave configuration unless you have done exhaustive testing of every production scenario
  • Consider using the Arduino ESP32 I2C implementation
  • Subscribe to notifications on Issue 13377 to get updates on a possible alternative implementation or fix

Summary

As of this writing the native I2C driver from Espressif in IDF appears to have several fundamental implementation flaws that can create very-hard-to-diagnose bugs. If you are using it you should exhaustively test your product to make sure the behaviors mentioned in the issue above do not affect you. Hopefully Espressif will address the problems and issue an update soon.

This type of bug ties into the Deterministic Pillar of Production because the master can read random data from the device. This random data could be misinterpreted as valid data, breaking your product in unexpected ways.

Join the community and get the weekly Production ESP32 newsletter. Concise, relevant content about ESP32 development right in your inbox.

© Kevin Sidwar

Comments powered by Disqus.