mirror of
https://github.com/esphome/esphome.git
synced 2025-04-01 16:38:19 +01:00
add i2c
This commit is contained in:
parent
60d31a0f9f
commit
04bf125448
@ -16,14 +16,17 @@ from esphome.const import (
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_RP2040,
|
||||
PLATFORM_NRF52,
|
||||
)
|
||||
from esphome.core import coroutine_with_priority, CORE
|
||||
from esphome.components.zephyr import zephyr_add_overlay, zephyr_add_prj_conf
|
||||
|
||||
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 +40,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
|
||||
|
||||
|
||||
@ -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,6 +79,29 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
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;
|
||||
}};
|
||||
}};
|
||||
}};
|
||||
"""
|
||||
)
|
||||
|
||||
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]))
|
||||
@ -82,9 +110,9 @@ async def to_code(config):
|
||||
cg.add(var.set_scl_pullup_enabled(config[CONF_SCL_PULLUP_ENABLED]))
|
||||
|
||||
cg.add(var.set_frequency(int(config[CONF_FREQUENCY])))
|
||||
cg.add(var.set_scan(config[CONF_SCAN]))
|
||||
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 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
|
||||
|
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
|
@ -1,7 +1,10 @@
|
||||
#ifdef USE_ZEPHYR
|
||||
#ifdef USE_NRF52
|
||||
#include <zephyr/init.h>
|
||||
#include <hal/nrf_power.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace nrf52 {
|
||||
|
||||
static int board_esphome_init(void) {
|
||||
/* if the board is powered from USB
|
||||
* (high voltage mode), GPIO output voltage is set to 1.8 volts by
|
||||
@ -28,6 +31,11 @@ static int board_esphome_init(void) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace nrf52
|
||||
} // namespace esphome
|
||||
|
||||
static int board_esphome_init(void) { return esphome::nrf52::board_esphome_init(); }
|
||||
|
||||
SYS_INIT(board_esphome_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
||||
|
||||
#endif
|
13
tests/components/ads1115/test.nrf52-adafruit.yaml
Normal file
13
tests/components/ads1115/test.nrf52-adafruit.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
i2c:
|
||||
- id: i2c_ads1115
|
||||
sda: P1.04
|
||||
scl: P1.06
|
||||
|
||||
ads1115:
|
||||
address: 0x48
|
||||
|
||||
sensor:
|
||||
- platform: ads1115
|
||||
multiplexer: A0_A1
|
||||
gain: 1.024
|
||||
id: ads1115_sensor
|
13
tests/components/ads1115/test.nrf52.yaml
Normal file
13
tests/components/ads1115/test.nrf52.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
i2c:
|
||||
- id: i2c_ads1115
|
||||
sda: P1.04
|
||||
scl: P1.06
|
||||
|
||||
ads1115:
|
||||
address: 0x48
|
||||
|
||||
sensor:
|
||||
- platform: ads1115
|
||||
multiplexer: A0_A1
|
||||
gain: 1.024
|
||||
id: ads1115_sensor
|
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
|
3
tests/components/i2c/test.nrf52.yaml
Normal file
3
tests/components/i2c/test.nrf52.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
i2c:
|
||||
sda: P1.04
|
||||
scl: P1.06
|
@ -41,6 +41,7 @@ output:
|
||||
id: rest_gpio
|
||||
- platform: gpio
|
||||
pin:
|
||||
inverted: true
|
||||
number: 13
|
||||
mode:
|
||||
output: true
|
||||
@ -88,21 +89,37 @@ debug:
|
||||
sensor:
|
||||
- platform: uptime
|
||||
name: Uptime Sensor
|
||||
update_interval: 5sec
|
||||
- platform: adc
|
||||
pin: VDDHDIV5
|
||||
name: "VDDH Voltage"
|
||||
update_interval: 5sec
|
||||
filters:
|
||||
- multiply: 5
|
||||
- platform: adc
|
||||
pin: VDD
|
||||
name: "VDD Voltage"
|
||||
update_interval: 5sec
|
||||
- platform: adc
|
||||
pin: AIN0
|
||||
name: "AIN0 Voltage"
|
||||
update_interval: 5sec
|
||||
- platform: ads1115
|
||||
multiplexer: 'A0_GND'
|
||||
gain: 6.144
|
||||
name: "ADS1115 Channel A0-GND"
|
||||
update_interval: 1s
|
||||
- platform: ads1115
|
||||
multiplexer: 'A1_GND'
|
||||
gain: 6.144
|
||||
name: "ADS1115 Channel A1-GND"
|
||||
update_interval: 1s
|
||||
- platform: ads1115
|
||||
multiplexer: 'A2_GND'
|
||||
gain: 6.144
|
||||
name: "ADS1115 Channel A2-GND"
|
||||
update_interval: 1s
|
||||
- platform: ads1115
|
||||
multiplexer: 'A3_GND'
|
||||
gain: 6.144
|
||||
name: "ADS1115 Channel A3-GND"
|
||||
update_interval: 1s
|
||||
|
||||
deep_sleep:
|
||||
|
||||
@ -111,4 +128,10 @@ text_sensor:
|
||||
reset_reason:
|
||||
name: "Reset Reason"
|
||||
|
||||
zephyr_shell:
|
||||
i2c:
|
||||
sda: P1.04
|
||||
scl: P1.06
|
||||
scan: false
|
||||
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
|
@ -6,7 +6,7 @@ esphome:
|
||||
name: nrf52-test-nrf-adafruit
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
level: NONE
|
||||
logs:
|
||||
switch: NONE
|
||||
|
||||
@ -78,4 +78,3 @@ text_sensor:
|
||||
reset_reason:
|
||||
name: "Reset Reason"
|
||||
|
||||
zephyr_shell:
|
||||
|
Loading…
x
Reference in New Issue
Block a user