Skip to content

Commit 4c896cb

Browse files
committed
fix(i2c): fix issues related to timeout and alive interval tick
Fixes #4999 Former usage of I2C_CMD_ALIVE_INTERVAL_TICK macro overrode the ticks_to_wait parameter when the latter was too big
1 parent d1baafb commit 4c896cb

File tree

1 file changed

+10
-8
lines changed
  • components/driver/i2c

1 file changed

+10
-8
lines changed

components/driver/i2c/i2c.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -1503,7 +1503,7 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle,
15031503
// Sometimes when the FSM get stuck, the ACK_ERR interrupt will occur endlessly until we reset the FSM and clear bus.
15041504
esp_err_t ret = ESP_FAIL;
15051505
i2c_obj_t *p_i2c = p_i2c_obj[i2c_num];
1506-
TickType_t ticks_start = xTaskGetTickCount();
1506+
const TickType_t ticks_start = xTaskGetTickCount();
15071507
portBASE_TYPE res = xSemaphoreTake(p_i2c->cmd_mux, ticks_to_wait);
15081508
if (res == pdFALSE) {
15091509
return ESP_ERR_TIMEOUT;
@@ -1543,13 +1543,15 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle,
15431543
i2c_cmd_evt_t evt;
15441544
while (1) {
15451545
TickType_t wait_time = xTaskGetTickCount();
1546-
if (wait_time - ticks_start > ticks_to_wait) { // out of time
1547-
wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
1546+
const TickType_t elapsed = wait_time - ticks_start;
1547+
if (elapsed >= ticks_to_wait) { // out of time
1548+
/* Before triggering a timeout, empty the queue by giving a wait_time of 0:
1549+
* - if the queue is empty, `pdFALSE` will be returned and the loop will be exited
1550+
* - if the queue is not empty, we will pop an element and come back here again
1551+
*/
1552+
wait_time = 0;
15481553
} else {
1549-
wait_time = ticks_to_wait - (wait_time - ticks_start);
1550-
if (wait_time < I2C_CMD_ALIVE_INTERVAL_TICK) {
1551-
wait_time = I2C_CMD_ALIVE_INTERVAL_TICK;
1552-
}
1554+
wait_time = MIN(ticks_to_wait - elapsed, I2C_CMD_ALIVE_INTERVAL_TICK);
15531555
}
15541556
// In master mode, since we don't have an interrupt to detective bus error or FSM state, what we do here is to make
15551557
// sure the interrupt mechanism for master mode is still working.

0 commit comments

Comments
 (0)