We have written a simple i2c master code that reads a register value of an i2c slave(tla2024) only once, so there is no loop/thread.
When we read it once it gives NACK for slave address 0x90, as seen in below screenshot. But when we continuously reset the device, it randomly reads the register values as seen in below screenshot. What is the exact issue as we have searched other such related posts, but didn't got issue solved.
Code: Select all
int chip_addr = 0x48;
int data_addr = 0x01;
int len = 2;
uint8_t *data = malloc(len);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = 9,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = 8,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 400000,
// .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */
};
i2c_param_config(I2C_NUM_0, &conf);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
if (data_addr != -1) {
i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
i2c_master_start(cmd);
}
i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
if (len > 1) {
i2c_master_read(cmd, data, len - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
for (int i = 0; i < len; i++) {
printf("0x%02x ", data[i]);
if ((i + 1) % 16 == 0) {
printf("\r\n");
}
}
if (len % 16) {
printf("\r\n");
}
} else if (ret == ESP_ERR_TIMEOUT) {
ESP_LOGW(TAG, "Bus is busy");
} else {
ESP_LOGW(TAG, "Read failed %d",ret);
}
free(data);
i2c_driver_delete(I2C_NUM_0);
Akshaay