Hello,
My issue with Xiao board is that i can not read the available bytes from I2C, Wire.available() >=2, but if i flash the same code on Arduino Nano it work fine.
My code is below:
Step1: i call the function to read data with for-loop
Step2: i call the readValue function
Step3: I2C communication with the slave
From Serial monitor i see that i enter the readValue function with the correct Address and Register but does not access the Wire.available() to get the data.
SERIAL MONITOR
10:03:24.621 -> Read Value
10:03:24.621 -> address: A
10:03:24.621 -> regNum: 1
10:03:24.621 -> AK: A,1,0,0
10:03:24.621 -> Read Value
10:03:24.621 -> address: A
10:03:24.621 -> regNum: 2
10:03:24.621 -> AK: A,2,0,0
10:03:24.621 -> Read Value
10:03:24.621 -> address: A
10:03:24.621 -> regNum: 4
10:03:24.621 -> AK: A,4,0,0
10:03:24.621 -> Read Value
10:03:24.621 -> address: A
10:03:24.621 -> regNum: 3C
10:03:24.621 -> AK: A,3C,0,0
10:03:24.621 -> 1,0,0,0,0,0
10:03:24.667 -> 11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0
10:03:24.699 -> 12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0
CODE
------------- Step 1 --------------------------------------
bool LTC1760::readSystemData(uint16_t* sysData) {
// bool ret = true;
for (int i = 0; i < SYSTEM_VALUE_COUNT; i++) {
switch (i) {
case POS_SYSTEM_ID: sysData[POS_SYSTEM_ID] = systemId; break;
case POS_SYSTEM_STATE: readSystemState(&sysData[POS_SYSTEM_STATE]); break;
case POS_SYSTEM_STATE_CONT: readSystemStateCont(&sysData[POS_SYSTEM_STATE_CONT]); break;
case POS_SYSTEM_INFO: readSystemInfo(&sysData[POS_SYSTEM_INFO]); break;
case POS_SYSTEM_LTC: readSystemLtc(&sysData[POS_SYSTEM_LTC]); break;
default: break;
}
// if (ret == false) break;
}
// return ret;
}
------------- Step 2 --------------------------------------
bool LTC1760::readSystemState(uint16_t* systemState) {
return readValue(hostAddress, REG_SYSTEM_STATE, systemState, true) && (*systemState != UINT16_MAX);
}
bool LTC1760::readSystemStateCont(uint16_t* systemStateCont) {
return readValue(hostAddress, REG_SYSTEM_STATE_CONT, systemStateCont, true) && (*systemStateCont != UINT16_MAX);
}
bool LTC1760::readSystemInfo(uint16_t* systemInfo) {
return readValue(hostAddress, REG_SYSTEM_INFO, systemInfo, true) && (*systemInfo != UINT16_MAX);
}
bool LTC1760::readSystemLtc(uint16_t* ltc) {
return readValue(hostAddress, REG_SYSTEM_LTC, ltc, true) && (*ltc != UINT16_MAX);
}
------------- Step 3 --------------------------------------
bool LTC1760::readValue(byte address, byte regNum, uint16_t* ret, bool twiceOnError /*= false*/) {
Serial.println("Read Value");
Serial.print("address: ");
Serial.println(address,HEX);
Serial.print("regNum: ");
Serial.println(regNum,HEX);
bool erg = false;
Wire.beginTransmission(address);
Wire.write(regNum);
Wire.endTransmission(false);
Wire.requestFrom(address, 2);
*ret = 0;
if (Wire.available() >=2 ) {
Serial.println("READ SLAVE DATA");
*ret = Wire.read(); // receive low byte
*ret |= Wire.read() << 8; // receive high byte
erg = true;
}
if (twiceOnError == true && *ret == UINT16_MAX) {
erg = readValue(address, regNum, ret);
}
Serial.print("AK: ");
Serial.print(address,HEX);
Serial.print(",");
Serial.print(regNum,HEX);
Serial.print(",");
Serial.print(*ret);
Serial.print(",");
Serial.println(erg);
return erg;
}
Also if i run I2C scanner i can see the address
10:08:29.328 -> I2C scanner. Scanning ...
10:08:29.328 -> 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA
10:08:29.328 -> **Found address: 10 (0xA)**
10:08:29.328 -> 0xB
10:08:29.328 -> Found address: 11 (0xB)
10:08:29.466 -> Found 2 device(s).
Thank you in advance
Alexis