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.

I2c program for EEPROM in TMS320F28027

Dear

I had written code as said by 

1.I am using TMs320F28027 launcpad and 24LC256 EEPROM

2. Please guide me it is not working

3. Should I give delay between each read and write comand

4. Is there required to address NACK from EEPROM. how to address, I mean to say after NACK interrupt from were should i start the program

5 Is every time register ready interrupt needs to be addressed, other than read cycle.


  • Dear

    I had written code as said by 

    1.I am using TMs320F28027 launcpad and 24LC256 EEPROM

    2. Please guide me it is not working

    3. Should I give delay between each read and write comand

    4. Is there required to address NACK from EEPROM. how to address, I mean to say after NACK interrupt from were should i start the program

    5 Is every time register ready interrupt needs to be addressed, other than read cycle.

    void I2CA_Init(void);

    void write(void);
    void read(void);
    interrupt void i2c_int1a_isr(void);
    void i2ccmdrsettings(Uint16 count, Uint16 read);
    Uint16 counter=0;
    int current=23;
    Uint16 date=32;
    Uint16 month=12;
    Uint16 year=2015;
    Uint16 cap_status=0xA0A0;
    char slash='/';
    Uint16 cap_status1=0;
    Uint16 year1=0;
    Uint16 reading=0;
    void main(void)
    {

    // Step 1. Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the DSP2802x_SysCtrl.c file.
    InitSysCtrl();
    // Step 2. Initalize GPIO:
    // This example function is found in the DSP2802x_Gpio.c file and
    // illustrates how to set the GPIO to it's default state.
    // InitGpio();
    // Setup only the GP I/O only for I2C functionality
    InitI2CGpio();

    // Step 3. Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
    DINT;

    // Initialize PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    // This function is found in the DSP2802x_PieCtrl.c file.
    InitPieCtrl();

    // Disable CPU interrupts and clear all CPU interrupt flags:
    IER = 0x0000;
    IFR = 0x0000;

    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example. This is useful for debug purposes.
    // The shell ISR routines are found in DSP2802x_DefaultIsr.c.
    // This function is found in DSP2802x_PieVect.c.
    InitPieVectTable();

    // Interrupts that are used in this example are re-mapped to
    // ISR functions found within this file.
    EALLOW; // This is needed to write to EALLOW protected registers
    PieVectTable.I2CINT1A = &i2c_int1a_isr;
    // PieVectTable.I2CINT2A = &i2c_int2a_isr; for FIFO interrupts
    EDIS; // This is needed to disable write to EALLOW protected registers

    // Step 4. Initialize all the Device Peripherals:
    // This function is found in DSP2802x_InitPeripherals.c
    // InitPeripherals(); // Not required for this example
    // Enable interrupts required for this example

    // Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
    PieCtrlRegs.PIEIER8.bit.INTx1 = 1;

    // Enable CPU INT8 which is connected to PIE group 8
    IER |= M_INT8;
    EINT;
    I2CA_Init();
    write();
    if (reading==2)
    {read();}
    }

    void write(void)
    {
    i2ccmdrsettings(8 ,1); // 5 isinclusive of two bytes of memory address
    I2caRegs.I2CDXR =0x00;// MemoryHighAddr; //Send eeprom high address
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =0x30;// MemoryLowAddr; //Send eeprom low address
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =cap_status; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =cap_status>>8; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =date; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =month; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =year; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =year>>8; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    }
    void read(void)
    {
    i2ccmdrsettings(2 ,2); //Set count to 2 address bytes
    I2caRegs.I2CDXR = 0x00;//MemoryHighAddr; //Send eeprom high address
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =0x30;// MemoryLowAddr; //Send eeprom low address
    while(I2caRegs.I2CSTR.bit.ARDY == 0){};
    i2ccmdrsettings(5 ,1);
    reading=1;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    cap_status = I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    cap_status1 = I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    date = I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    month = I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    year=I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    year1=I2caRegs.I2CDRR;
    while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read?
    year =year | (year1<<8);
    cap_status=cap_status|(cap_status1<<8);
    }

    interrupt void i2c_int1a_isr(void)
    {
    if (reading==1)
    { I2caRegs.I2CSTR.bit.RRDY = 1;}
    Uint16 IntSource;
    IntSource = I2caRegs.I2CISRC.all;
    if(IntSource == 0x6)// stop dtected
    { reading=2;}
    if(IntSource == 0x3)
    { I2caRegs.I2CSTR.bit.ARDY = 1;}
    // Enable future I2C (PIE Group 8) interrupts
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
    }
    void I2CA_Init(void)
    {
    // Initialize I2C
    I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
    // I2CCLK = SYSCLK/(I2CPSC+1)
    #if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
    I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
    #endif
    #if (CPU_FRQ_60MHZ)
    I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
    #endif
    I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
    I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
    I2caRegs.I2CIER.all = 0x1E; // Enable SCD,XRDY,RRDY,ARDY & NACK interrupts
    I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
    return;
    }

    void i2ccmdrsettings(Uint16 count, Uint16 read)
    {
    I2caRegs.I2CSAR = 0x50; //Set slave address
    while (I2caRegs.I2CSTR.bit.BB != 1){}; // check for busy bit
    I2caRegs.I2CCNT = count;
    if (read==1)
    {I2caRegs.I2CMDR.bit.TRX = 0; } //Set to Transmit mode
    if (read==0)
    {I2caRegs.I2CMDR.bit.TRX = 1;} //Set to Transmit mode
    I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode
    I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode
    if(read==2)
    {I2caRegs.I2CMDR.bit.STP = 0; //Stop when internal counter becomes 0
    I2caRegs.I2CMDR.bit.TRX = 1;}
    else
    {I2caRegs.I2CMDR.bit.STP = 1;}
    I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow
    }




  • Hi,

    does the EEPROM ACK its address? If you are getting a NACK on the address, it could be you have the wrong address or the EEPROM is not responding. 

  • Do I. Need to put delay of few milliseconds between write cycle or read cycle. Because if I write data and if eeprom is still busy writing previous data it may generate nack for which should I generate stop bit or wait again or nack bit is cleared manually and repeat the data write
    Please guide
    The program is copied above u can suggest if any thing is wrong
  • I might be wrong but I would have imagined that the EEPROM would not have acked the write had it not finished writing.

    Try adding some delay between writes and reads and see if that solves the problem.
  • 1.Is my mthodolgy to write the viable year to i2cdxr register ok or first I need to convert it to hex and then write to i2cdxr
    Uint16 year=2015;
    I2caRegs.I2CDXR =year; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
    I2caRegs.I2CDXR =year>>8; //Send out the message
    while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out
  • you dont need to convert to hex. its correct, you have to sent the count I2CCNT to 2, before you do all this.

    You also set the mode I2CMDR to set the start and stop bits, release I2C from reset, set I2C to master transmitter.