I’m trying to write and read from a Microchip 24AA01 EEPROM over the I2C1 of the TMS570LS3137. I configured the I2C driver with HALCoGen (version 3.00.00) in master mode, address size = 7 bits, count = 8 bits and baud rate = 100 kHz. I had to correct the generated initialization sequence: I2CMDR.BC = 0 instead of 7 and I2CMDR.FDF = 1 (otherwise it doesn’t work) instead of 0. At this time I can write to the EEPROM, but the reading still fails since the second start condition isn’t set correctly. I should set Start Bit - Device Address - Internal Address – Start Bit – Device Address (with read) – Response 0 - … - Response N – Stop Bit, but on the I2C bus I get Start Bit - Device Address - Internal Address– Device Address (with read) – Start Bit – Response 0. My read function is:
// Set the bus in master and transmitter mode and set the START condition
I2C->MDR |= I2C_MASTER | I2C_TRANSMITTER | I2C_START_COND;
// Send the slave address
i2cSendByte(I2C, slaveDeviceAddress);
// Send the internal address
i2cSendByte(I2C, (Uint8)readAddress);
// Set the bus in master, repeat and receiver mode and set the START condition
I2C->MDR &= ~I2C_TRANSMITTER;
I2C->MDR |= I2C_MASTER | I2C_REPEATMODE | I2C_START_COND;
// Send the slave address (for reading)
i2cSendByte(I2C, slaveDeviceAddress | 0x01);
// Receive n-1 bytes
while (readLength > 1)
{
*readBuffer++ = i2cReceiveByte(I2C);
readLength--;
}
// Receive the last bytes (and set the STOP condition)
I2C->MDR |= I2C_STOP_COND;
*readBuffer = i2cReceiveByte(I2C);
// Wait until the bus isn't busy and the master mode bit is cleared
while (I2C->STR & I2C_BUSBUSY);
while (I2C->MDR & I2C_MASTER);