diff --git a/components/driver/i2c/i2c_master.c b/components/driver/i2c/i2c_master.c index d78c5fe804..cfb8842963 100644 --- a/components/driver/i2c/i2c_master.c +++ b/components/driver/i2c/i2c_master.c @@ -270,6 +270,13 @@ static bool s_i2c_read_command(i2c_master_bus_handle_t i2c_master, i2c_operation i2c_master->read_buf_pos = i2c_master->trans_idx; } else { i2c_ll_master_write_cmd_reg(hal->dev, hw_cmd, i2c_master->cmd_idx); + // If the read position has not been marked, that means the transaction doesn't contain read-ack + // operation, then mark the read position in read-nack operation. + // i2c_master->read_buf_pos will never be 0. + if (i2c_master->read_buf_pos == 0) { + i2c_master->read_buf_pos = i2c_master->trans_idx; + i2c_master->read_len_static = i2c_master->rx_cnt; + } i2c_master->cmd_idx++; } i2c_master->trans_idx++; @@ -579,6 +586,7 @@ static esp_err_t s_i2c_transaction_start(i2c_master_dev_handle_t i2c_dev, int xf i2c_master->cmd_idx = 0; i2c_master->rx_cnt = 0; i2c_master->read_len_static = 0; + i2c_master->read_buf_pos = 0; I2C_CLOCK_SRC_ATOMIC() { i2c_hal_set_bus_timing(hal, i2c_dev->scl_speed_hz, i2c_master->base->clk_src, i2c_master->base->clk_src_freq_hz); @@ -642,7 +650,10 @@ IRAM_ATTR static void i2c_isr_receive_handler(i2c_master_bus_t *i2c_master) i2c_operation_t *i2c_operation = &i2c_master->i2c_trans.ops[i2c_master->read_buf_pos]; portENTER_CRITICAL_ISR(&i2c_master->base->spinlock); i2c_ll_read_rxfifo(hal->dev, i2c_operation->data + i2c_operation->bytes_used, i2c_master->read_len_static); - i2c_ll_read_rxfifo(hal->dev, i2c_master->i2c_trans.ops[i2c_master->read_buf_pos + 1].data, 1); + // If the read command only contain nack marker, no read it for the second time. + if (i2c_master->i2c_trans.ops[i2c_master->read_buf_pos + 1].data) { + i2c_ll_read_rxfifo(hal->dev, i2c_master->i2c_trans.ops[i2c_master->read_buf_pos + 1].data, 1); + } i2c_master->w_r_size = i2c_master->read_len_static + 1; i2c_master->contains_read = false; portEXIT_CRITICAL_ISR(&i2c_master->base->spinlock); @@ -1211,7 +1222,8 @@ esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t i2c_dev, ESP_RETURN_ON_FALSE(i2c_operation != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c operation pointer is invalid"); ESP_RETURN_ON_FALSE(operation_list_num <= (SOC_I2C_CMD_REG_NUM), ESP_ERR_INVALID_ARG, TAG, "i2c command list cannot contain so many commands"); - i2c_operation_t i2c_ops[operation_list_num] = {}; + i2c_operation_t i2c_ops[operation_list_num]; + memset(i2c_ops, 0, sizeof(i2c_ops)/sizeof(i2c_operation_t)); for (int i = 0; i < operation_list_num; i++) { switch (i2c_operation[i].command) { case I2C_MASTER_CMD_START: diff --git a/components/driver/i2c/i2c_slave.c b/components/driver/i2c/i2c_slave.c index 32070ebd78..8134908d2f 100644 --- a/components/driver/i2c/i2c_slave.c +++ b/components/driver/i2c/i2c_slave.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -284,7 +284,10 @@ err: static esp_err_t i2c_slave_bus_destroy(i2c_slave_dev_handle_t i2c_slave) { if (i2c_slave) { - i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR); + if (i2c_slave->base) { + i2c_ll_disable_intr_mask(i2c_slave->base->hal.dev, I2C_LL_SLAVE_EVENT_INTR); + i2c_release_bus_handle(i2c_slave->base); + } if (i2c_slave->slv_rx_mux) { vSemaphoreDeleteWithCaps(i2c_slave->slv_rx_mux); i2c_slave->slv_rx_mux = NULL; @@ -300,7 +303,6 @@ static esp_err_t i2c_slave_bus_destroy(i2c_slave_dev_handle_t i2c_slave) if (i2c_slave->slv_evt_queue) { vQueueDeleteWithCaps(i2c_slave->slv_evt_queue); } - i2c_release_bus_handle(i2c_slave->base); } free(i2c_slave);