ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
i2c_bus_zephyr.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2
3#include "i2c_bus_zephyr.h"
4#include <zephyr/drivers/i2c.h>
5#include "esphome/core/log.h"
6
7namespace esphome::i2c {
8
9ESPHOME_LOG_TAG(TAG, "i2c");
10
11static const char *get_speed(uint32_t dev_config) {
12 switch (I2C_SPEED_GET(dev_config)) {
13 case I2C_SPEED_STANDARD:
14 return "100 kHz";
15 case I2C_SPEED_FAST:
16 return "400 kHz";
17 case I2C_SPEED_FAST_PLUS:
18 return "1 MHz";
19 case I2C_SPEED_HIGH:
20 return "3.4 MHz";
21 case I2C_SPEED_ULTRA:
22 return "5 MHz";
23 }
24 return "unknown";
25}
26
28 if (!device_is_ready(this->i2c_dev_)) {
29 ESP_LOGE(TAG, "I2C dev is not ready.");
31 return;
32 }
33
34 int ret = i2c_configure(this->i2c_dev_, this->dev_config_);
35 if (ret < 0) {
36 ESP_LOGE(TAG, "I2C: Failed to configure device");
37 }
38
39 this->recovery_result_ = i2c_recover_bus(this->i2c_dev_);
40 if (this->recovery_result_ != 0) {
41 ESP_LOGE(TAG, "I2C recover bus failed, err %d", this->recovery_result_);
42 }
43 if (this->scan_) {
44 ESP_LOGV(TAG, "Scanning I2C bus for active devices...");
45 this->i2c_scan_();
46 }
47}
48
50 ESP_LOGCONFIG(TAG,
51 "I2C Bus:\n"
52 " SDA Pin: GPIO%u\n"
53 " SCL Pin: GPIO%u\n"
54 " Frequency: %s\n"
55 " Name: %s",
56 this->sda_pin_, this->scl_pin_, get_speed(this->dev_config_), this->i2c_dev_->name);
57
58 if (this->recovery_result_ != 0) {
59 ESP_LOGCONFIG(TAG, " Recovery: failed, err %d", this->recovery_result_);
60 } else {
61 ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
62 }
63 this->dump_scan_results_();
64}
65
66ErrorCode ZephyrI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
67 uint8_t *read_buffer, size_t read_count) {
68 if (!device_is_ready(this->i2c_dev_)) {
70 }
71
72 i2c_msg msgs[2]{};
73 size_t cnt = 0;
74 uint8_t dst = 0x00; // dummy data to not use random value
75
76 if (read_count == 0 && write_count == 0) {
77 msgs[cnt].buf = &dst;
78 msgs[cnt].len = 0U;
79 msgs[cnt++].flags = I2C_MSG_WRITE;
80 } else {
81 if (write_count) {
82 // the same struct is used for read/write — const cast is fine; data isn't modified
83 msgs[cnt].buf = const_cast<uint8_t *>(write_buffer);
84 msgs[cnt].len = write_count;
85 msgs[cnt++].flags = I2C_MSG_WRITE;
86 }
87 if (read_count) {
88 msgs[cnt].buf = const_cast<uint8_t *>(read_buffer);
89 msgs[cnt].len = read_count;
90 msgs[cnt++].flags = I2C_MSG_READ | I2C_MSG_RESTART;
91 }
92 }
93
94 msgs[cnt - 1].flags |= I2C_MSG_STOP;
95
96 auto err = i2c_transfer(this->i2c_dev_, msgs, cnt, address);
97
98 if (err == -EIO) {
100 }
101
102 if (err != 0) {
103 ESP_LOGE(TAG, "i2c transfer error %d", err);
104 return ERROR_UNKNOWN;
105 }
106
107 return ERROR_OK;
108}
109
110static uint32_t speed_config(uint32_t dev_config, uint32_t frequency) {
111 const uint32_t speed = frequency >= 400000 ? I2C_SPEED_FAST : I2C_SPEED_STANDARD;
112 return (dev_config & ~I2C_SPEED_MASK) | I2C_SPEED_SET(speed);
113}
114
116#ifdef I2C_PORT_FREQUENCY_COUNT
117 this->frequency_ = frequency;
118#endif
119 this->dev_config_ = speed_config(this->dev_config_, frequency);
120}
121
122#ifdef I2C_PORT_FREQUENCY_COUNT
124 if (this->frequency_ == frequency) {
125 return ERROR_OK;
126 }
127 const uint32_t dev_config = speed_config(this->dev_config_, frequency);
128 // Until ready, setup() applies dev_config_
129 if (this->is_ready() && i2c_configure(this->i2c_dev_, dev_config) < 0) {
130 return ERROR_UNKNOWN;
131 }
132 this->dev_config_ = dev_config;
133 this->frequency_ = frequency;
134 return ERROR_OK;
135}
136#endif
137
138} // namespace esphome::i2c
139
140#endif
uint16_le_t frequency
Definition bl0942.h:6
void mark_failed()
Mark this component as failed.
bool is_ready() const
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:73
void dump_scan_results_()
Logs the scan results at config level, if the bus was scanned.
Definition i2c.cpp:24
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c.cpp:11
void set_frequency(uint32_t frequency)
ErrorCode switch_frequency(uint32_t frequency) override
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
int speed
Definition fan.h:3
int ret
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:17
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:19
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:21
ESPHOME_LOG_TAG(TAG, "i2c")
static void uint32_t