Skip to content

Commit c5c8580

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 c5c8580

File tree

7 files changed

+1932
-381
lines changed

7 files changed

+1932
-381
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: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,43 @@ else()
44
set(req driver freertos esp_idf_lib_helpers)
55
endif()
66

7-
idf_component_register(
8-
SRCS i2cdev.c
9-
INCLUDE_DIRS .
10-
REQUIRES ${req}
11-
)
7+
# ESP-IDF version detection for automatic driver selection
8+
# Check for manual override via Kconfig
9+
if(CONFIG_I2CDEV_USE_LEGACY_DRIVER)
10+
set(USE_LEGACY_DRIVER TRUE)
11+
message(STATUS "i2cdev: Manual override - using legacy driver (CONFIG_I2CDEV_USE_LEGACY_DRIVER=y)")
12+
elseif(NOT DEFINED IDF_VERSION_MAJOR)
13+
# In case older ESP-IDF versions that don't define IDF_VERSION_MAJOR
14+
set(USE_LEGACY_DRIVER TRUE)
15+
message(STATUS "i2cdev: IDF_VERSION_MAJOR not defined, using legacy driver")
16+
elseif(IDF_VERSION_MAJOR LESS 5)
17+
set(USE_LEGACY_DRIVER TRUE)
18+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.x detected, using legacy driver")
19+
elseif(IDF_VERSION_MAJOR EQUAL 5 AND IDF_VERSION_MINOR LESS 3)
20+
set(USE_LEGACY_DRIVER TRUE)
21+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR} detected, using legacy driver")
22+
else()
23+
set(USE_LEGACY_DRIVER FALSE)
24+
message(STATUS "i2cdev: ESP-IDF v${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR} detected, using new i2c_master driver")
25+
endif()
26+
27+
# Conditionally set the source file based on version detection or Kconfig override
28+
if(USE_LEGACY_DRIVER)
29+
set(SRCS "i2cdev_legacy.c")
30+
message(STATUS "i2cdev: Compiling with legacy I2C driver (i2cdev_legacy.c)")
31+
else()
32+
set(SRCS "i2cdev.c")
33+
message(STATUS "i2cdev: Compiling with new I2C master driver (i2cdev.c)")
34+
endif()
35+
36+
# Register the component
37+
idf_component_register(SRCS ${SRCS}
38+
INCLUDE_DIRS "."
39+
REQUIRES ${req})
40+
41+
# Register tests if enabled
42+
#if(INCLUDE_TESTS OR "$ENV{TEST_COMPONENTS}" STREQUAL "*" OR "$ENV{TEST_COMPONENTS}" STREQUAL "i2cdev")
43+
# Include the test directory
44+
#add_subdirectory(test)
45+
#endif()
46+

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ COMPONENT_ADD_INCLUDEDIRS = .
22

33
ifdef CONFIG_IDF_TARGET_ESP8266
44
COMPONENT_DEPENDS = esp8266 freertos esp_idf_lib_helpers
5+
# ESP8266 always uses legacy driver
6+
COMPONENT_SRCS = i2cdev_legacy.c
57
else
68
COMPONENT_DEPENDS = driver freertos esp_idf_lib_helpers
9+
# For ESP32 family, check for manual override first
10+
ifdef CONFIG_I2CDEV_USE_LEGACY_DRIVER
11+
COMPONENT_SRCS = i2cdev_legacy.c
12+
else
13+
# Check if version variables are available, fallback to legacy if not
14+
ifdef IDF_VERSION_MAJOR
15+
ifeq ($(shell test $(IDF_VERSION_MAJOR) -lt 5 && echo 1),1)
16+
COMPONENT_SRCS = i2cdev_legacy.c
17+
else ifeq ($(shell test $(IDF_VERSION_MAJOR) -eq 5 -a $(IDF_VERSION_MINOR) -lt 3 && echo 1),1)
18+
COMPONENT_SRCS = i2cdev_legacy.c
19+
else
20+
COMPONENT_SRCS = i2cdev.c
21+
endif
22+
else
23+
# Version variables not available - fallback to legacy driver for safety
24+
COMPONENT_SRCS = i2cdev_legacy.c
25+
endif
26+
endif
727
endif

0 commit comments

Comments
 (0)