Hi
Here is my code for enable, write and read a byte to the EEPROM 24LC64 on I2C from the TM4C123GH6PM micro:
For some reason I fail to write and read the eeprom memory.
Please give me a hint.
#define SLAVE_ADDRESS 0xA0
void I2C_Init() {
// Set the clocking
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
// The I2C0 peripheral must be enabled before use.
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
//I2C0 is used with PortB[3:2].
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlDelay(10000);
// Select the I2C function for these pins. This function will also
// configure the GPIO pins pins for I2C operation, setting them to
// open-drain operation with weak pull-ups.
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
// Configure the pin muxing for I2C0 functions on port B2 and B3.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Enable and initialize the I2C0 master module. Use the system clock for
// the I2C0 module. The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps. For this example we will use a data rate of 100kbps.
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
SysCtlDelay(10000); // delay mandatory here - otherwise portion of SlaveAddrSet() lost!
//Reconfigure for correct operation
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
//I2C0_BASE
I2CMasterEnable(I2C0_BASE);
}
void eepromWrite(uint16_t address_u16, uint8_t data_u8)
{
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterDataPut(I2C0_BASE, 0x00); //WRITE
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterDataPut(I2C0_BASE, address_u16); //ADDRESS
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterDataPut(I2C0_BASE, data_u8); //DATA
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C0_BASE)) {}
}
void eepromRead(uint16_t address_u16, uint32_t *rxdata_pu8, uint32_t rxdataLen_u32)
{
uint32_t ret = 0;
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterDataPut(I2C0_BASE, 0x00); //READ
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterDataPut(I2C0_BASE, address_u16); //ADDRESS
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
while(I2CMasterBusy(I2C0_BASE)) {}
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE );
while(I2CMasterBusy(I2C0_BASE)) {}
ret = I2CMasterDataGet(I2C0_BASE) & 0xFF;
rxdata_pu8[0] = ret;
}
int main() {
uint8_t tx;
tx = 0x99;
uint32_t rx[1];
I2C_Init();
while (1){
eepromWrite(0x0, tx);
SimpleDelay();
rx[0] = 0x0;
eepromRead(0x0, rx, 1);
printf("Tx=%X Rx=%X / ", tx, rx[0]);
}
}
Thank You.
Regards.