Skip to content

Add access methods to get the Wire bus number and I2C bus handle #11570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cores/esp32/esp32-hal-i2c-ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions cores/esp32/esp32-hal-i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "soc/soc_caps.h"
#if SOC_I2C_SUPPORTED
#include "esp_idf_version.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading