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. Max %u supported.", SOC_I2C_NUM);
35 memset(&conf, 0,
sizeof(conf));
36 conf.mode = I2C_MODE_MASTER;
42 #ifdef USE_ESP32_VARIANT_ESP32S2 44 conf.clk_flags = I2C_SCLK_SRC_FLAG_AWARE_DFS;
46 esp_err_t err = i2c_param_config(
port_, &conf);
48 ESP_LOGW(TAG,
"i2c_param_config failed: %s", esp_err_to_name(err));
54 ESP_LOGW(TAG,
"i2c timeout of %" PRIu32
"us greater than max of 13ms on esp-idf, setting to max",
timeout_);
59 ESP_LOGW(TAG,
"i2c_set_timeout failed: %s", esp_err_to_name(err));
63 ESP_LOGV(TAG,
"i2c_timeout set to %" PRIu32
" ticks (%" PRIu32
" us)",
timeout_ * 80,
timeout_);
66 err = i2c_driver_install(
port_, I2C_MODE_MASTER, 0, 0, ESP_INTR_FLAG_IRAM);
68 ESP_LOGW(TAG,
"i2c_driver_install failed: %s", esp_err_to_name(err));
74 ESP_LOGV(TAG,
"Scanning i2c bus for active devices...");
79 ESP_LOGCONFIG(TAG,
"I2C Bus:");
80 ESP_LOGCONFIG(TAG,
" SDA Pin: GPIO%u", this->
sda_pin_);
81 ESP_LOGCONFIG(TAG,
" SCL Pin: GPIO%u", this->
scl_pin_);
82 ESP_LOGCONFIG(TAG,
" Frequency: %" PRIu32
" Hz", this->
frequency_);
84 ESP_LOGCONFIG(TAG,
" Timeout: %" PRIu32
"us", this->
timeout_);
86 switch (this->recovery_result_) {
88 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
91 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
94 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
98 ESP_LOGI(TAG,
"Results from i2c bus scan:");
100 ESP_LOGI(TAG,
"Found no i2c devices!");
104 ESP_LOGI(TAG,
"Found i2c device at address 0x%02X", s.first);
106 ESP_LOGE(TAG,
"Unknown error at address 0x%02X", s.first);
117 ESP_LOGVV(TAG,
"i2c bus not initialized!");
120 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
121 esp_err_t err = i2c_master_start(cmd);
123 ESP_LOGVV(TAG,
"RX from %02X master start failed: %s", address, esp_err_to_name(err));
124 i2c_cmd_link_delete(cmd);
127 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ,
true);
129 ESP_LOGVV(TAG,
"RX from %02X address write failed: %s", address, esp_err_to_name(err));
130 i2c_cmd_link_delete(cmd);
133 for (
size_t i = 0; i < cnt; i++) {
134 const auto &buf = buffers[i];
137 err = i2c_master_read(cmd, buf.data, buf.len, i == cnt - 1 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);
139 ESP_LOGVV(TAG,
"RX from %02X data read failed: %s", address, esp_err_to_name(err));
140 i2c_cmd_link_delete(cmd);
144 err = i2c_master_stop(cmd);
146 ESP_LOGVV(TAG,
"RX from %02X stop failed: %s", address, esp_err_to_name(err));
147 i2c_cmd_link_delete(cmd);
150 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
153 i2c_cmd_link_delete(cmd);
154 if (err == ESP_FAIL) {
156 ESP_LOGVV(TAG,
"RX from %02X failed: not acked", address);
158 }
else if (err == ESP_ERR_TIMEOUT) {
159 ESP_LOGVV(TAG,
"RX from %02X failed: timeout", address);
161 }
else if (err != ESP_OK) {
162 ESP_LOGVV(TAG,
"RX from %02X failed: %s", address, esp_err_to_name(err));
166 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 168 std::string debug_hex;
170 for (
size_t i = 0; i < cnt; i++) {
171 const auto &buf = buffers[i];
172 for (
size_t j = 0; j < buf.len; j++) {
173 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
174 debug_hex += debug_buf;
177 ESP_LOGVV(TAG,
"0x%02X RX %s", address, debug_hex.c_str());
186 ESP_LOGVV(TAG,
"i2c bus not initialized!");
190 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 192 std::string debug_hex;
194 for (
size_t i = 0; i < cnt; i++) {
195 const auto &buf = buffers[i];
196 for (
size_t j = 0; j < buf.len; j++) {
197 snprintf(debug_buf,
sizeof(debug_buf),
"%02X", buf.data[j]);
198 debug_hex += debug_buf;
201 ESP_LOGVV(TAG,
"0x%02X TX %s", address, debug_hex.c_str());
204 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
205 esp_err_t err = i2c_master_start(cmd);
207 ESP_LOGVV(TAG,
"TX to %02X master start failed: %s", address, esp_err_to_name(err));
208 i2c_cmd_link_delete(cmd);
211 err = i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE,
true);
213 ESP_LOGVV(TAG,
"TX to %02X address write failed: %s", address, esp_err_to_name(err));
214 i2c_cmd_link_delete(cmd);
217 for (
size_t i = 0; i < cnt; i++) {
218 const auto &buf = buffers[i];
221 err = i2c_master_write(cmd, buf.data, buf.len,
true);
223 ESP_LOGVV(TAG,
"TX to %02X data write failed: %s", address, esp_err_to_name(err));
224 i2c_cmd_link_delete(cmd);
229 err = i2c_master_stop(cmd);
231 ESP_LOGVV(TAG,
"TX to %02X master stop failed: %s", address, esp_err_to_name(err));
232 i2c_cmd_link_delete(cmd);
236 err = i2c_master_cmd_begin(
port_, cmd, 20 / portTICK_PERIOD_MS);
237 i2c_cmd_link_delete(cmd);
238 if (err == ESP_FAIL) {
240 ESP_LOGVV(TAG,
"TX to %02X failed: not acked", address);
242 }
else if (err == ESP_ERR_TIMEOUT) {
243 ESP_LOGVV(TAG,
"TX to %02X failed: timeout", address);
245 }
else if (err != ESP_OK) {
246 ESP_LOGVV(TAG,
"TX to %02X failed: %s", address, esp_err_to_name(err));
255 void IDFI2CBus::recover_() {
256 ESP_LOGI(TAG,
"Performing I2C bus recovery");
258 const gpio_num_t scl_pin =
static_cast<gpio_num_t
>(
scl_pin_);
259 const gpio_num_t sda_pin =
static_cast<gpio_num_t
>(
sda_pin_);
267 const auto half_period_usec = 7;
270 gpio_set_level(scl_pin, 1);
271 gpio_config_t scl_config{};
272 scl_config.pin_bit_mask = 1ULL <<
scl_pin_;
273 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
274 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
275 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
276 scl_config.intr_type = GPIO_INTR_DISABLE;
277 gpio_config(&scl_config);
280 gpio_set_level(sda_pin, 1);
281 gpio_config_t sda_conf{};
282 sda_conf.pin_bit_mask = 1ULL <<
sda_pin_;
283 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
284 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
285 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
286 sda_conf.intr_type = GPIO_INTR_DISABLE;
287 gpio_config(&sda_conf);
292 if (gpio_get_level(scl_pin) == 0) {
293 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the I2C bus");
305 for (
auto i = 0; i < 9; i++) {
306 gpio_set_level(scl_pin, 0);
308 gpio_set_level(scl_pin, 1);
319 while (wait-- && gpio_get_level(scl_pin) == 0) {
323 if (gpio_get_level(scl_pin) == 0) {
324 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
333 if (gpio_get_level(sda_pin) == 0) {
334 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
351 gpio_set_level(sda_pin, 0);
360 gpio_set_level(sda_pin, 1);
368 #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