Skip to content

Commit 84f599b

Browse files
Glauber Maroto Ferreiranashif
authored andcommitted
esp32: i2c: fix: fixed error status handling
Fixed error status handling on ESP32's I2C. Signed-off-by: Glauber Maroto Ferreira <glauber.ferreira@espressif.com>
1 parent 4640e6e commit 84f599b

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

drivers/i2c/i2c_esp32.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2017 Intel Corporation
3+
* Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -59,6 +60,7 @@ struct i2c_esp32_cmd {
5960
struct i2c_esp32_data {
6061
uint32_t dev_config;
6162
uint16_t address;
63+
uint32_t err_status;
6264

6365
struct k_sem fifo_sem;
6466
struct k_sem transfer_sem;
@@ -293,29 +295,22 @@ static int i2c_esp32_transmit(const struct device *dev)
293295
{
294296
const struct i2c_esp32_config *config = dev->config;
295297
struct i2c_esp32_data *data = dev->data;
296-
uint32_t status;
297298

298299
/* Start transmission and wait for the ISR to give the semaphore */
299300
sys_set_bit(I2C_CTR_REG(config->index), I2C_TRANS_START_S);
300301
if (k_sem_take(&data->fifo_sem, K_MSEC(I2C_ESP32_TIMEOUT_MS)) < 0) {
301302
return -ETIMEDOUT;
302303
}
303304

304-
status = sys_read32(I2C_INT_RAW_REG(config->index));
305-
if (status & (I2C_ARBITRATION_LOST_INT_RAW | I2C_ACK_ERR_INT_RAW)) {
306-
return -EIO;
307-
}
308-
if (status & I2C_TIME_OUT_INT_RAW) {
309-
return -ETIMEDOUT;
310-
}
311-
312305
return 0;
313306
}
314307

315308
static int i2c_esp32_wait(const struct device *dev,
316309
volatile struct i2c_esp32_cmd *wait_cmd)
317310
{
318311
const struct i2c_esp32_config *config = dev->config;
312+
struct i2c_esp32_data *data = dev->data;
313+
uint32_t status;
319314
int counter = 0;
320315
int ret;
321316

@@ -336,6 +331,16 @@ static int i2c_esp32_wait(const struct device *dev,
336331
}
337332
}
338333

334+
status = data->err_status;
335+
if (status & (I2C_ARBITRATION_LOST_INT_RAW | I2C_ACK_ERR_INT_RAW)) {
336+
data->err_status = 0;
337+
return -EIO;
338+
}
339+
if (status & I2C_TIME_OUT_INT_RAW) {
340+
data->err_status = 0;
341+
return -ETIMEDOUT;
342+
}
343+
339344
return 0;
340345
}
341346

@@ -554,14 +559,24 @@ static void i2c_esp32_isr(const struct device *dev)
554559
I2C_TRANS_COMPLETE_INT_ST |
555560
I2C_ARBITRATION_LOST_INT_ST;
556561
const struct i2c_esp32_config *config = dev->config;
562+
uint32_t status = sys_read32(I2C_INT_STATUS_REG(config->index));
557563

558-
if (sys_read32(I2C_INT_STATUS_REG(config->index)) & fifo_give_mask) {
564+
if (status & fifo_give_mask) {
559565
struct i2c_esp32_data *data = dev->data;
560566

561567
/* Only give the semaphore if a watched interrupt happens.
562568
* Error checking is performed at the other side of the
563569
* semaphore, by reading the status register.
564570
*/
571+
if (status & I2C_ACK_ERR_INT_ST) {
572+
data->err_status |= I2C_ACK_ERR_INT_ST;
573+
}
574+
if (status & I2C_ARBITRATION_LOST_INT_ST) {
575+
data->err_status |= I2C_ARBITRATION_LOST_INT_ST;
576+
}
577+
if (status & I2C_TIME_OUT_INT_ST) {
578+
data->err_status |= I2C_TIME_OUT_INT_ST;
579+
}
565580
k_sem_give(&data->fifo_sem);
566581
}
567582

0 commit comments

Comments
 (0)