Skip to content

Commit 2cb6fbc

Browse files
Add access methods to get the Wire bus number and I2C bus handle (#11570)
* feat(i2c): Add method to access the I2C bus handle * feat(wire): Add access method to get the I2C bus number * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 241e257 commit 2cb6fbc

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

cores/esp32/esp32-hal-i2c-ng.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ static bool i2cDetachBus(void *bus_i2c_num) {
5656
return true;
5757
}
5858

59+
void *i2cBusHandle(uint8_t i2c_num) {
60+
if (i2c_num >= SOC_I2C_NUM) {
61+
return NULL;
62+
}
63+
return bus[i2c_num].bus_handle;
64+
}
65+
5966
bool i2cIsInit(uint8_t i2c_num) {
6067
if (i2c_num >= SOC_I2C_NUM) {
6168
return false;

cores/esp32/esp32-hal-i2c.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "soc/soc_caps.h"
2121
#if SOC_I2C_SUPPORTED
22+
#include "esp_idf_version.h"
2223

2324
#ifdef __cplusplus
2425
extern "C" {
@@ -39,6 +40,10 @@ esp_err_t i2cWriteReadNonStop(
3940
);
4041
bool i2cIsInit(uint8_t i2c_num);
4142

43+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
44+
void *i2cBusHandle(uint8_t i2c_num);
45+
#endif
46+
4247
#ifdef __cplusplus
4348
}
4449
#endif

libraries/Wire/src/Wire.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern "C" {
3939
#include "Arduino.h"
4040

4141
TwoWire::TwoWire(uint8_t bus_num)
42-
: num(bus_num & 1), sda(-1), scl(-1), bufferSize(I2C_BUFFER_LENGTH) // default Wire Buffer Size
42+
: num(bus_num), sda(-1), scl(-1), bufferSize(I2C_BUFFER_LENGTH) // default Wire Buffer Size
4343
,
4444
rxBuffer(NULL), rxIndex(0), rxLength(0), txBuffer(NULL), txLength(0), txAddress(0), _timeOutMillis(50), nonStop(false)
4545
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -62,6 +62,10 @@ TwoWire::~TwoWire() {
6262
#endif
6363
}
6464

65+
uint8_t TwoWire::getBusNum() {
66+
return num;
67+
}
68+
6569
bool TwoWire::initPins(int sdaPin, int sclPin) {
6670
if (sdaPin < 0) { // default param passed
6771
if (num == 0) {

libraries/Wire/src/Wire.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class TwoWire : public HardwareI2C {
105105

106106
bool end() override;
107107

108+
uint8_t getBusNum();
109+
108110
bool setClock(uint32_t freq) override;
109111

110112
void beginTransmission(uint8_t address) override;

0 commit comments

Comments
 (0)