I’m really struggling with this now, for some reason I can’t wrap my head around the big-endian and little-endian difference for I2C. As you can see I tried swapping the data order and sending it using a LittleWne
This is the only code I’m running, I’m just trying to figure out the crazy ness on the I2C bus. Not the code works perfectly on an Arduino.
I’ve read the datasheet a few times now, focusing mainly on Table4.2 teh read - N-bytes command.
void setup() {
WiFi.off();
Serial.begin(115200); //turn on serial communication
Wire.setSpeed(CLOCK_SPEED_100KHZ); // Set Speed to 400kHz for production 100kHz for debugging
Wire.begin();
}
void loop() {
uint8_t numBytesToRead = 28;
uint8_t ReadDataBuf[8];
uint8_t LittlEendianData[8];
uint8_t byteArray[35];
uint32_t checksumTotal = 0;
int i2c_addr = 0x74;
int i;
ReadDataBuf[0] = 0xA5; // Header
ReadDataBuf[1] = 0x08; // Num bytes
ReadDataBuf[2] = 0x41; // Command - set address pointer
ReadDataBuf[3] = 0x00;
ReadDataBuf[4] = 0x02;
ReadDataBuf[5] = 0x4E; // Command - read register, N bytes
ReadDataBuf[6] = 0x20;
ReadDataBuf[7] = 0; // Checksum 0x05E - computed below
for(i = 0; i < 7; i++) {
checksumTotal += ReadDataBuf[i];
}
ReadDataBuf[7] = checksumTotal % 256; // 0x5E = 94
// Flip the data for little endian
for(i = 0; i < 8; i++){
LittlEendianData[i] = ReadDataBuf[8 - i];
}
Wire.beginTransmission(i2c_addr);
for(i= 0; i < 8; i++) {
Wire.write(ReadDataBuf[i]);
}
Wire.endTransmission();
// delay(100);
//
// Read the specified length of data
//
Wire.requestFrom(i2c_addr, (uint8_t)(numBytesToRead + 3));
int requestDataLength = Wire.available();
if (requestDataLength==(numBytesToRead + 3)) {
//Wire.readBytes((char*)byteArray, requestDataLength); //Wire.readBytes((byte*) byteArray[I], requestDataLength);
for (i = 0; i < (numBytesToRead + 3) ; i++) {
byteArray[i] = Wire.read();
}
}
// delay(1000);
}
When in run the code with everything after the Read the specified length of data
comment I get a clean logic analyzer capture.
As you can see, A5, 08, 41, 00, 02, 4E, 20, 5E
all form the read command.
Yet when I add the Wire.requestFrom(i2c_addr, (uint8_t)numberBytesToRead + 3));
I get strange SCLK strechingwhich makes no sense. If I remove the first delay(100);
Column one shows just the Wire.write(); command (ReadDataBuf), Column two shows when I add the Wire.read(); command code, Column Three show what it looks like Working on an Arduino.
I’m not sure why the CLK has any early trigger and irregularity, it’s like it’s being stretched and messed with yet nowhere in teh above code is that support to happen.
I’m now on day 3 and I feel like I’m going backward, so any help would be appreciated.
@ScruffR I also tried Wire.readBytes(((char*)byteArray, requestDataLength));
but I’m not sure that really helped as the data was more chaotic.
Thanks again for the help !