ESPHome 2025.5.0
Loading...
Searching...
No Matches
i2c_bus_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP_IDF
2
3#include "i2c_bus_esp_idf.h"
4#include <cinttypes>
5#include <cstring>
7#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10
11#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
12#define SOC_HP_I2C_NUM SOC_I2C_NUM
13#endif
14
15namespace esphome {
16namespace i2c {
17
18static const char *const TAG = "i2c.idf";
19
21 ESP_LOGCONFIG(TAG, "Setting up I2C bus...");
22 static i2c_port_t next_port = I2C_NUM_0;
23 port_ = next_port;
24#if SOC_HP_I2C_NUM > 1
25 next_port = (next_port == I2C_NUM_0) ? I2C_NUM_1 : I2C_NUM_MAX;
26#else
27 next_port = I2C_NUM_MAX;
28#endif
29
30 if (port_ == I2C_NUM_MAX) {
31 ESP_LOGE(TAG, "Too many I2C buses configured. Max %u supported.", SOC_HP_I2C_NUM);
32 this->mark_failed();
33 return;
34 }
35
36 recover_();
37
38 i2c_config_t conf{};
39 memset(&conf, 0, sizeof(conf));
40 conf.mode = I2C_MODE_MASTER;
41 conf.sda_io_num = sda_pin_;
42 conf.sda_pullup_en = sda_pullup_enabled_;
43 conf.scl_io_num = scl_pin_;
44 conf.scl_pullup_en = scl_pullup_enabled_;
45 conf.master.clk_speed = frequency_;
46#ifdef USE_ESP32_VARIANT_ESP32S2
47 // workaround for https://github.com/esphome/issues/issues/6718
48 conf.clk_flags = I2C_SCLK_SRC_FLAG_AWARE_DFS;
49#endif
50 esp_err_t err = i2c_param_config(port_, &conf);
51 if (err != ESP_OK) {
52 ESP_LOGW(TAG, "i2c_param_config failed: %s", esp_err_to_name(err));
53 this->mark_failed();
54 return;
55 }
56 if (timeout_ > 0) { // if timeout specified in yaml:
57 if (timeout_ > 13000) {
58 ESP_LOGW(TAG, "i2c timeout of %" PRIu32 "us greater than max of 13ms on esp-idf, setting to max", timeout_);
59 timeout_ = 13000;
60 }
61 err = i2c_set_timeout(port_, timeout_ * 80); // unit: APB 80MHz clock cycle
62 if (err != ESP_OK) {
63 ESP_LOGW(TAG, "i2c_set_timeout failed: %s", esp_err_to_name(err));
64 this->mark_failed();
65 return;
66 } else {
67 ESP_LOGV(TAG, "i2c_timeout set to %" PRIu32 " ticks (%" PRIu32 " us)", timeout_ * 80, timeout_);
68 }
69 }
70 err = i2c_driver_install(port_, I2C_MODE_MASTER, 0, 0, 0);
71 if (err != ESP_OK) {
72 ESP_LOGW(TAG, "i2c_driver_install failed: %s", esp_err_to_name(err));
73 this->mark_failed();
74 return;
75 }
76 initialized_ = true;
77 if (this->scan_) {
78 ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
79 this->i2c_scan_();
80 }
81}
83 ESP_LOGCONFIG(TAG, "I2C Bus:");
84 ESP_LOGCONFIG(TAG, " SDA Pin: GPIO%u", this->sda_pin_);
85 ESP_LOGCONFIG(TAG, " SCL Pin: GPIO%u", this->scl_pin_);
86 ESP_LOGCONFIG(TAG, " Frequency: %" PRIu32 " Hz", this->frequency_);
87 if (timeout_ > 0) {
88 ESP_LOGCONFIG(TAG, " Timeout: %" PRIu32 "us", this->timeout_);
89 }
90 switch (this->recovery_result_) {
92 ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
93 break;
95 ESP_LOGCONFIG(TAG, " Recovery: failed, SCL is held low on the bus");
96 break;
98 ESP_LOGCONFIG(TAG, " Recovery: failed, SDA is held low on the bus");
99 break;
100 }
101 if (this->scan_) {
102 ESP_LOGI(TAG, "Results from i2c bus scan:");
103 if (scan_results_.empty()) {
104 ESP_LOGI(TAG, "Found no i2c devices!");
105 } else {
106 for (const auto &s : scan_results_) {
107 if (s.second) {
108 ESP_LOGI(TAG, "Found i2c device at address 0x%02X", s.first);
109 } else {
110 ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
111 }
112 }
113 }
114 }
115}
116
117ErrorCode IDFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
118 // logging is only enabled with vv level, if warnings are shown the caller
119 // should log them
120 if (!initialized_) {
121 ESP_LOGVV(TAG, "i2c bus not initialized!");
123 }
124 i2c_cmd_handle_t cmd = i2c_cmd_link_create();
125 esp_err_t err = i2c_master_start(cmd);
126 if (err != ESP_OK) {
127 ESP_LOGVV(TAG, "RX from %02X master start failed: %s", address, esp_err_to_name(err));
128 i2c_cmd_link_delete(cmd);
129 return ERROR_UNKNOWN;
130 }
131 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ, true);
132 if (err != ESP_OK) {
133 ESP_LOGVV(TAG, "RX from %02X address write failed: %s", address, esp_err_to_name(err));
134 i2c_cmd_link_delete(cmd);
135 return ERROR_UNKNOWN;
136 }
137 for (size_t i = 0; i < cnt; i++) {
138 const auto &buf = buffers[i];
139 if (buf.len == 0)
140 continue;
141 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
142 if (err != ESP_OK) {
143 ESP_LOGVV(TAG, "RX from %02X data read failed: %s", address, esp_err_to_name(err));
144 i2c_cmd_link_delete(cmd);
145 return ERROR_UNKNOWN;
146 }
147 }
148 err = i2c_master_stop(cmd);
149 if (err != ESP_OK) {
150 ESP_LOGVV(TAG, "RX from %02X stop failed: %s", address, esp_err_to_name(err));
151 i2c_cmd_link_delete(cmd);
152 return ERROR_UNKNOWN;
153 }
154 err = i2c_master_cmd_begin(port_, cmd, 20 / portTICK_PERIOD_MS);
155 // i2c_master_cmd_begin() will block for a whole second if no ack:
156 // https://github.com/espressif/esp-idf/issues/4999
157 i2c_cmd_link_delete(cmd);
158 if (err == ESP_FAIL) {
159 // transfer not acked
160 ESP_LOGVV(TAG, "RX from %02X failed: not acked", address);
162 } else if (err == ESP_ERR_TIMEOUT) {
163 ESP_LOGVV(TAG, "RX from %02X failed: timeout", address);
164 return ERROR_TIMEOUT;
165 } else if (err != ESP_OK) {
166 ESP_LOGVV(TAG, "RX from %02X failed: %s", address, esp_err_to_name(err));
167 return ERROR_UNKNOWN;
168 }
169
170#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
171 char debug_buf[4];
172 std::string debug_hex;
173
174 for (size_t i = 0; i < cnt; i++) {
175 const auto &buf = buffers[i];
176 for (size_t j = 0; j < buf.len; j++) {
177 snprintf(debug_buf, sizeof(debug_buf), "%02X", buf.data[j]);
178 debug_hex += debug_buf;
179 }
180 }
181 ESP_LOGVV(TAG, "0x%02X RX %s", address, debug_hex.c_str());
182#endif
183
184 return ERROR_OK;
185}
186ErrorCode IDFI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) {
187 // logging is only enabled with vv level, if warnings are shown the caller
188 // should log them
189 if (!initialized_) {
190 ESP_LOGVV(TAG, "i2c bus not initialized!");
192 }
193
194#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
195 char debug_buf[4];
196 std::string debug_hex;
197
198 for (size_t i = 0; i < cnt; i++) {
199 const auto &buf = buffers[i];
200 for (size_t j = 0; j < buf.len; j++) {
201 snprintf(debug_buf, sizeof(debug_buf), "%02X", buf.data[j]);
202 debug_hex += debug_buf;
203 }
204 }
205 ESP_LOGVV(TAG, "0x%02X TX %s", address, debug_hex.c_str());
206#endif
207
208 i2c_cmd_handle_t cmd = i2c_cmd_link_create();
209 esp_err_t err = i2c_master_start(cmd);
210 if (err != ESP_OK) {
211 ESP_LOGVV(TAG, "TX to %02X master start failed: %s", address, esp_err_to_name(err));
212 i2c_cmd_link_delete(cmd);
213 return ERROR_UNKNOWN;
214 }
215 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, true);
216 if (err != ESP_OK) {
217 ESP_LOGVV(TAG, "TX to %02X address write failed: %s", address, esp_err_to_name(err));
218 i2c_cmd_link_delete(cmd);
219 return ERROR_UNKNOWN;
220 }
221 for (size_t i = 0; i < cnt; i++) {
222 const auto &buf = buffers[i];
223 if (buf.len == 0)
224 continue;
225 err = i2c_master_write(cmd, buf.data, buf.len, true);
226 if (err != ESP_OK) {
227 ESP_LOGVV(TAG, "TX to %02X data write failed: %s", address, esp_err_to_name(err));
228 i2c_cmd_link_delete(cmd);
229 return ERROR_UNKNOWN;
230 }
231 }
232 if (stop) {
233 err = i2c_master_stop(cmd);
234 if (err != ESP_OK) {
235 ESP_LOGVV(TAG, "TX to %02X master stop failed: %s", address, esp_err_to_name(err));
236 i2c_cmd_link_delete(cmd);
237 return ERROR_UNKNOWN;
238 }
239 }
240 err = i2c_master_cmd_begin(port_, cmd, 20 / portTICK_PERIOD_MS);
241 i2c_cmd_link_delete(cmd);
242 if (err == ESP_FAIL) {
243 // transfer not acked
244 ESP_LOGVV(TAG, "TX to %02X failed: not acked", address);
246 } else if (err == ESP_ERR_TIMEOUT) {
247 ESP_LOGVV(TAG, "TX to %02X failed: timeout", address);
248 return ERROR_TIMEOUT;
249 } else if (err != ESP_OK) {
250 ESP_LOGVV(TAG, "TX to %02X failed: %s", address, esp_err_to_name(err));
251 return ERROR_UNKNOWN;
252 }
253 return ERROR_OK;
254}
255
259void IDFI2CBus::recover_() {
260 ESP_LOGI(TAG, "Performing I2C bus recovery");
261
262 const gpio_num_t scl_pin = static_cast<gpio_num_t>(scl_pin_);
263 const gpio_num_t sda_pin = static_cast<gpio_num_t>(sda_pin_);
264
265 // For the upcoming operations, target for a 60kHz toggle frequency.
266 // 1000kHz is the maximum frequency for I2C running in standard-mode,
267 // but lower frequencies are not a problem.
268 // Note: the timing that is used here is chosen manually, to get
269 // results that are close to the timing that can be archieved by the
270 // implementation for the Arduino framework.
271 const auto half_period_usec = 7;
272
273 // Configure SCL pin for open drain input/output, with a pull up resistor.
274 gpio_set_level(scl_pin, 1);
275 gpio_config_t scl_config{};
276 scl_config.pin_bit_mask = 1ULL << scl_pin_;
277 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
278 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
279 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
280 scl_config.intr_type = GPIO_INTR_DISABLE;
281 gpio_config(&scl_config);
282
283 // Configure SDA pin for open drain input/output, with a pull up resistor.
284 gpio_set_level(sda_pin, 1);
285 gpio_config_t sda_conf{};
286 sda_conf.pin_bit_mask = 1ULL << sda_pin_;
287 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
288 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
289 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
290 sda_conf.intr_type = GPIO_INTR_DISABLE;
291 gpio_config(&sda_conf);
292
293 // If SCL is pulled low on the I2C bus, then some device is interfering
294 // with the SCL line. In that case, the I2C bus cannot be recovered.
295 delayMicroseconds(half_period_usec);
296 if (gpio_get_level(scl_pin) == 0) {
297 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW on the I2C bus");
298 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
299 return;
300 }
301
302 // From the specification:
303 // "If the data line (SDA) is stuck LOW, send nine clock pulses. The
304 // device that held the bus LOW should release it sometime within
305 // those nine clocks."
306 // We don't really have to detect if SDA is stuck low. We'll simply send
307 // nine clock pulses here, just in case SDA is stuck. Actual checks on
308 // the SDA line status will be done after the clock pulses.
309 for (auto i = 0; i < 9; i++) {
310 gpio_set_level(scl_pin, 0);
311 delayMicroseconds(half_period_usec);
312 gpio_set_level(scl_pin, 1);
313 delayMicroseconds(half_period_usec);
314
315 // When SCL is kept LOW at this point, we might be looking at a device
316 // that applies clock stretching. Wait for the release of the SCL line,
317 // but not forever. There is no specification for the maximum allowed
318 // time. We yield and reset the WDT, so as to avoid triggering reset.
319 // No point in trying to recover the bus by forcing a uC reset. Bus
320 // should recover in a few ms or less else not likely to recovery at
321 // all.
322 auto wait = 250;
323 while (wait-- && gpio_get_level(scl_pin) == 0) {
324 App.feed_wdt();
325 delayMicroseconds(half_period_usec * 2);
326 }
327 if (gpio_get_level(scl_pin) == 0) {
328 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW during clock pulse cycle");
329 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
330 return;
331 }
332 }
333
334 // By now, any stuck device ought to have sent all remaining bits of its
335 // transaction, meaning that it should have freed up the SDA line, resulting
336 // in SDA being pulled up.
337 if (gpio_get_level(sda_pin) == 0) {
338 ESP_LOGE(TAG, "Recovery failed: SDA is held LOW after clock pulse cycle");
339 recovery_result_ = RECOVERY_FAILED_SDA_LOW;
340 return;
341 }
342
343 // From the specification:
344 // "I2C-bus compatible devices must reset their bus logic on receipt of
345 // a START or repeated START condition such that they all anticipate
346 // the sending of a target address, even if these START conditions are
347 // not positioned according to the proper format."
348 // While the 9 clock pulses from above might have drained all bits of a
349 // single byte within a transaction, a device might have more bytes to
350 // transmit. So here we'll generate a START condition to snap the device
351 // out of this state.
352 // SCL and SDA are already high at this point, so we can generate a START
353 // condition by making the SDA signal LOW.
354 delayMicroseconds(half_period_usec);
355 gpio_set_level(sda_pin, 0);
356
357 // From the specification:
358 // "A START condition immediately followed by a STOP condition (void
359 // message) is an illegal format. Many devices however are designed to
360 // operate properly under this condition."
361 // Finally, we'll bring the I2C bus into a starting state by generating
362 // a STOP condition.
363 delayMicroseconds(half_period_usec);
364 gpio_set_level(sda_pin, 1);
365
366 recovery_result_ = RECOVERY_COMPLETED;
367}
368
369} // namespace i2c
370} // namespace esphome
371
372#endif // USE_ESP_IDF
uint8_t address
Definition bl0906.h:4
void feed_wdt(uint32_t time=0)
virtual void mark_failed()
Mark this component as failed.
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:108
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:107
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c_bus.h:97
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:16
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:15
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:17
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
Application App
Global storage of Application pointer - only one Application can exist.
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition i2c_bus.h:24
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition i2c_bus.h:30