14 static const char *
const TAG =
"i2c.idf";
17 static i2c_port_t next_port = 0;
23 memset(&conf, 0,
sizeof(conf));
24 conf.mode = I2C_MODE_MASTER;
30 esp_err_t err = i2c_param_config(
port_, &conf);
32 ESP_LOGW(TAG,
"i2c_param_config failed: %s", esp_err_to_name(err));
36 err = i2c_driver_install(
port_, I2C_MODE_MASTER, 0, 0, ESP_INTR_FLAG_IRAM);
38 ESP_LOGW(TAG,
"i2c_driver_install failed: %s", esp_err_to_name(err));
44 ESP_LOGV(TAG,
"Scanning i2c bus for active devices...");
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_) {
55 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
58 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
61 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
65 ESP_LOGI(TAG,
"Results from i2c bus scan:");
67 ESP_LOGI(TAG,
"Found no i2c devices!");
71 ESP_LOGI(TAG,
"Found i2c device at address 0x%02X", s.first);
73 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
84 ESP_LOGVV(TAG,
"i2c bus not initialized!");
87 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
88 esp_err_t err = i2c_master_start(cmd);
90 ESP_LOGVV(TAG,
"RX from %02X master start failed: %s", address, esp_err_to_name(err));
91 i2c_cmd_link_delete(cmd);
94 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ,
true);
96 ESP_LOGVV(TAG,
"RX from %02X address write failed: %s", address, esp_err_to_name(err));
97 i2c_cmd_link_delete(cmd);
100 for (
size_t i = 0; i < cnt; i++) {
101 const auto &buf = buffers[i];
104 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
106 ESP_LOGVV(TAG,
"RX from %02X data read failed: %s", address, esp_err_to_name(err));
107 i2c_cmd_link_delete(cmd);
111 err = i2c_master_stop(cmd);
113 ESP_LOGVV(TAG,
"RX from %02X stop failed: %s", address, esp_err_to_name(err));
114 i2c_cmd_link_delete(cmd);
117 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
118 i2c_cmd_link_delete(cmd);
119 if (err == ESP_FAIL) {
121 ESP_LOGVV(TAG,
"RX from %02X failed: not acked", address);
123 }
else if (err == ESP_ERR_TIMEOUT) {
124 ESP_LOGVV(TAG,
"RX from %02X failed: timeout", address);
126 }
else if (err != ESP_OK) {
127 ESP_LOGVV(TAG,
"RX from %02X failed: %s", address, esp_err_to_name(err));
131 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 133 std::string debug_hex;
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;
142 ESP_LOGVV(TAG,
"0x%02X RX %s", address, debug_hex.c_str());
151 ESP_LOGVV(TAG,
"i2c bus not initialized!");
155 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 157 std::string debug_hex;
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;
166 ESP_LOGVV(TAG,
"0x%02X TX %s", address, debug_hex.c_str());
169 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
170 esp_err_t err = i2c_master_start(cmd);
172 ESP_LOGVV(TAG,
"TX to %02X master start failed: %s", address, esp_err_to_name(err));
173 i2c_cmd_link_delete(cmd);
176 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE,
true);
178 ESP_LOGVV(TAG,
"TX to %02X address write failed: %s", address, esp_err_to_name(err));
179 i2c_cmd_link_delete(cmd);
182 for (
size_t i = 0; i < cnt; i++) {
183 const auto &buf = buffers[i];
186 err = i2c_master_write(cmd, buf.data, buf.len,
true);
188 ESP_LOGVV(TAG,
"TX to %02X data write failed: %s", address, esp_err_to_name(err));
189 i2c_cmd_link_delete(cmd);
193 err = i2c_master_stop(cmd);
195 ESP_LOGVV(TAG,
"TX to %02X master stop failed: %s", address, esp_err_to_name(err));
196 i2c_cmd_link_delete(cmd);
199 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
200 i2c_cmd_link_delete(cmd);
201 if (err == ESP_FAIL) {
203 ESP_LOGVV(TAG,
"TX to %02X failed: not acked", address);
205 }
else if (err == ESP_ERR_TIMEOUT) {
206 ESP_LOGVV(TAG,
"TX to %02X failed: timeout", address);
208 }
else if (err != ESP_OK) {
209 ESP_LOGVV(TAG,
"TX to %02X failed: %s", address, esp_err_to_name(err));
218 void IDFI2CBus::recover_() {
219 ESP_LOGI(TAG,
"Performing I2C bus recovery");
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_);
230 const auto half_period_usec = 7;
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);
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);
255 if (gpio_get_level(scl_pin) == 0) {
256 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the I2C bus");
268 for (
auto i = 0; i < 9; i++) {
269 gpio_set_level(scl_pin, 0);
271 gpio_set_level(scl_pin, 1);
282 while (wait-- && gpio_get_level(scl_pin) == 0) {
286 if (gpio_get_level(scl_pin) == 0) {
287 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
296 if (gpio_get_level(sda_pin) == 0) {
297 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
314 gpio_set_level(sda_pin, 0);
323 gpio_set_level(sda_pin, 1);
331 #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.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)