Skip to content

Fix LP_I2C_NUM_0 support: use LP_I2C_SCLK_DEFAULT clock source#13

Open
lptr wants to merge 1 commit into
esp-idf-lib:mainfrom
cornucopia-machines:fix/lp-i2c-clock-source
Open

Fix LP_I2C_NUM_0 support: use LP_I2C_SCLK_DEFAULT clock source#13
lptr wants to merge 1 commit into
esp-idf-lib:mainfrom
cornucopia-machines:fix/lp-i2c-clock-source

Conversation

@lptr

@lptr lptr commented May 27, 2026

Copy link
Copy Markdown

Summary

Fixes #12.

i2c_setup_port hardcoded .clk_source = I2C_CLK_SRC_DEFAULT in the
i2c_master_bus_config_t initializer. For LP_I2C_NUM_0 this is the wrong
value: clk_source and lp_source_clk are union members in the struct, and
LP_I2C_SCLK_DEFAULT is not a valid i2c_clock_source_t — passing
I2C_CLK_SRC_DEFAULT for an LP port causes i2c_new_master_bus to return
ESP_ERR_NOT_SUPPORTED.

The fix detects the LP port and sets LP_I2C_SCLK_DEFAULT, guarded by
#if SOC_LP_I2C_SUPPORTED so it is a no-op on chips without LP_I2C.

Testing

Validated on ESP32-C6 (the only current ESP-IDF target with SOC_LP_I2C_SUPPORTED):
a firmware project using i2cdev devices (INA219, BQ27220) on LP_I2C_NUM_0
(GPIO6/GPIO7) now initialises correctly. Previously the first i2cdev transaction
on the LP port returned ESP_ERR_NOT_SUPPORTED.

HP I2C behaviour on ESP32-C6 and ESP32-S3 is unchanged — the else branch
keeps I2C_CLK_SRC_DEFAULT for all HP ports, and the #if SOC_LP_I2C_SUPPORTED
guard means the new code is not compiled on chips without LP_I2C.

Notes

  • The official ESP-IDF example for LP_I2C also uses .clk_source = LP_I2C_SCLK_DEFAULT
    (not the .lp_source_clk field name) — both field names work because they are
    union members, but the value is what matters.
  • See also the ESP-IDF component manager issue
    for context on why we ended up pinning the fork as a submodule while waiting for
    this PR to land.

i2c_setup_port always passed I2C_CLK_SRC_DEFAULT to i2c_new_master_bus,
which returns ESP_ERR_NOT_SUPPORTED for LP_I2C_NUM_0. LP I2C requires
LP_I2C_SCLK_DEFAULT instead.

clk_source and lp_source_clk are union members in i2c_master_bus_config_t
sharing the same memory, but LP_I2C_SCLK_DEFAULT is not a valid
i2c_clock_source_t value so the driver rejects the HP default when given
an LP port number.

Fix: set clk_source = LP_I2C_SCLK_DEFAULT when dev->port == LP_I2C_NUM_0,
guarded by SOC_LP_I2C_SUPPORTED so it compiles cleanly on chips without
an LP I2C peripheral. All HP bus behaviour is unchanged.

Fixes: esp-idf-lib#12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LP_I2C_NUM_0 not supported: hardcoded clk_source causes ESP_ERR_NOT_SUPPORTED

1 participant