ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
i2c_bus_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
3#include "i2c_bus_esp_idf.h"
4
5#include <driver/gpio.h>
6#include <cinttypes>
7#include <cstring>
9#include "esphome/core/hal.h"
11#include "esphome/core/log.h"
12
13namespace esphome::i2c {
14
15static const char *const TAG = "i2c.idf";
16
17// Maximum bytes to log in hex format (truncates larger transfers)
18static constexpr size_t I2C_MAX_LOG_BYTES = 32;
19
21 static i2c_port_t next_hp_port = I2C_NUM_0;
22#if SOC_LP_I2C_SUPPORTED
23 static i2c_port_t next_lp_port = LP_I2C_NUM_0;
24#endif
25
26 if (this->timeout_ > 13000) {
27 ESP_LOGW(TAG, "Using max allowed timeout: 13 ms");
28 this->timeout_ = 13000;
29 }
30
31 this->recover_();
32
33 i2c_master_bus_config_t bus_conf{};
34 memset(&bus_conf, 0, sizeof(bus_conf));
35 bus_conf.sda_io_num = gpio_num_t(sda_pin_);
36 bus_conf.scl_io_num = gpio_num_t(scl_pin_);
37 bus_conf.glitch_ignore_cnt = 7;
38#if SOC_LP_I2C_SUPPORTED
39 if (this->lp_mode_) {
40 if ((next_lp_port - LP_I2C_NUM_0) == SOC_LP_I2C_NUM) {
41 ESP_LOGE(TAG, "No more than %u LP buses supported", SOC_LP_I2C_NUM);
42 this->mark_failed();
43 return;
44 }
45 this->port_ = next_lp_port;
46 next_lp_port = (i2c_port_t) (next_lp_port + 1);
47 bus_conf.lp_source_clk = LP_I2C_SCLK_DEFAULT;
48 } else {
49#endif
50 if (next_hp_port == SOC_HP_I2C_NUM) {
51 ESP_LOGE(TAG, "No more than %u HP buses supported", SOC_HP_I2C_NUM);
52 this->mark_failed();
53 return;
54 }
55 this->port_ = next_hp_port;
56 next_hp_port = (i2c_port_t) (next_hp_port + 1);
57 bus_conf.clk_source = I2C_CLK_SRC_DEFAULT;
58#if SOC_LP_I2C_SUPPORTED
59 }
60#endif
61 bus_conf.i2c_port = this->port_;
62 bus_conf.flags.enable_internal_pullup = sda_pullup_enabled_ || scl_pullup_enabled_;
63 esp_err_t err = i2c_new_master_bus(&bus_conf, &this->bus_);
64 if (err != ESP_OK) {
65 ESP_LOGW(TAG, "i2c_new_master_bus failed: %s", esp_err_to_name(err));
66 this->mark_failed();
67 return;
68 }
69
70 i2c_device_config_t dev_conf{};
71 memset(&dev_conf, 0, sizeof(dev_conf));
72 dev_conf.dev_addr_length = I2C_ADDR_BIT_LEN_7;
73 dev_conf.device_address = I2C_DEVICE_ADDRESS_NOT_USED;
74 dev_conf.scl_speed_hz = this->frequency_;
75 dev_conf.scl_wait_us = this->timeout_;
76 err = i2c_master_bus_add_device(this->bus_, &dev_conf, &this->dev_);
77 if (err != ESP_OK) {
78 ESP_LOGW(TAG, "i2c_master_bus_add_device failed: %s", esp_err_to_name(err));
79 this->mark_failed();
80 return;
81 }
82
83 this->initialized_ = true;
84
85 if (this->scan_) {
86 ESP_LOGV(TAG, "Scanning for devices");
87 this->i2c_scan_();
88 }
89}
90
92 ESP_LOGCONFIG(TAG, "I2C Bus:");
93 ESP_LOGCONFIG(TAG,
94 " SDA Pin: GPIO%u\n"
95 " SCL Pin: GPIO%u\n"
96 " Frequency: %" PRIu32 " Hz",
97 this->sda_pin_, this->scl_pin_, this->frequency_);
98 if (timeout_ > 0) {
99 ESP_LOGCONFIG(TAG, " Timeout: %" PRIu32 "us", this->timeout_);
100 }
101 switch (this->recovery_result_) {
103 ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
104 break;
106 ESP_LOGCONFIG(TAG, " Recovery: failed, SCL is held low on the bus");
107 break;
109 ESP_LOGCONFIG(TAG, " Recovery: failed, SDA is held low on the bus");
110 break;
111 }
112 if (this->scan_) {
113 ESP_LOGCONFIG(TAG, "Results from bus scan:");
114 if (scan_results_.empty()) {
115 ESP_LOGCONFIG(TAG, "Found no devices");
116 } else {
117 for (const auto &s : scan_results_) {
118 if (s.second) {
119 ESP_LOGCONFIG(TAG, "Found device at address 0x%02X", s.first);
120 } else {
121 ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
122 }
123 }
124 }
125 }
126}
127
128ErrorCode IDFI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
129 size_t read_count) {
130 // logging is only enabled with v level, if warnings are shown the caller
131 // should log them
132 if (!initialized_) {
133 ESP_LOGW(TAG, "i2c bus not initialized!");
135 }
136
137 i2c_operation_job_t jobs[8]{};
138 size_t num_jobs = 0;
139 uint8_t write_addr = (address << 1) | I2C_MASTER_WRITE;
140 uint8_t read_addr = (address << 1) | I2C_MASTER_READ;
141 ESP_LOGV(TAG, "Writing %zu bytes, reading %zu bytes", write_count, read_count);
142 if (read_count == 0 && write_count == 0) {
143 // basically just a bus probe. Send a start, address and stop
144 ESP_LOGV(TAG, "0x%02X BUS PROBE", address);
145 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
146 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
147 jobs[num_jobs].write.ack_check = true;
148 jobs[num_jobs].write.data = &write_addr;
149 jobs[num_jobs++].write.total_bytes = 1;
150 } else {
151 if (write_count != 0) {
152#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
153 char hex_buf[format_hex_pretty_size(I2C_MAX_LOG_BYTES)];
154 ESP_LOGV(TAG, "0x%02X TX %s", address, format_hex_pretty_to(hex_buf, write_buffer, write_count));
155#endif
156 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
157 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
158 jobs[num_jobs].write.ack_check = true;
159 jobs[num_jobs].write.data = &write_addr;
160 jobs[num_jobs++].write.total_bytes = 1;
161 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
162 jobs[num_jobs].write.ack_check = true;
163 jobs[num_jobs].write.data = (uint8_t *) write_buffer;
164 jobs[num_jobs++].write.total_bytes = write_count;
165 }
166 if (read_count != 0) {
167 ESP_LOGV(TAG, "0x%02X RX bytes %zu", address, read_count);
168 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
169 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
170 jobs[num_jobs].write.ack_check = true;
171 jobs[num_jobs].write.data = &read_addr;
172 jobs[num_jobs++].write.total_bytes = 1;
173 if (read_count > 1) {
174 jobs[num_jobs].command = I2C_MASTER_CMD_READ;
175 jobs[num_jobs].read.ack_value = I2C_ACK_VAL;
176 jobs[num_jobs].read.data = read_buffer;
177 jobs[num_jobs++].read.total_bytes = read_count - 1;
178 }
179 jobs[num_jobs].command = I2C_MASTER_CMD_READ;
180 jobs[num_jobs].read.ack_value = I2C_NACK_VAL;
181 jobs[num_jobs].read.data = read_buffer + read_count - 1;
182 jobs[num_jobs++].read.total_bytes = 1;
183 }
184 }
185 jobs[num_jobs++].command = I2C_MASTER_CMD_STOP;
186 ESP_LOGV(TAG, "Sending %zu jobs", num_jobs);
187 esp_err_t err = i2c_master_execute_defined_operations(this->dev_, jobs, num_jobs, 100);
188 if (err == ESP_ERR_INVALID_STATE) {
189 ESP_LOGV(TAG, "TX to %02X failed: not acked", address);
191 } else if (err == ESP_ERR_TIMEOUT) {
192 ESP_LOGV(TAG, "TX to %02X failed: timeout", address);
193 return ERROR_TIMEOUT;
194 } else if (err != ESP_OK) {
195 ESP_LOGV(TAG, "TX to %02X failed: %s", address, esp_err_to_name(err));
196 return ERROR_UNKNOWN;
197 }
198 return ERROR_OK;
199}
200
204void IDFI2CBus::recover_() {
205 ESP_LOGI(TAG, "Performing bus recovery");
206
207 const auto scl_pin = static_cast<gpio_num_t>(scl_pin_);
208 const auto sda_pin = static_cast<gpio_num_t>(sda_pin_);
209
210 // For the upcoming operations, target for a 60kHz toggle frequency.
211 // 1000kHz is the maximum frequency for I2C running in standard-mode,
212 // but lower frequencies are not a problem.
213 // Note: the timing that is used here is chosen manually, to get
214 // results that are close to the timing that can be archieved by the
215 // implementation for the Arduino framework.
216 const auto half_period_usec = 7;
217
218 // Configure SCL pin for open drain input/output, with a pull up resistor.
219 gpio_set_level(scl_pin, 1);
220 gpio_config_t scl_config{};
221 scl_config.pin_bit_mask = 1ULL << scl_pin_;
222 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
223 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
224 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
225 scl_config.intr_type = GPIO_INTR_DISABLE;
226 gpio_config(&scl_config);
227
228 // Configure SDA pin for open drain input/output, with a pull up resistor.
229 gpio_set_level(sda_pin, 1);
230 gpio_config_t sda_conf{};
231 sda_conf.pin_bit_mask = 1ULL << sda_pin_;
232 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
233 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
234 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
235 sda_conf.intr_type = GPIO_INTR_DISABLE;
236 gpio_config(&sda_conf);
237
238 // If SCL is pulled low on the I2C bus, then some device is interfering
239 // with the SCL line. In that case, the I2C bus cannot be recovered.
240 delayMicroseconds(half_period_usec);
241 if (gpio_get_level(scl_pin) == 0) {
242 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW on the bus");
243 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
244 return;
245 }
246
247 // From the specification:
248 // "If the data line (SDA) is stuck LOW, send nine clock pulses. The
249 // device that held the bus LOW should release it sometime within
250 // those nine clocks."
251 // We don't really have to detect if SDA is stuck low. We'll simply send
252 // nine clock pulses here, just in case SDA is stuck. Actual checks on
253 // the SDA line status will be done after the clock pulses.
254 for (auto i = 0; i < 9; i++) {
255 gpio_set_level(scl_pin, 0);
256 delayMicroseconds(half_period_usec);
257 gpio_set_level(scl_pin, 1);
258 delayMicroseconds(half_period_usec);
259
260 // When SCL is kept LOW at this point, we might be looking at a device
261 // that applies clock stretching. Wait for the release of the SCL line,
262 // but not forever. There is no specification for the maximum allowed
263 // time. We yield and reset the WDT, so as to avoid triggering reset.
264 // No point in trying to recover the bus by forcing a uC reset. Bus
265 // should recover in a few ms or less else not likely to recovery at
266 // all.
267 auto wait = 250;
268 while (wait-- && gpio_get_level(scl_pin) == 0) {
269 App.feed_wdt();
270 delayMicroseconds(half_period_usec * 2);
271 }
272 if (gpio_get_level(scl_pin) == 0) {
273 ESP_LOGE(TAG, "Recovery failed: SCL is held LOW during clock pulse cycle");
274 recovery_result_ = RECOVERY_FAILED_SCL_LOW;
275 return;
276 }
277 }
278
279 // By now, any stuck device ought to have sent all remaining bits of its
280 // transaction, meaning that it should have freed up the SDA line, resulting
281 // in SDA being pulled up.
282 if (gpio_get_level(sda_pin) == 0) {
283 ESP_LOGE(TAG, "Recovery failed: SDA is held LOW after clock pulse cycle");
284 recovery_result_ = RECOVERY_FAILED_SDA_LOW;
285 return;
286 }
287
288 // From the specification:
289 // "I2C-bus compatible devices must reset their bus logic on receipt of
290 // a START or repeated START condition such that they all anticipate
291 // the sending of a target address, even if these START conditions are
292 // not positioned according to the proper format."
293 // While the 9 clock pulses from above might have drained all bits of a
294 // single byte within a transaction, a device might have more bytes to
295 // transmit. So here we'll generate a START condition to snap the device
296 // out of this state.
297 // SCL and SDA are already high at this point, so we can generate a START
298 // condition by making the SDA signal LOW.
299 delayMicroseconds(half_period_usec);
300 gpio_set_level(sda_pin, 0);
301
302 // From the specification:
303 // "A START condition immediately followed by a STOP condition (void
304 // message) is an illegal format. Many devices however are designed to
305 // operate properly under this condition."
306 // Finally, we'll bring the I2C bus into a starting state by generating
307 // a STOP condition.
308 delayMicroseconds(half_period_usec);
309 gpio_set_level(sda_pin, 1);
310
311 recovery_result_ = RECOVERY_COMPLETED;
312}
313
314} // namespace esphome::i2c
315#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
void feed_wdt(uint32_t time=0)
void mark_failed()
Mark this component as failed.
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:61
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:60
void i2c_scan_()
Scans the I2C bus for devices.
Definition i2c.cpp:12
i2c_master_bus_handle_t bus_
i2c_master_dev_handle_t dev_
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:17
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:16
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:18
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:20
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:29
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:353
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1103
Application App
Global storage of Application pointer - only one Application can exist.