i2c_master_cmd_begin() returns ESP_FAIL
Posted: 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.
here is the code:
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
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.
here is the code:
- #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;
- }
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