• 您的位置:首页 > 新闻动态 > 技术文章

    cc2640 I2C实现

    2018/1/23      点击:

    以keyfob为例:

    一、board.c中添加如下定义(根据自己板子实际情况选择io):

    /* I2C Board */
    #define Board_I2C_SCL                       IOID_13          /* RF1.7  */
    #define Board_I2C_SDA                       IOID_12          /* RF1.9  */


    二、board.c中添加如下定义:

    /*
    * ============================= I2C Begin=====================================
    */
    /* Place into subsections to allow the TI linker to remove items properly */
    #if defined(__TI_COMPILER_VERSION__)
    #pragma DATA_SECTION(I2C_config, ".const:I2C_config")
    #pragma DATA_SECTION(i2cCC26xxHWAttrs, ".const:i2cCC26xxHWAttrs")
    #endif


    /* Include drivers */
    #include


    /* I2C objects */
    I2CCC26XX_Object i2cCC26xxObjects[CC2650_I2CCOUNT];


    /* I2C configuration structure, describing which pins are to be used */
    const I2CCC26XX_HWAttrs i2cCC26xxHWAttrs[CC2650_I2CCOUNT] = {
    {
    .baseAddr = I2C0_BASE,
    .intNum = INT_I2C,
    .powerMngrId = PERIPH_I2C0,
    .sdaPin = Board_I2C_SDA,
    .sclPin = Board_I2C_SCL,
    }
    };


    const I2C_Config I2C_config[] = {
    {&I2CCC26XX_fxnTable, &i2cCC26xxObjects[0], &i2cCC26xxHWAttrs[0]},
    {NULL, NULL, NULL}
    };
    /*
    * ========================== I2C end =========================================
    */


    三、keyfobdemo.c中添加如下内容:


    1、

    //add by gyb

    #include


    static I2C_Handle SbpI2cHandle;
    static I2C_Params SbpI2cParams;


    2、初始化函数中添加

    //add by gyb i2c driver
     I2C_Params_init(&SbpI2cParams);
     SbpI2cHandle = I2C_open(CC2650_I2C0, &SbpI2cParams);


    3、定义自己的读写函数(仅供参考):

    void writeRegister(uint8 addr, uint8 buf)
    {
    I2C_Transaction i2cTransaction;
    uint8 txBuf[] = {addr, buf, 2, 3};
    uint8 rxBuf[5];
    i2cTransaction.writeBuf = txBuf;
    i2cTransaction.writeCount = 2;
    i2cTransaction.readBuf = rxBuf;
    i2cTransaction.readCount = 0;
    i2cTransaction.slaveAddress = LSM6DS3_SLAVE_ADDR; //arbitrary for demo
    I2C_transfer(SbpI2cHandle, &i2cTransaction);
    }




    void readRegister(uint8 *buf, uint8 addr)
    {


    I2C_Transaction i2cTransaction;
     //uint8 txBuf[] = {addr, 0x00};
     uint8 txBuf[] = {addr};
     uint8 rxBuf[5];
     i2cTransaction.writeBuf = txBuf;
     i2cTransaction.writeCount = 1;
     i2cTransaction.readBuf = buf;
     i2cTransaction.readCount = 1;
     i2cTransaction.slaveAddress = LSM6DS3_SLAVE_ADDR; //slave addr
     I2C_transfer(SbpI2cHandle, &i2cTransaction);
    }

    四、添加  i2c.c  i2c.h文件到工程:

    文件路径:C:\ti\tirtos_simplelink_2_13_00_06\packages\ti\drivers

    ok,完成上述步骤,i2c应该可以工作了