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 bare metal example

Part Number: 66AK2G12

Hi!

I am using pdk_k2g_1_0_16 and K2Gx evaluation board on windows host pc. I am trying to create I2C bare metal application. I have imported APIs from "C:\ti\pdk_k2g_1_0_16\packages\ti\csl\example\i2c\i2c_led_blink\main.c" to I2C_BasicExample_evmK2G_armExampleProject\main_eeprom_read.c.  I am getting some compilation error.

csl library is already included. I have attached the project and main source file for your reference so that you can compile the project at your end.

Is there any SetupI2CReception function is there in CSL or drv like SetupI2CTransmit?

Please suggest any solution.

I2C_BasicExample_evmK2G_armExampleProject.rar

/**
 *  \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>


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


void SetupI2C(void);


#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)

/**********************************************************************
 ************************** 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 */

    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)
{

    SetupI2C();

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

#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)
{
    /* Do a software reset */
    I2CSoftReset(I2C0_BASE_ADDRESS);

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

    while (!I2CSystemStatusGet(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_0);

    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);
}

Regards,

Gaurav

  • Hi Gaurav,

    What kind of errors did you see?

    Regards,

    Jianzhong

  • Jianzhong,

    Please find attached log file for your reference. I have included header files as it is given in example source file "C:\ti\pdk_k2g_1_0_16\packages\ti\csl\example\i2c\i2c_led_blink\main.c", still I am getting undefined errors. I have attached source file in previous post.

    **** Build of configuration Debug for project I2C_BasicExample_evmK2G_armExampleProject ****
    
    "C:\\ti\\ccs901\\ccs\\utils\\bin\\gmake" -k -j 4 all -O 
     
    Building file: "C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c"
    Invoking: GNU Compiler
    "C:/ti/ccs901/ccs/tools/compiler/gcc-arm-none-eabi-7-2017-q4-major-win32/bin/arm-none-eabi-gcc-7.2.1.exe" -c -mcpu=cortex-a15 -mtune=cortex-a15 -marm -mfloat-abi=hard -Dk2g02 -Darm0 -DUSE_BIOS -DSOC_K2G -DevmK2G -I"C:/ti/pdk_k2g_1_0_16/packages/MyExampleProjects/I2C_BasicExample_evmK2G_armExampleProject" -I"C:/ti/ccs901/ccs/tools/compiler/gcc-arm-none-eabi-7-2017-q4-major-win32/arm-none-eabi/include/newlib-nano" -I"C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c" -I"C:/ti/pdk_k2g_1_0_16/packages" -I"/packages" -I"C:/ti/ccs901/ccs/tools/compiler/gcc-arm-none-eabi-7-2017-q4-major-win32/arm-none-eabi/include" -Og -g -gdwarf-3 -gstrict-dwarf -Wall -c -MMD -MP -MMD -MP -MF"main_eeprom_read.d" -MT"main_eeprom_read.o"  @"configPkg/compiler.opt" -o"main_eeprom_read.o" "C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c"
    subdir_rules.mk:23: recipe for target 'main_eeprom_read.o' failed
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c: In function 'SetupI2C':
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:328:5: warning: implicit declaration of function 'I2CSoftReset' [-Wimplicit-function-declaration]
         I2CSoftReset(I2C0_BASE_ADDRESS);
         ^~~~~~~~~~~~
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:333:13: warning: implicit declaration of function 'I2CSystemStatusGet'; did you mean 'I2CMasterDataGet'? [-Wimplicit-function-declaration]
         while (!I2CSystemStatusGet(I2C0_BASE_ADDRESS)) ;
                 ^~~~~~~~~~~~~~~~~~
                 I2CMasterDataGet
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:348:22: error: 'I2C_OWN_ADDR_0' undeclared (first use in this function); did you mean 'I2C_OWN_ADDR'?
                          I2C_OWN_ADDR_0);
                          ^~~~~~~~~~~~~~
                          I2C_OWN_ADDR
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:348:22: note: each undeclared identifier is reported only once for each function it appears in
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:347:5: error: too many arguments to function 'I2COwnAddressSet'
         I2COwnAddressSet(I2C0_BASE_ADDRESS, 0,
         ^~~~~~~~~~~~~~~~
    In file included from C:/ti/pdk_k2g_1_0_16/packages/ti/csl/csl_i2c.h:51:0,
                     from C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:64:
    C:/ti/pdk_k2g_1_0_16/packages/ti/csl/src/ip/i2c/V0/i2c.h:182:14: note: declared here
     extern  void I2COwnAddressSet(uint32_t baseAddr, uint32_t ownAddr);
                  ^~~~~~~~~~~~~~~~
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:350:5: warning: implicit declaration of function 'I2CSyscInit'; did you mean 'I2C_init'? [-Wimplicit-function-declaration]
         I2CSyscInit(I2C0_BASE_ADDRESS, 0x08);
         ^~~~~~~~~~~
         I2C_init
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:353:5: warning: implicit declaration of function 'I2CFIFOThresholdConfig' [-Wimplicit-function-declaration]
         I2CFIFOThresholdConfig(I2C0_BASE_ADDRESS, 0, I2C_TX_MODE);
         ^~~~~~~~~~~~~~~~~~~~~~
    C:/ti/pdk_k2g_1_0_16/packages/ti/drv/i2c/example/eeprom_read/src/main_eeprom_read.c:364:5: warning: implicit declaration of function 'I2CMasterEnableFreeRun'; did you mean 'I2CMasterEnable'? [-Wimplicit-function-declaration]
         I2CMasterEnableFreeRun(I2C0_BASE_ADDRESS);
         ^~~~~~~~~~~~~~~~~~~~~~
         I2CMasterEnable
    gmake: *** [main_eeprom_read.o] Error 1
    gmake: Target 'all' not remade because of errors.
    
    **** Build Finished ****
    

    Regards,

    Gaurav

  • Jianzhong, 

    Have you checked the log file?

    Regards,

    Gaurav