Skip to content

Commit f7ddaef

Browse files
committed
Merge branch 'fix/make_nack_log_debug_lvl_v5.5' into 'release/v5.5'
fix(i2c): Make i2c nack log as debug level ,etc (backport v5.5) See merge request espressif/esp-idf!40534
2 parents 84e9496 + c4b6797 commit f7ddaef

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

components/esp_driver_i2c/i2c_common.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "esp_clk_tree.h"
2929
#include "clk_ctrl_os.h"
3030
#include "esp_private/gpio.h"
31+
#include "esp_private/esp_gpio_reserve.h"
3132
#if SOC_LP_I2C_SUPPORTED
3233
#include "hal/rtc_io_ll.h"
3334
#include "driver/rtc_io.h"
@@ -319,6 +320,17 @@ static esp_err_t s_hp_i2c_pins_config(i2c_bus_handle_t handle)
319320
{
320321
int port_id = handle->port_num;
321322

323+
// reserve the GPIO output path, because we don't expect another peripheral to signal to the same GPIO
324+
uint64_t old_gpio_rsv_mask = esp_gpio_reserve(BIT64(handle->sda_num) | BIT64(handle->scl_num));
325+
// check if the GPIO is already used by others
326+
if (old_gpio_rsv_mask & BIT64(handle->sda_num)) {
327+
ESP_LOGW(TAG, "GPIO %d is not usable, maybe conflict with others", handle->sda_num);
328+
}
329+
// check if the GPIO is already used by others
330+
if (old_gpio_rsv_mask & BIT64(handle->scl_num)) {
331+
ESP_LOGW(TAG, "GPIO %d is not usable, maybe conflict with others", handle->scl_num);
332+
}
333+
322334
// SDA pin configurations
323335
ESP_RETURN_ON_ERROR(gpio_set_level(handle->sda_num, 1), TAG, "i2c sda pin set level failed");
324336
gpio_input_enable(handle->sda_num);
@@ -357,6 +369,17 @@ static esp_err_t s_lp_i2c_pins_config(i2c_bus_handle_t handle)
357369
ESP_RETURN_ON_ERROR(!rtc_gpio_is_valid_gpio(handle->sda_num), TAG, "LP I2C SDA GPIO invalid");
358370
ESP_RETURN_ON_ERROR(!rtc_gpio_is_valid_gpio(handle->scl_num), TAG, "LP I2C SCL GPIO invalid");
359371

372+
// reserve the GPIO output path, because we don't expect another peripheral to signal to the same GPIO
373+
uint64_t old_gpio_rsv_mask = esp_gpio_reserve(BIT64(handle->sda_num) | BIT64(handle->scl_num));
374+
// check if the GPIO is already used by others
375+
if (old_gpio_rsv_mask & BIT64(handle->sda_num)) {
376+
ESP_LOGW(TAG, "GPIO %d is not usable, maybe conflict with others", handle->sda_num);
377+
}
378+
// check if the GPIO is already used by others
379+
if (old_gpio_rsv_mask & BIT64(handle->scl_num)) {
380+
ESP_LOGW(TAG, "GPIO %d is not usable, maybe conflict with others", handle->scl_num);
381+
}
382+
360383
#if !SOC_LP_GPIO_MATRIX_SUPPORTED
361384
/* Verify that the SDA and SCL line belong to the LP IO Mux I2C function group */
362385
ESP_RETURN_ON_FALSE((handle->sda_num == LP_I2C_SDA_IOMUX_PAD), ESP_ERR_INVALID_ARG, TAG, LP_I2C_SDA_PIN_ERR_LOG);
@@ -419,6 +442,9 @@ esp_err_t i2c_common_deinit_pins(i2c_bus_handle_t handle)
419442
{
420443
int port_id = handle->port_num;
421444

445+
esp_gpio_revoke(BIT64(handle->sda_num));
446+
esp_gpio_revoke(BIT64(handle->scl_num));
447+
422448
if (handle->is_lp_i2c == false) {
423449
ESP_RETURN_ON_ERROR(gpio_output_disable(handle->sda_num), TAG, "disable i2c pins failed");
424450
esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2c_periph_signal[port_id].sda_in_sig, 0);

components/esp_driver_i2c/i2c_master.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#if CONFIG_I2C_ENABLE_DEBUG_LOG
1515
// The local log level must be defined before including esp_log.h
1616
// Set the maximum log level for this source file
17-
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
17+
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
1818
#endif
1919
#include "esp_log.h"
2020
#include "esp_intr_alloc.h"
@@ -157,7 +157,7 @@ static void s_i2c_err_log_print(i2c_master_event_t event, bool bypass_nack_log)
157157
}
158158
if (bypass_nack_log != true) {
159159
if (event == I2C_EVENT_NACK) {
160-
ESP_LOGE(TAG, "I2C transaction unexpected nack detected");
160+
ESP_LOGD(TAG, "I2C transaction unexpected nack detected");
161161
}
162162
}
163163
}
@@ -507,7 +507,7 @@ static void s_i2c_send_commands(i2c_master_bus_handle_t i2c_master, TickType_t t
507507
}
508508

