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