Skip to content

I2C Master Wire.requestFrom timeout not obeyed, takes a whole second to read when slave is not present #5934

Closed
@leifclaesson

Description

@leifclaesson

What you are trying to do?
I'm trying to read from an I2C device which is not always present.
With ESP32 Arduino 1.0.6, the read fails instantly as it should. With 2.0.1 it blocks for a full second (1000ms) every time.

This sketch will duplicate the error, no hardware required.

#include <Wire.h>

void setup()
{
	Serial.begin(115200);

	Wire.begin();
}

void loop()
{

	uint32_t time_before=millis();

	const int address=10;
	const int length=16;

	int rcvd = Wire.requestFrom(address, (uint8_t) length, (uint8_t) true);
	if(rcvd==length)
	{
		uint8_t data[length];
		Wire.readBytes(data, length);
	}

	Serial.printf("I2C: Received %u bytes in %u ms\n",rcvd,millis()-time_before);

	delay(250);
}

If run on a regular ESP32 board with Arduino Core version 1.0.6, each Wire.requestFrom completes instantly or within 1 millisecond. With 2.0.1 it takes 1001 milliseconds.

I discovered the issue when recompiling a large project with the new Arduino core, so I stripped it down until I found the issue, but I don't know what's causing it.

I followed the code into esp32-hal-i2c.c and attempted to change the last parameter of i2c_master_cmd_begin to a hardcoded 1, and it still took a full second to complete the failed read -- the parameter change made no difference at all, so it seems perhaps the issue is all the way down inside the IDF?

Hardware:

Board: Generic 38-pin ESP32 dev board
Core Installation version: 2.0.1 (and for reference 1.0.6)
IDE name: Sloeber 4.4.0
Flash Frequency: 40Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Windows 10

Debug Messages:

I tried, there are no I2C related debug messages even on Verbose.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions