(solved) issues with I2C

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

(solved) issues with I2C

Postby mzimmers » Sat Oct 16, 2021 12:40 am

Hi all -

I'm porting some code to the ESP32. The app connects to an external RTC https://www.mouser.com/datasheet/2/268/25010A-71550.pdf. I'm trying to convert its primitive functions to the IDF. This is the existing sequence for a register read:

Code: Select all

    // uint8_t result = 0;
    // SI2C_Start();
    // SI2C_WriteByte(I2C_MCP7940_W);
    // SI2C_WriteByte(Address);
    // SI2C_Restart();
    // SI2C_WriteByte(I2C_MCP7940_R);
    // result = SI2C_ReadByte();
    // SI2C_Acknowledge(1);
    // SI2C_Stop();

    // return result;
The restart function just calls their start function. I've replaced their calls with:

Code: Select all

i2cCommand = i2c_cmd_link_create();
err = i2c_master_start(i2cCommand);
err = i2c_master_write_byte(i2cCommand, I2C_MCP7940_W, true);
err = i2c_master_write_byte(i2cCommand, Address, true);
err = i2c_master_start(i2cCommand);
err = i2c_master_write_byte(i2cCommand, I2C_MCP7940_R, true);
err = i2c_master_read_byte(i2cCommand, &data, I2C_MASTER_ACK);
err = i2c_master_stop(i2cCommand);
err = i2c_master_cmd_begin(I2C_PORT_NBR, i2cCommand, I2C_WAITTIME_TICKS);
i2c_cmd_link_delete(i2cCommand);
And I get an ESP_ERR_TIMEOUT on the begin.

I don't understand the need for the restart in the middle, but I've tried it with and without, with same results. Does anyone have an idea what I might be doing wrong here?

Thanks...
Last edited by mzimmers on Sun Oct 17, 2021 10:04 pm, edited 1 time in total.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: issues with I2C

Postby mzimmers » Sun Oct 17, 2021 10:03 pm

I figured it out with help from the tools example -- I needed to have my ack settings as follows:

Code: Select all

        err = (i2c_master_start(i2cCommand));
        err = (i2c_master_write_byte(i2cCommand, I2C_MCP7940_W, ACK_CHECK_EN));
        err = (i2c_master_write_byte(i2cCommand, 0x00, ACK_CHECK_EN));  
        err = (i2c_master_start(i2cCommand));
        err = (i2c_master_write_byte(i2cCommand, I2C_MCP7940_R, ACK_CHECK_EN));
        err = i2c_master_read(i2cCommand, (uint8_t *) &t, readLen, ACK_VAL);
        err = i2c_master_read_byte(i2cCommand, &(t.Year), NACK_VAL);
        ESP_LOGI(TAG, "MCP7940_GetTimeDate(): about to execute i2c_master_cmd_begin().");
        err = i2c_master_stop(i2cCommand);

Who is online

Users browsing this forum: Slinisa and 115 guests