mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 20:05:31 +08:00
[esp32] Use the IDF I2C implementation on Arduino (#12076)
This commit is contained in:
@@ -47,18 +47,20 @@ MULTI_CONF = True
|
|||||||
|
|
||||||
|
|
||||||
def _bus_declare_type(value):
|
def _bus_declare_type(value):
|
||||||
|
if CORE.is_esp32:
|
||||||
|
return cv.declare_id(IDFI2CBus)(value)
|
||||||
if CORE.using_arduino:
|
if CORE.using_arduino:
|
||||||
return cv.declare_id(ArduinoI2CBus)(value)
|
return cv.declare_id(ArduinoI2CBus)(value)
|
||||||
if CORE.using_esp_idf:
|
|
||||||
return cv.declare_id(IDFI2CBus)(value)
|
|
||||||
if CORE.using_zephyr:
|
if CORE.using_zephyr:
|
||||||
return cv.declare_id(ZephyrI2CBus)(value)
|
return cv.declare_id(ZephyrI2CBus)(value)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
def validate_config(config):
|
def validate_config(config):
|
||||||
if CORE.using_esp_idf:
|
if CORE.is_esp32:
|
||||||
return cv.require_framework_version(esp_idf=cv.Version(5, 4, 2))(config)
|
return cv.require_framework_version(
|
||||||
|
esp_idf=cv.Version(5, 4, 2), esp32_arduino=cv.Version(3, 2, 1)
|
||||||
|
)(config)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
@@ -67,12 +69,12 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
{
|
{
|
||||||
cv.GenerateID(): _bus_declare_type,
|
cv.GenerateID(): _bus_declare_type,
|
||||||
cv.Optional(CONF_SDA, default="SDA"): pins.internal_gpio_pin_number,
|
cv.Optional(CONF_SDA, default="SDA"): pins.internal_gpio_pin_number,
|
||||||
cv.SplitDefault(CONF_SDA_PULLUP_ENABLED, esp32_idf=True): cv.All(
|
cv.SplitDefault(CONF_SDA_PULLUP_ENABLED, esp32=True): cv.All(
|
||||||
cv.only_with_esp_idf, cv.boolean
|
cv.only_on_esp32, cv.boolean
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_SCL, default="SCL"): pins.internal_gpio_pin_number,
|
cv.Optional(CONF_SCL, default="SCL"): pins.internal_gpio_pin_number,
|
||||||
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32_idf=True): cv.All(
|
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32=True): cv.All(
|
||||||
cv.only_with_esp_idf, cv.boolean
|
cv.only_on_esp32, cv.boolean
|
||||||
),
|
),
|
||||||
cv.SplitDefault(
|
cv.SplitDefault(
|
||||||
CONF_FREQUENCY,
|
CONF_FREQUENCY,
|
||||||
@@ -151,7 +153,7 @@ async def to_code(config):
|
|||||||
cg.add(var.set_scan(config[CONF_SCAN]))
|
cg.add(var.set_scan(config[CONF_SCAN]))
|
||||||
if CONF_TIMEOUT in config:
|
if CONF_TIMEOUT in config:
|
||||||
cg.add(var.set_timeout(int(config[CONF_TIMEOUT].total_microseconds)))
|
cg.add(var.set_timeout(int(config[CONF_TIMEOUT].total_microseconds)))
|
||||||
if CORE.using_arduino:
|
if CORE.using_arduino and not CORE.is_esp32:
|
||||||
cg.add_library("Wire", None)
|
cg.add_library("Wire", None)
|
||||||
|
|
||||||
|
|
||||||
@@ -248,14 +250,16 @@ def final_validate_device_schema(
|
|||||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||||
{
|
{
|
||||||
"i2c_bus_arduino.cpp": {
|
"i2c_bus_arduino.cpp": {
|
||||||
PlatformFramework.ESP32_ARDUINO,
|
|
||||||
PlatformFramework.ESP8266_ARDUINO,
|
PlatformFramework.ESP8266_ARDUINO,
|
||||||
PlatformFramework.RP2040_ARDUINO,
|
PlatformFramework.RP2040_ARDUINO,
|
||||||
PlatformFramework.BK72XX_ARDUINO,
|
PlatformFramework.BK72XX_ARDUINO,
|
||||||
PlatformFramework.RTL87XX_ARDUINO,
|
PlatformFramework.RTL87XX_ARDUINO,
|
||||||
PlatformFramework.LN882X_ARDUINO,
|
PlatformFramework.LN882X_ARDUINO,
|
||||||
},
|
},
|
||||||
"i2c_bus_esp_idf.cpp": {PlatformFramework.ESP32_IDF},
|
"i2c_bus_esp_idf.cpp": {
|
||||||
|
PlatformFramework.ESP32_ARDUINO,
|
||||||
|
PlatformFramework.ESP32_IDF,
|
||||||
|
},
|
||||||
"i2c_bus_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
|
"i2c_bus_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#ifdef USE_ARDUINO
|
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||||
|
|
||||||
#include "i2c_bus_arduino.h"
|
#include "i2c_bus_arduino.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
@@ -15,16 +15,7 @@ static const char *const TAG = "i2c.arduino";
|
|||||||
void ArduinoI2CBus::setup() {
|
void ArduinoI2CBus::setup() {
|
||||||
recover_();
|
recover_();
|
||||||
|
|
||||||
#if defined(USE_ESP32)
|
#if defined(USE_ESP8266)
|
||||||
static uint8_t next_bus_num = 0;
|
|
||||||
if (next_bus_num == 0) {
|
|
||||||
wire_ = &Wire;
|
|
||||||
} else {
|
|
||||||
wire_ = new TwoWire(next_bus_num); // NOLINT(cppcoreguidelines-owning-memory)
|
|
||||||
}
|
|
||||||
this->port_ = next_bus_num;
|
|
||||||
next_bus_num++;
|
|
||||||
#elif defined(USE_ESP8266)
|
|
||||||
wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory)
|
wire_ = new TwoWire(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||||
#elif defined(USE_RP2040)
|
#elif defined(USE_RP2040)
|
||||||
static bool first = true;
|
static bool first = true;
|
||||||
@@ -54,10 +45,7 @@ void ArduinoI2CBus::set_pins_and_clock_() {
|
|||||||
wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
|
wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
|
||||||
#endif
|
#endif
|
||||||
if (timeout_ > 0) { // if timeout specified in yaml
|
if (timeout_ > 0) { // if timeout specified in yaml
|
||||||
#if defined(USE_ESP32)
|
#if defined(USE_ESP8266)
|
||||||
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp
|
|
||||||
wire_->setTimeOut(timeout_ / 1000); // unit: ms
|
|
||||||
#elif defined(USE_ESP8266)
|
|
||||||
// https://github.com/esp8266/Arduino/blob/master/libraries/Wire/Wire.h
|
// https://github.com/esp8266/Arduino/blob/master/libraries/Wire/Wire.h
|
||||||
wire_->setClockStretchLimit(timeout_); // unit: us
|
wire_->setClockStretchLimit(timeout_); // unit: us
|
||||||
#elif defined(USE_RP2040)
|
#elif defined(USE_RP2040)
|
||||||
@@ -76,9 +64,7 @@ void ArduinoI2CBus::dump_config() {
|
|||||||
" Frequency: %u Hz",
|
" Frequency: %u Hz",
|
||||||
this->sda_pin_, this->scl_pin_, this->frequency_);
|
this->sda_pin_, this->scl_pin_, this->frequency_);
|
||||||
if (timeout_ > 0) {
|
if (timeout_ > 0) {
|
||||||
#if defined(USE_ESP32)
|
#if defined(USE_ESP8266)
|
||||||
ESP_LOGCONFIG(TAG, " Timeout: %u ms", this->timeout_ / 1000);
|
|
||||||
#elif defined(USE_ESP8266)
|
|
||||||
ESP_LOGCONFIG(TAG, " Timeout: %u us", this->timeout_);
|
ESP_LOGCONFIG(TAG, " Timeout: %u us", this->timeout_);
|
||||||
#elif defined(USE_RP2040)
|
#elif defined(USE_RP2040)
|
||||||
ESP_LOGCONFIG(TAG, " Timeout: %u ms", this->timeout_ / 1000);
|
ESP_LOGCONFIG(TAG, " Timeout: %u ms", this->timeout_ / 1000);
|
||||||
@@ -275,4 +261,4 @@ void ArduinoI2CBus::recover_() {
|
|||||||
} // namespace i2c
|
} // namespace i2c
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
#endif // USE_ESP_IDF
|
#endif // defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef USE_ARDUINO
|
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
@@ -29,7 +29,7 @@ class ArduinoI2CBus : public InternalI2CBus, 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_; }
|
int get_port() const override { return 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void recover_();
|
void recover_();
|
||||||
@@ -37,7 +37,6 @@ class ArduinoI2CBus : public InternalI2CBus, public Component {
|
|||||||
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_;
|
||||||
@@ -49,4 +48,4 @@ class ArduinoI2CBus : public InternalI2CBus, public Component {
|
|||||||
} // namespace i2c
|
} // namespace i2c
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
#endif // USE_ARDUINO
|
#endif // defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#ifdef USE_ESP_IDF
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
#include "i2c_bus_esp_idf.h"
|
#include "i2c_bus_esp_idf.h"
|
||||||
|
|
||||||
@@ -299,4 +299,4 @@ void IDFI2CBus::recover_() {
|
|||||||
|
|
||||||
} // namespace i2c
|
} // namespace i2c
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
#endif // USE_ESP_IDF
|
#endif // USE_ESP32
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef USE_ESP_IDF
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "i2c_bus.h"
|
#include "i2c_bus.h"
|
||||||
@@ -53,4 +53,4 @@ class IDFI2CBus : public InternalI2CBus, public Component {
|
|||||||
} // namespace i2c
|
} // namespace i2c
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
#endif // USE_ESP_IDF
|
#endif // USE_ESP32
|
||||||
|
|||||||
Reference in New Issue
Block a user