Hello,
I am writing code to read and write data from an EEPROM using I2C without the use of interrupts. Due to project constraints, I cannot use interrupts.
I have confirmed my read function works correctly. I am able to write to my eeprom using different code and read it back using my read function.
However, my write function does not work correctly. For example, when I write 0x3A to a memory location I read back 0xCC. Since I have confirmed the read function works ok, I believe my write function is the source of the incorrect read data.
Any help would be appreciated.
Below is the is the write function:
// Read data from EEPROM using polling instead of interrupts
void I2cRx(structI2CMSG *msg)
{
Uint16 i;
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
I2caRegs.I2CMDR.bit.STP = 0;
}
I2caRegs.I2CSAR = 0x0050;
// Check if bus busy
while(I2caRegs.I2CSTR.bit.BB == 1){}
I2caRegs.I2CCNT = 2;
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
I2caRegs.I2CMDR.all = 0x2620; // orig 0x2620. Send data to setup EEPROM address
while(I2caRegs.I2CSTR.bit.ARDY == 0)
{
// wait until communication complete and registers ready
}
I2caRegs.I2CCNT = msg->NumOfBytes; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // originally 0x2C20. Send restart as master receiver
while(I2caRegs.I2CFFRX.bit.RXFFINT == 0)
{
// wait until I2CDRR is ready to be read
}
//Clear message buffer for debugging purposes to ensure correct data is read from I2CDRR.
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = 0;
}
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = I2caRegs.I2CDRR;
}
}