5#include <driver/gpio.h>
13#ifdef USE_I2C_ISR_GUARD
14#include <esp_intr_alloc.h>
15#include <hal/i2c_ll.h>
16#if SOC_I2C_SUPPORT_HW_FSM_RST || I2C_LL_SUPPORT_HW_FSM_RST
17#error "This chip has an I2C FSM reset; the ISR guard is no longer needed"
23static constexpr uint32_t I2C_ERROR_INTR = I2C_LL_INTR_NACK | I2C_LL_INTR_TIMEOUT | I2C_LL_INTR_ARBITRATION;
24static_assert((I2C_LL_MASTER_EVENT_INTR & I2C_ERROR_INTR) == I2C_ERROR_INTR,
25 "The master event mask must cover the error interrupts");
28 intr_handler_t handler;
32static I2CIsrGuard i2c_isr_guards[SOC_HP_I2C_NUM];
34static void IRAM_ATTR i2c_isr_guard(
void *arg) {
35 auto *guard =
static_cast<I2CIsrGuard *
>(arg);
37 i2c_ll_get_intr_mask(guard->dev, &before);
38 guard->handler(guard->arg);
40 i2c_ll_get_intr_mask(guard->dev, &after);
41 if (before & after & I2C_ERROR_INTR)
42 i2c_ll_disable_intr_mask(guard->dev, I2C_LL_MASTER_EVENT_INTR);
45static int i2c_port_for_source(
int source) {
46 for (
int port = 0; port < SOC_HP_I2C_NUM; port++) {
47 if (source == i2c_periph_signal[port].irq)
57 void *arg, intr_handle_t *ret_handle);
60 void *arg, intr_handle_t *ret_handle) {
61 int port = esphome::i2c::i2c_port_for_source(source);
64 auto &guard = esphome::i2c::i2c_isr_guards[port];
65 auto previous = guard;
66 guard = {handler, arg, I2C_LL_GET_HW(port)};
82static constexpr size_t I2C_MAX_LOG_BYTES = 32;
85 static i2c_port_t next_hp_port = I2C_NUM_0;
86#if SOC_LP_I2C_SUPPORTED
87 static i2c_port_t next_lp_port = LP_I2C_NUM_0;
91 ESP_LOGW(TAG,
"Using max allowed timeout: 13 ms");
97 i2c_master_bus_config_t bus_conf{};
98 memset(&bus_conf, 0,
sizeof(bus_conf));
99 bus_conf.sda_io_num = gpio_num_t(this->
sda_pin_);
100 bus_conf.scl_io_num = gpio_num_t(this->
scl_pin_);
101 bus_conf.glitch_ignore_cnt = 7;
102#if SOC_LP_I2C_SUPPORTED
104 if ((next_lp_port - LP_I2C_NUM_0) == SOC_LP_I2C_NUM) {
105 ESP_LOGE(TAG,
"No more than %u LP buses supported", SOC_LP_I2C_NUM);
109 this->
port_ = next_lp_port;
110 next_lp_port = (i2c_port_t) (next_lp_port + 1);
111 bus_conf.lp_source_clk = LP_I2C_SCLK_DEFAULT;
114 if (next_hp_port == SOC_HP_I2C_NUM) {
115 ESP_LOGE(TAG,
"No more than %u HP buses supported", SOC_HP_I2C_NUM);
119 this->
port_ = next_hp_port;
120 next_hp_port = (i2c_port_t) (next_hp_port + 1);
121 bus_conf.clk_source = I2C_CLK_SRC_DEFAULT;
122#if SOC_LP_I2C_SUPPORTED
125 bus_conf.i2c_port = this->
port_;
127 esp_err_t err = i2c_new_master_bus(&bus_conf, &this->
bus_);
129 ESP_LOGW(TAG,
"i2c_new_master_bus failed: %s", esp_err_to_name(err));
135 if (this->
dev_ ==
nullptr) {
139#ifdef I2C_PORT_FREQUENCY_COUNT
141 for (
auto &device : this->
devices_) {
142 device.dev = device.frequency == this->
frequency_ ? this->
dev_ : this->add_device_(device.frequency);
143 if (device.dev ==
nullptr) {
153 ESP_LOGV(TAG,
"Scanning for devices");
159 i2c_device_config_t dev_conf{};
160 dev_conf.dev_addr_length = I2C_ADDR_BIT_LEN_7;
161 dev_conf.device_address = I2C_DEVICE_ADDRESS_NOT_USED;
163 dev_conf.scl_wait_us = this->
timeout_;
164 i2c_master_dev_handle_t dev{};
165 const esp_err_t err = i2c_master_bus_add_device(this->
bus_, &dev_conf, &dev);
167 ESP_LOGW(TAG,
"i2c_master_bus_add_device failed: %s", esp_err_to_name(err));
174 ESP_LOGCONFIG(TAG,
"I2C Bus:");
178 " Frequency: %" PRIu32
" Hz",
181 ESP_LOGCONFIG(TAG,
" Timeout: %" PRIu32
"us", this->
timeout_);
183#ifdef USE_I2C_ISR_GUARD
185 if (i2c_isr_guards[this->
port_].handler ==
nullptr) {
186 ESP_LOGW(TAG,
" Interrupt guard inactive");
189 switch (this->recovery_result_) {
191 ESP_LOGCONFIG(TAG,
" Recovery: bus successfully recovered");
194 ESP_LOGCONFIG(TAG,
" Recovery: failed, SCL is held low on the bus");
197 ESP_LOGCONFIG(TAG,
" Recovery: failed, SDA is held low on the bus");
208 ESP_LOGW(TAG,
"i2c bus not initialized!");
212 i2c_operation_job_t jobs[8]{};
214 uint8_t write_addr = (
address << 1) | I2C_MASTER_WRITE;
215 uint8_t read_addr = (
address << 1) | I2C_MASTER_READ;
216 ESP_LOGV(TAG,
"Writing %zu bytes, reading %zu bytes", write_count, read_count);
217 if (read_count == 0 && write_count == 0) {
219 ESP_LOGV(TAG,
"0x%02X BUS PROBE",
address);
220 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
221 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
222 jobs[num_jobs].write.ack_check =
true;
223 jobs[num_jobs].write.data = &write_addr;
224 jobs[num_jobs++].write.total_bytes = 1;
226 if (write_count != 0) {
227 ESP_LOGV_HEX(TAG, write_buffer, write_count, I2C_MAX_LOG_BYTES,
"0x%02X TX %s",
address);
228 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
229 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
230 jobs[num_jobs].write.ack_check =
true;
231 jobs[num_jobs].write.data = &write_addr;
232 jobs[num_jobs++].write.total_bytes = 1;
233 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
234 jobs[num_jobs].write.ack_check =
true;
235 jobs[num_jobs].write.data = (uint8_t *) write_buffer;
236 jobs[num_jobs++].write.total_bytes = write_count;
238 if (read_count != 0) {
239 ESP_LOGV(TAG,
"0x%02X RX bytes %zu",
address, read_count);
240 jobs[num_jobs++].command = I2C_MASTER_CMD_START;
241 jobs[num_jobs].command = I2C_MASTER_CMD_WRITE;
242 jobs[num_jobs].write.ack_check =
true;
243 jobs[num_jobs].write.data = &read_addr;
244 jobs[num_jobs++].write.total_bytes = 1;
245 if (read_count > 1) {
246 jobs[num_jobs].command = I2C_MASTER_CMD_READ;
247 jobs[num_jobs].read.ack_value = I2C_ACK_VAL;
248 jobs[num_jobs].read.data = read_buffer;
249 jobs[num_jobs++].read.total_bytes = read_count - 1;
251 jobs[num_jobs].command = I2C_MASTER_CMD_READ;
252 jobs[num_jobs].read.ack_value = I2C_NACK_VAL;
253 jobs[num_jobs].read.data = read_buffer + read_count - 1;
254 jobs[num_jobs++].read.total_bytes = 1;
257 jobs[num_jobs++].command = I2C_MASTER_CMD_STOP;
258 ESP_LOGV(TAG,
"Sending %zu jobs", num_jobs);
259 esp_err_t err = i2c_master_execute_defined_operations(this->
dev_, jobs, num_jobs, 100);
260 if (err == ESP_ERR_INVALID_STATE || err == ESP_ERR_INVALID_RESPONSE) {
261 ESP_LOGV(TAG,
"TX to %02X failed: not acked",
address);
263 }
else if (err == ESP_ERR_TIMEOUT) {
264 ESP_LOGV(TAG,
"TX to %02X failed: timeout",
address);
266 }
else if (err != ESP_OK) {
267 ESP_LOGV(TAG,
"TX to %02X failed: %s",
address, esp_err_to_name(err));
273#ifdef I2C_PORT_FREQUENCY_COUNT
275 for (
const auto &device : this->
devices_) {
280 this->devices_.push_back({
frequency,
nullptr});
292 for (
const auto &device : this->
devices_) {
294 this->
dev_ = device.dev;
306void IDFI2CBus::recover_() {
307 ESP_LOGI(TAG,
"Performing bus recovery");
309 const auto scl_pin =
static_cast<gpio_num_t
>(this->
scl_pin_);
310 const auto sda_pin =
static_cast<gpio_num_t
>(this->
sda_pin_);
318 const auto half_period_usec = 7;
321 gpio_set_level(scl_pin, 1);
322 gpio_config_t scl_config{};
323 scl_config.pin_bit_mask = 1ULL << this->
scl_pin_;
324 scl_config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
325 scl_config.pull_up_en = GPIO_PULLUP_ENABLE;
326 scl_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
327 scl_config.intr_type = GPIO_INTR_DISABLE;
328 gpio_config(&scl_config);
331 gpio_set_level(sda_pin, 1);
332 gpio_config_t sda_conf{};
333 sda_conf.pin_bit_mask = 1ULL << this->
sda_pin_;
334 sda_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
335 sda_conf.pull_up_en = GPIO_PULLUP_ENABLE;
336 sda_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
337 sda_conf.intr_type = GPIO_INTR_DISABLE;
338 gpio_config(&sda_conf);
343 if (gpio_get_level(scl_pin) == 0) {
344 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW on the bus");
356 for (
auto i = 0; i < 9; i++) {
357 gpio_set_level(scl_pin, 0);
359 gpio_set_level(scl_pin, 1);
370 while (wait-- && gpio_get_level(scl_pin) == 0) {
374 if (gpio_get_level(scl_pin) == 0) {
375 ESP_LOGE(TAG,
"Recovery failed: SCL is held LOW during clock pulse cycle");
384 if (gpio_get_level(sda_pin) == 0) {
385 ESP_LOGE(TAG,
"Recovery failed: SDA is held LOW after clock pulse cycle");
402 gpio_set_level(sda_pin, 0);
411 gpio_set_level(sda_pin, 1);
void feed_wdt()
Feed the task watchdog.
void mark_failed()
Mark this component as failed.
bool scan_
Should we scan ? Can be set in the yaml.
void dump_scan_results_()
Logs the scan results at config level, if the bus was scanned.
void i2c_scan_()
Scans the I2C bus for devices.
ErrorCode switch_frequency(uint32_t frequency) override
StaticVector< FrequencyDevice, I2C_PORT_FREQUENCY_COUNT+1 > devices_
i2c_master_bus_handle_t bus_
void add_frequency(uint32_t frequency)
Register a frequency used on the bus so setup() creates its device handle.
void dump_config() override
i2c_master_dev_handle_t dev_
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
esp_err_t __wrap_esp_intr_alloc_intrstatus(int source, int flags, uint32_t reg, uint32_t mask, intr_handler_t handler, void *arg, intr_handle_t *ret_handle)
esp_err_t __real_esp_intr_alloc_intrstatus(int source, int flags, uint32_t reg, uint32_t mask, intr_handler_t handler, void *arg, intr_handle_t *ret_handle)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
@ RECOVERY_FAILED_SDA_LOW
@ RECOVERY_FAILED_SCL_LOW
ESPHOME_LOG_TAG(TAG, "i2c")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Application App
Global storage of Application pointer - only one Application can exist.