Author Topic: ESP32 I2C writes work but not reads?  (Read 1674 times)

0 Members and 1 Guest are viewing this topic.

Online NiHaoMikeTopic starter

  • Super Contributor
  • ***
  • Posts: 9238
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
ESP32 I2C writes work but not reads?
« on: November 28, 2018, 06:04:24 am »
I'm working on a project that involves an ESP32 communicating with various I2C slave devices, such as a MCP23017.

This works as expected:
Code: [Select]
void MCP_writeA(uint8_t dat) {
  Wire.beginTransmission(0x20);
  Wire.write(0x12);
  Wire.write(dat);
  Wire.endTransmission();
}

This always returns 0 regardless of what the slave actually sent:
Code: [Select]
uint8_t MCP_readA() {
  Wire.beginTransmission(0x20);
  Wire.write(0x12);
  Wire.endTransmission(false);
  Wire.requestFrom(0x20, 1);
  return Wire.read();
}

This, for reading the cell voltages from the BMS, also doesn't work:
Code: [Select]
void BMS_Vcell(uint16_t ret[]) {
  Wire.beginTransmission(0x19);
  Wire.write(0x00);
  Wire.endTransmission(false);
  Wire.requestFrom(0x19, 8);
  ret[0] = (Wire.read() << 8) | Wire.read();
  ret[1] = (Wire.read() << 8) | Wire.read();
  ret[2] = (Wire.read() << 8) | Wire.read();
  ret[3] = (Wire.read() << 8) | Wire.read();
}

In all cases, I have verified that the traffic on the bus is as expected with a logic analyzer. (As in the slaves are in fact returning data.) I'm using the stable (1.0.0) arduino-esp32 core on the Arduino 1.8.7 IDE. I'm simply calling Wire.begin() to initialize the I2C.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2812
  • Country: nz
Re: ESP32 I2C writes work but not reads?
« Reply #1 on: November 29, 2018, 11:32:41 pm »
I have sometimes had these sorts of issues with a multi-function pin that isn't configured with the desired function. See if you use a different I/O pin and get similar results.

... humm... just had a look on the web while waiting for something to happen, and it looks like it should work.

Have you looked at what Wire.available() says?
« Last Edit: November 29, 2018, 11:49:37 pm by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline tsman

  • Frequent Contributor
  • **
  • Posts: 599
  • Country: gb
Re: ESP32 I2C writes work but not reads?
« Reply #2 on: November 30, 2018, 03:08:57 am »
There are several I2C bugs in arduino-esp32 core 1.0.0. They're only fixed in the development branch so try 1.0.1RC1.
« Last Edit: November 30, 2018, 01:02:23 pm by tsman »
 
The following users thanked this post: NiHaoMike, thm_w

Online NiHaoMikeTopic starter

  • Super Contributor
  • ***
  • Posts: 9238
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: ESP32 I2C writes work but not reads?
« Reply #3 on: November 30, 2018, 05:13:47 am »
There are several I2C bugs in arduino-esp32 core 1.0.0. They're only fixed in the development branch so try 1.0.0RC1.
That was it. Over 2 years since the ESP32 was released to public and something as basic as I2C is still broken on the stable branch? Just imagine how confusing it would be without a logic analyzer to get the ground truth of what's going on...
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline tsman

  • Frequent Contributor
  • **
  • Posts: 599
  • Country: gb
Re: ESP32 I2C writes work but not reads?
« Reply #4 on: November 30, 2018, 01:44:25 pm »
That was it. Over 2 years since the ESP32 was released to public and something as basic as I2C is still broken on the stable branch? Just imagine how confusing it would be without a logic analyzer to get the ground truth of what's going on...
Oops. I made a typo on the version since it should be 1.0.1RC1 but you worked that out.

ESP32 Arduino core 1.0.0 only came out a few months ago but that isn't any excuse for this mess. This I2C bug was known about months before 1.0.0 was even released and somebody had already made a fixed version of the library.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf