esp-idf-lib icon indicating copy to clipboard operation
esp-idf-lib copied to clipboard

feat: migrate i2cdev to new ESP-IDF I2C master driver API

Open quinkq opened this issue 8 months ago • 5 comments

Summary

Migrate i2cdev to new ESP-IDF I2C master driver API (v5.3+)

Changes Made

  • Replace legacy command-based I2C with handle-based master API for ESP-IDF v5.3 and newer.
  • Add device handle tracking and automatic resource cleanup
  • Enhance addressing support (7-bit/10-bit) and error handling
  • Implement smart retry logic with exponential backoff
  • Add buffer overflow protection and memory optimization
  • Full Backward Compatibility

Compatibility

  • Drop-in replacement for existing sensor drivers
  • Legacy compatibility maintained in i2cdev_legacy.c
  • Automated Build-Time Driver Selection - build system automatically detects the ESP-IDF version and selects the appropriate driver (modern or legacy).
  • Manual Override: A Kconfig option (I2CDEV_USE_LEGACY_DRIVER) to force the use of the legacy driver on any platform.
  • Same public API signatures preserved

Testing

  • Tested in two configurations where devices shared a single I2C bus: 1. ESP32-C3 with SHT40/BME280 & 2. ESP32-S3 with SHT40, BME280, 2x ADS1115, INA219, and AS5600 (own esp-idf-lib compatible driver).
  • Verified backward compatibility by successfully toggling I2CDEV_USE_LEGACY_DRIVER in Kconfig and by compiling for ESP8266.

FIXES #667

quinkq avatar Jun 09 '25 15:06 quinkq

Hi @quinkq

I just tested it with both idf-5.2 and idf-5.4 branchs. CONFIG_I2CDEV_USE_LEGACY_DRIVER=n, so the IDF version decides to use new or legacy driver. On idf-5.2, it works. On idf-5.4, it does not work: W (1672) i2cdev: [0x44 at 0] Cannot probe - bus not ready on port 0 W (1679) i2cdev: [0x45 at 0] Cannot probe - bus not ready on port 0 Do you have any idea what could be wrong? Or any application code change is required for new driver?

Thanks, Axel

AxelLin avatar Jun 15 '25 04:06 AxelLin

Hi @AxelLin Thanks for testing, issue was that when it switched to run modern version of the driver (i2c_master) in idf-5.4, i2c_dev_check_present (modern version of i2c_dev_probe) wasn't calling i2c_setup_port before probing, unlike other I2C functions. Should be fixed now.

quinkq avatar Jun 15 '25 20:06 quinkq

Hi @quinkq

Thank you for the fix. Confirm it's working now with idf-5.4.

Regards, Axel

AxelLin avatar Jun 16 '25 01:06 AxelLin

@quinkq Thanks for the proposal. I will certainly have a close look.

trombik avatar Jun 16 '25 02:06 trombik

Also, please have a look at my proposal and have your voice heard.

trombik avatar Jun 16 '25 02:06 trombik

Hi @trombik Do you have target timeline to merge this PR?

AxelLin avatar Jun 30 '25 00:06 AxelLin

@AxelLin Sorry for my inactivity. I'll take a look and merge it ASAP.

trombik avatar Jul 06 '25 09:07 trombik

Just a brief comment about the PR: I can see how you devoted your time and you I appreciate it.

trombik avatar Jul 06 '25 11:07 trombik

@AxelLin Merged, thanks!

trombik avatar Jul 10 '25 12:07 trombik

Forgot to mention @quinkq. Thank you, too.

trombik avatar Jul 10 '25 12:07 trombik