Skip to content

Commit 8faefbe

Browse files
committed
feat: migrate i2cdev to new ESP-IDF I2C master driver API
- Replace legacy command-based I2C with handle-based master API - 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 - Maintain backward compatibility via i2cdev_legacy.c bridge - Preserve existing sensor driver API compatibility potentialy fixes #667 #713 #741
1 parent a02cd6b commit 8faefbe

File tree

7 files changed

+1908
-384
lines changed

7 files changed

+1908
-384
lines changed

components/i2cdev/.eil.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
---
21
name: i2cdev
32
description: ESP-IDF I2C master thread-safe utilities
4-
version: 1.5.0
3+
version: 2.0.0
54
groups:
65
- common
76
code_owners:
@@ -26,4 +25,4 @@ targets:
2625
license: MIT
2726
copyrights:
2827
- name: UncleRus
29-
year: 2018
28+
year: 2018

components/i2cdev/CMakeLists.txt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
if(${IDF_TARGET} STREQUAL esp8266)
2-
set(req esp8266 freertos esp_idf_lib_helpers)
1+
# ESP-IDF CMake component for i2cdev library
2+
set(req driver freertos esp_idf_lib_helpers)
3+
4+
# ESP-IDF version detection for automatic driver selection
5+
# Check for manual override via Kconfig
6+
if(CONFIG_I2CDEV_USE_LEGACY_DRIVER)
7+
set(USE_LEGACY_DRIVER TRUE)
8+
message(STATUS "i2cdev: Manual override - using legacy driver (CONFIG_I2CDEV_USE_LEGACY_DRIVER=y)")
9+
elseif(NOT DEFINED IDF_VERSION_MAJOR)
10+
# In case older ESP-IDF versions that don't define IDF_VERSION_MAJOR
11+
set(USE_LEGACY_DRIVER TRUE)
12+
message(STATUS "i2cdev: IDF_VERSION_MAJOR not defined, using legacy driver")
13+
elseif(IDF_VERSION_MAJOR LESS 5)
14+
set(USE_LEGACY_DRIVER TRUE)
15+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.x detected, using legacy driver")
16+
elseif(IDF_VERSION_MAJOR EQUAL 5 AND IDF_VERSION_MINOR LESS 3)
17+
set(USE_LEGACY_DRIVER TRUE)
18+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR} detected, using legacy driver")
319
else()
4-
set(req driver freertos esp_idf_lib_helpers)
20+
set(USE_LEGACY_DRIVER FALSE)
21+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR} detected, using new i2c_master driver")
522
endif()
623

7-
idf_component_register(
8-
SRCS i2cdev.c
9-
INCLUDE_DIRS .
10-
REQUIRES ${req}
11-
)
24+
# Conditionally set the source file based on version detection or Kconfig override
25+
if(USE_LEGACY_DRIVER)
26+
set(SRCS "i2cdev_legacy.c")
27+
message(STATUS "i2cdev: Compiling with legacy I2C driver (i2cdev_legacy.c)")
28+
else()
29+
set(SRCS "i2cdev.c")
30+
message(STATUS "i2cdev: Compiling with new I2C master driver (i2cdev.c)")
31+
endif()
32+
33+
# Register the component
34+
idf_component_register(SRCS ${SRCS}
35+
INCLUDE_DIRS "."
36+
REQUIRES ${req})
37+

components/i2cdev/Kconfig

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
menu "I2C"
22

3+
config I2CDEV_USE_LEGACY_DRIVER
4+
bool "Use Legacy I2C Driver API"
5+
default n
6+
help
7+
Select this option to use the older ESP-IDF I2C driver API (driver/i2c.h)
8+
instead of the newer driver API (driver/i2c_master.h).
9+
10+
This is automatically determined by the build system based on your ESP-IDF version.
11+
For ESP-IDF versions prior to v5.3, the legacy driver will be used automatically.
12+
You can manually override this setting if needed.
13+
14+
config I2CDEV_DEFAULT_SDA_PIN
15+
int "Default I2C SDA pin"
16+
default 21
17+
help
18+
Default SDA pin for I2C devices.
19+
20+
config I2CDEV_DEFAULT_SCL_PIN
21+
int "Default I2C SCL pin"
22+
default 22
23+
help
24+
Default SCL pin for I2C devices.
25+
26+
config I2CDEV_MAX_DEVICES_PER_PORT
27+
int "Maximum number of devices per I2C port"
28+
default 8
29+
help
30+
Maximum number of devices that can be registered on a single I2C port.
31+
332
config I2CDEV_TIMEOUT
433
int "I2C transaction timeout, milliseconds"
534
default 1000
@@ -13,5 +42,5 @@ config I2CDEV_NOLOCK
1342
drivers will become non-thread safe.
1443
Use this option if you need to access your I2C devices
1544
from interrupt handlers.
16-
45+
1746
endmenu

components/i2cdev/component.mk

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ COMPONENT_ADD_INCLUDEDIRS = .
22

33
ifdef CONFIG_IDF_TARGET_ESP8266
44
COMPONENT_DEPENDS = esp8266 freertos esp_idf_lib_helpers
5+
# ESP8266 RTOS SDK auto-detects all .c files, so use COMPONENT_OBJS to override
6+
# This prevents both i2cdev.c and i2cdev_legacy.c from being compiled
7+
COMPONENT_OBJS := i2cdev_legacy.o
8+
COMPONENT_SRCDIRS := .
59
else
610
COMPONENT_DEPENDS = driver freertos esp_idf_lib_helpers
11+
# For ESP32 family, check for manual override first
12+
ifdef CONFIG_I2CDEV_USE_LEGACY_DRIVER
13+
COMPONENT_SRCS = i2cdev_legacy.c
14+
else
15+
# Check if version variables are available, fallback to legacy if not
16+
ifdef IDF_VERSION_MAJOR
17+
ifeq ($(shell test $(IDF_VERSION_MAJOR) -lt 5 && echo 1),1)
18+
COMPONENT_SRCS = i2cdev_legacy.c
19+
else ifeq ($(shell test $(IDF_VERSION_MAJOR) -eq 5 -a $(IDF_VERSION_MINOR) -lt 3 && echo 1),1)
20+
COMPONENT_SRCS = i2cdev_legacy.c
21+
else
22+
COMPONENT_SRCS = i2cdev.c
23+
endif
24+
else
25+
# Version variables not available - fallback to legacy driver for safety
26+
COMPONENT_SRCS = i2cdev_legacy.c
27+
endif
28+
endif
729
endif

0 commit comments

Comments
 (0)