This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EK-TM4C123GXL: I'm trying to make functions that read and write data to external EEPROM

Part Number: EK-TM4C123GXL

I made functions but I have no idea why my code is not working. Is my I2C protocol is wrong? My EEPROM is 24LC43A


void I2C_setup(){
    RCGCGPIO |= 0x8;//port E clock enable
    RCGCI2C |= 0x8;//I2C module 3 enable
    GPIODEN_PORTD = 0x3;//enable digital for port D0 and D1 => 00000011
    GPIOAFSEL_PORTD |= 0x3;//There is a alternative function for D0 and D1
    GPIOPCTL_PORTD |= 0x33;//function 3 for D0 and D1 which is SCL and SDA
    GPIOODR_PORTD |= 0x2;//drain for D1 00000010
    I2CMCR_3 = 0x10;//this is master
    I2C_set_SCL_peiiod(0.0001);
}

void I2C_set_SCL_peiiod(double SCL_period){
    I2CMTPR_3 = SCL_period * 800000 + 1;
}

void I2Ceeprom_byte_write(char data, int address)
{
    char upper_address = (unsigned)address >> 8;
    char lower_address = (unsigned)address & 0xff;
    // Send start condition
    I2CMSA_3 = 0xA0;//1010 000 0 transmit
    I2CMCS_3 = 0x3;//00011 generate start bit and I2C enable
    I2C_eeprom_wait();


    I2CMDR_3 = upper_address;//send address(1/2)
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMDR_3 = lower_address;//send address(2/2)
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();

    I2CMDR_3 = data;
    I2CMCS_3 = 0x5;//generate stop
    I2C_eeprom_wait();
    I2C_eeprom_bus_wait();
}

int I2Ceeprom_byte_read(int address)
{
    char upper_address = (unsigned)address >> 8;
    char lower_address = (unsigned)address & 0xff;
    int data;

    I2CMSA_3 = 0xA0;
    I2CMCS_3 = 0x3;
    I2C_eeprom_wait();

    I2CMDR_3 = upper_address;
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMDR_3 = lower_address;
    I2CMCS_3 = 0x1;
    I2C_eeprom_wait();


    I2CMSA_3 = 0xA1;
    I2CMCS_3 = 7;
    I2C_eeprom_wait();
    I2C_eeprom_bus_wait();
    data = I2CMDR_3;
    return data;
}

void I2C_eeprom_wait(){
    while(I2CMCS_3 & 0x1);
}

void I2C_eeprom_bus_wait(){
    while(I2CMCS_3 & 0x40);
}