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 module setup need help

Other Parts Discussed in Thread: TMS320F28020

hi:

I need EEPROM to store paramets about control motor .    but many days pasted , I cannot do it , because it is very confuse about I2C in TI device .   even though controlsuit has example .  

but example is not common coder for application .   it is very confuse in state machine .    if send number over 2 .    it will not work .

So , I re-code my own program  . but can not read .      I beg someone can help me to check my code.

note:   write program is ok , not stuck . but read function stuck in    "  while(I2caRegs.I2CSTR.bit.XRDY == 0){}; "

that is my code bellow:

void I2CA_Init(void)
{
   // Initialize I2C
   I2caRegs.I2CMDR.all = 0x0000;
   I2caRegs.I2CSAR = 0;     // Slave address

   I2caRegs.I2CPSC.all = 6;                 // Prescaler - need 7-12 Mhz on module clk
   I2caRegs.I2CCLKL = 10;                   // NOTE: must be non zero
   I2caRegs.I2CCLKH = 5;                    // NOTE: must be non zero

   //I2caRegs.I2CIER.all = 0x24;            // Enable SCD & ARDY interrupts  0010 0100
   I2caRegs.I2CIER.bit.AAS = 0;             // Addressed as slave interrupt enable bit
   I2caRegs.I2CIER.bit.SCD = 0;             // Stop condition detected interrupt enable bit
   I2caRegs.I2CIER.bit.XRDY = 0;            // Transmit-data-ready interrupt enable bit
   I2caRegs.I2CIER.bit.RRDY = 0;            // Receive-data-ready interrupt enable bit
   I2caRegs.I2CIER.bit.ARDY = 0;            // Register-access-ready interrupt enable bit
   I2caRegs.I2CIER.bit.NACK = 0;            // No-acknowledgment interrupt enable bit
   I2caRegs.I2CIER.bit.ARBL = 0;            // Arbitration-lost interrupt enable bit

   //I2caRegs.I2CMDR.all = 0x0020;          // Take I2C out of reset,Stop I2C when suspended
   I2caRegs.I2CMDR.bit.NACKMOD = 0;         // NACK mode bit
   I2caRegs.I2CMDR.bit.FREE = 0;            // Stop I2C when suspended
   I2caRegs.I2CMDR.bit.STT = 0;             // START condition bit
   I2caRegs.I2CMDR.bit.STP = 0;             // STOP condition bit
   I2caRegs.I2CMDR.bit.MST = 0;             // Slave mode
   I2caRegs.I2CMDR.bit.TRX = 0;             // Receiver mode
   I2caRegs.I2CMDR.bit.XA = 0;              // 7-bit addressing mode
   I2caRegs.I2CMDR.bit.RM = 0;              // Nonrepeat mode
   I2caRegs.I2CMDR.bit.DLB = 0;             // Digital loopback mode is disabled
   I2caRegs.I2CMDR.bit.IRS = 1;             // The I2C module is enabled
   I2caRegs.I2CMDR.bit.STB = 0;             // The I2C module is not in the START byte mode
   I2caRegs.I2CMDR.bit.FDF = 0;             // Free data format mode is disabled
   I2caRegs.I2CMDR.bit.BC = 0;              // 8 bits per data byte

   //I2caRegs.I2CFFTX.all = 0x6000;         // Enable FIFO mode and TXFIFO
//   I2caRegs.I2CFFTX.bit.I2CFFEN = 1;        // Enable the I2C FIFO mode
//   I2caRegs.I2CFFTX.bit.TXFFRST = 1;        // Enable the transmit FIFO operation

   //I2caRegs.I2CFFTX.all = 0x0000;         // Disable FIFO mode and TXFIFO
   I2caRegs.I2CFFTX.bit.I2CFFEN = 0;        // Enable the I2C FIFO mode
   I2caRegs.I2CFFTX.bit.TXFFRST = 0;        // Enable the transmit FIFO operation
   I2caRegs.I2CFFTX.bit.TXFFINTCLR = 0;     // Clear the TXFFINT flag
   I2caRegs.I2CFFTX.bit.TXFFIENA = 0;       // TXFFINT flag does not generate an interrupt when set
   I2caRegs.I2CFFTX.bit.TXFFIL = 0;         // Transmit FIFO interrupt level

   //I2caRegs.I2CFFRX.all = 0x2040;         // Enable RXFIFO, clear RXFFINT
//   I2caRegs.I2CFFRX.bit.RXFFRST = 1;        // Enable the receive FIFO operation
//   I2caRegs.I2CFFRX.bit.RXFFINTCLR = 1;     // Clear the RXFFINT flag

   //I2caRegs.I2CFFRX.all = 0x000;         // Disable RXFIFO, clear RXFFINT
   I2caRegs.I2CFFRX.bit.RXFFRST = 0;        // Enable the receive FIFO operation
   I2caRegs.I2CFFRX.bit.RXFFINTCLR = 0;     // Clear the RXFFINT flag
   I2caRegs.I2CFFRX.bit.RXFFIENA = 0;       // RXFFINT flag does generate an interrupt when set
   I2caRegs.I2CFFRX.bit.RXFFIL = 0;         // Receive FIFO interrupt level

}

