i2c driving me nuts - unexpected adresses?!? please advice!

licht77
Posts: 6
Joined: Sun Apr 08, 2018 8:31 am

i2c driving me nuts - unexpected adresses?!? please advice!

Postby licht77 » Sat Jul 14, 2018 9:37 am

Hi folks!

I am playing around with a project and i2c is just driving me nuts. I get occasional freezes of the whole ESP32 what is a show stopper.
As a rookie I want to learn and try to understand what is going wrong - so I bought a cheap scope and logic analyzer.

For pinning down the problems I started by setting up a simple test circuit with just ONE i2c device: a MCP23017 port expander.

Using the standard Adafruit library with the most simple code possible (see code at the end of the post). The code IS WORKING and the pin is turned on as expected. Now I had a look what is going on under the hood.

What I expected:
The slaves adress is 0x20 (as in the documentation as well as in the library as a constant #define MCP23017_ADDRESS 0x20).
Without going too far and pretend to understand what the library is doing exactly, I expected at least some traffic adressing 0x20 after the start condition. Lets say: "Start" -> "Address" (0100 0000) -> Read/Write -> Ack. Then whatever the library is reading/writing to that slave.

What I got instead:

Thats whats on the bus actually - no 0x20 but adressing to 0x40??

Code: Select all

0.000007666666667,I2C,Setup Write to [@ (0x40)] + ACK
0.000100166666667,I2C,'0' (0x00) + ACK
0.000192666666667,I2C,'255' (0xFF) + ACK
0.000377083333333,I2C,Setup Write to [@ (0x40)] + ACK
0.000469583333333,I2C,'1' (0x01) + ACK
0.000562000000000,I2C,'255' (0xFF) + ACK
0.000733166666667,I2C,Setup Write to [@ (0x40)] + ACK
0.000825583333333,I2C,'0' (0x00) + ACK
0.000994500000000,I2C,Setup Read to [A (0x41)] + ACK
0.001086916666667,I2C,'255' (0xFF) + NAK
0.001254083333333,I2C,Setup Write to [@ (0x40)] + ACK
0.001346583333333,I2C,'0' (0x00) + ACK
0.001439000000000,I2C,'254' (0xFE) + ACK
5.001864583333333,I2C,Setup Write to [@ (0x40)] + ACK
5.001957083333333,I2C,'20' (0x14) + ACK
5.002120500000000,I2C,Setup Read to [A (0x41)] + ACK
5.002213000000000,I2C,'1' (0x01) + NAK
5.002378166666666,I2C,Setup Write to [@ (0x40)] + ACK
5.002470666666667,I2C,'18' (0x12) + ACK
5.002563083333333,I2C,'1' (0x01) + ACK
See the same communication split up into 2 screenshots as well as the scope log with the first transmission.

Does this makes any sense? Any hint is highly apprectiated!

Code: Select all

#include <Wire.h>
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

Adafruit_MCP23017 mcp;
  
void setup() {  
  mcp.begin();      // use default address 0

  mcp.pinMode(0, OUTPUT);

    delay(5000);

  mcp.digitalWrite(0, HIGH);
}
Attachments
logic1.png
logic1.png (24.58 KiB) Viewed 9259 times
logic2.png
logic2.png (19.13 KiB) Viewed 9259 times
comm1.png
comm1.png (12.06 KiB) Viewed 9259 times

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

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby kolban » Sat Jul 14, 2018 1:42 pm

Have a look at this article:

https://en.wikipedia.org/wiki/I%C2%B2C

Specifically, search within the page for a heading called "Addressing structure" immediately followed by "7-bit addressing". What you will find there is the bit format of an I2C address. Try not to think of I2C as working with a stream of bytes but instead a stream of bits. What you will find is that an address is defined by 8 bits.

The first 7 bits are the address followed by a single bit which defines whether the request (to that address) is a read (1) or a write (0).

What this means is that if you are writing to a device at address 0x20 (remember addresses are SEVEN bits) ... this will be:

0b0100000 - address
0b0 - Write

Which is you were to interpret as 8 bits would be:

0b01000000 -> 0x40

If what you were doing was a read, you would see:

0b01000001 -> 0x41
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

licht77
Posts: 6
Joined: Sun Apr 08, 2018 8:31 am

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby licht77 » Sun Jul 15, 2018 7:39 am

Aaargh cant believe how obviosly :)

Thanks Kolban - that makes it clear to me of course! Now I can go on and have a look why i2c seems to freeze the whole esp32 chip every now and then...

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby fly135 » Sun Jul 15, 2018 1:05 pm

Just for reference I'm calling i2c functions in my app all the time and haven't experienced any issues.

John A

licht77
Posts: 6
Joined: Sun Apr 08, 2018 8:31 am

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby licht77 » Mon Jul 16, 2018 8:17 am

@fly135: Yes - normally it works fine!
But there are several known flaws (eg i2c Write Issue #834) in the i2c implementation within esp32... and one of them is really nagging me :)

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby fly135 » Mon Jul 16, 2018 3:26 pm

licht77 wrote:@fly135: Yes - normally it works fine!
But there are several known flaws (eg i2c Write Issue #834) in the i2c implementation within esp32...
Don't know where you find #834. I checked the issues on the espressif/esp-if github and didn't see it.

John A

licht77
Posts: 6
Joined: Sun Apr 08, 2018 8:31 am

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby licht77 » Mon Jul 16, 2018 5:52 pm


User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: i2c driving me nuts - unexpected adresses?!? please advice!

Postby fly135 » Mon Jul 16, 2018 6:19 pm

I'm thinking this problem doesn't exist with the esp-idf. I am reading i2c from multiple tasks continuously.

John A

Who is online

Users browsing this forum: Bing [Bot] and 150 guests