Hello
I am currently working on a board that interfaces with a flight controller via I2C.
I created a pcb using an ESP32 S3, since i am familiar with the regular ESP32.
I noticed that there was this weird artifact appearing in the data that the flight controller was receiving.
I created a simplified version of the code and ran it, same issue.
Replaced the flight controller with an ESP32, same issue.
Replaced the ESP32 S3 with an ESP32 (two esp32 talking with eachother), the issue disappeared.
I noticed that if the slave sends more bytes than the master requests then the issue becomes worse.
The data that was to be sent from the slave is 0x01FF03FF. With 2 ESP32 this is what i get.
When i change the slave to an ESP32 S3 i get 0x61, 0xff, 0x03, 0xff on the master.
What i did notice is that the address is 0x30, the first byte on the data that the master gets is the address, bit shifted left once and the write bit is on. I Tried changing the address and the values on this byte change accordingly.
If i request 5 bytes then something even weirder happens, the response in the master shifts around.
It looks like it is loosing sync with the I2C frame.
The code is as follows:
Slave
#include "Wire.h" // i2c library
#define I2C_DEV_ADDR 0x30 // i2c adress, needs to be the same as in the lua script
int opcode = 0; // received operation code
int i2cCommand = 0; // received i2c command
typedef union Dados_I { // union for sending int via i2c
int i;
byte b[4];
};
Dados_I batata;
void setup() {
Serial.begin(115200); // start the serial port, used for debuging
Serial.setDebugOutput(true);
Wire1.onReceive(onReceive); // if there is a i2c receive call then onReceive function is called
Wire1.onRequest(onRequest); // if there is a i2c request call then onRequest function is called
//Wire1.begin(I2C_DEV_ADDR);
Wire1.begin(I2C_DEV_ADDR,14,21,0 );// start the i2c channel with the I2C_DEV_ADDR adress
Serial.println("setup");
batata.i=0x01FF03FF;
}
void loop() {
// put your main code here, to run repeatedly:
}
void onReceive(int len) { // on receive function, used to update local variables or activate local functions
Serial.println("receive");
Serial.println(len);
i2cCommand = 0; // resets the command
opcode = Wire1.read(); // reads the operation code
Serial.println(opcode);
if (len == 2) { // if there are 2 bytes
i2cCommand = Wire1.read(); // the second byte is the command
Serial.println(i2cCommand);
}
while (Wire1.available()) { // clears the buffer if there are more requests
Serial.println("lixo");
int dadosExtra = Wire1.read();
}
}
void onRequest() { // on request function, sends data via i2c depeding on the request
Serial.println("send");
Wire1.write(batata.b, 4);
}
Master:
#include "Wire.h"
#define I2C_DEV_ADDR 0x30
uint32_t i = 0;
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Wire.begin();
}
void loop() {
delay(100);
uint8_t bytesReceived = Wire.requestFrom(I2C_DEV_ADDR, 4);
Serial.printf("requestFrom: %u\n", bytesReceived);
if ((bool)bytesReceived) { //If received more than zero bytes
uint8_t temp[bytesReceived];
Wire.readBytes(temp, bytesReceived);
log_print_buf(temp, bytesReceived);
Serial.println(Wire.available());
}
}
Does anyone know a solution for this problem?