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));
50 ESP_LOGW(TAG,
"i2c timeout of %" PRIu32
"us greater than max of 13ms on esp-idf, setting to max",
timeout_);
55 ESP_LOGW(TAG,
"i2c_set_timeout failed: %s", esp_err_to_name(err));
59 ESP_LOGV(TAG,
"i2c_timeout set to %" PRIu32
" ticks (%" PRIu32
" us)",
timeout_ * 80,
timeout_);
62 err = i2c_driver_install(
port_, I2C_MODE_MASTER, 0, 0, ESP_INTR_FLAG_IRAM);
64 ESP_LOGW(TAG,
"i2c_driver_install failed: %s", esp_err_to_name(err));
70 ESP_LOGV(TAG,
"Scanning i2c bus for active devices...");
75 ESP_LOGCONFIG(TAG,
"I2C Bus:");
76 ESP_LOGCONFIG(TAG,
" SDA Pin: GPIO%u", this->
sda_pin_);
77 ESP_LOGCONFIG(TAG,
" SCL Pin: GPIO%u", this->
scl_pin_);
78 ESP_LOGCONFIG(TAG,
" Frequency: %" PRIu32
" Hz", this->
frequency_);
80 ESP_LOGCONFIG(TAG,
" Timeout: %" PRIu32
"us", this->
timeout_);
82 switch (this->recovery_result_) {
84 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
87 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
90 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
94 ESP_LOGI(TAG,
"Results from i2c bus scan:");
96 ESP_LOGI(TAG,
"Found no i2c devices!");
100 ESP_LOGI(TAG,
"Found i2c device at address 0x%02X", s.first);
102 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
113 ESP_LOGVV(TAG,
"i2c bus not initialized!");
116 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
117 esp_err_t err = i2c_master_start(cmd);
119 ESP_LOGVV(TAG,
"RX from %02X master start failed: %s", address, esp_err_to_name(err));
120 i2c_cmd_link_delete(cmd);
123 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ,
true);
125 ESP_LOGVV(TAG,
"RX from %02X address write failed: %s", address, esp_err_to_name(err));
126 i2c_cmd_link_delete(cmd);
129 for (
size_t i = 0; i < cnt; i++) {
130 const auto &buf = buffers[i];
133 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
135 ESP_LOGVV(TAG,
"RX from %02X data read failed: %s", address, esp_err_to_name(err));
136 i2c_cmd_link_delete(cmd);
140 err = i2c_master_stop(cmd);
142 ESP_LOGVV(TAG,
"RX from %02X stop failed: %s", address, esp_err_to_name(err));
143 i2c_cmd_link_delete(cmd);
146 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
149 i2c_cmd_link_delete(cmd);
150 if (err == ESP_FAIL) {
152 ESP_LOGVV(TAG,
"RX from %02X failed: not acked", address);
154 }
else if (err == ESP_ERR_TIMEOUT) {
155 ESP_LOGVV(TAG,
"RX from %02X failed: timeout", address);
157 }
else if (err != ESP_OK) {
158 ESP_LOGVV(TAG,
"RX from %02X failed: %s", address, esp_err_to_name(err));
162 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 164 std::string debug_hex;
166 for (
size_t i = 0; i < cnt; i++) {
167 const auto &buf = buffers[i];
168 for (
size_t j = 0; j < buf.len; j++) {
169 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
170 debug_hex += debug_buf;
173 ESP_LOGVV(TAG,
"0x%02X RX %s", address, debug_hex.c_str());
182 ESP_LOGVV(TAG,
"i2c bus not initialized!");
186 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 188 std::string debug_hex;
190 for (
size_t i = 0; i < cnt; i++) {
191 const auto &buf = buffers[i];
192 for (
size_t j = 0; j < buf.len; j++) {
193 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
194 debug_hex += debug_buf;
197 ESP_LOGVV(TAG,
"0x%02X TX %s", address, debug_hex.c_str());
200 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
201 esp_err_t err = i2c_master_start(cmd);
203 ESP_LOGVV(TAG,
"TX to %02X master start failed: %s", address, esp_err_to_name(err));
204 i2c_cmd_link_delete(cmd);
207 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE,
true);
209 ESP_LOGVV(TAG,
"TX to %02X address write failed: %s", address, esp_err_to_name(err));
210 i2c_cmd_link_delete(cmd);
213 for (
size_t i = 0; i < cnt; i++) {
214 const auto &buf = buffers[i];
217 err = i2c_master_write(cmd, buf.data, buf.len,
true);
219 ESP_LOGVV(TAG,
"TX to %02X data write failed: %s", address, esp_err_to_name(err));
220 i2c_cmd_link_delete(cmd);
225 err = i2c_master_stop(cmd);
227 ESP_LOGVV(TAG,
"TX to %02X master stop failed: %s", address, esp_err_to_name(err));
228 i2c_cmd_link_delete(cmd);
232 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
233 i2c_cmd_link_delete(cmd);
234 if (err == ESP_FAIL) {
236 ESP_LOGVV(TAG,
"TX to %02X failed: not acked", address);
238 }
else if (err == ESP_ERR_TIMEOUT) {
239 ESP_LOGVV(TAG,
"TX to %02X failed: timeout", address);
241 }
else if (err != ESP_OK) {
242 ESP_LOGVV(TAG,
"TX to %02X failed: %s", address, esp_err_to_name(err));
251 void IDFI2CBus::recover_() {
252 ESP_LOGI(TAG,
"Performing I2C bus recovery");
254 const gpio_num_t scl_pin =
static_cast<gpio_num_t
>(
scl_pin_);
255 const gpio_num_t sda_pin =
static_cast<gpio_num_t
>(
sda_pin_);
263 const auto half_period_usec = 7;
266 gpio_set_level(scl_pin, 1);
267 gpio_config_t scl_config{};
268 scl_config.pin_bit_mask = 1ULL <<
scl_pin_;
269 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
270 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
271 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
272 scl_config.intr_type = GPIO_INTR_DISABLE;
273 gpio_config(&scl_config);
276 gpio_set_level(sda_pin, 1);
277 gpio_config_t sda_conf{};
278 sda_conf.pin_bit_mask = 1ULL <<
sda_pin_;
279 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
280 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
281 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
282 sda_conf.intr_type = GPIO_INTR_DISABLE;
283 gpio_config(&sda_conf);
288 if (gpio_get_level(scl_pin) == 0) {
289 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the I2C bus");
301 for (
auto i = 0; i < 9; i++) {
302 gpio_set_level(scl_pin, 0);
304 gpio_set_level(scl_pin, 1);
315 while (wait-- && gpio_get_level(scl_pin) == 0) {
319 if (gpio_get_level(scl_pin) == 0) {
320 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
329 if (gpio_get_level(sda_pin) == 0) {
330 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
347 gpio_set_level(sda_pin, 0);
356 gpio_set_level(sda_pin, 1);
364 #endif // USE_ESP_IDF the WriteBuffer structure stores a pointer to a write buffer and its length
void dump_config() override
void i2c_scan_()
Scans the I2C bus for devices.
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
the ReadBuffer structure stores a pointer to a read buffer and its length
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override
timeout while waiting to receive bytes
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override
No error found during execution of method.
I2C bus acknowledgment not received.
Application App
Global storage of Application pointer - only one Application can exist.
bool scan_
Should we scan ? Can be set in the yaml.
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
miscellaneous I2C error during execution
call method to a not initialized bus