void WriteEEn(I2caHandle p)
{
    uint8_t i;
    //////////////////////////
    // Write data to EEPROM //
    //////////////////////////
    //I2C_SLAVE_ADDR = 0x50 address of the EEPROM
    //I am writing 'h','e','l','l','o' to the EEPROM and then read it back later
    //I2C_NUMBYTES = 5
    //[MemoryHighAddr:MemoryLowAddr] -Address within the eeprom where you want to read/write
    //Data[] has the word 'hello' and RxdData[] will store data read from the eeprom
       I2caRegs.I2CSAR = p->diviceaddress;            //Set slave address
       I2caRegs.I2CCNT = p->numbersend+ 2;          //Set count to 5 characters plus 2 address bytes
       I2caRegs.I2CDXR = p->Shighaddress;            //Send eeprom high address
       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
       I2caRegs.I2CMDR.bit.STP = 1;                 //Stop when internal counter becomes 0
       I2caRegs.I2CMDR.bit.STT = 1;                 //Send the start bit, transmission will follow
       while(I2caRegs.I2CSTR.bit.XRDY == 0){};      //Do nothing till data is shifted out
       I2caRegs.I2CDXR = p->Slowaddress;             //Send eeprom low address

       for(i = 0; i < p->numbersend; i++){
        while(I2caRegs.I2CSTR.bit.XRDY == 0){};     //Do nothing till data is shifted out
        I2caRegs.I2CDXR = p->sendmemory[i];                  //Send out the message
       }
return;
}

void ReadEEn(I2caHandle p)
{
    uint8_t i;
    //////////////////////////
    // Read data from EEPROM//
    //////////////////////////
       I2caRegs.I2CSAR = p->diviceaddress;            //Set slave address
       I2caRegs.I2CCNT = 2;                         //Set count to 2 address bytes
       I2caRegs.I2CDXR = p->Rhightaddress;            //Send eeprom high address
       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
       I2caRegs.I2CMDR.bit.STP = 0;                 //Dont release the bus after Tx
       I2caRegs.I2CMDR.bit.STT = 1;                 //Send the start bit, transmission will follow

       while(I2caRegs.I2CSTR.bit.XRDY == 0){};      //Do nothing till data is shifted out
       I2caRegs.I2CDXR = p->Rlowaddress;             //Send eeprom low address
       I2caRegs.I2CCNT = p->numberarive;              //read 5 bytes from eeprom
       I2caRegs.I2CMDR.bit.TRX = 0;                 //Set to Recieve mode
       I2caRegs.I2CMDR.bit.MST = 1;                 //Set to Master mode
       I2caRegs.I2CMDR.bit.FREE = 1;                //Run in FREE mode
       I2caRegs.I2CMDR.bit.STP = 1;                 //Stop when internal counter becomes 0
       I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow
       for(i = 0; i < p->numberarive; i++)
       {
        while(I2caRegs.I2CSTR.bit.RRDY == 0){};     //I2CDRR not ready to read?
        p->arrivermemory[i] = I2caRegs.I2CDRR;
       }
 return;
}

in I2C.h there is :

typedef struct
{
     void (*writeEEprom)(void*);
     void (*readEEprom)(void*);
     uint8_t diviceaddress;  //   EEPROM 设备地址
     uint8_t Shighaddress;   //  发送数据地址高
     uint8_t Slowaddress;    //  发送数据地址低
     uint8_t Rhightaddress; //   接收数据地址高
     uint8_t Rlowaddress;   //   接收数据地址低
     uint16_t numbersend;  //   发送数据个数
     uint16_t numberarive;  //   接收数据个数
     SENDI2CSTATE_stat_e  outi2cstate;// 发送状态
     ARRIVEI2CSTATE_stat_e  ini2cstate;// 接收状态
     uint8_t * sendmemory; //   发送数组指针
     uint8_t * arrivermemory;  //  接收数组指针
}I2CA_1;

typedef I2CA_1 * I2caHandle;

#define I2C_DEFAULT {   \
    (void(*)(void*))WriteEEn,\
    (void(*)(void*))ReadEEn,\
    0x50,\
    0x00,\
    0x00,\
    0x00,\
    0x00,\
    10,\
    10,\
    SENDINACTIVE,\
    RIVERINACTIVE,\
    EepromBuff,\
    EepromBuffin\
}

in main.c :

            i2c_modul.writeEEprom( myI2c);  //
            DELAY_US(100);
            i2c_modul.readEEprom( myI2c); //

            // IDLE loop. Just sit and loop forever:
                for(;;)  //infinite loop
                {
                    BackTicker++;

                    // State machine entry & exit point
                    //===========================================================
                    (*Alpha_State_Ptr)();   // jump to an Alpha state (A0,B0,...)
                    //===========================================================

                }

can someone help me to check it . where is wrong ?

best regard

thanks

hunk