Skip to content

Implement new I2C driver#531

Merged
KenVanHoeylandt merged 2 commits into
mainfrom
esp32-i2c-master
Jun 12, 2026
Merged

Implement new I2C driver#531
KenVanHoeylandt merged 2 commits into
mainfrom
esp32-i2c-master

Conversation

@KenVanHoeylandt

@KenVanHoeylandt KenVanHoeylandt commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes

    • Resolved I2C driver conflicts improving Tab5 keyboard detection and responsiveness.
    • Fixed RGB LED and register access reliability for the keyboard.
  • New Features

    • Added support for a low-power I2C master controller on ESP32 to improve peripheral compatibility.
  • Refactor

    • Reworked keyboard I2C integration and device startup for more robust initialization and probing.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2b574330-6734-431c-8362-9bfc60160261

📥 Commits

Reviewing files that changed from the base of the PR and between 7baf809 and 38f92a6.

📒 Files selected for processing (6)
  • Devices/m5stack-tab5/Source/devices/Display.cpp
  • Platforms/platform-esp32/source/drivers/esp32_i2c.cpp
  • Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp
  • TactilityKernel/include/tactility/drivers/gpio.h
  • TactilityKernel/include/tactility/drivers/i2c_controller.h
  • TactilityKernel/source/drivers/i2c_controller.cpp

📝 Walkthrough

Walkthrough

This PR adds a new esp32_i2c_master driver and devicetree/binding types, updates platform/module build wiring and SDK defaults, extends the I2C controller API with an optional probe callback, and refactors Tab5Keyboard to accept a ::Device* I2C controller and use i2c_controller_* register/read/write/probe routines instead of direct ESP-IDF i2c_master calls. Device-tree now declares a low-power I2C bus for the keyboard and per-board properties were consolidated.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Implement new I2C driver' directly summarizes the main objective of this changeset, which introduces a new ESP32 I2C master driver across multiple files and modules.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch esp32-i2c-master

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
Platforms/platform-esp32/source/module.cpp (1)

78-86: ⚠️ Potential issue | 🔴 Critical | 🏗️ Heavy lift

Critical: Driver teardown order violates dependency chain.

In stop(), drivers are removed in the SAME order as they were constructed in start(), rather than reverse order. This violates the dependency relationship where esp32_i2c_master_driver depends on esp32_gpio_driver (it acquires GPIO descriptors during start() and releases them during stop()).

Currently:

  • Line 78 removes esp32_gpio_driver
  • Line 80 removes esp32_i2c_master_driver

When esp32_i2c_master_driver.stop() executes at line 80, it calls gpio_descriptor_release() (line 230-231 in esp32_i2c_master.cpp), but the GPIO driver has already been destructed at line 78. This could cause crashes or undefined behavior.

Drivers must be torn down in reverse construction order.

🔧 Proposed fix: reverse teardown order for core drivers
 static error_t stop() {
     /* We crash when destruct fails, because if a single driver fails to destruct,
      * there is no guarantee that the previously destroyed drivers can be recovered */
 `#if` SOC_USB_OTG_SUPPORTED
     check(driver_remove_destruct(&esp32_usbhost_msc_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_usbhost_midi_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_usbhost_hid_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_usbhost_driver) == ERROR_NONE);
 `#endif`
 `#if` defined(CONFIG_BT_NIMBLE_ENABLED)
     check(driver_remove_destruct(&esp32_ble_hid_device_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_ble_midi_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_ble_serial_driver) == ERROR_NONE);
     check(driver_remove_destruct(&esp32_bluetooth_driver) == ERROR_NONE);
 `#endif`
-    check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE);
-    check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
-    check(driver_remove_destruct(&esp32_i2c_master_driver) == ERROR_NONE);
-    check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_uart_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_spi_driver) == ERROR_NONE);
 `#if` SOC_SDMMC_HOST_SUPPORTED
     check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE);
 `#endif`
-    check(driver_remove_destruct(&esp32_spi_driver) == ERROR_NONE);
-    check(driver_remove_destruct(&esp32_uart_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_i2c_master_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
+    check(driver_remove_destruct(&esp32_gpio_driver) == ERROR_NONE);
     return ERROR_NONE;
 }
🧹 Nitpick comments (1)
Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp (1)

163-163: Clarify GPIO_FLAG_PULL_UP semantics for ESP32 I2C internal pull-ups

esp32_i2c_master.cpp uses a single ESP-IDF bus flag i2c_master_bus_config_t.flags.enable_internal_pullup, which enables internal pull-ups for both SDA and SCL simultaneously. That matches the current logic:

.enable_internal_pullup = ((sda_flags & GPIO_FLAG_PULL_UP) != 0) || ((scl_flags & GPIO_FLAG_PULL_UP) != 0),

In this repo’s device trees, GPIO_FLAG_PULL_UP is currently specified together for both pins (e.g. Devices/m5stack-tab5/m5stack,tab5.dts sets both pin-sda and pin-scl to GPIO_FLAG_PULL_UP), so there’s no existing “SDA-only vs SCL-only” mismatch.

If the intended meaning of GPIO_FLAG_PULL_UP is “per-pin” (rather than “request bus pull-ups”), document that the DT flag is treated as a bus-level request and/or consider switching to an AND policy (sda_flags && scl_flags) to require both pins be marked.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4e11aac1-bcaf-4a2b-acd3-4f990fa07366

📥 Commits

Reviewing files that changed from the base of the PR and between 543390a and 7baf809.

📒 Files selected for processing (13)
  • Buildscripts/sdkconfig/default.properties
  • Devices/m5stack-tab5/Source/Configuration.cpp
  • Devices/m5stack-tab5/Source/devices/Tab5Keyboard.cpp
  • Devices/m5stack-tab5/Source/devices/Tab5Keyboard.h
  • Devices/m5stack-tab5/device.properties
  • Devices/m5stack-tab5/m5stack,tab5.dts
  • Platforms/platform-esp32/CMakeLists.txt
  • Platforms/platform-esp32/bindings/espressif,esp32-i2c-master.yaml
  • Platforms/platform-esp32/bindings/espressif,esp32-i2c.yaml
  • Platforms/platform-esp32/include/tactility/bindings/esp32_i2c_master.h
  • Platforms/platform-esp32/include/tactility/drivers/esp32_i2c_master.h
  • Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp
  • Platforms/platform-esp32/source/module.cpp
💤 Files with no reviewable changes (1)
  • Devices/m5stack-tab5/device.properties

Comment thread Devices/m5stack-tab5/Source/Configuration.cpp
Comment thread Devices/m5stack-tab5/Source/devices/Tab5Keyboard.cpp
Comment thread Platforms/platform-esp32/source/drivers/esp32_i2c_master.cpp
@KenVanHoeylandt KenVanHoeylandt merged commit 8dabda2 into main Jun 12, 2026
58 checks passed
@KenVanHoeylandt KenVanHoeylandt deleted the esp32-i2c-master branch June 12, 2026 16:46
@KenVanHoeylandt

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution, @Shadowtrance !

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.

1 participant