• 您的位置:首頁 > 技術(shù)支持

    CC2650的I2C驅(qū)動方法

    2017/12/15??????點擊:

    一、board.c中添加如下定義(根據(jù)自己板子實際情況選擇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中添加如下內(nèi)容:

    1、

    #include

    static I2C_Handle SbpI2cHandle;
    static I2C_Params SbpI2cParams;

    2、初始化函數(shù)中添加

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

    3、定義自己的讀寫函數(shù)(僅供參考):

    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應(yīng)該可以工作了