i2c instability

johnty
Posts: 4
Joined: Thu Aug 24, 2017 4:49 pm

i2c instability

Postby johnty » Thu Aug 31, 2017 8:59 pm

I've been trying to use the I2C for a number of digital sensors. They're generally 3.3V-5V capable, and for the 5V ones, I have a mosfet-based bi-directional level shifter.

Just testing the port using the arduino i2c scanner example and have it compiled+flashed via the Arduino IDE using the latest Arduino-ESP library. I've also tried the esp-idf and modified the i2c example to generate the same queries. In both cases the code basically goes through every address and simply writes the 7 bits, write bit, and then sees if there's an ACK from a slave on the bus responding to that particular address. Probing SDA/SCL without anything attached shows the correct i2c stream generated (using a bitscope micro as a logic analyzer).

However, as soon as I start trying the actual sensors, I get extremely unreliable behaviour: some devices get polled correctly, but after replugging it, the i2c port may stop working. Some devices will cause the ESP to hang completely, while others don't respond. I've tried different combinations of internal pullu-ps only as well as disabling them and using external pullups. Also tried with and without level shifting. It seems like the port is either very sensitive hardware-wise, or the i2c driver has a lot of places where it hangs/stops working. Sometimes the code still runs, but I don't see any signals on scl/sda...

My goal was to build a general purpose interface that allows plug-and-play for various i2c based sensors, but it looks like the current state of the i2c implementation might not be developed to that point yet? In contrast, I've used the same sensors (and just tried the scanning sketch) on an Arduino Uno and it works flawlessly. Also no issues running them with a Raspberry Pi using the bi-directional level shifter...

Comments/suggestion welcome!

User avatar
kolban
Posts: 1680
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: i2c instability

Postby kolban » Fri Sep 01, 2017 3:37 am

Do you have a $10 logic analyzer in your possession? I have found these to be invaluable when tracking down I2C puzzles and whole heartedly recommend one. With one of those, you can look at the SDA and CLK lines and unambiguously see what is actually happening.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Available for ESP32 consulting.

johnty
Posts: 4
Joined: Thu Aug 24, 2017 4:49 pm

Re: i2c instability

Postby johnty » Sat Sep 02, 2017 12:06 am

Do you have a $10 logic analyzer in your possession? I have found these to be invaluable when tracking down I2C puzzles and whole heartedly recommend one. With one of those, you can look at the SDA and CLK lines and unambiguously see what is actually happening.


Yes, I have a bitscope micro and using the Logic software (it's more like $100, but I'm not certain it is better than the cheaper $10 ones that exist for purely the logic analyzer feature... :p).

The main problem is that when it stops working, I get nothing on the bus! So not much to look at there... Unless of course the bitscope itself is doing something funky. But I'm not sure that is the case - often if I do a hard reset of the ESP32, I can get it to emit some valid and observable signals on the bus again.

User avatar
kolban
Posts: 1680
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: i2c instability

Postby kolban » Sat Sep 02, 2017 4:12 am

Can you clarify that the I2C apps you are trying to run are hosted within the ESP32 Arduino framework as opposed to the ESP-IDF framework? If so, then we probably want to move this thread to the Arduino sub-forum.

It has been my understanding that the I2C driver implementation for the ESP32 Arduino framework is not the same as the ESP32 I2C drivers found in the ESP-IDF framework.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Available for ESP32 consulting.

mgsnmp
Posts: 3
Joined: Mon Feb 20, 2017 2:07 pm

Re: i2c instability

Postby mgsnmp » Sat Sep 02, 2017 12:14 pm

I apologize for English. Google Translate
I had the following problem with I2C.
On my board I have the connector I2C, SPI, and IO.
When I plugged a cable ribbon on this connector, I2C completely dropped off, and
I had to be RESET.
If I plug the connector into individual cables, the I2C works properly.

johnty
Posts: 4
Joined: Thu Aug 24, 2017 4:49 pm

Re: i2c instability

Postby johnty » Wed Sep 06, 2017 8:11 pm

Can you clarify that the I2C apps you are trying to run are hosted within the ESP32 Arduino framework as opposed to the ESP-IDF framework? If so, then we probably want to move this thread to the Arduino sub-forum.

It has been my understanding that the I2C driver implementation for the ESP32 Arduino framework is not the same as the ESP32 I2C drivers found in the ESP-IDF framework.


I'm using both the IDF and Arduino library. For the Arduino version I'm using the scanner app. For the IDF version, I'm basing it on the i2c example application that calls the following scan function after the setup on the master is complete:

Code: Select all

static esp_err_t i2c_scan_addr(i2c_port_t i2c_num, uint8_t addr)
{
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, (addr << 1) | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    int ret = i2c_master_cmd_begin(i2c_num, cmd, 100/ portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    return ret; // ESP_FAIL or ESP_OK
}


I then go through the list of addresses and try to see which one responds with the ACK bit.

I'm not sure if what mgsnmp mentioned is related, but general instability is indeed what I'm seeing. Basically when it works everything looks fine, when it doesn't, its somewhat inexplicable...

johnty
Posts: 4
Joined: Thu Aug 24, 2017 4:49 pm

Re: i2c instability

Postby johnty » Tue Oct 31, 2017 9:16 pm

minor update: in case anyone stumbles across this, on the arduino-esp side, perhaps this could help in recovering the bus after something goes wrong...

i expect a similar solution exists on the idf side where one could call the lower level reset if things hang up. i haven't dug into the i2c libraries for the regular arduino boards but perhaps there is a lot of bus resets/re-inits built in between commands that make it look a lot more seamless, and prevents what looks like lockups when doing things like hot-plugging i2c devices. i also expect the arduino boards to be more robust hardware-wise, being able to perhaps handle different pull-up resistances etc. (for example, when i have 2 or three devices on an arduino uno, the scanner returns the corresponding multiple addresses. on the esp32, there are quite a few sensors that don't even show up individually)

not having extensive resources to dive deep into the issue, i leave it here as some thoughts that might inspire someone else to look into. i've since given up on using i2c on the esp32 for now...

barth_bh
Posts: 2
Joined: Sun Sep 16, 2018 12:36 am

Re: i2c instability

Postby barth_bh » Sun Sep 16, 2018 12:39 am

Has anyone solved the problem with the hanging MCP23017 on ESP32 and the Arduino IDE platform?

Archibald
Posts: 99
Joined: Mon Mar 05, 2018 12:44 am

Re: i2c instability

Postby Archibald » Sun Sep 16, 2018 10:34 am

There's an alternative Arduino-ESP32 'Wire' library here that is supposed to be better:
https://github.com/stickbreaker/arduino ... aries/Wire

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

Re: i2c instability

Postby mikemoy » Sun Sep 16, 2018 11:52 pm

Do you have 2 pull-up resistors on your SCL/SDA lines ?

Who is online

Users browsing this forum: ESP_igrr and 7 guests