12 static const char *
const TAG =
"i2c.idf";
15 static i2c_port_t next_port = 0;
21 memset(&conf, 0,
sizeof(conf));
22 conf.mode = I2C_MODE_MASTER;
28 esp_err_t err = i2c_param_config(
port_, &conf);
30 ESP_LOGW(TAG,
"i2c_param_config failed: %s", esp_err_to_name(err));
34 err = i2c_driver_install(
port_, I2C_MODE_MASTER, 0, 0, ESP_INTR_FLAG_IRAM);
36 ESP_LOGW(TAG,
"i2c_driver_install failed: %s", esp_err_to_name(err));
42 ESP_LOGV(TAG,
"Scanning i2c bus for active devices...");
47 ESP_LOGCONFIG(TAG,
"I2C Bus:");
48 ESP_LOGCONFIG(TAG,
" SDA Pin: GPIO%u", this->
sda_pin_);
49 ESP_LOGCONFIG(TAG,
" SCL Pin: GPIO%u", this->
scl_pin_);
50 ESP_LOGCONFIG(TAG,
" Frequency: %u Hz", this->
frequency_);
51 switch (this->recovery_result_) {
53 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
56 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
59 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
63 ESP_LOGI(TAG,
"Results from i2c bus scan:");
65 ESP_LOGI(TAG,
"Found no i2c devices!");
69 ESP_LOGI(TAG,
"Found i2c device at address 0x%02X", s.first);
71 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
82 ESP_LOGVV(TAG,
"i2c bus not initialized!");
85 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
86 esp_err_t err = i2c_master_start(cmd);
88 ESP_LOGVV(TAG,
"RX from %02X master start failed: %s", address, esp_err_to_name(err));
89 i2c_cmd_link_delete(cmd);
92 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ,
true);
94 ESP_LOGVV(TAG,
"RX from %02X address write failed: %s", address, esp_err_to_name(err));
95 i2c_cmd_link_delete(cmd);
98 for (
size_t i = 0; i < cnt; i++) {
99 const auto &buf = buffers[i];
102 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
104 ESP_LOGVV(TAG,
"RX from %02X data read failed: %s", address, esp_err_to_name(err));
105 i2c_cmd_link_delete(cmd);
109 err = i2c_master_stop(cmd);
111 ESP_LOGVV(TAG,
"RX from %02X stop failed: %s", address, esp_err_to_name(err));
112 i2c_cmd_link_delete(cmd);
115 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
116 i2c_cmd_link_delete(cmd);
117 if (err == ESP_FAIL) {
119 ESP_LOGVV(TAG,
"RX from %02X failed: not acked", address);
121 }
else if (err == ESP_ERR_TIMEOUT) {
122 ESP_LOGVV(TAG,
"RX from %02X failed: timeout", address);
124 }
else if (err != ESP_OK) {
125 ESP_LOGVV(TAG,
"RX from %02X failed: %s", address, esp_err_to_name(err));
129 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 131 std::string debug_hex;
133 for (
size_t i = 0; i < cnt; i++) {
134 const auto &buf = buffers[i];
135 for (
size_t j = 0; j < buf.len; j++) {
136 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
137 debug_hex += debug_buf;
140 ESP_LOGVV(TAG,
"0x%02X RX %s", address, debug_hex.c_str());
149 ESP_LOGVV(TAG,
"i2c bus not initialized!");
153 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 155 std::string debug_hex;
157 for (
size_t i = 0; i < cnt; i++) {
158 const auto &buf = buffers[i];
159 for (
size_t j = 0; j < buf.len; j++) {
160 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
161 debug_hex += debug_buf;
164 ESP_LOGVV(TAG,
"0x%02X TX %s", address, debug_hex.c_str());
167 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
168 esp_err_t err = i2c_master_start(cmd);
170 ESP_LOGVV(TAG,
"TX to %02X master start failed: %s", address, esp_err_to_name(err));
171 i2c_cmd_link_delete(cmd);
174 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE,
true);
176 ESP_LOGVV(TAG,
"TX to %02X address write failed: %s", address, esp_err_to_name(err));
177 i2c_cmd_link_delete(cmd);
180 for (
size_t i = 0; i < cnt; i++) {
181 const auto &buf = buffers[i];
184 err = i2c_master_write(cmd, buf.data, buf.len,
true);
186 ESP_LOGVV(TAG,
"TX to %02X data write failed: %s", address, esp_err_to_name(err));
187 i2c_cmd_link_delete(cmd);
191 err = i2c_master_stop(cmd);
193 ESP_LOGVV(TAG,
"TX to %02X master stop failed: %s", address, esp_err_to_name(err));
194 i2c_cmd_link_delete(cmd);
197 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
198 i2c_cmd_link_delete(cmd);
199 if (err == ESP_FAIL) {
201 ESP_LOGVV(TAG,
"TX to %02X failed: not acked", address);
203 }
else if (err == ESP_ERR_TIMEOUT) {
204 ESP_LOGVV(TAG,
"TX to %02X failed: timeout", address);
206 }
else if (err != ESP_OK) {
207 ESP_LOGVV(TAG,
"TX to %02X failed: %s", address, esp_err_to_name(err));
216 void IDFI2CBus::recover_() {
217 ESP_LOGI(TAG,
"Performing I2C bus recovery");
219 const gpio_num_t scl_pin =
static_cast<gpio_num_t
>(
scl_pin_);
220 const gpio_num_t sda_pin =
static_cast<gpio_num_t
>(
sda_pin_);
228 const auto half_period_usec = 7;
231 gpio_set_level(scl_pin, 1);
232 gpio_config_t scl_config{};
233 scl_config.pin_bit_mask = 1ULL <<
scl_pin_;
234 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
235 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
236 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
237 scl_config.intr_type = GPIO_INTR_DISABLE;
238 gpio_config(&scl_config);
241 gpio_set_level(sda_pin, 1);
242 gpio_config_t sda_conf{};
243 sda_conf.pin_bit_mask = 1ULL <<
sda_pin_;
244 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
245 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
246 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
247 sda_conf.intr_type = GPIO_INTR_DISABLE;
248 gpio_config(&sda_conf);
253 if (gpio_get_level(scl_pin) == 0) {
254 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the I2C bus");
266 for (
auto i = 0; i < 9; i++) {
267 gpio_set_level(scl_pin, 0);
269 gpio_set_level(scl_pin, 1);
277 while (wait-- && gpio_get_level(scl_pin) == 0) {
280 if (gpio_get_level(scl_pin) == 0) {
281 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
290 if (gpio_get_level(sda_pin) == 0) {
291 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
308 gpio_set_level(sda_pin, 0);
317 gpio_set_level(sda_pin, 1);
325 #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
virtual void mark_failed()
Mark this component as failed.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
void IRAM_ATTR HOT delay(uint32_t ms)