Skip to content

esp32/machine_i2c: Set default for new I2C driver at esp-idf >= v5.5.2.#19256

Open
robert-hh wants to merge 2 commits into
micropython:masterfrom
robert-hh:esp32_i2c_len0
Open

esp32/machine_i2c: Set default for new I2C driver at esp-idf >= v5.5.2.#19256
robert-hh wants to merge 2 commits into
micropython:masterfrom
robert-hh:esp32_i2c_len0

Conversation

@robert-hh

Copy link
Copy Markdown
Contributor

Summary

With v5.5.2 and up the the additional bus probing is not needed any more. It was needed because transfers to a non-existing address caused an error message to be printed instead of just returning an error code. With esp-idf v5.5.2 it returns just an error code. For 0-length writes as being used by i2c.scan(), an additional code path using i2c_master_execute_defined_operations() is needed to avoid an error message.
By default the new I2C driver is enabled for esp-idf version >= 5.5.2.

There is a slight inconsistency: reading 0 bytes from a non-existing address return an empty bytes object and does not raise an error. Writing 0 bytes to a non-existing address returns an error. That case is used by i2c.scan().

Testing

Tested with a Generic ESP32 and esp-idf v5.5.1, v5.5.2 and v5.5.4. Tested as well using the new driver with v5.5.1.
Tested V5.5.2 with Generic ESP32, ESP32S2, ESP32S3, ESP32C2, ESP32C3, ESP32C5, ESP32C6, ESP32P4. Driving an external device as I2C controller.

Trade-offs and Alternatives

There is still a problem with I2CTarget mode. But that exists back to v5.4.2 and did not get worse with v5.5.2 and v5.5.4

Generative AI

I did not use generative AI tools when creating this PR.

@robert-hh

Copy link
Copy Markdown
Contributor Author

@dpgeorge The code path for the new I2C driver still contains code to support esp-idf versions < 5.5.2. For code clarity, that can be removed and a Pre-Compiler check added that flags an error is the esp-idf version is < v5.5.2. Likewise the old driver path could get a check for esp-idf < v6.0.

@github-actions

github-actions Bot commented May 21, 2026

Copy link
Copy Markdown

Code size report:

Reference:  rp2/boards/SOLDERED_NULA_MAX_RP2350: Add Soldered NULA Max board def. [dd23554]
Comparison: esp32/machine_i2c: Restrict the new I2C driver to esp-idf >=v5.5.2. [merge of 4f9b904]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32: +6228 +0.356% ESP32_GENERIC[incl +2408(data) +32(bss)]
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

By default the new I2C driver is enabled for esp-idf version >= 5.5.2.

With v5.5.2 and up the the additional bus probing is not needed any
more. It was needed because transfers to a non-existing address caused
an error message to be printed instead of just returning an error code.
With esp-idf v5.5.2 it returns just an error code.
For 0-length writes as being used by i2c.scan(), an additional code path
using i2c_master_execute_defined_operations() is needed to avoid an
error message.

Tested with a Generic ESP32 and esp-idf v5.5.1, v5.5.2 and v5.5.4.
Tested as well using the new driver with v5.5.1.
Tested V5.5.2 with Generic ESP32, ESP32S2, ESP32S3, ESP32C2, ESP32C3,
ESP32C5, ESP32C6, ESP32P4. Driving an external device as I2C controller.

There is a slight inconsistency: reading 0 bytes from a non-existing
address return an empty bytes object and does not raise an error.
Writing 0 bytes to a non-existing address returns an error. That case
is used by i2c.scan().

Signed-off-by: robert-hh <robert@hammelrath.com>
@Josverl

Josverl commented May 26, 2026

Copy link
Copy Markdown
Contributor

That is a significant growth:

esp32: +6228 +0.356% ESP32_GENERIC

Would be good to better understand what is brought in by #include "driver/i2c_master.h"

@robert-hh

robert-hh commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

I have seen this growth, but I did not look into the reasons why. There seems to be no benefit in using the new driver. Only from esp-idf v6.0 on the old driver is not supported any more and will be removed in v7.

If the new driver is enabled for versions previous to v5.5.2, an error
is raised and compilation aborted.

Signed-off-by: robert-hh <robert@hammelrath.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants