AnsweredAssumed Answered

Between EEPROM write and read need a delay

Question asked by danielc@nexsens.com on Feb 14, 2013
Latest reply on Mar 12, 2013 by MMA

Hi,

I use I2C to control a external EEPROM. Now my problem is that between the writing and reading, I need a delay(0x2f00), otherwise, it can not read. It is same if I invoke two write command, the second write command will stuck. But if I just invoke a read command no write command, it works. I did not use interrupt, the following code is my code.

 

Any ideas about this?

 

Thank you.

 

void delay (long int length)

{

  while (length >0)

    length--;

}

void I2C_EE_RdByte(unsigned int uiAddr,unsigned char *ucByte)

{

    I2cFifoFlush(MASTER,ENABLE);        // Flush FIFO buffer

    I2cFifoFlush(MASTER,DISABLE); 

    unsigned char ucHiAddr = (unsigned char)((uiAddr >>8)&0x00ff);

    unsigned char ucLoAddr = (unsigned char)(uiAddr & 0x00ff);

    I2cTx(MASTER,ucHiAddr);             // Send address MSB

    I2cTx(MASTER,ucLoAddr);             // Send address LSB

    I2cMWrCfg(0xA0);  

   while( ( I2cSta(MASTER) & I2CMSTA_TXFSTA_MSK ) != I2CMSTA_TXFSTA_EMPTY ){}   // If no that delay, it will stuck here

                                // Wait for FIFO empty

    I2cMRdCfg(0xA0,1,DISABLE);  // Send read one byte command

    while((I2cSta(MASTER) & I2CMSTA_TCOMP_SET) != I2CMSTA_TCOMP_SET){}

    *ucByte = I2cRx(MASTER);

}

 

void I2C_EE_WrByte(unsigned int uiAddr,unsigned char ucVal)

{

    unsigned char ucTxIndex = 0;

    unsigned char ucTxDat[3];

    unsigned char ucHiAddr = (unsigned char)((uiAddr >>8)&0x00ff);

    unsigned char ucLoAddr = (unsigned char)(uiAddr & 0x00ff);

    ucTxDat[0] = ucHiAddr;

    ucTxDat[1] = ucLoAddr;

    ucTxDat[2] = ucVal;

    I2cFifoFlush(MASTER,ENABLE);        // Flush the FIFO buffer

    I2cFifoFlush(MASTER,DISABLE);

    I2cMWrCfg(0xA0);         

    I2cTx(MASTER, ucTxDat[ucTxIndex++]);    // send 1st data

    ucComplete = 0;

    while(ucComplete == 0)

    {

        while( ( I2cSta(MASTER) & I2CMSTA_TXFSTA_MSK ) != I2CMSTA_TXFSTA_EMPTY ){}

                                // Wait for FIFO buffer empty

        if(ucTxIndex < 3)       //Check if send out all data

            I2cTx(MASTER, ucTxDat[ucTxIndex++]);

        else

        {

            while((I2cSta(MASTER) & I2CMSTA_TCOMP_SET) != I2CMSTA_TCOMP_SET){}

            ucComplete = 1;

        }

 

    }

    delay(0x3000);   // If there is no this delay, for the reading behind this write command, it will stuck in read status part

}

 

 

int main(void)

{

    WdtGo(T3CON_ENABLE_DIS); 

 

    DioCfg(pADI_GP2, 0x05);       // Configure P2.0/P2.1 as I2C pins

    DioPul(pADI_GP2, 0xFC);       // External pull ups required externally

    pADI_GP1->GPOEN = 0x1;

 

    // configure the clocks

    ClkCfg(CLK_CD0,CLK_HF,CLKSYSDIV_DIV2EN_DIS,CLK_UCLKCG);

    ClkSel(CLK_CD0,CLK_CD0,CLK_CD0,CLK_CD0);

    ClkDis(CLKDIS_DISSPI0CLK|CLKDIS_DISSPI1CLK|CLKDIS_DISUARTCLK|CLKDIS_DISPWMCLK|

              CLKDIS_DIST0CLK|CLKDIS_DIST1CLK|CLKDIS_DISDACCLK|CLKDIS_DISDMACLK|CLKDIS_DISADCCLK);  

    I2cMCfg(I2CMCON_TXDMA_DIS|I2CMCON_RXDMA_DIS,  0, I2CMCON_MAS_EN); 

    I2cBaud(0x1E,0x1F); // 400kHz clock

 

    unsigned char ucVal;

    I2C_EE_WrByte(0x000,0x88);

    // if no delay, this read does not work

    I2C_EE_RdByte(0x000,&ucVal);

    printf("content is %x\n",ucVal);

 

}

Outcomes