cc2640调试经验之 I2C驱动添加

       之前项目中用到过i2c驱动,当时查阅资料之后调通了i2c,后来换了公司,时隔一年又需要使用cc2640的i2c通讯,由于记性太差,之前的工作全忘了,所以只有重头再来,为了以后使用方便,把添加方法分享出来,也方便自己以后查看。以下操作以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 <ti/drivers/i2c/I2CCC26XX.h>


/* 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 <ti/drivers/I2C.h>


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应该可以工作了

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路人 假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值