Hi,
I am using I2C 1 module of TM4C123H6PM to interface 24LC512 EEPROM. I am able to make I2C module work. However, the functionality is random and may times it fails to Write or Read data to/from external EEPROM. The addressing scheme is of 16bits to cover the full range of EEPROM.
The below code is not consistent. What am i missing??
Find below code:
void ConfigureI2C(void)
{
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(I2C1_BASE));
//
// Enable Peripheral Clocks
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable pin PA7 for I2C1 I2C1SDA
//
MAP_GPIOPinConfigure(GPIO_PA7_I2C1SDA);
MAP_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
//
// Enable pin PA6 for I2C1 I2C1SCL
//
MAP_GPIOPinConfigure(GPIO_PA6_I2C1SCL);
MAP_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), false);
}
unsigned char
I2CRegRead(unsigned long ulI2CBase, unsigned char ucSlaveAdress, _I2C_LOC_ADDR LocAddr)
{
unsigned char ulRegValue = 0;
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);
//
// Place the command to be sent in the data register.
//
I2CMasterDataPut(ulI2CBase, LocAddr.SPLIT.HIGHBYTE);
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(1000);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Place the command to be sent in the data register.
//
I2CMasterDataPut(ulI2CBase, LocAddr.SPLIT.LOWBYTE);
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND);
SysCtlDelay(1000);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Tell the master module what address it will place on the bus when
// reading from the slave.
//
I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 1);
//
// Tell the master to read data.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_RECEIVE);
// SysCtlDelay(1000);
//
// Wait until master module is done receiving.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Read the data from the master.
//
ulRegValue = I2CMasterDataGet(ulI2CBase);
//
// Return the register value.
//
return ulRegValue;
}
unsigned long
I2CRegWrite(unsigned long ulI2CBase, unsigned char ucSlaveAdress,
_I2C_LOC_ADDR LocAddr, unsigned char ucValue)
{
//
// Check the arguments.
//
ASSERT(I2CMasterBaseValid(ulI2CBase));
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Tell the master module what address it will place on the bus when
// writing to the slave.
//
I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0);
//
// Place the command to be sent in the data register.
//
I2CMasterDataPut(ulI2CBase, LocAddr.SPLIT.HIGHBYTE);
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_START);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Place the value to be sent in the data register.
//
I2CMasterDataPut(ulI2CBase, LocAddr.SPLIT.LOWBYTE);
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_CONT);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Place the value to be sent in the data register.
//
I2CMasterDataPut(ulI2CBase, ucValue);
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_CONT);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Initiate send of data from the master.
//
I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_BURST_SEND_FINISH);
//
// Wait until master module is done transferring.
//
while(I2CMasterBusy(ulI2CBase))
{
};
//
// Check for errors.
//
if(I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE)
{
return 0;
}
//
// Return 1 if there is no error.
//
return 1;
}
void main(void )
{
MAP_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
ConfigureI2C(I2C1_BASE, false);
I2CLocAddr.val = 0x00;
for(I2CLocAddr.val = 0; I2CLocAddr.val < 20; I2CLocAddr.val++)
{
I2CRegWrite(I2C1_BASE, 0x50, I2CLocAddr, 0xBB);
_nop();
SysCtlDelay(5000);
}
for(I2CLocAddr1.val = 0; I2CLocAddr1.val < 20; I2CLocAddr1.val++)
{
test1 = I2CRegRead(I2C1_BASE, 0x50, I2CLocAddr1);
_nop();
SysCtlDelay(5000);
}
}