i2c_master_cmd_begin() returns ESP_FAIL

adi_1234
Posts: 1
Joined: Tue Jun 29, 2021 10:51 am

i2c_master_cmd_begin() returns ESP_FAIL

Postby adi_1234 » Tue Jun 29, 2021 11:40 am

Hello,
i am trying to read time from the rtc - PCF85263ATT, so i start with writing the write-slave-address (0xA2) and the register to start reading from (seconds register 0x1) and i get NACK (as it shows on the oscilloscope). This NACK causes the failure of i2c_master_cmd_begin() with ESP_FAIL error.
scl pin is set to 18, sda is pin 19, i2c_port is I2C_NUM_0.
IMG_20210629_131754.jpg
IMG_20210629_131754.jpg (1.81 MiB) Viewed 4071 times
here is the code:

Code: Untitled.c Select all



#define PCF85263ATT_SLAVE_REG_WRITE 0xA2
#define PCF85263ATT_SLAVE_REG_READ 0xA3
#define PCF85263ATT_REG_SECONDS 0x01

typedef struct RtcDriver {
int sda_pin_num;
int scl_pin_num;
i2c_port_t i2c_port;
} RtcDriver;


esp_err_t rtc_driver_init(RtcDriver* rtc_driver) {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = rtc_driver->sda_pin_num,
.sda_pullup_en = GPIO_PULLUP_DISABLE, //there are pullup resistors in the circuit
.scl_io_num = rtc_driver->scl_pin_num,
.scl_pullup_en = GPIO_PULLUP_DISABLE, //there are pullup resistors in the circuit
.master.clk_speed = 400000,
};
esp_err_t err;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_driver_install(rtc_driver->i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_set_data_mode(rtc_driver->i2c_port, I2C_DATA_MODE_MSB_FIRST, I2C_DATA_MODE_MSB_FIRST));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_param_config(rtc_driver->i2c_port, &conf));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_set_timeout(rtc_driver->i2c_port, 9000 / portTICK_RATE_MS));
return 0;
}

esp_err_t rtc_driver_get_time(RtcDriver* rtc_driver, uint8_t* sec, uint8_t* min, uint8_t* hour){
i2c_cmd_handle_t cmd = NULL;
cmd = i2c_cmd_link_create();
if(cmd == NULL) {
ESP_LOGI(TAG, "i2c_cmd_link_create failed!" );
}
uint8_t data[3];
data[0] = PCF85263ATT_SLAVE_REG_WRITE;
data[1] = PCF85263ATT_REG_SECONDS;
data[2] = PCF85263ATT_SLAVE_REG_READ;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_start(cmd));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_byte(cmd, data[0] , ACK_CHECK_DIS)); //slave addr
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_byte(cmd, data[1], ACK_CHECK_EN)); //start to read from sec rgstr
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_stop(cmd));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_cmd_begin(rtc_driver->i2c_port, cmd, 1000 / portTICK_RATE_MS));
i2c_cmd_link_delete(cmd);
vTaskDelay(100 / portTICK_RATE_MS);
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_start(cmd));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write(cmd, &data[2], 1, true));
data[0] = 0;
data[1] = 0;
data[2] = 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_read(cmd, &data[0], sizeof(data) ,I2C_MASTER_LAST_NACK));
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_stop(cmd));
esp_err_t err;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_cmd_begin(rtc_driver->i2c_port, cmd, 1000 / portTICK_RATE_MS));
i2c_cmd_link_delete(cmd);
return 0;
}
The second call to i2c_master_cmd_begin() returns ESP_ERR_TIMEOUT.
here is the datasheet of the rtc: https://www.nxp.com/docs/en/data-sheet/PCF85263A.pdf

What am i doing wrong?
i really wish to figure out why the rtc won't ack and why do i get ESP_ERR_TIMEOUT on the second call to i2c_master_cmd_begin().

Thank You,
Adi

kalpanavengala
Posts: 3
Joined: Sat Jun 12, 2021 2:04 pm

Re: i2c_master_cmd_begin() returns ESP_FAIL

Postby kalpanavengala » Wed Jul 07, 2021 11:01 am

Hi,
I have seen your code, maybe in line 41, the third parameter in the i2c_master_write_byte as ACK_CHECK_EN. If you are disabling the acknowledge check condition if the slave sends the NACK master will not check the condition and it will not enable the I2C_NACK_INTR maybe that is the cause to get the failure.

Who is online

Users browsing this forum: Amazon [Bot], Semrush [Bot] and 7 guests