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.

66AK2G12: I2C reading

Part Number: 66AK2G12

Hi 

I am using pdk_k2g_1_0_16 with K2Gx evaluation board on windows host pc. I am trying to create I2C bare metal example. I have imported I2C_BasicExample_evmK2G_armExampleProject and in that I have modified source file according to bare metal APIs. I am trying to read first page of EEPROM, but it is not reading any data. 

I am not sure whether I2C has configured correctly or not. I have attached project and source file to cross verify at your end. Please let me know the solution.

8168.I2C_BasicExample_evmK2G_armExampleProject.rar

1680.main_eeprom_read.c
/**
 *  \file   main_eeprom_read.c
 *
 *  \brief  Example application main file. This application will read the data
 *          from eeprom and compares it with the known data.
 *
 */

/*
 * Copyright (C) 2014 - 2017 Texas Instruments Incorporated - http://www.ti.com/
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of Texas Instruments Incorporated nor the names of
 * its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef BARE_METAL
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#endif

#include <stdio.h>
#include <string.h>

/* TI-RTOS Header files */
#include <ti/drv/i2c/I2C.h>
#include <ti/drv/i2c/soc/I2C_soc.h>
#include <ti/drv/i2c/test/eeprom_read/src/I2C_log.h>
#include <ti/drv/i2c/test/eeprom_read/src/I2C_board.h>


#include <ti/csl/csl_i2c.h>
#include <ti/csl/soc.h>


//#include <ti/csl/src/ip/i2c/V2/i2c.h>
//#include <ti/csl/src/ip/i2c/V2/hw_i2c.h>

#if defined (SOC_AM335X) || defined (SOC_AM437x)
/* EEPROM data -Board specific */
extern char eepromData[I2C_EEPROM_RX_LENGTH];
#endif

uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag);
void SetupI2C(void);
void I2CFIFOThresholdConfig(uint32_t baseAddr,
                            uint32_t thresholdVal,
                            uint32_t flag);
void I2CSoftReset(uint32_t baseAddr);

void I2CMasterEnableFreeRun(uint32_t baseAddr);

void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag);

uint32_t I2CSystemStatusGet(uint32_t baseAddr);

void sampleDelay(int32_t delay);
void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd);

void I2CFIFOClear(uint32_t baseAddr, uint32_t flag);


uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer);
void I2CMasterEnable_1(uint32_t baseAddr);

#define I2C0_BASE_ADDRESS       0x02530000

#define HSI2C_SLAVE_ADDR            ((uint8_t) 0x50U)

#define     I2C_TX_MODE                  ((uint32_t) 1U)
/**
 * \brief I2C_RX_MODE     - Receive mode.
 */
#define     I2C_RX_MODE                  ((uint32_t) 0U)


#define I2C_OWN_ADDR                 0x10


#define I2C_BUF                     (0x94U)

#define I2C_BUF_TXTRSH_SHIFT                                                                                ((uint32_t)0U)
#define I2C_BUF_TXTRSH_MASK                                                                                 (0x0000003fU)

#define I2C_BUF_RXTRSH_SHIFT                                                                                ((uint32_t)8U)
#define I2C_BUF_RXTRSH_MASK                                                                                 (0x00003f00U)


#define I2C_SYSC_SRST_SHIFT                                                                                 ((uint32_t)1U)
#define I2C_SYSC_SRST_MASK                                                                                  (0x00000002U)
#define I2C_SYSC_SRST_NMODE                                                                                  ((uint32_t)0U)
#define I2C_SYSC_SRST_RSTMODE                                                                                ((uint32_t)1U)

#define I2C_SYSC                                                                                            (0x10U)

#define I2C_SYSTEST                                                                                         (0xbcU)


#define I2C_SYSTEST_FREE_SHIFT                                                                              ((uint32_t)14U)
#define I2C_SYSTEST_FREE_MASK                                                                               (0x00004000U)
#define I2C_SYSTEST_FREE_STOP                                                                                ((uint32_t)0U)
#define I2C_SYSTEST_FREE_FREE                                                                                ((uint32_t)1U)
#define I2C_SYSS                                                                                            (0x90U)
#define I2C_SYSS_RDONE_MASK                                                                                 (0x00000001U)
#define I2C_CON_TRX_SHIFT                                                                                   ((uint32_t)9U)
#define I2C_CON_TRX_MASK                                                                                    (0x00000200U)
#define I2C_CON_TRX_RCV                                                                                      ((uint32_t)0U)
#define I2C_CON_TRX_TRX                                                                                      ((uint32_t)1U)
#define I2C_CON_MST_SHIFT                                                                                   ((uint32_t)10U)
#define I2C_CON_MST_MASK                                                                                    (0x00000400U)
#define I2C_CON_MST_SLV                                                                                      ((uint32_t)0U)
#define I2C_CON_MST_MST                                                                                      ((uint32_t)1U)

#define     I2C_CFG_MST_TX              (((uint32_t) I2C_CON_TRX_MASK) | \
                                         (uint32_t) (I2C_CON_MST_MASK))

#define     I2C_INT_ALL                  ((uint32_t) 0x7FFFU)

#define I2C_CON                                                                                             (0xa4U)

#define I2C_CON_I2C_EN_SHIFT                                                                                ((uint32_t)15U)
#define I2C_CON_I2C_EN_MASK                                                                                 (0x00008000U)
#define I2C_CON_I2C_EN_DISABLE                                                                               ((uint32_t)0U)
#define I2C_CON_I2C_EN_ENABLE                                                                                ((uint32_t)1U)

#define I2C_BUF_RXFIFO_CLR_SHIFT                                                                            ((uint32_t)14U)
#define I2C_BUF_RXFIFO_CLR_MASK                                                                             (0x00004000U)
#define I2C_BUF_RXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
#define I2C_BUF_RXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)

#define I2C_BUF_TXFIFO_CLR_SHIFT                                                                            ((uint32_t)6U)
#define I2C_BUF_TXFIFO_CLR_MASK                                                                             (0x00000040U)
#define I2C_BUF_TXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
#define I2C_BUF_TXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)

#define I2C_IRQSTATUS_RAW                                                                                   (0x24U)


#define     I2C_CFG_MST_RX              ((uint32_t) I2C_CON_MST_MASK)
/**********************************************************************
 ************************** Macros ************************************
 **********************************************************************/

#define I2C_TRANSACTION_TIMEOUT         (2000U)


/**********************************************************************
 ************************** Internal functions ************************
 **********************************************************************/

/* Data compare function */
bool CompareData(char *expData, char *rxData, unsigned int length);

/**********************************************************************
 ************************** Global Variables **************************
 **********************************************************************/

#if defined (idkAM572x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x32,
                              0x49, 0x44};
#elif defined(idkAM574x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x34,
                              0x49, 0x44};
#elif defined (idkAM571x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x31,
                              0x49, 0x44};
#elif defined (evmAM572x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4d, 0x35, 0x37, 0x32,
                              0x50, 0x4d};
#elif defined (evmDRA72x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x4A, 0x36, 0x45, 0x43, 0x4F,
                              0x43, 0x50};
#elif defined (evmDRA75x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0xEE, 0x33, 0x55, 0xAA, 0x35, 0x37, 0x37, 0x37,
                              0x78, 0x43};
#elif defined (evmDRA78x)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x41, 0x44, 0x41, 0x53, 0x2D,
                              0x4C, 0x4F};
#elif defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (evmC6678) || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                              0x00, 0x00};
#else
#endif

/*
 *  ======== Board_initI2C ========
 */
bool Board_initI2C(void)
{
    Board_initCfg boardCfg;
    Board_STATUS  boardStatus;
#if defined (idkAM571x)
    Board_IDInfo  id;
#endif
    I2C_HwAttrs   i2c_cfg;
#if defined (evmK2G)
    Board_SoCInfo socInfo;
#endif

    /* Get the default I2C init configurations */
    I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);

    /* Modify the default I2C configurations if necessary */

    /* Set the default I2C init configurations */
    I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);

#if defined(evmK2E) || defined(evmC6678)
    boardCfg = BOARD_INIT_MODULE_CLOCK |
        BOARD_INIT_UART_STDIO;
#else
    boardCfg = BOARD_INIT_PINMUX_CONFIG |
        BOARD_INIT_MODULE_CLOCK |
        BOARD_INIT_UART_STDIO;
#endif
    boardStatus = Board_init(boardCfg);
    if (boardStatus != BOARD_SOK)
    {
        return (false);
    }

#if defined (idkAM571x)
    boardStatus = Board_getIDInfo(&id);
    if (boardStatus != BOARD_SOK)
    {
        return (false);
    }
    memcpy(eepromData, &id.header[I2C_EEPROM_TEST_ADDR],
           BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR);
    memcpy(&eepromData[BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR],
           id.boardName,
           I2C_EEPROM_TEST_LENGTH - BOARD_EEPROM_HEADER_LENGTH + I2C_EEPROM_TEST_ADDR);
#endif

#if defined (evmK2G)
    /* Read the SoC info to get the System clock value */
    Board_getSoCInfo(&socInfo);
    if(socInfo.sysClock != BOARD_SYS_CLK_DEFAULT)
    {
        /* Get the default I2C init configurations */
        I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
        /* Update the I2C functional clock based on CPU clock - 1G or 600MHz */
        i2c_cfg.funcClk = socInfo.sysClock/I2C_MODULE_CLOCK_DIVIDER;
        /* Set the default I2C init configurations */
        I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
    }

    I2CMasterInitExpClk(I2C0_BASE_ADDRESS, i2c_cfg.funcClk,
                        12000000, 100000);
#endif

    return (true);
}

/*
 *  ======== test function ========
 */
void i2c_test(UArg arg0, UArg arg1)
{
    I2C_Params i2cParams;
    I2C_Handle handle = NULL;
    I2C_Transaction i2cTransaction;
    char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
    char rxBuf[I2C_EEPROM_TEST_LENGTH] = {0x00, };
    bool status,test_pass=FALSE;
    int16_t transferStatus;

    /* Set the I2C EEPROM write/read address */
    txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
    txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */

    I2C_init();

    I2C_Params_init(&i2cParams);

    handle = I2C_open(I2C_EEPROM_INSTANCE, &i2cParams);

    I2C_transactionInit(&i2cTransaction);
    i2cTransaction.slaveAddress = I2C_EEPROM_ADDR;
    i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
    i2cTransaction.writeCount = I2C_EEPROM_ADDR_SIZE;
    i2cTransaction.readBuf = (uint8_t *)&rxBuf[0];
    i2cTransaction.readCount = I2C_EEPROM_TEST_LENGTH;
    i2cTransaction.timeout   = I2C_TRANSACTION_TIMEOUT;
    transferStatus = I2C_transfer(handle, &i2cTransaction);

    if(I2C_STS_SUCCESS != transferStatus)
    {
        I2C_log("\n Data Transfer failed with transfer status %d \n",transferStatus);
        test_pass=FALSE;
        goto exit_test;
    }

    I2C_close(handle);

#if defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (iceK2G) || defined (evmC6678)  || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
    /* EEPROM not programmed on K2 EVMs, copy rx data to eepromData
       so it can pass the test */
    memcpy(eepromData, rxBuf, I2C_EEPROM_TEST_LENGTH);
#endif
    status = CompareData(&eepromData[0], &rxBuf[0], I2C_EEPROM_TEST_LENGTH);
    if(TRUE == status)
    {
        I2C_log("\n EEPROM data matched \n");
       test_pass=TRUE;
    }
    else
    {
       test_pass=FALSE;
    }

exit_test:

   if(TRUE == test_pass)
   {
       UART_printStatus("\n All tests have passed. \n");
    }
    else
    {
        UART_printStatus("\n Some tests have failed. \n");
    }

    while (1) {

    }
}

