Skip to content

Commit 75ec40f

Browse files
authored
Merge pull request #707 from quinkq/feature-i2cdev-i2c-master-support
feat: migrate i2cdev to new ESP-IDF I2C master driver API
2 parents a02cd6b + 9175b81 commit 75ec40f

File tree

7 files changed

+1910
-342
lines changed

7 files changed

+1910
-342
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: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
1-
menu "I2C"
1+
menu "I2C Device Library"
2+
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_AUTO_ENABLE_PULLUPS
15+
bool "Automatically enable internal I2C pullups when not configured"
16+
default n
17+
depends on !IDF_TARGET_ESP8266
18+
help
19+
When enabled, internal pullup resistors are automatically enabled
20+
when both sda_pullup_en and scl_pullup_en are false (default state).
21+
22+
Useful for development and prototyping. Disable for production
23+
systems with external pullups to avoid interference.
24+
25+
Considerations:
26+
- May increase power consumption slightly
27+
- Could interfere with carefully tuned external pullups
28+
- Not recommended for battery-powered applications
29+
30+
Note: This option only affects the modern i2cdev driver (ESP32 family).
31+
Legacy driver behavior is unchanged for compatibility.
32+
33+
config I2CDEV_DEFAULT_SDA_PIN
34+
int "Default I2C SDA pin"
35+
default 21
36+
help
37+
Default SDA pin for I2C devices.
38+
39+
config I2CDEV_DEFAULT_SCL_PIN
40+
int "Default I2C SCL pin"
41+
default 22
42+
help
43+
Default SCL pin for I2C devices.
44+
45+
config I2CDEV_MAX_DEVICES_PER_PORT
46+
int "Maximum number of devices per I2C port"
47+
default 8
48+
help
49+
Maximum number of devices that can be registered on a single I2C port.
250

351
config I2CDEV_TIMEOUT
452
int "I2C transaction timeout, milliseconds"
@@ -13,5 +61,5 @@ config I2CDEV_NOLOCK
1361
drivers will become non-thread safe.
1462
Use this option if you need to access your I2C devices
1563
from interrupt handlers.
16-
64+
1765
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)