ci2ceeprom

I2C Communication with Tiva TM4C123GXL


I am trying to connect a Tiva TM4C123GXL with I2C. I think I am writing to the data register properly, however every single time I read from I2C, I am always getting 0xFF back. I am connected to an EEPROM.

I have no idea where the problem could be. I am not allowed to use any libraries so I am accessing the registers directlyt. Any help would be much appreciated. My code for reading, writing, and initializing the i2c module is shown below:

void write_i2c(unsigned int data) {
int i;
I2C3_MSA_R = 0xA0;
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000011;

while((I2C3_MCS_R & BIT_0) != 0);

I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000001;
while((I2C3_MCS_R & BIT_0) != 0);

I2C3_MDR_R = data;
I2C3_MCS_R = 0b00000101;

while((I2C3_MCS_R & BIT_0) != 0);
for(i = 0; i < 10000; i++);
}

unsigned int read_i2c() {
I2C3_MSA_R = 0xA0;
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000011;
while((I2C3_MCS_R & BIT_0) != 0);
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000001;

I2C3_MSA_R = 0xA1;
unsigned int data;
// 0xA0 is EEPROM address shifted by 1
I2C3_MCS_R = 0b00000101;

// I2C3_MCS_R = 0b00000101;
while((I2C3_MCS_R & BIT_0) != 0);

data = I2C3_MDR_R;
return data;
}

void init_i2c() {
//address of eeprom is 1010000
// A0 is PB0, A1 is PB1, A2 is PB2
SYSCTL_RCGCI2C_R = BIT_3;

GPIO_PORTD_PUR_R |= BIT_0 | BIT_1;

// Port D Alternate Function
// PD0 is SCL, PD1 is SDA
GPIO_PORTD_AFSEL_R |= BIT_0 | BIT_1;
GPIO_PORTD_PCTL_R |= 0x33;
GPIO_PORTD_ODR_R |= BIT_1;
GPIO_PORTD_DEN_R |= BIT_0 | BIT_1;

// Enable I2C3 Master
I2C3_MCR_R = BIT_4;
I2C3_MTPR_R = 0x64;
}

Solution

  • I found the solution. There were two main reasons for my code not working:

    1. Using Ports B and D at the same time causes issues. They are connected in some way on the board, so since I was using port B already in another function, the readings were being messed up

    2. The values that I was sending to the I2CMCS register and the order they were being sent in was wrong for the read function