509509
if (atomic_load(&i2c_master->status) == I2C_STATUS_ACK_ERROR) {
510-
ESP_LOGE(TAG, "I2C hardware NACK detected");
510+
ESP_LOGD(TAG, "I2C hardware NACK detected");
511511
const i2c_ll_hw_cmd_t hw_stop_cmd = {
512512
.op_code = I2C_LL_CMD_STOP,
513513
};
@@ -942,7 +942,10 @@ static esp_err_t s_i2c_synchronous_transaction(i2c_master_dev_handle_t i2c_dev,
942942
i2c_dev->master_bus->trans_finish = false;
943943
i2c_dev->master_bus->queue_trans = false;
944944
i2c_dev->master_bus->ack_check_disable = i2c_dev->ack_check_disable;
945-
ESP_GOTO_ON_ERROR(s_i2c_transaction_start(i2c_dev, timeout_ms), err, TAG, "I2C transaction failed");
945+
ret = s_i2c_transaction_start(i2c_dev, timeout_ms);
946+
if (ret != ESP_OK) {
947+
goto err;
948+
}
946949
xSemaphoreGive(i2c_dev->master_bus->bus_lock_mux);
947950
return ret;
948951

@@ -955,9 +958,6 @@ static esp_err_t s_i2c_synchronous_transaction(i2c_master_dev_handle_t i2c_dev,
955958

956959
esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *bus_config, i2c_master_bus_handle_t *ret_bus_handle)
957960
{
958-
#if CONFIG_I2C_ENABLE_DEBUG_LOG
959-
esp_log_level_set(TAG, ESP_LOG_DEBUG);
960-
#endif
961961
esp_err_t ret = ESP_OK;
962962
i2c_master_bus_t *i2c_master = NULL;
963963
i2c_port_num_t i2c_port_num = bus_config->i2c_port;
@@ -1194,6 +1194,7 @@ esp_err_t i2c_master_multi_buffer_transmit(i2c_master_dev_handle_t i2c_dev, i2c_
11941194
ESP_RETURN_ON_FALSE(array_size <= (SOC_I2C_CMD_REG_NUM - 2), ESP_ERR_INVALID_ARG, TAG, "i2c command list cannot contain so many commands");
11951195
ESP_RETURN_ON_FALSE(buffer_info_array != NULL, ESP_ERR_INVALID_ARG, TAG, "buffer info array is empty");
11961196

1197+
esp_err_t ret = ESP_OK;
11971198
size_t op_index = 0;
11981199
i2c_operation_t i2c_ops[SOC_I2C_CMD_REG_NUM] = {};
11991200
i2c_ops[op_index++].hw_cmd.op_code = I2C_LL_CMD_RESTART;
@@ -1211,11 +1212,11 @@ esp_err_t i2c_master_multi_buffer_transmit(i2c_master_dev_handle_t i2c_dev, i2c_
12111212

12121213
i2c_ops[op_index++].hw_cmd.op_code = I2C_LL_CMD_STOP;
12131214
if (i2c_dev->master_bus->async_trans == false) {
1214-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms), TAG, "I2C transaction failed");
1215+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms);
12151216
} else {
1216-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms), TAG, "I2C transaction failed");
1217+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms);
12171218
}
1218-
return ESP_OK;
1219+
return ret;
12191220
}
12201221

