[i2c] Expose internal i2c bus port number (#9136)

This commit is contained in:
Jesse Hills 2025-06-19 13:50:47 +12:00 committed by GitHub
parent 2e9ac8945d
commit d527398dae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 11 deletions

View File

@ -22,8 +22,9 @@ import esphome.final_validate as fv
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
i2c_ns = cg.esphome_ns.namespace("i2c") i2c_ns = cg.esphome_ns.namespace("i2c")
I2CBus = i2c_ns.class_("I2CBus") I2CBus = i2c_ns.class_("I2CBus")
ArduinoI2CBus = i2c_ns.class_("ArduinoI2CBus", I2CBus, cg.Component) InternalI2CBus = i2c_ns.class_("InternalI2CBus", I2CBus)
IDFI2CBus = i2c_ns.class_("IDFI2CBus", I2CBus, cg.Component) ArduinoI2CBus = i2c_ns.class_("ArduinoI2CBus", InternalI2CBus, cg.Component)
IDFI2CBus = i2c_ns.class_("IDFI2CBus", InternalI2CBus, cg.Component)
I2CDevice = i2c_ns.class_("I2CDevice") I2CDevice = i2c_ns.class_("I2CDevice")
@ -71,6 +72,7 @@ CONFIG_SCHEMA = cv.All(
@coroutine_with_priority(1.0) @coroutine_with_priority(1.0)
async def to_code(config): async def to_code(config):
cg.add_global(i2c_ns.using) cg.add_global(i2c_ns.using)
cg.add_define("USE_I2C")
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <cstdint>
#include <cstddef> #include <cstddef>
#include <cstdint>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -108,5 +108,11 @@ class I2CBus {
bool scan_{false}; ///< Should we scan ? Can be set in the yaml bool scan_{false}; ///< Should we scan ? Can be set in the yaml
}; };
class InternalI2CBus : public I2CBus {
/// @brief Returns the I2C port number.
/// @return the port number of the internal I2C bus
virtual int get_port() const = 0;
};
} // namespace i2c } // namespace i2c
} // namespace esphome } // namespace esphome

View File

@ -1,11 +1,11 @@
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include "i2c_bus_arduino.h" #include "i2c_bus_arduino.h"
#include <Arduino.h>
#include <cstring>
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include <Arduino.h>
#include <cstring>
namespace esphome { namespace esphome {
namespace i2c { namespace i2c {
@ -23,6 +23,7 @@ void ArduinoI2CBus::setup() {
} else { } else {
wire_ = new TwoWire(next_bus_num); // NOLINT(cppcoreguidelines-owning-memory) wire_ = new TwoWire(next_bus_num); // NOLINT(cppcoreguidelines-owning-memory)
} }
this->port_ = next_bus_num;
next_bus_num++; next_bus_num++;
#elif defined(USE_ESP8266) #elif defined(USE_ESP8266)
wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory) wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory)

View File

@ -2,9 +2,9 @@
#ifdef USE_ARDUINO #ifdef USE_ARDUINO
#include "i2c_bus.h"
#include "esphome/core/component.h"
#include <Wire.h> #include <Wire.h>
#include "esphome/core/component.h"
#include "i2c_bus.h"
namespace esphome { namespace esphome {
namespace i2c { namespace i2c {
@ -15,7 +15,7 @@ enum RecoveryCode {
RECOVERY_COMPLETED, RECOVERY_COMPLETED,
}; };
class ArduinoI2CBus : public I2CBus, public Component { class ArduinoI2CBus : public InternalI2CBus, public Component {
public: public:
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
@ -29,12 +29,15 @@ class ArduinoI2CBus : public I2CBus, public Component {
void set_frequency(uint32_t frequency) { frequency_ = frequency; } void set_frequency(uint32_t frequency) { frequency_ = frequency; }
void set_timeout(uint32_t timeout) { timeout_ = timeout; } void set_timeout(uint32_t timeout) { timeout_ = timeout; }
int get_port() const override { return this->port_; }
private: private:
void recover_(); void recover_();
void set_pins_and_clock_(); void set_pins_and_clock_();
RecoveryCode recovery_result_; RecoveryCode recovery_result_;
protected: protected:
int8_t port_{-1};
TwoWire *wire_; TwoWire *wire_;
uint8_t sda_pin_; uint8_t sda_pin_;
uint8_t scl_pin_; uint8_t scl_pin_;

View File

@ -2,9 +2,9 @@
#ifdef USE_ESP_IDF #ifdef USE_ESP_IDF
#include "i2c_bus.h"
#include "esphome/core/component.h"
#include <driver/i2c.h> #include <driver/i2c.h>
#include "esphome/core/component.h"
#include "i2c_bus.h"
namespace esphome { namespace esphome {
namespace i2c { namespace i2c {
@ -15,7 +15,7 @@ enum RecoveryCode {
RECOVERY_COMPLETED, RECOVERY_COMPLETED,
}; };
class IDFI2CBus : public I2CBus, public Component { class IDFI2CBus : public InternalI2CBus, public Component {
public: public:
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
@ -31,6 +31,8 @@ class IDFI2CBus : public I2CBus, public Component {
void set_frequency(uint32_t frequency) { frequency_ = frequency; } void set_frequency(uint32_t frequency) { frequency_ = frequency; }
void set_timeout(uint32_t timeout) { timeout_ = timeout; } void set_timeout(uint32_t timeout) { timeout_ = timeout; }
int get_port() const override { return static_cast<int>(this->port_); }
private: private:
void recover_(); void recover_();
RecoveryCode recovery_result_; RecoveryCode recovery_result_;

View File

@ -136,6 +136,7 @@
#define USE_ESP32_BLE_CLIENT #define USE_ESP32_BLE_CLIENT
#define USE_ESP32_BLE_SERVER #define USE_ESP32_BLE_SERVER
#define USE_ESP32_CAMERA #define USE_ESP32_CAMERA
#define USE_I2C
#define USE_IMPROV #define USE_IMPROV
#define USE_MICROPHONE #define USE_MICROPHONE
#define USE_PSRAM #define USE_PSRAM
@ -179,6 +180,7 @@
#define USE_CAPTIVE_PORTAL #define USE_CAPTIVE_PORTAL
#define USE_ESP8266_PREFERENCES_FLASH #define USE_ESP8266_PREFERENCES_FLASH
#define USE_HTTP_REQUEST_ESP8266_HTTPS #define USE_HTTP_REQUEST_ESP8266_HTTPS
#define USE_I2C
#define USE_SOCKET_IMPL_LWIP_TCP #define USE_SOCKET_IMPL_LWIP_TCP
#define USE_SPI #define USE_SPI
@ -195,6 +197,7 @@
#ifdef USE_RP2040 #ifdef USE_RP2040
#define USE_ARDUINO_VERSION_CODE VERSION_CODE(3, 3, 0) #define USE_ARDUINO_VERSION_CODE VERSION_CODE(3, 3, 0)
#define USE_I2C
#define USE_LOGGER_USB_CDC #define USE_LOGGER_USB_CDC
#define USE_SOCKET_IMPL_LWIP_TCP #define USE_SOCKET_IMPL_LWIP_TCP
#define USE_SPI #define USE_SPI