mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 14:18:14 +00:00
i2c
This commit is contained in:
parent
96682f5cbe
commit
dd923f13db
@ -1,29 +1,31 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
import esphome.final_validate as fv
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.zephyr import zephyr_add_overlay, zephyr_add_prj_conf
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_FREQUENCY,
|
||||
CONF_TIMEOUT,
|
||||
CONF_I2C_ID,
|
||||
CONF_ID,
|
||||
CONF_INPUT,
|
||||
CONF_OUTPUT,
|
||||
CONF_SCAN,
|
||||
CONF_SCL,
|
||||
CONF_SDA,
|
||||
CONF_ADDRESS,
|
||||
CONF_I2C_ID,
|
||||
CONF_TIMEOUT,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_RP2040,
|
||||
)
|
||||
from esphome.core import coroutine_with_priority, CORE
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
import esphome.final_validate as fv
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
i2c_ns = cg.esphome_ns.namespace("i2c")
|
||||
I2CBus = i2c_ns.class_("I2CBus")
|
||||
ArduinoI2CBus = i2c_ns.class_("ArduinoI2CBus", I2CBus, cg.Component)
|
||||
IDFI2CBus = i2c_ns.class_("IDFI2CBus", I2CBus, cg.Component)
|
||||
ZephyrI2CBus = i2c_ns.class_("ZephyrI2CBus", I2CBus, cg.Component)
|
||||
I2CDevice = i2c_ns.class_("I2CDevice")
|
||||
|
||||
|
||||
@ -37,6 +39,8 @@ def _bus_declare_type(value):
|
||||
return cv.declare_id(ArduinoI2CBus)(value)
|
||||
if CORE.using_esp_idf:
|
||||
return cv.declare_id(IDFI2CBus)(value)
|
||||
if CORE.using_zephyr:
|
||||
return cv.declare_id(ZephyrI2CBus)(value)
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@ -44,6 +48,7 @@ pin_with_input_and_output_support = pins.internal_gpio_pin_number(
|
||||
{CONF_OUTPUT: True, CONF_INPUT: True}
|
||||
)
|
||||
|
||||
PLATFORM_NRF52 = "nrf52"
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
@ -64,7 +69,7 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_SCAN, default=True): cv.boolean,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040]),
|
||||
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040, PLATFORM_NRF52]),
|
||||
)
|
||||
|
||||
|
||||
@ -74,17 +79,40 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_sda_pin(config[CONF_SDA]))
|
||||
if CONF_SDA_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_sda_pullup_enabled(config[CONF_SDA_PULLUP_ENABLED]))
|
||||
cg.add(var.set_scl_pin(config[CONF_SCL]))
|
||||
if CONF_SCL_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_scl_pullup_enabled(config[CONF_SCL_PULLUP_ENABLED]))
|
||||
if CORE.using_zephyr:
|
||||
zephyr_add_prj_conf("I2C", True)
|
||||
zephyr_add_overlay(
|
||||
f"""
|
||||
&pinctrl {{
|
||||
i2c0_default: i2c0_default {{
|
||||
group1 {{
|
||||
psels = <NRF_PSEL(TWIM_SDA, {config[CONF_SDA] // 32}, {config[CONF_SDA] % 32})>,
|
||||
<NRF_PSEL(TWIM_SCL, {config[CONF_SCL] // 32}, {config[CONF_SCL] % 32})>;
|
||||
}};
|
||||
}};
|
||||
i2c0_sleep: i2c0_sleep {{
|
||||
group1 {{
|
||||
psels = <NRF_PSEL(TWIM_SDA, {config[CONF_SDA] // 32}, {config[CONF_SDA] % 32})>,
|
||||
<NRF_PSEL(TWIM_SCL, {config[CONF_SCL] // 32}, {config[CONF_SCL] % 32})>;
|
||||
low-power-enable;
|
||||
}};
|
||||
}};
|
||||
}};
|
||||
"""
|
||||
)
|
||||
|
||||
cg.add(var.set_frequency(int(config[CONF_FREQUENCY])))
|
||||
else:
|
||||
cg.add(var.set_sda_pin(config[CONF_SDA]))
|
||||
if CONF_SDA_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_sda_pullup_enabled(config[CONF_SDA_PULLUP_ENABLED]))
|
||||
cg.add(var.set_scl_pin(config[CONF_SCL]))
|
||||
if CONF_SCL_PULLUP_ENABLED in config:
|
||||
cg.add(var.set_scl_pullup_enabled(config[CONF_SCL_PULLUP_ENABLED]))
|
||||
|
||||
cg.add(var.set_frequency(int(config[CONF_FREQUENCY])))
|
||||
if CONF_TIMEOUT in config:
|
||||
cg.add(var.set_timeout(int(config[CONF_TIMEOUT].total_microseconds)))
|
||||
cg.add(var.set_scan(config[CONF_SCAN]))
|
||||
if CONF_TIMEOUT in config:
|
||||
cg.add(var.set_timeout(int(config[CONF_TIMEOUT].total_microseconds)))
|
||||
if CORE.using_arduino:
|
||||
cg.add_library("Wire", None)
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace i2c {
|
||||
@ -102,6 +103,8 @@ class I2CBus {
|
||||
} else if (err == ERROR_UNKNOWN) {
|
||||
scan_results_.emplace_back(address, false);
|
||||
}
|
||||
// it takes 16sec to scan on nrf52. It prevents board reset.
|
||||
arch_feed_wdt();
|
||||
}
|
||||
}
|
||||
std::vector<std::pair<uint8_t, bool>> scan_results_; ///< array containing scan results
|
||||
|
@ -17,14 +17,14 @@ void IDFI2CBus::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up I2C bus...");
|
||||
static i2c_port_t next_port = I2C_NUM_0;
|
||||
port_ = next_port;
|
||||
#if SOC_I2C_NUM > 1
|
||||
#if I2C_NUM_MAX > 1
|
||||
next_port = (next_port == I2C_NUM_0) ? I2C_NUM_1 : I2C_NUM_MAX;
|
||||
#else
|
||||
next_port = I2C_NUM_MAX;
|
||||
#endif
|
||||
|
||||
if (port_ == I2C_NUM_MAX) {
|
||||
ESP_LOGE(TAG, "Too many I2C buses configured. Max %u supported.", SOC_I2C_NUM);
|
||||
ESP_LOGE(TAG, "Too many I2C buses configured");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
157
esphome/components/i2c/i2c_bus_zephyr.cpp
Normal file
157
esphome/components/i2c/i2c_bus_zephyr.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
#ifdef USE_ZEPHYR
|
||||
|
||||
#include "i2c_bus_zephyr.h"
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace i2c {
|
||||
|
||||
static const char *const TAG = "i2c.zephyr";
|
||||
|
||||
void ZephyrI2CBus::setup() {
|
||||
const device *i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c0));
|
||||
if (!device_is_ready(i2c_dev)) {
|
||||
ESP_LOGE(TAG, "I2C dev is not ready.");
|
||||
return;
|
||||
}
|
||||
i2c_dev_ = i2c_dev;
|
||||
|
||||
this->recovery_result_ = i2c_recover_bus(i2c_dev_);
|
||||
if (this->recovery_result_ != 0) {
|
||||
ESP_LOGE(TAG, "I2C recover bus failed, err %d", this->recovery_result_);
|
||||
}
|
||||
if (this->scan_) {
|
||||
// FIXME it should be done one by one since it takes over 18sec
|
||||
ESP_LOGV(TAG, "Scanning I2C bus for active devices...");
|
||||
this->i2c_scan_();
|
||||
}
|
||||
}
|
||||
|
||||
void ZephyrI2CBus::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "I2C Bus:");
|
||||
// ESP_LOGCONFIG(TAG, " SDA Pin: GPIO%u", this->sda_pin_);
|
||||
// ESP_LOGCONFIG(TAG, " SCL Pin: GPIO%u", this->scl_pin_);
|
||||
if (!i2c_dev_) {
|
||||
ESP_LOGCONFIG(TAG, " Not initialized");
|
||||
return;
|
||||
}
|
||||
uint32_t dev_config = 0;
|
||||
// FIXME this function is not implemented in driver
|
||||
int err = i2c_get_config(i2c_dev_, &dev_config);
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "Cannot get I2C config, err %d", err);
|
||||
} else {
|
||||
auto get_speed = [](uint32_t dev_config) {
|
||||
switch (I2C_SPEED_GET(dev_config)) {
|
||||
case I2C_SPEED_STANDARD:
|
||||
return "100 kHz";
|
||||
case I2C_SPEED_FAST:
|
||||
return "400 kHz";
|
||||
case I2C_SPEED_FAST_PLUS:
|
||||
return "1 MHz";
|
||||
case I2C_SPEED_HIGH:
|
||||
return "3.4 MHz";
|
||||
case I2C_SPEED_ULTRA:
|
||||
return "5 MHz";
|
||||
}
|
||||
return "unknown";
|
||||
};
|
||||
ESP_LOGCONFIG(TAG, " Frequency: %s", get_speed(dev_config));
|
||||
}
|
||||
|
||||
// if (timeout_ > 0) {
|
||||
// ESP_LOGCONFIG(TAG, " Timeout: %" PRIu32 "us", this->timeout_);
|
||||
// }
|
||||
if (this->recovery_result_ != 0) {
|
||||
ESP_LOGCONFIG(TAG, " Recovery: failed, err %d", this->recovery_result_);
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
|
||||
}
|
||||
if (this->scan_) {
|
||||
ESP_LOGI(TAG, "Results from I2C bus scan:");
|
||||
if (scan_results_.empty()) {
|
||||
ESP_LOGI(TAG, "Found no I2C devices!");
|
||||
} else {
|
||||
for (const auto &s : scan_results_) {
|
||||
if (s.second) {
|
||||
ESP_LOGI(TAG, "Found I2C device at address 0x%02X", s.first);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ErrorCode ZephyrI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
|
||||
if (!i2c_dev_) {
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
std::vector<i2c_msg> msgs(cnt);
|
||||
|
||||
for (size_t i = 0; i < cnt; ++i) {
|
||||
msgs[i].buf = buffers[i].data;
|
||||
msgs[i].len = buffers[i].len;
|
||||
// TODO how to use I2C_MSG_RESTART
|
||||
msgs[i].flags = I2C_MSG_READ | I2C_MSG_RESTART;
|
||||
}
|
||||
msgs[cnt - 1].flags |= I2C_MSG_STOP;
|
||||
|
||||
auto err = i2c_transfer(i2c_dev_, &msgs[0], msgs.size(), address);
|
||||
|
||||
if (err == -EIO) {
|
||||
return ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "i2c writing error %d", err);
|
||||
return ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
ErrorCode ZephyrI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) {
|
||||
if (!i2c_dev_) {
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
uint8_t dst = 0x00; // dummy data to not use random value
|
||||
|
||||
std::vector<i2c_msg> msgs(cnt == 0 ? 1 : cnt);
|
||||
|
||||
for (size_t i = 0; i < cnt; ++i) {
|
||||
if (buffers) {
|
||||
msgs[i].buf = const_cast<uint8_t *>(buffers[i].data);
|
||||
msgs[i].len = buffers[i].len;
|
||||
} else {
|
||||
msgs[i].buf = &dst;
|
||||
msgs[i].len = 0U;
|
||||
}
|
||||
msgs[i].flags = I2C_MSG_WRITE;
|
||||
}
|
||||
|
||||
if (stop) {
|
||||
msgs[cnt - 1].flags |= I2C_MSG_STOP;
|
||||
}
|
||||
|
||||
auto err = i2c_transfer(i2c_dev_, &msgs[0], msgs.size(), address);
|
||||
|
||||
if (err == -EIO) {
|
||||
return ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "i2c writing error %d", err);
|
||||
return ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
} // namespace i2c
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP_IDF
|
31
esphome/components/i2c/i2c_bus_zephyr.h
Normal file
31
esphome/components/i2c/i2c_bus_zephyr.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ZEPHYR
|
||||
|
||||
#include "i2c_bus.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
struct device;
|
||||
|
||||
namespace esphome {
|
||||
namespace i2c {
|
||||
|
||||
class ZephyrI2CBus : public I2CBus, public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
|
||||
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override;
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
void set_scan(bool scan) { scan_ = scan; }
|
||||
|
||||
protected:
|
||||
const struct device *i2c_dev_ = nullptr;
|
||||
int recovery_result_ = 0;
|
||||
};
|
||||
|
||||
} // namespace i2c
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP_IDF
|
3
tests/components/i2c/test.nrf52-adafruit.yaml
Normal file
3
tests/components/i2c/test.nrf52-adafruit.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
i2c:
|
||||
sda: P1.04
|
||||
scl: P1.06
|
8
tests/components/i2c/test.nrf52-mcumgr.yaml
Normal file
8
tests/components/i2c/test.nrf52-mcumgr.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
external_components:
|
||||
- source: github://pr#7049
|
||||
components: [nrf52, zephyr]
|
||||
refresh: always
|
||||
|
||||
i2c:
|
||||
sda: P1.04
|
||||
scl: P1.06
|
@ -0,0 +1,20 @@
|
||||
external_components:
|
||||
- source: github://pr#7049
|
||||
components: [nrf52, zephyr]
|
||||
refresh: always
|
||||
|
||||
esphome:
|
||||
name: componenttestnrf52
|
||||
friendly_name: $component_name
|
||||
|
||||
nrf52:
|
||||
board: adafruit_itsybitsy_nrf52840
|
||||
|
||||
logger:
|
||||
level: VERY_VERBOSE
|
||||
|
||||
packages:
|
||||
component_under_test: !include
|
||||
file: $component_test_file
|
||||
vars:
|
||||
component_test_file: $component_test_file
|
@ -0,0 +1,15 @@
|
||||
esphome:
|
||||
name: componenttestnrf52
|
||||
friendly_name: $component_name
|
||||
|
||||
nrf52:
|
||||
board: adafruit_feather_nrf52840
|
||||
|
||||
logger:
|
||||
level: VERY_VERBOSE
|
||||
|
||||
packages:
|
||||
component_under_test: !include
|
||||
file: $component_test_file
|
||||
vars:
|
||||
component_test_file: $component_test_file
|
Loading…
x
Reference in New Issue
Block a user