feat: migrate i2cdev to new ESP-IDF I2C master driver API
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
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
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.
Hi @quinkq
Thank you for the fix. Confirm it's working now with idf-5.4.
Regards, Axel
@quinkq Thanks for the proposal. I will certainly have a close look.
Also, please have a look at my proposal and have your voice heard.
Hi @trombik Do you have target timeline to merge this PR?
@AxelLin Sorry for my inactivity. I'll take a look and merge it ASAP.
Just a brief comment about the PR: I can see how you devoted your time and you I appreciate it.
@AxelLin Merged, thanks!
Forgot to mention @quinkq. Thank you, too.