2018-11-28 01:52 AM
Hi,
I'm trying to interface this eeprom with Bluenrg2
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8828-SEEPROM-AT24CM02-Datasheet.pdf
I can write correctly. I can read 1 byte at time correctly. Trying to read more than 1 bytes results in incorrect readed data.
when dataLen=1, read works correctly.
when dataLen>1, read returns incorrect data
bool Board_i2cReadEeprom(uint8_t deviceAddr, uint32_t position, uint8_t* data, uint8_t dataLen) {
I2C_TransactionType t;
uint32_t ii = 0;
deviceAddr |= (position >> 16);
Log_debug("Board", "reading");
Log_debug("Board", "deviceAddr: %d", deviceAddr);
Log_debug("Board", "position: %"PRIu32" ", position);
t.Operation = I2C_Operation_Write;
t.Address = deviceAddr;
t.StartByte = I2C_StartByte_Disable;
t.AddressType = I2C_AddressType_7Bit;
t.StopCondition = I2C_StopCondition_Disable;
t.Length = 2;
I2C_FlushTx((I2C_Type*) BOARD_I2C);
while (I2C_WaitFlushTx((I2C_Type*) BOARD_I2C) == I2C_OP_ONGOING) {
if (ii++ > 1000000) {
ii = 0;
Log_error("Board", "i2c read1 timeout");
return false;
}
}
Board_i2c_eot = RESET;
I2C_BeginTransaction((I2C_Type*) BOARD_I2C, &t);
I2C_FillTxFIFO((I2C_Type*) BOARD_I2C, position >> 8);
I2C_FillTxFIFO((I2C_Type*) BOARD_I2C, position);
do {
if (I2C_GetStatus((I2C_Type*) BOARD_I2C) == I2C_OP_ABORTED) {
Log_error("Board", "i2c read op aborted");
return false;
}
if (ii++ > 1000000) {
ii = 0;
Log_error("Board", "i2c read2 timeout");
return false;
}
} while (Board_i2c_eot == RESET);
while (!Board_i2cIsReady(deviceAddr)) {
Board_wait(1);
}
t.Operation = I2C_Operation_Read;
t.Address = deviceAddr;
t.StartByte = I2C_StartByte_Disable;
t.AddressType = I2C_AddressType_7Bit;
t.StopCondition = I2C_StopCondition_Enable;
t.Length = dataLen;
Board_i2c_eot = RESET;
I2C_BeginTransaction((I2C_Type*) BOARD_I2C, &t);
do {
if (I2C_GetStatus((I2C_Type*) BOARD_I2C) == I2C_OP_ABORTED) {
Log_error("Board", "i2c read op aborted");
Log_error("Board", "cause: %"PRIu32" ", ((I2C_Type*) BOARD_I2C)->SR_b.CAUSE);
return false;
}
if (ii++ > 1000000) {
ii = 0;
Log_error("Board", "i2c read3 timeout");
return false;
}
} while (Board_i2c_eot == RESET);
for (uint8_t i = 0; i < dataLen; i++) {
*data = I2C_ReceiveData((I2C_Type*) BOARD_I2C);
data++;
}
return true;
}
void I2C2_Handler(void) {
I2C_ClearITPendingBit((I2C_Type*) BOARD_I2C, I2C_IT_MTD | I2C_IT_MTDWS);
Board_i2c_eot = SET;
}
2019-02-20 10:23 PM
Hi Francesco,
Could you provide the traces for I2C bus?
The function looks fine to me, except that i don't quite understand the reasons for line 47-49.
while (!Board_i2cIsReady(deviceAddr)) {
Board_wait(1);
}
Thank you.
Best Regards,
Winfred