12211222
esp_err_t i2c_master_transmit(i2c_master_dev_handle_t i2c_dev, const uint8_t *write_buffer, size_t write_size, int xfer_timeout_ms)
@@ -1235,6 +1236,7 @@ esp_err_t i2c_master_transmit_receive(i2c_master_dev_handle_t i2c_dev, const uin
12351236
ESP_RETURN_ON_FALSE((write_buffer != NULL) && (write_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c transmit buffer or size invalid");
12361237
ESP_RETURN_ON_FALSE((read_buffer != NULL) && (read_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c receive buffer or size invalid");
12371238

1239+
esp_err_t ret = ESP_OK;
12381240
i2c_operation_t i2c_ops[] = {
12391241
{.hw_cmd = I2C_TRANS_START_COMMAND},
12401242
{.hw_cmd = I2C_TRANS_WRITE_COMMAND(i2c_dev->ack_check_disable ? false : true), .data = (uint8_t *)write_buffer, .total_bytes = write_size},
@@ -1245,17 +1247,18 @@ esp_err_t i2c_master_transmit_receive(i2c_master_dev_handle_t i2c_dev, const uin
12451247
};
12461248

12471249
if (i2c_dev->master_bus->async_trans == false) {
1248-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1250+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12491251
} else {
1250-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1252+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12511253
}
1252-
return ESP_OK;
1254+
return ret;
12531255
}
12541256

12551257
esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buffer, size_t read_size, int xfer_timeout_ms)
12561258
{
12571259
ESP_RETURN_ON_FALSE(i2c_dev != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
12581260
ESP_RETURN_ON_FALSE((read_buffer != NULL) && (read_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c receive buffer or size invalid");
1261+
esp_err_t ret = ESP_OK;
12591262

12601263
i2c_operation_t i2c_ops[] = {
12611264
{.hw_cmd = I2C_TRANS_START_COMMAND},
@@ -1265,11 +1268,11 @@ esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buff
12651268
};
12661269

12671270
if (i2c_dev->master_bus->async_trans == false) {
1268-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1271+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12691272
} else {
1270-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1273+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12711274
}
1272-
return ESP_OK;
1275+
return ret;
12731276
}
12741277

12751278
esp_err_t i2c_master_probe(i2c_master_bus_handle_t bus_handle, uint16_t address, int xfer_timeout_ms)
@@ -1362,6 +1365,7 @@ esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t i2c_dev,
13621365
ESP_RETURN_ON_FALSE(i2c_operation != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c operation pointer is invalid");
13631366
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");
13641367

1368+
esp_err_t ret = ESP_OK;
13651369
i2c_operation_t i2c_ops[operation_list_num];
13661370
memset(i2c_ops, 0, sizeof(i2c_ops));
13671371
for (int i = 0; i < operation_list_num; i++) {
@@ -1398,11 +1402,11 @@ esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t i2c_dev,
13981402
}
13991403

14001404
if (i2c_dev->master_bus->async_trans == false) {
1401-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms), TAG, "I2C transaction failed");
1405+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms);
14021406
} else {
1403-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms), TAG, "I2C transaction failed");
1407+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms);
14041408
}
1405-
return ESP_OK;
1409+
return ret;
14061410
}
14071411

14081412
esp_err_t i2c_master_register_event_callbacks(i2c_master_dev_handle_t i2c_dev, const i2c_master_event_callbacks_t *cbs, void *user_data)
@@ -1445,3 +1449,11 @@ esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int t
14451449
return ESP_OK;
14461450

14471451
}
1452+
1453+
#if CONFIG_I2C_ENABLE_DEBUG_LOG
1454+
__attribute__((constructor))
1455+
static void i2c_master_override_default_log_level(void)
1456+
{
1457+
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
1458+
}
1459+
#endif

0 commit comments

Comments
 (0)