/*
 *  ======== main ========
 */
int main(void)
{

 //   Board_initCfg boardCfg;

    char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
    char rxBuf[I2C_EEPROM_TEST_LENGTH] = {0x00, };


    /* Set the I2C EEPROM write/read address */
    txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
    txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */

 //  boardCfg = BOARD_INIT_PINMUX_CONFIG |
  //          BOARD_INIT_MODULE_CLOCK;
           // BOARD_INIT_UART_STDIO;

  //      Board_init(boardCfg);

  //  SetupI2C();


 //   I2CReceive(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,I2C_EEPROM_TEST_LENGTH,I2C_EEPROM_ADDR_SIZE,rxBuf);

    if (Board_initI2C() == false)
    {
        return (0);
    }


            SetupI2C();

            I2CReceive(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,I2C_EEPROM_TEST_LENGTH,I2C_EEPROM_ADDR_SIZE,rxBuf);

#if defined (SOC_AM335X) || defined (SOC_AM437x) || defined (SOC_OMAPL137)
	Task_Handle task;
	Error_Block eb;

    Error_init(&eb);

    task = Task_create(i2c_test, NULL, &eb);
    if (task == NULL) {
        System_printf("Task_create() failed!\n");
        BIOS_exit(0);
    }
#endif

    /* Start BIOS */
   // BIOS_start();
    return (0);
}

/*
 *  ======== CompareData ========
 */
bool CompareData(char *expData, char *rxData, unsigned int length)
{
    uint32_t idx = 0;
    uint32_t match = 1;
    bool retVal = false;

    for(idx = 0; ((idx < length) && (match != 0)); idx++)
    {
        if(*expData != *rxData) match = 0;
        expData++;
        rxData++;
    }

    if(match == 1) retVal = true;

    return retVal;
}


void SetupI2C(void)
{


    I2COwnAddressSet(I2C0_BASE_ADDRESS,
                             I2C_OWN_ADDR);

    /* Do a software reset */
        I2CSoftReset(I2C0_BASE_ADDRESS);

    /* Enable i2c module */
    I2CMasterEnable(I2C0_BASE_ADDRESS);


    I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);

  //  while (I2CSystemStatusGet(I2C0_BASE_ADDRESS)) ;

    while(I2CMasterBusBusy(I2C0_BASE_ADDRESS));

    /* Put i2c in reset/disabled state */
  //  I2CMasterDisable(I2C0_BASE_ADDRESS);

    /* Configure i2c bus speed to 100khz */
#if defined (SOC_AM574x) || defined (SOC_AM572x) || defined (SOC_AM571x)
    I2CMasterInitExpClk(gI2cAddr, 96000000,
                        4000000U, 100000);
#else
    //I2CMasterInitExpClk(I2C0_BASE_ADDRESS, 24000000,
                      //  8000000, 100000);


#endif

   // I2COwnAddressSet(I2C0_BASE_ADDRESS, 0,
    //                 I2C_OWN_ADDR);

  //  I2COwnAddressSet(I2C0_BASE_ADDRESS,
             //            I2C_OWN_ADDR);


   // HW_WR_REG32(I2C0_BASE_ADDRESS + 0xA8u, I2C_OWN_ADDR);

   // I2CSyscInit(I2C0_BASE_ADDRESS, 0x08);

    /*Set Rx and Tx FIFO threshold value and reset the fifo*/
  //  I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_TX_MODE);

   // I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_RX_MODE);

    /* Set i2c slave address */
  //  I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);

    /* Bring i2c out of reset */
  //  I2CMasterEnable(I2C0_BASE_ADDRESS);

    /* Enable free run mode */
    //I2CMasterEnableFreeRun(I2C0_BASE_ADDRESS);
}

void I2CFIFOThresholdConfig(uint32_t baseAddr,
                            uint32_t thresholdVal,
                            uint32_t flag)
{
    if (I2C_TX_MODE == flag)
    {
        HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXTRSH, thresholdVal);
    }
    else
    {
        HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXTRSH, thresholdVal);
    }
}

void I2CSoftReset(uint32_t baseAddr)
{
   // HW_WR_FIELD32(baseAddr + I2C_SYSC, I2C_SYSC_SRST,
            //      I2C_SYSC_SRST_RSTMODE);

    *(unsigned int *)(baseAddr+0x24) = 1<<5;
}

void I2CMasterEnableFreeRun(uint32_t baseAddr)
{
    /* Set the I2C module in free running mode */
    HW_WR_FIELD32(baseAddr + I2C_SYSTEST, I2C_SYSTEST_FREE,
                  I2C_SYSTEST_FREE_FREE);
}

void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag)
{
    HW_WR_REG32(baseAddr + I2C_SYSC, syscFlag);
}
uint32_t I2CSystemStatusGet(uint32_t baseAddr)
{
    //return (HW_RD_REG32(baseAddr + I2C_SYSS) & I2C_SYSS_RDONE_MASK);

    return ((*(unsigned int *)(baseAddr + 0x08)) & 0x1000);
}


void SetupI2CTransmit(uint8_t *data, uint32_t numBytes)
{
    uint32_t i;

    I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_TX_MODE);
    I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_RX_MODE);

    /*Clear all interrupt status*/
    I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_ALL);

    /* Set data count */
    I2CSetDataCount(I2C0_BASE_ADDRESS, numBytes);

    /*
    ** Configure i2c as master-transmitter and to generate stop condition
    */
    I2CMasterControl_1(I2C0_BASE_ADDRESS, I2C_CFG_MST_TX);

    /* generate start */
    I2CMasterStart(I2C0_BASE_ADDRESS);

    /* wait for bus busy */
    while(I2CMasterBusBusy(I2C0_BASE_ADDRESS)==0)
    {
    }

    for (i = 0; i < numBytes; i++)
    {
        /*Wait for transmit interrupt to occur*/
        while (I2CMasterIntRawStatusEx(I2C0_BASE_ADDRESS,
                                       I2C_INT_TRANSMIT_READY) != 0x10) ;

        /* Send the data */
        I2CMasterDataPut(I2C0_BASE_ADDRESS, data[i]);

        /*Disable transmit ready and stop condition interrupt*/
        I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_TRANSMIT_READY);
    }

    /* generate stop when requested */
    I2CMasterStop(I2C0_BASE_ADDRESS);

    sampleDelay(5000);

    /*Clear data buffer length*/
    I2CSetDataCount(I2C0_BASE_ADDRESS, 0);
}

void sampleDelay(int32_t delay)
{
    volatile int32_t i, j;

    for (i = 0; i < delay; i++)
    {
        for (j = 0; j < 100; j++) ;
    }
}
void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd)
{
    HW_WR_REG32(baseAddr + I2C_CON, cmd | I2C_CON_I2C_EN_MASK);
}

void I2CFIFOClear(uint32_t baseAddr, uint32_t flag)
{
    if (I2C_TX_MODE == flag)
    {
        HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXFIFO_CLR,
                      I2C_BUF_TXFIFO_CLR_RSTMODE);

    }
    else
    {
        HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXFIFO_CLR,
                      I2C_BUF_RXFIFO_CLR_RSTMODE);
    }
}

uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag)
{
    return (HW_RD_REG32(baseAddr + I2C_IRQSTATUS_RAW) & intFlag);
}


uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer)
{
    //I2CMasterEnable(BaseAddress);

    /*Check I2CReceivePointer is NULL or not*/
    uint16_t n_txCount = 0;
    uint16_t n_rxCount=0;

    //I2CMasterSlaveAddrSet(BaseAddress, SlaveAddress);

    I2CSetDataCount(BaseAddress, OffsetAddressCount);

    /* Clear status of all interrupts */
    I2CMasterIntClearEx(BaseAddress, 0x7FF);

    /* Configure I2C controller in Master Transmitter mode. */
    I2CMasterControl(BaseAddress, I2C_CON_TRX_MASK,I2C_CFG_MST_TX);

    /* Generate Start Condition over I2C bus. */
    I2CMasterStart(BaseAddress);

    //I2C_Tick=TimerTick;

    while(I2CMasterBusBusy(BaseAddress));


    //I2C_Tick=TimerTick;

    for(n_txCount=0;n_txCount<OffsetAddressCount;n_txCount++)
    {
      //  while (0 == I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_TRANSMIT_READY))
       // {

        //}

        /*Put data to data transmit register of i2c*/
        I2CMasterDataPut(BaseAddress, I2CReceivePointer[n_txCount]);

        I2CMasterIntClearEx(BaseAddress, I2C_INT_TRANSMIT_READY);
    }

    //I2C_Tick=TimerTick;
    /* Wait until I2C registers are ready to be accessed. */
 //   while(!(I2CMasterIntRawStatus(BaseAddress) & (I2C_INT_ADRR_READY_ACESS)))
   // {

   // }

   // while(!((HW_RD_REG32(BaseAddress + I2C_IRQSTATUS_RAW)) & I2C_INT_ADRR_READY_ACESS));

    I2CMasterIntClearEx(BaseAddress,  0x7FF);

    /* Data Count specifies the number of bytes to be received. */
    I2CSetDataCount(BaseAddress, DataCountVal);


    I2CMasterControl(BaseAddress,I2C_CON_TRX_MASK, I2C_CFG_MST_RX);

   /* Generate Start Condition over I2C bus */
    I2CMasterStart(BaseAddress);

    //I2C_Tick=TimerTick;

    for(n_rxCount=0;n_rxCount<DataCountVal;n_rxCount++)
    {

       // while (0 == I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_RECV_READY))
      //  {

     //   }
        I2CReceivePointer[n_rxCount] = (unsigned char)I2CMasterDataGet(BaseAddress);


        I2CMasterIntClearEx(BaseAddress, I2C_INT_RECV_READY);
    }

    I2CMasterStop(BaseAddress);

    //I2C_Tick=TimerTick;

   // while(0 == (I2CMasterIntRawStatus(BaseAddress) & I2C_INT_STOP_CONDITION))
  //  {

  //  }

  //  while(!((HW_RD_REG32(BaseAddress + I2C_IRQSTATUS_RAW)) & I2C_INT_STOP_CONDITION));


    I2CMasterIntClearEx(BaseAddress, I2C_INT_STOP_CONDITION);

}
void I2CMasterEnable_1(uint32_t baseAddr)
{
    /* Bring the I2C module out of reset */
    HW_WR_FIELD32(baseAddr + I2C_CON, I2C_CON_I2C_EN,
                  I2C_CON_I2C_EN_ENABLE);
}

Regards,

