Skip to content

Commit

Permalink
fix(i2c): fix issues related to timeout and alive interval tick
Browse files Browse the repository at this point in the history
Fixes #4999

Former usage of I2C_CMD_ALIVE_INTERVAL_TICK macro overrode the ticks_to_wait
parameter when the latter was too big
  • Loading branch information
o-marshmallow committed Apr 2, 2024
1 parent d1baafb commit 4c896cb
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions components/driver/i2c/i2c.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -1503,7 +1503,7 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle,
// Sometimes when the FSM get stuck, the ACK_ERR interrupt will occur endlessly until we reset the FSM and clear bus.
esp_err_t ret = ESP_FAIL;
i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
TickType_t ticks_start = xTaskGetTickCount();
const TickType_t ticks_start = xTaskGetTickCount();
portBASE_TYPE res = xSemaphoreTake(p_i2c->cmd_mux, ticks_to_wait);
if (res == pdFALSE) {
return ESP_ERR_TIMEOUT;
Expand Down Expand Up @@ -1543,13 +1543,15 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle,
i2c_cmd_evt_t evt;
while (1) {
TickType_t wait_time = xTaskGetTickCount();
if (wait_time - ticks_start > ticks_to_wait) { // out of time
wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
const TickType_t elapsed = wait_time - ticks_start;
if (elapsed >= ticks_to_wait) { // out of time
/* Before triggering a timeout, empty the queue by giving a wait_time of 0:
* - if the queue is empty, `pdFALSE` will be returned and the loop will be exited
* - if the queue is not empty, we will pop an element and come back here again
*/
wait_time = 0;
} else {
wait_time = ticks_to_wait - (wait_time - ticks_start);
if (wait_time < I2C_CMD_ALIVE_INTERVAL_TICK) {
wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
}
wait_time = MIN(ticks_to_wait - elapsed, I2C_CMD_ALIVE_INTERVAL_TICK);
}
// In master mode, since we don't have an interrupt to detective bus error or FSM state, what we do here is to make
// sure the interrupt mechanism for master mode is still working.
Expand Down

0 comments on commit 4c896cb

Please sign in to comment.