Trouble reading I2C device

eskibrew
Posts: 9
Joined: Fri Jul 02, 2021 2:31 pm

Trouble reading I2C device

Postby eskibrew » Thu Oct 13, 2022 1:17 pm

PLATFORM: Espressif 32 (5.2.0) > OLIMEX ESP32-EVB
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog)
framework-espidf @ 3.40402.0 (4.4.2)

I'm struggling to get I2C working properly when reading. I've got an LCD working fine over I2c via a PCF8574 board so I know the wiring is good. I'm also trying to read data from a custom device (Arduino Uno, addr 10) & the ESP32 will not run the clock.

This scope image shows SCL (Blue) and SDA (Red). After the device is addressed, the ESP master should run the clock and read out the data but it does not, instead it times out.

Code: Select all

        i2c_cmd_handle_t cmd = i2c_cmd_link_create();
        err = i2c_master_start(cmd);

        i2c_master_write_byte(cmd, (RFID_ADDR << 1) | I2C_MASTER_READ, ACK_CHECK_EN);

        if (numBytes > 1)
        {
            i2c_master_read(cmd, data, numBytes - 1, ACK_VAL);
        }

        i2c_master_read_byte(cmd, data + numBytes - 1, NACK_VAL);

        err = i2c_master_stop(cmd);
        err = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
        i2c_cmd_link_delete(cmd);
        ok = err == ESP_OK;
Image

This scope image below shows a second Arduino Uno acting as master instead of the ESP32 (i.e. ESP32 removed and replaced with Uno), sending the same request and receiving the expected 5 byte response. For some reason, the ESP32 (i2c_master_cmd_begin) times out whereas the Uno works even when the signals are very rounded!

Image

Any ideas?

eskibrew
Posts: 9
Joined: Fri Jul 02, 2021 2:31 pm

Re: Trouble reading I2C device

Postby eskibrew » Thu Oct 13, 2022 3:49 pm

Bad form to be the first to reply to my own post but I wanted to add a bit - I have now changed to software I2C (i.e. bit banging) on the same gpio pins and it is working albeit more slowly. I don't think speed is the issue though because I previously slowed down the I2C clock to check that. Could this be a problem in ESP-IDF.

User avatar
mbratch
Posts: 317
Joined: Fri Jun 11, 2021 1:51 pm

Re: Trouble reading I2C device

Postby mbratch » Fri Oct 14, 2022 2:01 am

What version of ESP-IDF are you using?

eskibrew
Posts: 9
Joined: Fri Jul 02, 2021 2:31 pm

Re: Trouble reading I2C device

Postby eskibrew » Fri Oct 14, 2022 9:11 am

What version of ESP-IDF are you using?
framework-espidf @ 3.40402.0 (4.4.2)

So 4.4.2

User avatar
mbratch
Posts: 317
Joined: Fri Jun 11, 2021 1:51 pm

Re: Trouble reading I2C device

Postby mbratch » Fri Oct 14, 2022 11:57 am

If you are using ESP-IDF 4.4.2, you should then try using their improved, consolidated i2c function calls. After you configure the i2c, you just need `i2c_master_write_to_device` or `i2c_master_read_from_device`. The latest examples with ESP-IDF illustrate their use.

eskibrew
Posts: 9
Joined: Fri Jul 02, 2021 2:31 pm

Re: Trouble reading I2C device

Postby eskibrew » Fri Oct 14, 2022 12:08 pm

If you are using ESP-IDF 4.4.2, you should then try using their improved, consolidated i2c function calls. After you configure the i2c, you just need `i2c_master_write_to_device` or `i2c_master_read_from_device`. The latest examples with ESP-IDF illustrate their use.
Ah interesting - I did see those and did use them originally but went back back to the more usual ones when I could not get it working - I may however have had a different problem at that some so I will try again with those functions 8-)

Who is online

Users browsing this forum: meta-externalagent, Semrush [Bot] and 7 guests