I2C timeout

tatulea
Posts: 18
Joined: Wed Feb 06, 2019 12:39 pm

I2C timeout

Postby tatulea » Fri Aug 09, 2019 3:19 pm

Hi,

I have an ESP32 WROVER dev board and a BQ27220. I am trying to read its device number.
I have a logic analyzer to monitor the traffic as well.

This is the code that I am using:

Code: Select all

esp_err_t ret;
    i2c_cmd_handle_t cmd;
    
    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, true);
    i2c_master_write_byte(cmd, 0x00, true);
    i2c_master_write_byte(cmd, 0x01, true);
    i2c_master_write_byte(cmd, 0x00, true);
    i2c_master_stop(cmd);
    
    ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

    DELAY(1000);

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, true);
    i2c_master_write_byte(cmd, 0x40, true);
    i2c_master_stop(cmd);

    ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

    DELAY(2000);

    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, true);
    i2c_master_read(cmd, data, data_len, I2C_MASTER_LAST_NACK);
    i2c_master_stop(cmd);

    ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 5000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
The problem is that the read command returns 107 (timeout error). The logic analyzer shows that there is not response from BQ27220 after starting the reading.

To double check that BQ27220 is find, I got this library (https://github.com/sparkfun/SparkFun_BQ ... no_Library). It is for another IC, but the device number request sequence is the same. I tested it with an Arduino and it was working.
Also, I compiled this example for Arduino-ESP32 and uploaded it to my board and it worked.

This makes me think that there is a problem with ESP-IDF, but I cannot understand what is wrong.

How should I debug further?

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: I2C timeout

Postby mikemoy » Fri Aug 09, 2019 4:52 pm

I didn't have time to study the device your talking to, but here is a snippet of code i use to talk to a INA219 ( current shunt IC) that works fine in IDF. Hopefully you can see what you may be doing wrong. Note, that my device returns 2 bytes back.

Code: Select all

//**************************************************************************/
//
//    @brief  Reads a 16 bit values over I2C
//
//**************************************************************************/
esp_err_t INA219_readRegister(int icAddress, uint8_t reg, uint16_t *value)
{
	esp_err_t espRc;
	uint8_t msb, lsb;

	i2c_cmd_handle_t cmd = i2c_cmd_link_create();

	i2c_master_start(cmd);
	i2c_master_write_byte(cmd, icAddress << 1 | WRITE_BIT, ACK_CHECK_EN);
	i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);

	// Setup the read
	i2c_master_start(cmd);
	i2c_master_write_byte(cmd, icAddress << 1 | READ_BIT, ACK_CHECK_EN);
	i2c_master_read_byte(cmd, &msb,(i2c_ack_type_t) ACK_VAL);
	i2c_master_read_byte(cmd, &lsb,(i2c_ack_type_t) NACK_VAL);
	i2c_master_stop(cmd);

	// Shoot it out
	espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
	i2c_cmd_link_delete(cmd);

	 *value = (uint16_t)( (msb << 8) | lsb );

	return espRc;

}

tatulea
Posts: 18
Joined: Wed Feb 06, 2019 12:39 pm

Re: I2C timeout

Postby tatulea » Mon Aug 12, 2019 8:52 am

It looks like all I needed was i2c_set_timeout(I2C_NUM_0, 400000);

Who is online

Users browsing this forum: atx823, brushlow18, Google [Bot] and 233 guests