14 static const char *
const TAG =
"i2c.idf";
17 ESP_LOGCONFIG(TAG,
"Setting up I2C bus...");
18 static i2c_port_t next_port = I2C_NUM_0;
21 next_port = (next_port == I2C_NUM_0) ? I2C_NUM_1 : I2C_NUM_MAX;
23 next_port = I2C_NUM_MAX;
26 if (
port_ == I2C_NUM_MAX) {
27 ESP_LOGE(TAG,
"Too many I2C buses configured");
35 memset(&conf, 0,
sizeof(conf));
36 conf.mode = I2C_MODE_MASTER;
42 esp_err_t err = i2c_param_config(
port_, &conf);
44 ESP_LOGW(TAG,
"i2c_param_config failed: %s", esp_err_to_name(err));
48 err = i2c_driver_install(
port_, I2C_MODE_MASTER, 0, 0, ESP_INTR_FLAG_IRAM);
50 ESP_LOGW(TAG,
"i2c_driver_install failed: %s", esp_err_to_name(err));
56 ESP_LOGV(TAG,
"Scanning i2c bus for active devices...");
61 ESP_LOGCONFIG(TAG,
"I2C Bus:");
62 ESP_LOGCONFIG(TAG,
" SDA Pin: GPIO%u", this->
sda_pin_);
63 ESP_LOGCONFIG(TAG,
" SCL Pin: GPIO%u", this->
scl_pin_);
64 ESP_LOGCONFIG(TAG,
" Frequency: %" PRIu32
" Hz", this->
frequency_);
65 switch (this->recovery_result_) {
67 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
70 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
73 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
77 ESP_LOGI(TAG,
"Results from i2c bus scan:");
79 ESP_LOGI(TAG,
"Found no i2c devices!");
83 ESP_LOGI(TAG,
"Found i2c device at address 0x%02X", s.first);
85 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
96 ESP_LOGVV(TAG,
"i2c bus not initialized!");
99 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
100 esp_err_t err = i2c_master_start(cmd);
102 ESP_LOGVV(TAG,
"RX from %02X master start failed: %s", address, esp_err_to_name(err));
103 i2c_cmd_link_delete(cmd);
106 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ,
true);
108 ESP_LOGVV(TAG,
"RX from %02X address write failed: %s", address, esp_err_to_name(err));
109 i2c_cmd_link_delete(cmd);
112 for (
size_t i = 0; i < cnt; i++) {
113 const auto &buf = buffers[i];
116 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
118 ESP_LOGVV(TAG,
"RX from %02X data read failed: %s", address, esp_err_to_name(err));
119 i2c_cmd_link_delete(cmd);
123 err = i2c_master_stop(cmd);
125 ESP_LOGVV(TAG,
"RX from %02X stop failed: %s", address, esp_err_to_name(err));
126 i2c_cmd_link_delete(cmd);
129 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
130 i2c_cmd_link_delete(cmd);
131 if (err == ESP_FAIL) {
133 ESP_LOGVV(TAG,
"RX from %02X failed: not acked", address);
135 }
else if (err == ESP_ERR_TIMEOUT) {
136 ESP_LOGVV(TAG,
"RX from %02X failed: timeout", address);
138 }
else if (err != ESP_OK) {
139 ESP_LOGVV(TAG,
"RX from %02X failed: %s", address, esp_err_to_name(err));
143 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 145 std::string debug_hex;
147 for (
size_t i = 0; i < cnt; i++) {
148 const auto &buf = buffers[i];
149 for (
size_t j = 0; j < buf.len; j++) {
150 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
151 debug_hex += debug_buf;
154 ESP_LOGVV(TAG,
"0x%02X RX %s", address, debug_hex.c_str());
163 ESP_LOGVV(TAG,
"i2c bus not initialized!");
167 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 169 std::string debug_hex;
171 for (
size_t i = 0; i < cnt; i++) {
172 const auto &buf = buffers[i];
173 for (
size_t j = 0; j < buf.len; j++) {
174 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
175 debug_hex += debug_buf;
178 ESP_LOGVV(TAG,
"0x%02X TX %s", address, debug_hex.c_str());
181 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
182 esp_err_t err = i2c_master_start(cmd);
184 ESP_LOGVV(TAG,
"TX to %02X master start failed: %s", address, esp_err_to_name(err));
185 i2c_cmd_link_delete(cmd);
188 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE,
true);
190 ESP_LOGVV(TAG,
"TX to %02X address write failed: %s", address, esp_err_to_name(err));
191 i2c_cmd_link_delete(cmd);
194 for (
size_t i = 0; i < cnt; i++) {
195 const auto &buf = buffers[i];
198 err = i2c_master_write(cmd, buf.data, buf.len,
true);
200 ESP_LOGVV(TAG,
"TX to %02X data write failed: %s", address, esp_err_to_name(err));
201 i2c_cmd_link_delete(cmd);
206 err = i2c_master_stop(cmd);
208 ESP_LOGVV(TAG,
"TX to %02X master stop failed: %s", address, esp_err_to_name(err));
209 i2c_cmd_link_delete(cmd);
213 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
214 i2c_cmd_link_delete(cmd);
215 if (err == ESP_FAIL) {
217 ESP_LOGVV(TAG,
"TX to %02X failed: not acked", address);
219 }
else if (err == ESP_ERR_TIMEOUT) {
220 ESP_LOGVV(TAG,
"TX to %02X failed: timeout", address);
222 }
else if (err != ESP_OK) {
223 ESP_LOGVV(TAG,
"TX to %02X failed: %s", address, esp_err_to_name(err));
232 void IDFI2CBus::recover_() {
233 ESP_LOGI(TAG,
"Performing I2C bus recovery");
235 const gpio_num_t scl_pin =
static_cast<gpio_num_t
>(
scl_pin_);
236 const gpio_num_t sda_pin =
static_cast<gpio_num_t
>(
sda_pin_);
244 const auto half_period_usec = 7;
247 gpio_set_level(scl_pin, 1);
248 gpio_config_t scl_config{};
249 scl_config.pin_bit_mask = 1ULL <<
scl_pin_;
250 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
251 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
252 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
253 scl_config.intr_type = GPIO_INTR_DISABLE;
254 gpio_config(&scl_config);
257 gpio_set_level(sda_pin, 1);
258 gpio_config_t sda_conf{};
259 sda_conf.pin_bit_mask = 1ULL <<
sda_pin_;
260 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
261 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
262 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
263 sda_conf.intr_type = GPIO_INTR_DISABLE;
264 gpio_config(&sda_conf);
269 if (gpio_get_level(scl_pin) == 0) {
270 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the I2C bus");
282 for (
auto i = 0; i < 9; i++) {
283 gpio_set_level(scl_pin, 0);
285 gpio_set_level(scl_pin, 1);
296 while (wait-- && gpio_get_level(scl_pin) == 0) {
300 if (gpio_get_level(scl_pin) == 0) {
301 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
310 if (gpio_get_level(sda_pin) == 0) {
311 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
328 gpio_set_level(sda_pin, 0);
337 gpio_set_level(sda_pin, 1);
345 #endif // USE_ESP_IDF
void dump_config() override
std::vector< std::pair< uint8_t, bool > > scan_results_
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.
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)