From 32e8efb7b7e6554a37fec3a2cd2111b2f3b7fb51 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 12 Mar 2026 13:24:27 -1000 Subject: [PATCH] Use formula-based description instead of incomplete GPIO pin lists Addresses review feedback: the hard-coded pin lists only covered RP2040 GPIOs and were incomplete for RP2350 (GPIO up to 47). Now describes the (gpio / 2) % 2 rule instead. --- esphome/components/i2c/__init__.py | 7 ++++--- esphome/components/i2c/i2c_bus_arduino.cpp | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/esphome/components/i2c/__init__.py b/esphome/components/i2c/__init__.py index 88a497ddf2c..1684f479ba3 100644 --- a/esphome/components/i2c/__init__.py +++ b/esphome/components/i2c/__init__.py @@ -178,9 +178,10 @@ def _final_validate(config): if len(set(controllers)) != len(controllers): raise cv.Invalid( "Multiple I2C buses are configured to use the same I2C controller. " - "Each bus must use pins on a different controller " - "(I2C0: SDA on GPIO 0,4,8,12,16,20,24,28; " - "I2C1: SDA on GPIO 2,6,10,14,18,22,26)." + "Each bus must use pins on a different controller. " + "The I2C controller is determined by (gpio / 2) % 2: " + "even pin pairs (0-1, 4-5, 8-9, ...) use I2C0, " + "odd pin pairs (2-3, 6-7, 10-11, ...) use I2C1." ) if CORE.is_esp32 and get_esp32_variant() in ESP32_I2C_CAPABILITIES: variant = get_esp32_variant() diff --git a/esphome/components/i2c/i2c_bus_arduino.cpp b/esphome/components/i2c/i2c_bus_arduino.cpp index 3a511edfdbb..47a06abe9ec 100644 --- a/esphome/components/i2c/i2c_bus_arduino.cpp +++ b/esphome/components/i2c/i2c_bus_arduino.cpp @@ -21,9 +21,9 @@ void ArduinoI2CBus::setup() { wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory) #elif defined(USE_RP2040) // Select Wire instance based on pin assignment, not definition order. - // RP2040 I2C controller is determined by GPIO: (pin / 2) % 2 - // See RP2040 datasheet Table 2 (section 1.4.3): https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf - // See RP2350 datasheet Table 7 (section 9.4): https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf + // I2C controller = (gpio / 2) % 2: even pairs (0-1,4-5,...) → I2C0, odd pairs (2-3,6-7,...) → I2C1 + // RP2040 datasheet Table 2 (section 1.4.3): https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf + // RP2350 datasheet Table 7 (section 9.4): https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf if ((this->sda_pin_ / 2) % 2 == 0) { wire_ = &Wire; } else {