Skip to content

Commit 6c22664

Browse files
committed
fix(i2c): Make i2c nack log as debug level
1 parent c7b0697 commit 6c22664

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

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"
@@ -156,7 +156,7 @@ static void s_i2c_err_log_print(i2c_master_event_t event, bool bypass_nack_log)
156156
}
157157
if (bypass_nack_log != true) {
158158
if (event == I2C_EVENT_NACK) {
159-
ESP_LOGE(TAG, "I2C transaction unexpected nack detected");
159+
ESP_LOGD(TAG, "I2C transaction unexpected nack detected");
160160
}
161161
}
162162
}
@@ -506,7 +506,7 @@ static void s_i2c_send_commands(i2c_master_bus_handle_t i2c_master, TickType_t t
506506
}
507507

508508
if (atomic_load(&i2c_master->status) == I2C_STATUS_ACK_ERROR) {
509-
ESP_LOGE(TAG, "I2C hardware NACK detected");
509+
ESP_LOGD(TAG, "I2C hardware NACK detected");
510510
const i2c_ll_hw_cmd_t hw_stop_cmd = {
511511
.op_code = I2C_LL_CMD_STOP,
512512
};
@@ -945,7 +945,10 @@ static esp_err_t s_i2c_synchronous_transaction(i2c_master_dev_handle_t i2c_dev,
945945
i2c_dev->master_bus->trans_finish = false;
946946
i2c_dev->master_bus->queue_trans = false;
947947
i2c_dev->master_bus->ack_check_disable = i2c_dev->ack_check_disable;
948-
ESP_GOTO_ON_ERROR(s_i2c_transaction_start(i2c_dev, timeout_ms), err, TAG, "I2C transaction failed");
948+
ret = s_i2c_transaction_start(i2c_dev, timeout_ms);
949+
if (ret != ESP_OK) {
950+
goto err;
951+
}
949952
xSemaphoreGive(i2c_dev->master_bus->bus_lock_mux);
950953
return ret;
951954

@@ -958,9 +961,6 @@ static esp_err_t s_i2c_synchronous_transaction(i2c_master_dev_handle_t i2c_dev,
958961

959962
esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *bus_config, i2c_master_bus_handle_t *ret_bus_handle)
960963
{
961-
#if CONFIG_I2C_ENABLE_DEBUG_LOG
962-
esp_log_level_set(TAG, ESP_LOG_DEBUG);
963-
#endif
964964
esp_err_t ret = ESP_OK;
965965
i2c_master_bus_t *i2c_master = NULL;
966966
i2c_port_num_t i2c_port_num = bus_config->i2c_port;
@@ -1197,6 +1197,7 @@ esp_err_t i2c_master_multi_buffer_transmit(i2c_master_dev_handle_t i2c_dev, i2c_
11971197
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");
11981198
ESP_RETURN_ON_FALSE(buffer_info_array != NULL, ESP_ERR_INVALID_ARG, TAG, "buffer info array is empty");
11991199

1200+
esp_err_t ret = ESP_OK;
12001201
size_t op_index = 0;
12011202
i2c_operation_t i2c_ops[SOC_I2C_CMD_REG_NUM] = {};
12021203
i2c_ops[op_index++].hw_cmd.op_code = I2C_LL_CMD_RESTART;
@@ -1214,11 +1215,11 @@ esp_err_t i2c_master_multi_buffer_transmit(i2c_master_dev_handle_t i2c_dev, i2c_
12141215

12151216
i2c_ops[op_index++].hw_cmd.op_code = I2C_LL_CMD_STOP;
12161217
if (i2c_dev->master_bus->async_trans == false) {
1217-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms), TAG, "I2C transaction failed");
1218+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms);
12181219
} else {
1219-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms), TAG, "I2C transaction failed");
1220+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, op_index, xfer_timeout_ms);
12201221
}
1221-
return ESP_OK;
1222+
return ret;
12221223
}
12231224

12241225
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)
@@ -1238,6 +1239,7 @@ esp_err_t i2c_master_transmit_receive(i2c_master_dev_handle_t i2c_dev, const uin
12381239
ESP_RETURN_ON_FALSE((write_buffer != NULL) && (write_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c transmit buffer or size invalid");
12391240
ESP_RETURN_ON_FALSE((read_buffer != NULL) && (read_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c receive buffer or size invalid");
12401241

1242+
esp_err_t ret = ESP_OK;
12411243
i2c_operation_t i2c_ops[] = {
12421244
{.hw_cmd = I2C_TRANS_START_COMMAND},
12431245
{.hw_cmd = I2C_TRANS_WRITE_COMMAND(i2c_dev->ack_check_disable ? false : true), .data = (uint8_t *)write_buffer, .total_bytes = write_size},
@@ -1248,17 +1250,18 @@ esp_err_t i2c_master_transmit_receive(i2c_master_dev_handle_t i2c_dev, const uin
12481250
};
12491251

12501252
if (i2c_dev->master_bus->async_trans == false) {
1251-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1253+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12521254
} else {
1253-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1255+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12541256
}
1255-
return ESP_OK;
1257+
return ret;
12561258
}
12571259

12581260
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)
12591261
{
12601262
ESP_RETURN_ON_FALSE(i2c_dev != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
12611263
ESP_RETURN_ON_FALSE((read_buffer != NULL) && (read_size > 0), ESP_ERR_INVALID_ARG, TAG, "i2c receive buffer or size invalid");
1264+
esp_err_t ret = ESP_OK;
12621265

12631266
i2c_operation_t i2c_ops[] = {
12641267
{.hw_cmd = I2C_TRANS_START_COMMAND},
@@ -1268,11 +1271,11 @@ esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buff
12681271
};
12691272

12701273
if (i2c_dev->master_bus->async_trans == false) {
1271-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1274+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12721275
} else {
1273-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms), TAG, "I2C transaction failed");
1276+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, DIM(i2c_ops), xfer_timeout_ms);
12741277
}
1275-
return ESP_OK;
1278+
return ret;
12761279
}
12771280

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

1371+
esp_err_t ret = ESP_OK;
13681372
i2c_operation_t i2c_ops[operation_list_num];
13691373
memset(i2c_ops, 0, sizeof(i2c_ops));
13701374
for (int i = 0; i < operation_list_num; i++) {
@@ -1401,11 +1405,11 @@ esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t i2c_dev,
14011405
}
14021406

14031407
if (i2c_dev->master_bus->async_trans == false) {
1404-
ESP_RETURN_ON_ERROR(s_i2c_synchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms), TAG, "I2C transaction failed");
1408+
ret = s_i2c_synchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms);
14051409
} else {
1406-
ESP_RETURN_ON_ERROR(s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms), TAG, "I2C transaction failed");
1410+
ret = s_i2c_asynchronous_transaction(i2c_dev, i2c_ops, operation_list_num, xfer_timeout_ms);
14071411
}
1408-
return ESP_OK;
1412+
return ret;
14091413
}
14101414

14111415
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)
@@ -1448,3 +1452,11 @@ esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int t
14481452
return ESP_OK;
14491453

14501454
}
1455+
1456+
#if CONFIG_I2C_ENABLE_DEBUG_LOG
1457+
__attribute__((constructor))
1458+
static void i2c_master_override_default_log_level(void)
1459+
{
1460+
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
1461+
}
1462+
#endif

0 commit comments

Comments
 (0)