Gaurav

  • Hi Gaurav,

    Before you made any modifications, were you able to successfully run the example code?

    Best regards,

    Dillon

  • Dillon,

    Examples are RTOS based and I am trying to create bare metal example. I have attached source file. Could you please check and verify at your end? 

    7824.main_eeprom_read.c
    /**
     *  \file   main_eeprom_read.c
     *
     *  \brief  Example application main file. This application will read the data
     *          from eeprom and compares it with the known data.
     *
     */
    
    /*
     * Copyright (C) 2014 - 2017 Texas Instruments Incorporated - http://www.ti.com/
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * Redistributions of source code must retain the above copyright
     * notice, this list of conditions and the following disclaimer.
     *
     * Redistributions in binary form must reproduce the above copyright
     * notice, this list of conditions and the following disclaimer in the
     * documentation and/or other materials provided with the
     * distribution.
     *
     * Neither the name of Texas Instruments Incorporated nor the names of
     * its contributors may be used to endorse or promote products derived
     * from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    
    #ifndef BARE_METAL
    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    #include <xdc/runtime/Error.h>
    
    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #endif
    
    #include <stdio.h>
    #include <string.h>
    
    /* TI-RTOS Header files */
    #include <ti/drv/i2c/I2C.h>
    #include <ti/drv/i2c/soc/I2C_soc.h>
    #include <ti/drv/i2c/test/eeprom_read/src/I2C_log.h>
    #include <ti/drv/i2c/test/eeprom_read/src/I2C_board.h>
    
    
    #include <ti/csl/csl_i2c.h>
    #include <ti/csl/soc.h>
    
    
    //#include <ti/csl/src/ip/i2c/V2/i2c.h>
    //#include <ti/csl/src/ip/i2c/V2/hw_i2c.h>
    
    #if defined (SOC_AM335X) || defined (SOC_AM437x)
    /* EEPROM data -Board specific */
    extern char eepromData[I2C_EEPROM_RX_LENGTH];
    #endif
    
    uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag);
    void SetupI2C(void);
    void I2CFIFOThresholdConfig(uint32_t baseAddr,
                                uint32_t thresholdVal,
                                uint32_t flag);
    void I2CSoftReset(uint32_t baseAddr);
    
    void I2CMasterEnableFreeRun(uint32_t baseAddr);
    
    void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag);
    
    uint32_t I2CSystemStatusGet(uint32_t baseAddr);
    
    void sampleDelay(int32_t delay);
    void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd);
    
    void I2CFIFOClear(uint32_t baseAddr, uint32_t flag);
    
    
    uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer);
    void I2CMasterEnable_1(uint32_t baseAddr);
    uint8_t I2CTransmit(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t *I2CReceivePointer);
    #define I2C0_BASE_ADDRESS       0x02530000
    
    //#define I2C0_BASE_ADDRESS       0x02530800
    #define HSI2C_SLAVE_ADDR            ((uint8_t) 0x50U)
    
    //#define HSI2C_SLAVE_ADDR            ((uint8_t) 0x2DU)
    
    #define     I2C_TX_MODE                  ((uint32_t) 1U)
    /**
     * \brief I2C_RX_MODE     - Receive mode.
     */
    #define     I2C_RX_MODE                  ((uint32_t) 0U)
    
    
    #define I2C_OWN_ADDR                 0x10
    
    
    #define I2C_BUF                     (0x94U)
    
    #define I2C_BUF_TXTRSH_SHIFT                                                                                ((uint32_t)0U)
    #define I2C_BUF_TXTRSH_MASK                                                                                 (0x0000003fU)
    
    #define I2C_BUF_RXTRSH_SHIFT                                                                                ((uint32_t)8U)
    #define I2C_BUF_RXTRSH_MASK                                                                                 (0x00003f00U)
    
    
    #define I2C_SYSC_SRST_SHIFT                                                                                 ((uint32_t)1U)
    #define I2C_SYSC_SRST_MASK                                                                                  (0x00000002U)
    #define I2C_SYSC_SRST_NMODE                                                                                  ((uint32_t)0U)
    #define I2C_SYSC_SRST_RSTMODE                                                                                ((uint32_t)1U)
    
    #define I2C_SYSC                                                                                            (0x10U)
    
    #define I2C_SYSTEST                                                                                         (0xbcU)
    
    
    #define I2C_SYSTEST_FREE_SHIFT                                                                              ((uint32_t)14U)
    #define I2C_SYSTEST_FREE_MASK                                                                               (0x00004000U)
    #define I2C_SYSTEST_FREE_STOP                                                                                ((uint32_t)0U)
    #define I2C_SYSTEST_FREE_FREE                                                                                ((uint32_t)1U)
    #define I2C_SYSS                                                                                            (0x90U)
    #define I2C_SYSS_RDONE_MASK                                                                                 (0x00000001U)
    #define I2C_CON_TRX_SHIFT                                                                                   ((uint32_t)9U)
    #define I2C_CON_TRX_MASK                                                                                    (0x00000200U)
    #define I2C_CON_TRX_RCV                                                                                      ((uint32_t)0U)
    #define I2C_CON_TRX_TRX                                                                                      ((uint32_t)1U)
    #define I2C_CON_MST_SHIFT                                                                                   ((uint32_t)10U)
    #define I2C_CON_MST_MASK                                                                                    (0x00000400U)
    #define I2C_CON_MST_SLV                                                                                      ((uint32_t)0U)
    #define I2C_CON_MST_MST                                                                                      ((uint32_t)1U)
    
    #define     I2C_CFG_MST_TX              (((uint32_t) I2C_CON_TRX_MASK) | \
                                             (uint32_t) (I2C_CON_MST_MASK))
    
    #define     I2C_INT_ALL                  ((uint32_t) 0x7FFFU)
    
    #define I2C_CON                                                                                             (0xa4U)
    
    #define I2C_CON_I2C_EN_SHIFT                                                                                ((uint32_t)15U)
    #define I2C_CON_I2C_EN_MASK                                                                                 (0x00008000U)
    #define I2C_CON_I2C_EN_DISABLE                                                                               ((uint32_t)0U)
    #define I2C_CON_I2C_EN_ENABLE                                                                                ((uint32_t)1U)
    
    #define I2C_BUF_RXFIFO_CLR_SHIFT                                                                            ((uint32_t)14U)
    #define I2C_BUF_RXFIFO_CLR_MASK                                                                             (0x00004000U)
    #define I2C_BUF_RXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
    #define I2C_BUF_RXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)
    
    #define I2C_BUF_TXFIFO_CLR_SHIFT                                                                            ((uint32_t)6U)
    #define I2C_BUF_TXFIFO_CLR_MASK                                                                             (0x00000040U)
    #define I2C_BUF_TXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
    #define I2C_BUF_TXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)
    
    #define I2C_IRQSTATUS_RAW                                                                                   (0x24U)
    #define I2C_CON_STP_SHIFT                                                                                   ((uint32_t)1U)
    #define I2C_CON_STP_MASK                                                                                    (0x00000002U)
    #define I2C_CON_STP_NSTP                                                                                     ((uint32_t)0U)
    #define I2C_CON_STP_STP                                                                                      ((uint32_t)1U)
    
    #define     I2C_CFG_MST_RX              ((uint32_t) I2C_CON_MST_MASK)
    
    #define     I2C_CFG_STOP                ((uint32_t) I2C_CON_STP_MASK)
    /**********************************************************************
     ************************** Macros ************************************
     **********************************************************************/
    
    #define I2C_TRANSACTION_TIMEOUT         (2000U)
    
    
    /**********************************************************************
     ************************** Internal functions ************************
     **********************************************************************/
    
    /* Data compare function */
    bool CompareData(char *expData, char *rxData, unsigned int length);
    
    /**********************************************************************
     ************************** Global Variables **************************
     **********************************************************************/
    
    #if defined (idkAM572x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x32,
                                  0x49, 0x44};
    #elif defined(idkAM574x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x34,
                                  0x49, 0x44};
    #elif defined (idkAM571x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x31,
                                  0x49, 0x44};
    #elif defined (evmAM572x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4d, 0x35, 0x37, 0x32,
                                  0x50, 0x4d};
    #elif defined (evmDRA72x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x4A, 0x36, 0x45, 0x43, 0x4F,
                                  0x43, 0x50};
    #elif defined (evmDRA75x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0xEE, 0x33, 0x55, 0xAA, 0x35, 0x37, 0x37, 0x37,
                                  0x78, 0x43};
    #elif defined (evmDRA78x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x41, 0x44, 0x41, 0x53, 0x2D,
                                  0x4C, 0x4F};
    #elif defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (evmC6678) || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                  0x00, 0x00};
    #else
    #endif
    
    /*
     *  ======== Board_initI2C ========
     */
    bool Board_initI2C(void)
    {
        Board_initCfg boardCfg;
        Board_STATUS  boardStatus;
    #if defined (idkAM571x)
        Board_IDInfo  id;
    #endif
        I2C_HwAttrs   i2c_cfg;
    #if defined (evmK2G)
        Board_SoCInfo socInfo;
    #endif
    
        /* Get the default I2C init configurations */
        I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
    
        /* Modify the default I2C configurations if necessary */
    
        /* Set the default I2C init configurations */
        I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
    
    #if defined(evmK2E) || defined(evmC6678)
        boardCfg = BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
    #else
        boardCfg = BOARD_INIT_PINMUX_CONFIG |
            BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
    #endif
        boardStatus = Board_init(boardCfg);
        if (boardStatus != BOARD_SOK)
        {
            return (false);
        }
    
    #if defined (idkAM571x)
        boardStatus = Board_getIDInfo(&id);
        if (boardStatus != BOARD_SOK)
        {
            return (false);
        }
        memcpy(eepromData, &id.header[I2C_EEPROM_TEST_ADDR],
               BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR);
        memcpy(&eepromData[BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR],
               id.boardName,
               I2C_EEPROM_TEST_LENGTH - BOARD_EEPROM_HEADER_LENGTH + I2C_EEPROM_TEST_ADDR);
    #endif
    
    #if defined (evmK2G)
        /* Read the SoC info to get the System clock value */
        Board_getSoCInfo(&socInfo);
        if(socInfo.sysClock != BOARD_SYS_CLK_DEFAULT)
        {
            /* Get the default I2C init configurations */
            I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
            /* Update the I2C functional clock based on CPU clock - 1G or 600MHz */
            i2c_cfg.funcClk = socInfo.sysClock/I2C_MODULE_CLOCK_DIVIDER;
            /* Set the default I2C init configurations */
            I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
        }
    
        I2CMasterInitExpClk(I2C0_BASE_ADDRESS, i2c_cfg.funcClk,
                            12000000, 100000);
    
      //  *(unsigned int *)(I2C0_BASE_ADDRESS+0x30) = 0x28;
    
      //  *(unsigned int *)(I2C0_BASE_ADDRESS+0xC) = 0xE;
    
      //  *(unsigned int *)(I2C0_BASE_ADDRESS+0x10) = 0xE;
    
    
    #endif
    
        return (true);
    }
    
    /*
     *  ======== test function ========
     */
    void i2c_test(UArg arg0, UArg arg1)
    {
        I2C_Params i2cParams;
        I2C_Handle handle = NULL;
        I2C_Transaction i2cTransaction;
        char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
        char rxBuf[I2C_EEPROM_TEST_LENGTH] = {0x00, };
        bool status,test_pass=FALSE;
        int16_t transferStatus;
    
        /* Set the I2C EEPROM write/read address */
        txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
        txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    
    
        txBuf[0] = 0x00;
        txBuf[1] = 0x00;
        I2C_init();
    
        I2C_Params_init(&i2cParams);
    
        handle = I2C_open(I2C_EEPROM_INSTANCE, &i2cParams);
    
        I2C_transactionInit(&i2cTransaction);
        i2cTransaction.slaveAddress = I2C_EEPROM_ADDR;
        i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
        i2cTransaction.writeCount = I2C_EEPROM_ADDR_SIZE;
        i2cTransaction.readBuf = (uint8_t *)&rxBuf[0];
        i2cTransaction.readCount = I2C_EEPROM_TEST_LENGTH;
        i2cTransaction.timeout   = I2C_TRANSACTION_TIMEOUT;
        transferStatus = I2C_transfer(handle, &i2cTransaction);
    
        if(I2C_STS_SUCCESS != transferStatus)
        {
            I2C_log("\n Data Transfer failed with transfer status %d \n",transferStatus);
            test_pass=FALSE;
            goto exit_test;
        }
    
        I2C_close(handle);
    
    #if defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (iceK2G) || defined (evmC6678)  || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
        /* EEPROM not programmed on K2 EVMs, copy rx data to eepromData
           so it can pass the test */
        memcpy(eepromData, rxBuf, I2C_EEPROM_TEST_LENGTH);
    #endif
        status = CompareData(&eepromData[0], &rxBuf[0], I2C_EEPROM_TEST_LENGTH);
        if(TRUE == status)
        {
            I2C_log("\n EEPROM data matched \n");
    
           test_pass=TRUE;
        }
        else
        {
           test_pass=FALSE;
        }
    
    exit_test:
    
       if(TRUE == test_pass)
       {
           UART_printStatus("\n All tests have passed. \n");
    
    
        }
        else
        {
            UART_printStatus("\n Some tests have failed. \n");
        }
    
        while (1) {
    
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
     {
    
    
     //   Board_initCfg boardCfg;
    
        unsigned char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
        unsigned char rxBuf[50] = {0x00, };
    
        unsigned int PLL_reg;
    
            unsigned int PLLM;
    
            PLL_reg = (*(unsigned int *)(0x02310108+0x108));
            PLLM = (*(unsigned int *)(0x02310108+0x110));
    
    
    
    
        /* Set the I2C EEPROM write/read address */
    //    txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
    //    txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    //    txBuf[2] = 0xAA;
            txBuf[0] = 0xFF;
            txBuf[01] = 0x80;
        txBuf[2] = 0x5A;
    
        rxBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
        rxBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    
        rxBuf[0] = 0x00;
        rxBuf[1] = 0x00;
    
     //  boardCfg = BOARD_INIT_PINMUX_CONFIG |
      //          BOARD_INIT_MODULE_CLOCK;
               // BOARD_INIT_UART_STDIO;
    
       //     Board_init(boardCfg);
    
    
        if (Board_initI2C() == false)
        {
            return (0);
        }
    
    
                SetupI2C();
    
            //    *(unsigned int *)(I2C0_BASE_ADDRESS+0x24) = (*(unsigned int *)(I2C0_BASE_ADDRESS+0x24))|(1<<6);
    
               // I2CTransmit(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,3,txBuf);
    
              //  sampleDelay(10000);
    
                I2CReceive(I2C0_BASE_ADDRESS,HSI2C_SLAVE_ADDR,1,2,rxBuf);
             //  I2CReceive(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,I2C_EEPROM_TEST_LENGTH,I2C_EEPROM_ADDR_SIZE,rxBuf);
    
    
    #if defined (SOC_AM335X) || defined (SOC_AM437x) || defined (SOC_OMAPL137)
    	Task_Handle task;
    	Error_Block eb;
    
        Error_init(&eb);
    
        task = Task_create(i2c_test, NULL, &eb);
        if (task == NULL) {
            System_printf("Task_create() failed!\n");
            BIOS_exit(0);
        }
    #endif
    
        /* Start BIOS */
      //  BIOS_start();
        return (0);
    }
    
    /*
     *  ======== CompareData ========
     */
    bool CompareData(char *expData, char *rxData, unsigned int length)
    {
        uint32_t idx = 0;
        uint32_t match = 1;
        bool retVal = false;
    
        for(idx = 0; ((idx < length) && (match != 0)); idx++)
        {
            if(*expData != *rxData) match = 0;
            expData++;
            rxData++;
        }
    
        if(match == 1) retVal = true;
    
        int i;
    
                      for(i=0;i<10;i++)
                      {
                          printf("tx[%d] = %d \n rx[%d] = %d\n",i,expData[i],i,rxData[i]);
                      }
    
        return retVal;
    }
    
    
    void SetupI2C(void)
    {
    
    
        I2COwnAddressSet(I2C0_BASE_ADDRESS,
                                 I2C_OWN_ADDR);
    
        /* Do a software reset */
            I2CSoftReset(I2C0_BASE_ADDRESS);
    
        /* Enable i2c module */
        I2CMasterEnable(I2C0_BASE_ADDRESS);
    
    
        I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);
    
      //  while (I2CSystemStatusGet(I2C0_BASE_ADDRESS)) ;
    
        /* wait for bus busy */
       // while(I2CMasterBusBusy(I2C0_BASE_ADDRESS)==0)
      //  {
       // }
    
        /* Put i2c in reset/disabled state */
      //  I2CMasterDisable(I2C0_BASE_ADDRESS);
    
        /* Configure i2c bus speed to 100khz */
    #if defined (SOC_AM574x) || defined (SOC_AM572x) || defined (SOC_AM571x)
        I2CMasterInitExpClk(gI2cAddr, 96000000,
                            4000000U, 100000);
    #else
        //I2CMasterInitExpClk(I2C0_BASE_ADDRESS, 24000000,
                          //  8000000, 100000);
    
    
    #endif
    
       // I2COwnAddressSet(I2C0_BASE_ADDRESS, 0,
        //                 I2C_OWN_ADDR);
    
      //  I2COwnAddressSet(I2C0_BASE_ADDRESS,
                 //            I2C_OWN_ADDR);
    
    
       // HW_WR_REG32(I2C0_BASE_ADDRESS + 0xA8u, I2C_OWN_ADDR);
    
       // I2CSyscInit(I2C0_BASE_ADDRESS, 0x08);
    
        /*Set Rx and Tx FIFO threshold value and reset the fifo*/
      //  I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_TX_MODE);
    
       // I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_RX_MODE);
    
        /* Set i2c slave address */
      //  I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);
    
        /* Bring i2c out of reset */
      //  I2CMasterEnable(I2C0_BASE_ADDRESS);
    
        /* Enable free run mode */
        //I2CMasterEnableFreeRun(I2C0_BASE_ADDRESS);
    }
    
    void I2CFIFOThresholdConfig(uint32_t baseAddr,
                                uint32_t thresholdVal,
                                uint32_t flag)
    {
        if (I2C_TX_MODE == flag)
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXTRSH, thresholdVal);
        }
        else
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXTRSH, thresholdVal);
        }
    }
    
    void I2CSoftReset(uint32_t baseAddr)
    {
       // HW_WR_FIELD32(baseAddr + I2C_SYSC, I2C_SYSC_SRST,
                //      I2C_SYSC_SRST_RSTMODE);
    
        *(unsigned int *)(baseAddr+0x24) = 0<<5;
    }
    
    void I2CMasterEnableFreeRun(uint32_t baseAddr)
    {
        /* Set the I2C module in free running mode */
        HW_WR_FIELD32(baseAddr + I2C_SYSTEST, I2C_SYSTEST_FREE,
                      I2C_SYSTEST_FREE_FREE);
    }
    
    void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag)
    {
        HW_WR_REG32(baseAddr + I2C_SYSC, syscFlag);
    }
    uint32_t I2CSystemStatusGet(uint32_t baseAddr)
    {
        //return (HW_RD_REG32(baseAddr + I2C_SYSS) & I2C_SYSS_RDONE_MASK);
    
        return ((*(unsigned int *)(baseAddr + 0x08)) & 0x1000);
    }
    
    
    void SetupI2CTransmit(uint8_t *data, uint32_t numBytes)
    {
        uint32_t i;
    
        I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_TX_MODE);
        I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_RX_MODE);
    
        /*Clear all interrupt status*/
        I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_ALL);
    
        /* Set data count */
        I2CSetDataCount(I2C0_BASE_ADDRESS, numBytes);
    
        /*
        ** Configure i2c as master-transmitter and to generate stop condition
        */
        I2CMasterControl_1(I2C0_BASE_ADDRESS, I2C_CFG_MST_TX);
    
        /* generate start */
        I2CMasterStart(I2C0_BASE_ADDRESS);
    
        /* wait for bus busy */
        while(I2CMasterBusBusy(I2C0_BASE_ADDRESS)==0)
        {
        }
    
        for (i = 0; i < numBytes; i++)
        {
            /*Wait for transmit interrupt to occur*/
            while (I2CMasterIntRawStatusEx(I2C0_BASE_ADDRESS,
                                           I2C_INT_TRANSMIT_READY) != 0x10) ;
    
            /* Send the data */
            I2CMasterDataPut(I2C0_BASE_ADDRESS, data[i]);
    
            /*Disable transmit ready and stop condition interrupt*/
            I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_TRANSMIT_READY);
        }
    
        /* generate stop when requested */
        I2CMasterStop(I2C0_BASE_ADDRESS);
    
        sampleDelay(5000);
    
        /*Clear data buffer length*/
        I2CSetDataCount(I2C0_BASE_ADDRESS, 0);
    }
    
    void sampleDelay(int32_t delay)
    {
        volatile int32_t i, j;
    
        for (i = 0; i < delay; i++)
        {
            for (j = 0; j < 100; j++) ;
        }
    }
    void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd)
    {
        HW_WR_REG32(baseAddr + I2C_CON, cmd | I2C_CON_I2C_EN_MASK);
    }
    
    void I2CFIFOClear(uint32_t baseAddr, uint32_t flag)
    {
        if (I2C_TX_MODE == flag)
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXFIFO_CLR,
                          I2C_BUF_TXFIFO_CLR_RSTMODE);
    
        }
        else
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXFIFO_CLR,
                          I2C_BUF_RXFIFO_CLR_RSTMODE);
        }
    }
    
    uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag)
    {
        return (HW_RD_REG32(baseAddr + 0x08) & intFlag);
    }
    
    
    uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer)
    {
        //I2CMasterEnable(BaseAddress);
    
        /*Check I2CReceivePointer is NULL or not*/
        uint16_t n_txCount = 0;
        uint16_t n_rxCount=0;
    
        //I2CMasterSlaveAddrSet(BaseAddress, SlaveAddress);
    
        I2CSetDataCount(BaseAddress, OffsetAddressCount);
    
        /* Clear status of all interrupts */
        I2CMasterIntClearEx(BaseAddress, 0x7FF);
    
        /* Configure I2C controller in Master Transmitter mode. */
        I2CMasterControl(BaseAddress, I2C_CON_TRX_MASK,I2C_CFG_MST_TX);
    
        /* Generate Start Condition over I2C bus. */
        I2CMasterStart(BaseAddress);
    
        //I2C_Tick=TimerTick;
    
    
    
        /* wait for bus busy */
        while(I2CMasterBusBusy(BaseAddress)==0)
        {
        }
    
    
        //I2C_Tick=TimerTick;
    
        for(n_txCount=0;n_txCount<OffsetAddressCount;n_txCount++)
        {
    
            //sampleDelay(5000);
    
            /*Wait for transmit interrupt to occur*/
              while (I2CMasterIntRawStatusEx(BaseAddress,
                                                      I2C_INT_TRANSMIT_READY) != 0x10) ;
            /*Put data to data transmit register of i2c*/
            I2CMasterDataPut(BaseAddress, I2CReceivePointer[n_txCount]);
    
            I2CMasterIntClearEx(BaseAddress, I2C_INT_TRANSMIT_READY);
        }
    
        //I2C_Tick=TimerTick;
        /* Wait until I2C registers are ready to be accessed. */
     //   while(!(I2CMasterIntRawStatus(BaseAddress) & (I2C_INT_ADRR_READY_ACESS)))
       // {
    
       // }
    
       // while(!((HW_RD_REG32(BaseAddress + I2C_IRQSTATUS_RAW)) & I2C_INT_ADRR_READY_ACESS));
        //sampleDelay(5000);
    
        I2CMasterIntClearEx(BaseAddress,  0x7FF);
    
        /* Data Count specifies the number of bytes to be received. */
        I2CSetDataCount(BaseAddress, DataCountVal);
    
    
        I2CMasterControl(BaseAddress,I2C_CON_TRX_MASK, I2C_CFG_MST_RX);
    
       /* Generate Start Condition over I2C bus */
        I2CMasterStart(BaseAddress);
    
        /* wait for bus busy */
                while(I2CMasterBusBusy(BaseAddress)==0)
                {
                }
    
        //I2C_Tick=TimerTick;
    
        for(n_rxCount=0;n_rxCount<DataCountVal;n_rxCount++)
        {
           // sampleDelay(5000);
    
         //   while (0 == I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_RECV_READY))
         //   {
    
         //   }
    
           // while (I2CMasterIntRawStatusEx(BaseAddress,
           //                                I2C_INT_RECV_READY) != 0x08) ;
            I2CReceivePointer[n_rxCount] = (unsigned char)I2CMasterDataGet(BaseAddress);
    
    
            I2CMasterIntClearEx(BaseAddress, I2C_INT_RECV_READY);
        }
    
        I2CMasterStop(BaseAddress);
    
        //I2C_Tick=TimerTick;
    
       // while(0 == (I2CMasterIntRawStatus(BaseAddress) & I2C_INT_STOP_CONDITION))
      //  {
    
      //  }
    
      //  while(!((HW_RD_REG32(BaseAddress + I2C_IRQSTATUS_RAW)) & I2C_INT_STOP_CONDITION));
    
    
        I2CMasterIntClearEx(BaseAddress, I2C_INT_STOP_CONDITION);
    
    }
    void I2CMasterEnable_1(uint32_t baseAddr)
    {
        /* Bring the I2C module out of reset */
        HW_WR_FIELD32(baseAddr + I2C_CON, I2C_CON_I2C_EN,
                      I2C_CON_I2C_EN_ENABLE);
    }
    uint8_t I2CTransmit(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t *I2CReceivePointer)
    {
    
    
    
            int i = 0;
    
            /*Clear all interrupt status*/
            I2CMasterIntClearEx(BaseAddress, 0x7FF);
    
            /* Set data count */
            I2CSetDataCount(BaseAddress, DataCountVal);
    
            /*
            ** Configure i2c as master-transmitter and to generate stop condition
            */
            I2CMasterControl(BaseAddress, I2C_CON_TRX_MASK,I2C_CFG_MST_TX);
    
            /* generate start */
            I2CMasterStart(BaseAddress);
    
            /* wait for bus busy */
            while(I2CMasterBusBusy(BaseAddress)==0)
            {
            }
    
            for (i = 0; i < DataCountVal; i++)
            {
                /*Wait for transmit interrupt to occur*/
               while (I2CMasterIntRawStatusEx(BaseAddress,
                                               I2C_INT_TRANSMIT_READY) != 0x10) ;
              //  sampleDelay(500);
    
                /* Send the data */
                I2CMasterDataPut(BaseAddress, I2CReceivePointer[i]);
    
                /*Disable transmit ready and stop condition interrupt*/
                I2CMasterIntClearEx(BaseAddress, I2C_INT_TRANSMIT_READY);
            }
    
            /* generate stop when requested */
            I2CMasterStop(BaseAddress);
    
            sampleDelay(5000);
    
            /*Clear data buffer length*/
            I2CSetDataCount(BaseAddress, 0);
    }
    

    May I know the data stored in first page of EEPROM?

    Regards,

    Gaurav

  • Dillon,

    Could you please help me in I2C on priority basis? Already deadline over.

    Regards,

    Gaurav

  • Hi Gaurav,

    I unfortunately do not have access to a K2G EVM, but we can try to debug this code together step by step to figure out why it's not reading any data. Have you tried connecting your K2G EVM to CCS, entering debug mode, and stepping through the code to see what occurs when trying to read data?

    Best regards,

    Dillon

  • Dillion,

    I got some progress in reading data from EERPOM through I2C, but unfortunately I am facing another problem. If I read only single byte it is working absolutely fine but If I try to read more than 1 byte then it is not  setting Receive-data-ready interrupt flag bit (ICRDRDY) in I2C_ICSTR register after fist or two bytes as I am polling this bit before reading data. I have attached source file for your reference. Please I2CReceive function.

    1832.main_eeprom_read.c
    /**
     *  \file   main_eeprom_read.c
     *
     *  \brief  Example application main file. This application will read the data
     *          from eeprom and compares it with the known data.
     *
     */
    
    /*
     * Copyright (C) 2014 - 2017 Texas Instruments Incorporated - http://www.ti.com/
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * Redistributions of source code must retain the above copyright
     * notice, this list of conditions and the following disclaimer.
     *
     * Redistributions in binary form must reproduce the above copyright
     * notice, this list of conditions and the following disclaimer in the
     * documentation and/or other materials provided with the
     * distribution.
     *
     * Neither the name of Texas Instruments Incorporated nor the names of
     * its contributors may be used to endorse or promote products derived
     * from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    
    #ifndef BARE_METAL
    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    #include <xdc/runtime/Error.h>
    
    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #endif
    
    #include <stdio.h>
    #include <string.h>
    
    /* TI-RTOS Header files */
    #include <ti/drv/i2c/I2C.h>
    #include <ti/drv/i2c/soc/I2C_soc.h>
    #include <ti/drv/i2c/test/eeprom_read/src/I2C_log.h>
    #include <ti/drv/i2c/test/eeprom_read/src/I2C_board.h>
    
    
    #include <ti/csl/csl_i2c.h>
    #include <ti/csl/soc.h>
    
    
    //#include <ti/csl/src/ip/i2c/V2/i2c.h>
    //#include <ti/csl/src/ip/i2c/V2/hw_i2c.h>
    
    #if defined (SOC_AM335X) || defined (SOC_AM437x)
    /* EEPROM data -Board specific */
    extern char eepromData[I2C_EEPROM_RX_LENGTH];
    #endif
    
    uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag);
    void SetupI2C(void);
    void I2CFIFOThresholdConfig(uint32_t baseAddr,
                                uint32_t thresholdVal,
                                uint32_t flag);
    void I2CSoftReset(uint32_t baseAddr);
    
    void I2CMasterEnableFreeRun(uint32_t baseAddr);
    
    void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag);
    
    uint32_t I2CSystemStatusGet(uint32_t baseAddr);
    
    void sampleDelay(int32_t delay);
    void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd);
    
    void I2CFIFOClear(uint32_t baseAddr, uint32_t flag);
    
    
    uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer);
    void I2CMasterEnable_1(uint32_t baseAddr);
    void I2CTransmit(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t *I2CReceivePointer);
    #define I2C0_BASE_ADDRESS       0x02530000
    
    //#define I2C0_BASE_ADDRESS       0x02530800
    #define HSI2C_SLAVE_ADDR            ((uint8_t) 0x50U)
    
    //#define HSI2C_SLAVE_ADDR            ((uint8_t) 0x2DU)
    
    #define     I2C_TX_MODE                  ((uint32_t) 1U)
    /**
     * \brief I2C_RX_MODE     - Receive mode.
     */
    #define     I2C_RX_MODE                  ((uint32_t) 0U)
    
    
    #define I2C_OWN_ADDR                 0x10
    
    
    #define I2C_BUF                     (0x94U)
    
    #define I2C_BUF_TXTRSH_SHIFT                                                                                ((uint32_t)0U)
    #define I2C_BUF_TXTRSH_MASK                                                                                 (0x0000003fU)
    
    #define I2C_BUF_RXTRSH_SHIFT                                                                                ((uint32_t)8U)
    #define I2C_BUF_RXTRSH_MASK                                                                                 (0x00003f00U)
    
    
    #define I2C_SYSC_SRST_SHIFT                                                                                 ((uint32_t)1U)
    #define I2C_SYSC_SRST_MASK                                                                                  (0x00000002U)
    #define I2C_SYSC_SRST_NMODE                                                                                  ((uint32_t)0U)
    #define I2C_SYSC_SRST_RSTMODE                                                                                ((uint32_t)1U)
    
    #define I2C_SYSC                                                                                            (0x10U)
    
    #define I2C_SYSTEST                                                                                         (0xbcU)
    
    
    #define I2C_SYSTEST_FREE_SHIFT                                                                              ((uint32_t)14U)
    #define I2C_SYSTEST_FREE_MASK                                                                               (0x00004000U)
    #define I2C_SYSTEST_FREE_STOP                                                                                ((uint32_t)0U)
    #define I2C_SYSTEST_FREE_FREE                                                                                ((uint32_t)1U)
    #define I2C_SYSS                                                                                            (0x90U)
    #define I2C_SYSS_RDONE_MASK                                                                                 (0x00000001U)
    #define I2C_CON_TRX_SHIFT                                                                                   ((uint32_t)9U)
    #define I2C_CON_TRX_MASK                                                                                    (0x00000200U)
    #define I2C_CON_TRX_RCV                                                                                      ((uint32_t)0U)
    #define I2C_CON_TRX_TRX                                                                                      ((uint32_t)1U)
    #define I2C_CON_MST_SHIFT                                                                                   ((uint32_t)10U)
    #define I2C_CON_MST_MASK                                                                                    (0x00000400U)
    #define I2C_CON_MST_SLV                                                                                      ((uint32_t)0U)
    #define I2C_CON_MST_MST                                                                                      ((uint32_t)1U)
    
    #define     I2C_CFG_MST_TX              (((uint32_t) I2C_CON_TRX_MASK) | \
                                             (uint32_t) (I2C_CON_MST_MASK))
    
    #define     I2C_INT_ALL                  ((uint32_t) 0x7FFFU)
    
    #define I2C_CON                                                                                             (0xa4U)
    
    #define I2C_CON_I2C_EN_SHIFT                                                                                ((uint32_t)15U)
    #define I2C_CON_I2C_EN_MASK                                                                                 (0x00008000U)
    #define I2C_CON_I2C_EN_DISABLE                                                                               ((uint32_t)0U)
    #define I2C_CON_I2C_EN_ENABLE                                                                                ((uint32_t)1U)
    
    #define I2C_BUF_RXFIFO_CLR_SHIFT                                                                            ((uint32_t)14U)
    #define I2C_BUF_RXFIFO_CLR_MASK                                                                             (0x00004000U)
    #define I2C_BUF_RXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
    #define I2C_BUF_RXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)
    
    #define I2C_BUF_TXFIFO_CLR_SHIFT                                                                            ((uint32_t)6U)
    #define I2C_BUF_TXFIFO_CLR_MASK                                                                             (0x00000040U)
    #define I2C_BUF_TXFIFO_CLR_NMODE                                                                             ((uint32_t)0U)
    #define I2C_BUF_TXFIFO_CLR_RSTMODE                                                                           ((uint32_t)1U)
    
    #define I2C_IRQSTATUS_RAW                                                                                   (0x24U)
    #define I2C_CON_STP_SHIFT                                                                                   ((uint32_t)1U)
    #define I2C_CON_STP_MASK                                                                                    (0x00000002U)
    #define I2C_CON_STP_NSTP                                                                                     ((uint32_t)0U)
    #define I2C_CON_STP_STP                                                                                      ((uint32_t)1U)
    
    #define     I2C_CFG_MST_RX              ((uint32_t) I2C_CON_MST_MASK)
    
    #define     I2C_CFG_STOP                ((uint32_t) I2C_CON_STP_MASK)
    /**********************************************************************
     ************************** Macros ************************************
     **********************************************************************/
    
    #define I2C_TRANSACTION_TIMEOUT         (2000U)
    
    
    /**********************************************************************
     ************************** Internal functions ************************
     **********************************************************************/
    
    /* Data compare function */
    bool CompareData(char *expData, char *rxData, unsigned int length);
    
    /**********************************************************************
     ************************** Global Variables **************************
     **********************************************************************/
    
    #if defined (idkAM572x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x32,
                                  0x49, 0x44};
    #elif defined(idkAM574x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x34,
                                  0x49, 0x44};
    #elif defined (idkAM571x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4D, 0x35, 0x37, 0x31,
                                  0x49, 0x44};
    #elif defined (evmAM572x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x55, 0x33, 0xEE, 0x41, 0x4d, 0x35, 0x37, 0x32,
                                  0x50, 0x4d};
    #elif defined (evmDRA72x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x4A, 0x36, 0x45, 0x43, 0x4F,
                                  0x43, 0x50};
    #elif defined (evmDRA75x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0xEE, 0x33, 0x55, 0xAA, 0x35, 0x37, 0x37, 0x37,
                                  0x78, 0x43};
    #elif defined (evmDRA78x)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x33, 0x55, 0xAA, 0x41, 0x44, 0x41, 0x53, 0x2D,
                                  0x4C, 0x4F};
    #elif defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (evmC6678) || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
    char eepromData[I2C_EEPROM_TEST_LENGTH] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                  0x00, 0x00};
    #else
    #endif
    
    /*
     *  ======== Board_initI2C ========
     */
    bool Board_initI2C(void)
    {
        Board_initCfg boardCfg;
        Board_STATUS  boardStatus;
    #if defined (idkAM571x)
        Board_IDInfo  id;
    #endif
        I2C_HwAttrs   i2c_cfg;
    #if defined (evmK2G)
        Board_SoCInfo socInfo;
    #endif
    
        /* Get the default I2C init configurations */
        I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
    
        /* Modify the default I2C configurations if necessary */
    
        /* Set the default I2C init configurations */
        I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
    
    #if defined(evmK2E) || defined(evmC6678)
        boardCfg = BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
    #else
        boardCfg = BOARD_INIT_PINMUX_CONFIG |
            BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
    #endif
        boardStatus = Board_init(boardCfg);
        if (boardStatus != BOARD_SOK)
        {
            return (false);
        }
    
    #if defined (idkAM571x)
        boardStatus = Board_getIDInfo(&id);
        if (boardStatus != BOARD_SOK)
        {
            return (false);
        }
        memcpy(eepromData, &id.header[I2C_EEPROM_TEST_ADDR],
               BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR);
        memcpy(&eepromData[BOARD_EEPROM_HEADER_LENGTH - I2C_EEPROM_TEST_ADDR],
               id.boardName,
               I2C_EEPROM_TEST_LENGTH - BOARD_EEPROM_HEADER_LENGTH + I2C_EEPROM_TEST_ADDR);
    #endif
    
    #if defined (evmK2G)
        /* Read the SoC info to get the System clock value */
        Board_getSoCInfo(&socInfo);
        if(socInfo.sysClock != BOARD_SYS_CLK_DEFAULT)
        {
            /* Get the default I2C init configurations */
            I2C_socGetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
            /* Update the I2C functional clock based on CPU clock - 1G or 600MHz */
            i2c_cfg.funcClk = socInfo.sysClock/I2C_MODULE_CLOCK_DIVIDER;
            /* Set the default I2C init configurations */
            I2C_socSetInitCfg(I2C_EEPROM_INSTANCE, &i2c_cfg);
        }
    
    
    
    
    #endif
    
        return (true);
    }
    
    /*
     *  ======== test function ========
     */
    void i2c_test(UArg arg0, UArg arg1)
    {
        I2C_Params i2cParams;
        I2C_Handle handle = NULL;
        I2C_Transaction i2cTransaction;
        char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
        char rxBuf[I2C_EEPROM_TEST_LENGTH] = {0x00, };
        bool status,test_pass=FALSE;
        int16_t transferStatus;
    
        /* Set the I2C EEPROM write/read address */
        txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
        txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    
    
        txBuf[0] = 0x00;
        txBuf[1] = 0x00;
        I2C_init();
    
        I2C_Params_init(&i2cParams);
    
        handle = I2C_open(I2C_EEPROM_INSTANCE, &i2cParams);
    
        I2C_transactionInit(&i2cTransaction);
        i2cTransaction.slaveAddress = I2C_EEPROM_ADDR;
        i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
        i2cTransaction.writeCount = I2C_EEPROM_ADDR_SIZE;
        i2cTransaction.readBuf = (uint8_t *)&rxBuf[0];
        i2cTransaction.readCount = I2C_EEPROM_TEST_LENGTH;
        i2cTransaction.timeout   = I2C_TRANSACTION_TIMEOUT;
        transferStatus = I2C_transfer(handle, &i2cTransaction);
    
        if(I2C_STS_SUCCESS != transferStatus)
        {
            I2C_log("\n Data Transfer failed with transfer status %d \n",transferStatus);
            test_pass=FALSE;
            goto exit_test;
        }
    
        I2C_close(handle);
    
    #if defined (evmK2H) || defined (evmK2K) || defined (evmK2E) || defined (evmK2L) || defined (evmK2G) || defined (iceK2G) || defined (evmC6678)  || defined (evmC6657) || defined (iceK2G) || defined (evmOMAPL137)
        /* EEPROM not programmed on K2 EVMs, copy rx data to eepromData
           so it can pass the test */
        memcpy(eepromData, rxBuf, I2C_EEPROM_TEST_LENGTH);
    #endif
        status = CompareData(&eepromData[0], &rxBuf[0], I2C_EEPROM_TEST_LENGTH);
        if(TRUE == status)
        {
            I2C_log("\n EEPROM data matched \n");
    
           test_pass=TRUE;
        }
        else
        {
           test_pass=FALSE;
        }
    
    exit_test:
    
       if(TRUE == test_pass)
       {
           UART_printStatus("\n All tests have passed. \n");
    
    
        }
        else
        {
            UART_printStatus("\n Some tests have failed. \n");
        }
    
        while (1) {
    
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
     {
    
    
     //   Board_initCfg boardCfg;
    
       // unsigned char txBuf[I2C_EEPROM_TEST_LENGTH + I2C_EEPROM_ADDR_SIZE] = {0x00, };
    
        unsigned char txBuf[20 + I2C_EEPROM_ADDR_SIZE] = {0x00, };
    
        unsigned char rxBuf[20] = {0x00, };
    
    
        int i = 0;
    
        /* Set the I2C EEPROM write/read address */
        txBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
        txBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    
        txBuf[0] = 0xFF;
        txBuf[1] = 0x00;
    
        txBuf[2] = 0xAA;
    
        unsigned char buffer = 0x55;
    
        for(i=2;i<22;i++)
        {
            buffer = ~buffer;
    
            txBuf[i] = buffer;
        }
    
        rxBuf[0] = (I2C_EEPROM_TEST_ADDR >> 8) & 0xff; /* EEPROM memory high address byte */
        rxBuf[1] = I2C_EEPROM_TEST_ADDR & 0xff;        /* EEPROM memory low address byte */
    
        rxBuf[0] = 0xFF;
            rxBuf[1] = 0x00;
    
     //  boardCfg = BOARD_INIT_PINMUX_CONFIG |
      //          BOARD_INIT_MODULE_CLOCK;
               // BOARD_INIT_UART_STDIO;
    
       //     Board_init(boardCfg);
    
    
        if (Board_initI2C() == false)
        {
            return (0);
        }
    
    
                SetupI2C();
    
    
    
                I2CTransmit(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,22,txBuf);
    
                sampleDelay(5000);
    
                I2CReceive(I2C0_BASE_ADDRESS,HSI2C_SLAVE_ADDR,5,2,rxBuf);
             //  I2CReceive(I2C0_BASE_ADDRESS,I2C_EEPROM_ADDR,I2C_EEPROM_TEST_LENGTH,I2C_EEPROM_ADDR_SIZE,rxBuf);
    
    
    #if defined (SOC_AM335X) || defined (SOC_AM437x) || defined (SOC_OMAPL137)
    	Task_Handle task;
    	Error_Block eb;
    
        Error_init(&eb);
    
        task = Task_create(i2c_test, NULL, &eb);
        if (task == NULL) {
            System_printf("Task_create() failed!\n");
            BIOS_exit(0);
        }
    #endif
    
        /* Start BIOS */
      //  BIOS_start();
        return (0);
    }
    
    /*
     *  ======== CompareData ========
     */
    bool CompareData(char *expData, char *rxData, unsigned int length)
    {
        uint32_t idx = 0;
        uint32_t match = 1;
        bool retVal = false;
    
        for(idx = 0; ((idx < length) && (match != 0)); idx++)
        {
            if(*expData != *rxData) match = 0;
            expData++;
            rxData++;
        }
    
        if(match == 1) retVal = true;
    
        int i;
    
                      for(i=0;i<10;i++)
                      {
                          printf("tx[%d] = %d \n rx[%d] = %d\n",i,expData[i],i,rxData[i]);
                      }
    
        return retVal;
    }
    
    
    void SetupI2C(void)
    {
    
        /* Do a software reset */
        I2CSoftReset(I2C0_BASE_ADDRESS);
    
        I2CMasterInitExpClk(I2C0_BASE_ADDRESS, 166666666,
                                12000000, 400000);
    
          //  *(unsigned int *)(I2C0_BASE_ADDRESS+0x30) = 0x28;
    
          //  *(unsigned int *)(I2C0_BASE_ADDRESS+0xC) = 0xE;
    
          //  *(unsigned int *)(I2C0_BASE_ADDRESS+0x10) = 0xE;
    
        I2COwnAddressSet(I2C0_BASE_ADDRESS,
                                 I2C_OWN_ADDR);
    
    
    
        /* Enable i2c module */
        I2CMasterEnable(I2C0_BASE_ADDRESS);
    
    
        I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);
    
      //  while (I2CSystemStatusGet(I2C0_BASE_ADDRESS)) ;
    
        /* wait for bus busy */
       // while(I2CMasterBusBusy(I2C0_BASE_ADDRESS)==0)
      //  {
       // }
    
        /* Put i2c in reset/disabled state */
      //  I2CMasterDisable(I2C0_BASE_ADDRESS);
    
        /* Configure i2c bus speed to 100khz */
    #if defined (SOC_AM574x) || defined (SOC_AM572x) || defined (SOC_AM571x)
        I2CMasterInitExpClk(gI2cAddr, 96000000,
                            4000000U, 100000);
    #else
        //I2CMasterInitExpClk(I2C0_BASE_ADDRESS, 24000000,
                          //  8000000, 100000);
    
    
    #endif
    
       // I2COwnAddressSet(I2C0_BASE_ADDRESS, 0,
        //                 I2C_OWN_ADDR);
    
      //  I2COwnAddressSet(I2C0_BASE_ADDRESS,
                 //            I2C_OWN_ADDR);
    
    
       // HW_WR_REG32(I2C0_BASE_ADDRESS + 0xA8u, I2C_OWN_ADDR);
    
       // I2CSyscInit(I2C0_BASE_ADDRESS, 0x08);
    
        /*Set Rx and Tx FIFO threshold value and reset the fifo*/
      //  I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_TX_MODE);
    
       // I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_RX_MODE);
    
        /* Set i2c slave address */
      //  I2CMasterSlaveAddrSet(I2C0_BASE_ADDRESS, HSI2C_SLAVE_ADDR);
    
        /* Bring i2c out of reset */
      //  I2CMasterEnable(I2C0_BASE_ADDRESS);
    
        /* Enable free run mode */
        //I2CMasterEnableFreeRun(I2C0_BASE_ADDRESS);
    }
    
    void I2CFIFOThresholdConfig(uint32_t baseAddr,
                                uint32_t thresholdVal,
                                uint32_t flag)
    {
        if (I2C_TX_MODE == flag)
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXTRSH, thresholdVal);
        }
        else
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXTRSH, thresholdVal);
        }
    }
    
    void I2CSoftReset(uint32_t baseAddr)
    {
       // HW_WR_FIELD32(baseAddr + I2C_SYSC, I2C_SYSC_SRST,
                //      I2C_SYSC_SRST_RSTMODE);
    
        *(unsigned int *)(baseAddr+0x24) = 0<<5;
    }
    
    void I2CMasterEnableFreeRun(uint32_t baseAddr)
    {
        /* Set the I2C module in free running mode */
        HW_WR_FIELD32(baseAddr + I2C_SYSTEST, I2C_SYSTEST_FREE,
                      I2C_SYSTEST_FREE_FREE);
    }
    
    void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag)
    {
        HW_WR_REG32(baseAddr + I2C_SYSC, syscFlag);
    }
    uint32_t I2CSystemStatusGet(uint32_t baseAddr)
    {
        //return (HW_RD_REG32(baseAddr + I2C_SYSS) & I2C_SYSS_RDONE_MASK);
    
        return ((*(unsigned int *)(baseAddr + 0x08)) & 0x1000);
    }
    
    
    void SetupI2CTransmit(uint8_t *data, uint32_t numBytes)
    {
        uint32_t i;
    
        I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_TX_MODE);
        I2CFIFOClear(I2C0_BASE_ADDRESS, I2C_RX_MODE);
    
        /*Clear all interrupt status*/
        I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_ALL);
    
        /* Set data count */
        I2CSetDataCount(I2C0_BASE_ADDRESS, numBytes);
    
        /*
        ** Configure i2c as master-transmitter and to generate stop condition
        */
        I2CMasterControl_1(I2C0_BASE_ADDRESS, I2C_CFG_MST_TX);
    
        /* generate start */
        I2CMasterStart(I2C0_BASE_ADDRESS);
    
        /* wait for bus busy */
        while(I2CMasterBusBusy(I2C0_BASE_ADDRESS)==0)
        {
        }
    
        for (i = 0; i < numBytes; i++)
        {
            /*Wait for transmit interrupt to occur*/
            while (I2CMasterIntRawStatusEx(I2C0_BASE_ADDRESS,
                                           I2C_INT_TRANSMIT_READY) != 0x10) ;
    
            /* Send the data */
            I2CMasterDataPut(I2C0_BASE_ADDRESS, data[i]);
    
            /*Disable transmit ready and stop condition interrupt*/
            I2CMasterIntClearEx(I2C0_BASE_ADDRESS, I2C_INT_TRANSMIT_READY);
        }
    
        /* generate stop when requested */
        I2CMasterStop(I2C0_BASE_ADDRESS);
    
        sampleDelay(5000);
    
        /*Clear data buffer length*/
        I2CSetDataCount(I2C0_BASE_ADDRESS, 0);
    }
    
    void sampleDelay(int32_t delay)
    {
        volatile int32_t i, j;
    
        for (i = 0; i < delay; i++)
        {
            for (j = 0; j < 100; j++) ;
        }
    }
    void I2CMasterControl_1(uint32_t baseAddr, uint32_t cmd)
    {
        HW_WR_REG32(baseAddr + I2C_CON, cmd | I2C_CON_I2C_EN_MASK);
    }
    
    void I2CFIFOClear(uint32_t baseAddr, uint32_t flag)
    {
        if (I2C_TX_MODE == flag)
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXFIFO_CLR,
                          I2C_BUF_TXFIFO_CLR_RSTMODE);
    
        }
        else
        {
            HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXFIFO_CLR,
                          I2C_BUF_RXFIFO_CLR_RSTMODE);
        }
    }
    
    uint32_t I2CMasterIntRawStatusEx(uint32_t baseAddr, uint32_t intFlag)
    {
        return (HW_RD_REG32(baseAddr + 0x08) & intFlag);
    
    }
    
    
    uint8_t I2CReceive(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t OffsetAddressCount,uint8_t *I2CReceivePointer)
    {
    
        printf("Data count value is %d\nOffset Address Count value is %d\n",DataCountVal,OffsetAddressCount);
        //I2CMasterEnable(BaseAddress);
    
        /*Check I2CReceivePointer is NULL or not*/
        uint16_t n_txCount = 0;
        uint16_t n_rxCount=0;
    
        //I2CMasterSlaveAddrSet(BaseAddress, SlaveAddress);
    
        I2CSetDataCount(BaseAddress, OffsetAddressCount);
    
        /* Clear status of all interrupts */
        I2CMasterIntClearEx(BaseAddress, 0x7FF);
    
        /* Configure I2C controller in Master Transmitter mode. */
        I2CMasterControl(BaseAddress, I2C_CON_TRX_MASK,I2C_CFG_MST_TX);
    
        /* Generate Start Condition over I2C bus. */
        I2CMasterStart(BaseAddress);
    
        //I2C_Tick=TimerTick;
    
    
    
        /* wait for bus busy */
        while(I2CMasterBusBusy(BaseAddress)==0)
        {
        }
    
    
        //I2C_Tick=TimerTick;
    
        //sampleDelay(5000);
    
        for(n_txCount=0;n_txCount<OffsetAddressCount;n_txCount++)
        {
    
            //sampleDelay(5000);
    
    
            /*Wait for transmit interrupt to occur*/
              while (I2CMasterIntRawStatusEx(BaseAddress,
                                                      I2C_INT_TRANSMIT_READY) != 0x10) ;
             // sampleDelay(5000);
            /*Put data to data transmit register of i2c*/
            I2CMasterDataPut(BaseAddress, I2CReceivePointer[n_txCount]);
            sampleDelay(5000);
    
            I2CMasterIntClearEx(BaseAddress, I2C_INT_TRANSMIT_READY);
           // sampleDelay(5000);
        }
    
        //I2C_Tick=TimerTick;
        /* Wait until I2C registers are ready to be accessed. */
    
      //  sampleDelay(5000);
    
        while(I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_ADRR_READY_ACESS)!=0x04);
    
       // while(!((HW_RD_REG32(BaseAddress + I2C_IRQSTATUS_RAW)) & I2C_INT_ADRR_READY_ACESS));
        //sampleDelay(5000);
    
        I2CMasterIntClearEx(BaseAddress,  0x7FF);
    
        /* Data Count specifies the number of bytes to be received. */
        I2CSetDataCount(BaseAddress, DataCountVal);
    
    
        I2CMasterControl(BaseAddress,I2C_CON_TRX_MASK, I2C_CFG_MST_RX);
    
       /* Generate Start Condition over I2C bus */
        I2CMasterStart(BaseAddress);
    
        /* wait for bus busy */
        while(I2CMasterBusBusy(BaseAddress)==0)
        {
        }
    
        //I2C_Tick=TimerTick;
      //  sampleDelay(5000);
    
        for(n_rxCount=0;n_rxCount<DataCountVal;n_rxCount++)
        {
           // sampleDelay(5000);
    
           // while (0 == I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_RECV_READY))
           // {
    
          //  }
    
            while (I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_RECV_READY) != 0x08);
    
         //   sampleDelay(20000);
    
            I2CReceivePointer[n_rxCount] = (unsigned char)I2CMasterDataGet(BaseAddress);
    
            sampleDelay(1000);
    
    
            I2CMasterIntClearEx(BaseAddress, I2C_INT_RECV_READY);
    
          //  sampleDelay(20000);
    
        }
    
        I2CMasterStop(BaseAddress);
    
        //I2C_Tick=TimerTick;
    
       // while(0 == (I2CMasterIntRawStatus(BaseAddress) & I2C_INT_STOP_CONDITION))
      //  {
    
      //  }
    
      //  sampleDelay(5000);
        while (I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_STOP_CONDITION)!= 0x20) ;
    
    
        I2CMasterIntClearEx(BaseAddress, I2C_INT_STOP_CONDITION);
    
        //sampleDelay(5000);
    
        /*Clear data buffer length*/
        I2CSetDataCount(BaseAddress, 0);
    
    }
    void I2CMasterEnable_1(uint32_t baseAddr)
    {
        /* Bring the I2C module out of reset */
        HW_WR_FIELD32(baseAddr + I2C_CON, I2C_CON_I2C_EN,
                      I2C_CON_I2C_EN_ENABLE);
    }
    void I2CTransmit(uint32_t BaseAddress,uint8_t SlaveAddress, uint16_t DataCountVal,uint8_t *I2CReceivePointer)
    {
    
    
    
            int i = 0;
    
            /* Set data count */
           I2CSetDataCount(BaseAddress, DataCountVal);
            /*Clear all interrupt status*/
            I2CMasterIntClearEx(BaseAddress, 0x7FF);
    
    
    
            /*
            ** Configure i2c as master-transmitter and to generate stop condition
            */
            I2CMasterControl(BaseAddress, I2C_CON_TRX_MASK,I2C_CFG_MST_TX);
    
            /* generate start */
            I2CMasterStart(BaseAddress);
    
            /* wait for bus busy */
            while(I2CMasterBusBusy(BaseAddress)==0)
            {
            }
    
          //  sampleDelay(5000);
    
            for (i = 0; i < DataCountVal; i++)
            {
                /*Wait for transmit interrupt to occur*/
               while (I2CMasterIntRawStatusEx(BaseAddress,
                                               I2C_INT_TRANSMIT_READY) != 0x10) ;
               // sampleDelay(5000);
    
                /* Send the data */
                I2CMasterDataPut(BaseAddress, I2CReceivePointer[i]);
    
                sampleDelay(5000);
    
                /*Disable transmit ready and stop condition interrupt*/
                I2CMasterIntClearEx(BaseAddress, I2C_INT_TRANSMIT_READY);
              //  sampleDelay(5000);
            }
    
          //  sampleDelay(5000);
            /* generate stop when requested */
            I2CMasterStop(BaseAddress);
    
          //  sampleDelay(5000);
    
            while (I2CMasterIntRawStatusEx(BaseAddress,I2C_INT_STOP_CONDITION)!= 0x20) ;
    
    
            I2CMasterIntClearEx(BaseAddress, I2C_INT_STOP_CONDITION);
    
           // sampleDelay(5000);
    
            /*Clear data buffer length*/
            I2CSetDataCount(BaseAddress, 0);
    }
    

    Regards,

    Gaurav

  • Your initialization seems alright, although the code you have for transmit and receive is in a different format than we typically use. Can you try looking at the i2c_bitrate_test function found in pdk/packages/ti/drv/i2c/test/eeprom_read/src/main_test.c and basing your transmit and receive code off of that function instead? 

    Best regards,

    Dillon

  • Dillon,

    Bit rate test is pass. Can you verify my source file with your colleague who has EVM access?

    It would be a great help as time is running out.

    Regards,

    Gaurav

  • I will ask around to see if any of my colleagues have access to a K2G EVM. I don't quite fully understand, so could you explain why exactly you need us to run the code? Is it to see if your K2G EVM is faulty? 

    That's good that the bit rate test is passing. Could you try altering your code to be in the format of the bit rate test instead? Specifically for using I2C_transfer instead of the I2CReceive that's currently in your code.

        memset(rxBuf, 0, I2C_EEPROM_TEST_LENGTH);
        I2C_transactionInit(&i2cTransaction);
        i2cTransaction.slaveAddress = I2C_EEPROM_ADDR;
        i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
        i2cTransaction.writeCount = I2C_EEPROM_ADDR_SIZE;
        i2cTransaction.readBuf = (uint8_t *)&rxBuf[0];
        i2cTransaction.readCount = I2C_EEPROM_TEST_LENGTH;
        i2cTransaction.timeout   = I2C_TRANSACTION_TIMEOUT;
        status = I2C_transfer(handle, &i2cTransaction);

    Best regards,

    Dillon

  • Dillon,

    I have tried for 400kbps also but same issue. I don't think EVM is faulty. II can't use I2C_tranfer function because I am trying to build complete bare metal application. I just to check my code at your end because you guys are expert. You guys can rectify my mistake, that's why I need to run the code at your end.

    Regards,

    Gaurav

  • You can use I2C_transfer() even for baremetal applications. The bit rate test mentioned earlier can actually be run as a baremetal test if USE_BIOS is not declared.

    #ifdef USE_BIOS
        RUN_TEST(test_I2C_Eeprom_TestApp);
    #else
        RUN_TEST(test_I2C_Eeprom_BareMetal_TestApp);
    #endif

    There is also more information regarding I2C_transfer() within I2C.h. Here's a snippet of one of the comments:

     *  ## Transferring data #
     *  A I2C transaction with a I2C peripheral is started by calling
     *  I2C_transfer(). The details of the I2C transaction is specified with a
     *  I2C_Transaction data structure. This structure allows for any of the three
     *  types of transactions: Write, Read, or Write/Read. Each transfer is
     *  performed atomically with the I2C master or slave peripheral.
     *
     *  @code
     *  I2C_Transaction i2cTransaction;
     *
     *  i2cTransaction.writeBuf = someWriteBuffer;
     *  i2cTransaction.writeCount = numOfBytesToWrite;
     *
     *  i2cTransaction.readBuf = someReadBuffer;
     *  i2cTransaction.readCount = numOfBytesToRead;
     *
     *  i2cTransaction.slaveAddress = some7BitI2CSlaveAddress;
     *
     *  ret = I2C_transfer(handle, &i2cTransaction);
     *  if (!ret) {
     *      System_printf("Unsuccessful I2C transfer");
     *  }
     *  @endcode
     *

    The E2E support team is here to help with any issue that you come across, but unfortunately we do not offer services for rewriting entire programs. Instead, we can work with you step-by-step to help solve the issue at a more granular level, and try to point you in the right direction for how to fix your code. 

    Best regards,

    Dillon

  • Dillon

    I am not asking for help in rewriting the problems. If you can run my source file you will get the problem and you can suggest any solution. I will do changes in my code based on your suggestion.

    I don't want to use OSAL that's why I am trying to build the application completely bare metal.

    Regards,

    Gaurav

  • Hello Gaurav,

    My name is Andrew, Dillon referred this thread to me.  Upon checking our records, I found this E2E post in which a customer describes a similar issue with the example project you are working with:  https://e2e.ti.com/support/processors-group/processors/f/processors-forum/874900/66ak2g12-eeprom-device-usability-in-66ak2g12-evaluation-board .  Can you try the solution listed there?

    Best regards,

    Andrew

  • Andrew,

    In transmitting multiple bytes I am not facing any issue as of now. I am facing issue while reading multiple bytes. I have tried by giving delay after starting the I2C but same issue.

    Regards,

    Gaurav

  • Hi Gaurav,

    Since you're experiencing the issue with reads, have you tried implementing a "repeated start" after a write?

    https://www.i2c-bus.org/repeated-start-condition/

    Best regards,

    Dillon

  • Dillion,

    I am doing  repeated start after transmitting offset address. You can see my source file as well for just verification.

    Regards,

    Gaurav

  • Hi Gaurav,

    Apologies for the delay. Have you made any progress on this? Or is it working?

    Best regards,

    Dillon

  • Dillion,

    Still this problem is there. I am stuck in this problem and I am in full pressure from client as this problem is there from last one month.

    Regards,

    Gaurav

  • Hi Gaurav, 

    I am currently working with my colleagues on this issue and will keep you updated with anything we discover.

    Best regards,

    Dillon

  • Dillion,

    Please check. My client is asking daily about this issue.

    Regards,

    Gaurav

  • Gaurav,

    Within the read portion of your I2CReceive(), can you try the following instead?

        uint8_t  i2cReadByte[2];
        uint32_t i2cReadVal;
        uint32_t temp1;
        uint32_t temp2;
    
        /* Set i2cReadByte count */
        I2CSetDataCount(SOC_I2C1_BASE, 2);
    
        /*
        ** Configure i2c as master-Receiver and to generate stop condition
        */
        I2CMasterControl(SOC_I2C1_BASE,
                        I2C_CFG_MST_RX | I2C_CFG_STOP | I2C_CFG_START);
    
        /* Wait for receive interrupt to occur */
        while (I2CMasterIntRawStatusEx(SOC_I2C1_BASE,
                                        I2C_INT_RECV_READY) != I2C_INT_RECV_READY) ;
    
        /* Disable transmit ready */
        I2CMasterIntClearEx(SOC_I2C1_BASE, I2C_INT_RECV_READY);
    
        /* Read i2cReadByte */
        i2cReadByte[0] = I2CMasterDataGet(SOC_I2C1_BASE);
    
        sampleDelay(50);
    
        /* Wait for receive interrupt to occur */
        while (I2CMasterIntRawStatusEx(SOC_I2C1_BASE,
                                        I2C_INT_RECV_READY) != I2C_INT_RECV_READY) ;
    
        /* Disable transmit ready */
        I2CMasterIntClearEx(SOC_I2C1_BASE, I2C_INT_RECV_READY);
    
        i2cReadByte[1] = I2CMasterDataGet(SOC_I2C1_BASE);
        
        I2CFlushFifo(SOC_I2C1_BASE);
    
        /* Clear all interrupt status */
        I2CMasterIntClearEx(SOC_I2C1_BASE, I2C_INT_ALL);
    
        /* Clear i2cReadByte buffer length */
        I2CSetDataCount(SOC_I2C1_BASE, 0);
    
        temp1 = i2cReadByte[0];
        temp2 = i2cReadByte[1];
    
        i2cReadVal = (temp1 | (temp2 << 8));

    The primary difference between this code and your code are the parameters that are set within I2CMasterControl()

        /*
        ** Configure i2c as master-Receiver and to generate stop condition
        */
        I2CMasterControl(SOC_I2C1_BASE,
                        I2C_CFG_MST_RX | I2C_CFG_STOP | I2C_CFG_START);

    This code comes from I2CRead() within the MCSPIMasterSlave example found in pdk/packages/ti/csl/example/mcspi/mcspiMasterSlave/mcspiMasterSlave_spi1_spi2.c. This program is a good reference, and I recommend basing your code off of it since it's achieving a similar result.

    Additionally, the MCSPIMasterSlave example sets up I2C with different parameters and calls their functions in a different order:

    void SetupI2C(void)
    {
        /* Do a software reset */
        I2CSoftReset(SOC_I2C1_BASE);
    
        /* Enable i2c module */
        I2CMasterEnable(SOC_I2C1_BASE);
    
        while (!I2CSystemStatusGet(SOC_I2C1_BASE)) ;
        /* Put i2c in reset/disabled state */
        I2CMasterDisable(SOC_I2C1_BASE);
    
        /* Configure i2c bus speed to 100khz */
        I2CMasterInitExpClk(SOC_I2C1_BASE, 24000000, 8000000, 100000);
    
        I2COwnAddressSet(SOC_I2C1_BASE, 0, I2C_OWN_ADDR_0);
    
        I2CSyscInit(SOC_I2C1_BASE, 0x08);
    
        /* Set Rx and Tx FIFO threshold value and reset the fifo */
        I2CFIFOThresholdConfig(SOC_I2C1_BASE, 0, I2C_TX_MODE);
    
        I2CFIFOThresholdConfig(SOC_I2C1_BASE, 0, I2C_RX_MODE);
    
        /* Set i2c slave address */
        I2CMasterSlaveAddrSet(SOC_I2C1_BASE, HSI2C_SLAVE_ADDR);
    
        /* Bring i2c out of reset */
        I2CMasterEnable(SOC_I2C1_BASE);
    
        /* Enable free run mode */
        I2CMasterEnableFreeRun(SOC_I2C1_BASE);
    }

    Can you try these out the read and setup changes and then let me know the status of the issue?

    Best regards,

    Dillon

  • Dillion,

    I am wondering about the following functions. I saw the definitions of these functions and I didn't find registers address in I2C section used in the following API.

    while (!I2CSystemStatusGet(SOC_I2C1_BASE)) ;

    I2CSyscInit(SOC_I2C1_BASE, 0x08);

    /* Set Rx and Tx FIFO threshold value and reset the fifo */
    I2CFIFOThresholdConfig(SOC_I2C1_BASE, 0, I2C_TX_MODE);

    I2CFIFOThresholdConfig(SOC_I2C1_BASE, 0, I2C_RX_MODE);

    uint32_t I2CSystemStatusGet(uint32_t baseAddr)
    {
    return (HW_RD_REG32(baseAddr + I2C_SYSS) & I2C_SYSS_RDONE_MASK);
    }

    There is no I2C_SYSS register in the I2C section of the TRM

    void I2CSyscInit(uint32_t baseAddr, uint32_t syscFlag)
    {
    HW_WR_REG32(baseAddr + I2C_SYSC, syscFlag);
    }

    There is no I2C_SYSCregister in the I2C section of the TRM

    void I2CFIFOThresholdConfig(uint32_t baseAddr,
    uint32_t thresholdVal,
    uint32_t flag)
    {
    if (I2C_TX_MODE == flag)
    {
    HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_TXTRSH, thresholdVal);
    }
    else
    {
    HW_WR_FIELD32(baseAddr + I2C_BUF, I2C_BUF_RXTRSH, thresholdVal);
    }
    }

    There is no I2C_BUF in the I2C section of the TRM.

    Could you give some idea about these registers?

    Regards,

    Gaurav

  • Dillion,

    If possible can you give your convenient time so that we can chat online one to one and resolve this issue faster?

     Regards,

    Gaurav

  • Dillion,

    Still I don't get any success. I am attaching complete project for your reference. Please note this is bare metal project with all required source files

    Gel file used "C:\ti\ccs901\ccs\ccs_base\emulation\boards\evmk2g\gel\evmk2g_arm.gel"

    I2C_Test.rar

    Regards,

    Gaurav

  • The registers you mentioned are found within pdk/packages/ti/csl/src/ip/i2c/V2/cslr_i2c.h

    • I2C_SYSS: System Status Register
    • I2C_SYSC: System Configuration Register
    • I2C_BUF: Buffer Configuration Register

    I downloaded your project and noticed the code was different than the code snippets that I mentioned in my previous response. Were you able to try all of the code snippets that I mentioned?

    Best regards,

    Dillon

  • Dillon,

    I'll check and will let you know.

    Regards,

    Gaurav

  • Dillion,

    Here this week is a holiday so not able to check this week. I'll check next week and will let you know.

    Regards,

    Gaurav

  • Sounds good. Have a good holiday.

    Best regards,

    Dillon

  • Dillion,

    I checked with your suggestions of code snippet but still no success. Can you help me beyond?

    Regards,

    Gaurav

  • Hi Gaurav,

    As a test, can you try running the original I2C_BasicExample_evmK2G_armExampleProject? I understand that you're looking for a baremetal solution, but just verifying that the original example works will at least give us a baseline.

    Best regards,

    Dillon

  • Dillion,

    I just tested a  I2C_BasicExample_evmK2G_armExampleProject. It is working absolutely fine. I don't know why my baremetal code is not working.

    Regards,

    Gaurav

  • If you're looking for baremetal code without OSAL, then that is essentially CSL. It's not quite supported for K2G, but a great example to look at is the I2C_led_blink example found in pdk/packages/ti/csl/example/i2c/i2c_led_blink/main.c. Can you try modeling your code (or creating a new copy) that is based off of this blink example?

    Best regards,

    Dillon