I2C wrong callback order on repeated start command

yehor_fedorov
Posts: 2
Joined: Tue May 24, 2022 11:36 am

I2C wrong callback order on repeated start command

Postby yehor_fedorov » Tue May 24, 2022 11:44 am

Board
ESP32 C3

Device Description
ESPRESSIF ESP32 C3 mini 1

Hardware Configuration
GPIO 5 - SDA
GPIO 6 - SCL

IDE Name
Arduino IDE

Operating System
macOS Big Sur v 11.6.5

Flash frequency
80 Mhz

PSRAM enabled
no

Upload speed
460800

Description
I'm trying to establish communication via i2c between 2 esp32 c3 devices. The idea is simple, I want to send non stop request to slave (with repeated start), where master write 2 bytes first and read data from slave. And it's working somehow but not in the way I expected because slave firstly run onRequest callback and then onReceive. This looks like a bug for me (correct me if I'm wrong)

As a result I'm receiving data from previous request...

Attaching my wiring and logic analyzer data for 2 requests

Sample MASTER code:

Code: Select all

### MASTER
#include "Wire.h"

#define I2C_DEV_ADDR 0x10

uint32_t i = 0;
int func = 0;
int reg = 10;

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  bool res = Wire.begin(5, 6, (uint32_t)400000);
  Serial.print("Connected: ");
  Serial.println(res ? "true" : "false");
}

void loop() {
  delay(5000);
  unsigned int first, second;

  //Write message to the slave
  Wire.beginTransmission(I2C_DEV_ADDR);
  Wire.write(func);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom(I2C_DEV_ADDR, 2, true);
  first = Wire.read();
  second = Wire.read();

  Serial.print("First: ");
  Serial.println(first);
  Serial.print("Second: ");
  Serial.println(second);

  Serial.print("Func: ");
  Serial.println(func);
  Serial.print("Register: ");
  Serial.println(reg);

  func++;
  reg++;
}
Sample SLAVE code:

Code: Select all

### SLAVE
#include "Wire.h"

#define I2C_DEV_ADDR 0x10

uint32_t i = 0;
uint8_t arr[2] = {0, 0};

void onRequest(){
  Wire.slaveWrite(arr, 2);
}

void onReceive(int len){
  arr[0] = Wire.read();
  arr[1] = Wire.read();
}

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Wire.onReceive(onReceive);
  Wire.onRequest(onRequest);
  Wire.begin((uint8_t)I2C_DEV_ADDR,5,6,(uint32_t)400000);
  Serial.println("SETUP");
}

void loop() {
}
Logs:

Code: Select all

Connected: true

// FIRST REQUEST //
First: 0
Second: 0
Func: 0
Register: 10
// END //

// SECOND REQUEST //
First: 0
Second: 10
Func: 1
Register: 11
// END //
Thanks in advance!
Attachments
first_req.jpeg
first_req.jpeg (43.22 KiB) Viewed 1471 times
wiring.jpeg
wiring.jpeg (261.71 KiB) Viewed 1471 times
second_req.jpeg
second_req.jpeg (49.29 KiB) Viewed 1471 times


raimbo
Posts: 1
Joined: Fri May 27, 2022 3:10 pm

Re: I2C wrong callback order on repeated start command

Postby raimbo » Fri May 27, 2022 3:13 pm

I'm having the same problem, I've no luck without calling Wire.endTransmission with "false".
How did you solve the problem?

Who is online

Users browsing this forum: PepeTheGreat and 58 guests