From 9eb66f361369c1fa0ba5581560b8a3956c9d95f8 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Tue, 8 Jul 2025 13:02:59 +0300 Subject: [PATCH 1/3] feat(i2c): Add method to access the I2C bus handle --- cores/esp32/esp32-hal-i2c-ng.c | 7 +++++++ cores/esp32/esp32-hal-i2c.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/cores/esp32/esp32-hal-i2c-ng.c b/cores/esp32/esp32-hal-i2c-ng.c index 8e48d0e0397..4f6f2cf40c7 100644 --- a/cores/esp32/esp32-hal-i2c-ng.c +++ b/cores/esp32/esp32-hal-i2c-ng.c @@ -56,6 +56,13 @@ static bool i2cDetachBus(void *bus_i2c_num) { return true; } +void * i2cBusHandle(uint8_t i2c_num) { + if (i2c_num >= SOC_I2C_NUM) { + return NULL; + } + return bus[i2c_num].bus_handle; +} + bool i2cIsInit(uint8_t i2c_num) { if (i2c_num >= SOC_I2C_NUM) { return false; diff --git a/cores/esp32/esp32-hal-i2c.h b/cores/esp32/esp32-hal-i2c.h index 35783d350b0..0e63d37f6b7 100644 --- a/cores/esp32/esp32-hal-i2c.h +++ b/cores/esp32/esp32-hal-i2c.h @@ -19,6 +19,7 @@ #include "soc/soc_caps.h" #if SOC_I2C_SUPPORTED +#include "esp_idf_version.h" #ifdef __cplusplus extern "C" { @@ -39,6 +40,10 @@ esp_err_t i2cWriteReadNonStop( ); bool i2cIsInit(uint8_t i2c_num); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0) +void * i2cBusHandle(uint8_t i2c_num); +#endif + #ifdef __cplusplus } #endif From 1a240e410a1ae5185e9c88d71777367a9f8d77d3 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Tue, 8 Jul 2025 13:03:30 +0300 Subject: [PATCH 2/3] feat(wire): Add access method to get the I2C bus number --- libraries/Wire/src/Wire.cpp | 6 +++++- libraries/Wire/src/Wire.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index f8d9496389f..34c814b5117 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -39,7 +39,7 @@ extern "C" { #include "Arduino.h" TwoWire::TwoWire(uint8_t bus_num) - : num(bus_num & 1), sda(-1), scl(-1), bufferSize(I2C_BUFFER_LENGTH) // default Wire Buffer Size + : num(bus_num), sda(-1), scl(-1), bufferSize(I2C_BUFFER_LENGTH) // default Wire Buffer Size , rxBuffer(NULL), rxIndex(0), rxLength(0), txBuffer(NULL), txLength(0), txAddress(0), _timeOutMillis(50), nonStop(false) #if !CONFIG_DISABLE_HAL_LOCKS @@ -62,6 +62,10 @@ TwoWire::~TwoWire() { #endif } +uint8_t TwoWire::getBusNum() { + return num; +} + bool TwoWire::initPins(int sdaPin, int sclPin) { if (sdaPin < 0) { // default param passed if (num == 0) { diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 0deab7d4a57..b84aa5b2131 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -105,6 +105,8 @@ class TwoWire : public HardwareI2C { bool end() override; + uint8_t getBusNum(); + bool setClock(uint32_t freq) override; void beginTransmission(uint8_t address) override; From 6300908a6771196309597fbe1da3163777ef381a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 8 Jul 2025 11:24:54 +0000 Subject: [PATCH 3/3] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-i2c-ng.c | 2 +- cores/esp32/esp32-hal-i2c.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c-ng.c b/cores/esp32/esp32-hal-i2c-ng.c index 4f6f2cf40c7..a3b2307b8a8 100644 --- a/cores/esp32/esp32-hal-i2c-ng.c +++ b/cores/esp32/esp32-hal-i2c-ng.c @@ -56,7 +56,7 @@ static bool i2cDetachBus(void *bus_i2c_num) { return true; } -void * i2cBusHandle(uint8_t i2c_num) { +void *i2cBusHandle(uint8_t i2c_num) { if (i2c_num >= SOC_I2C_NUM) { return NULL; } diff --git a/cores/esp32/esp32-hal-i2c.h b/cores/esp32/esp32-hal-i2c.h index 0e63d37f6b7..0e4f484bb46 100644 --- a/cores/esp32/esp32-hal-i2c.h +++ b/cores/esp32/esp32-hal-i2c.h @@ -41,7 +41,7 @@ esp_err_t i2cWriteReadNonStop( bool i2cIsInit(uint8_t i2c_num); #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0) -void * i2cBusHandle(uint8_t i2c_num); +void *i2cBusHandle(uint8_t i2c_num); #endif #ifdef __cplusplus