I2C wrong callback order on repeated start command
Posted: 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:
Sample SLAVE code:
Logs:
Thanks in advance!
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++;
}
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() {
}
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 //