大家好,
我现在正在试图使用CC1310的硬件I2C去驱动一个地磁传感器。目前使用的是CC1310的SDK中提供的TI-DRIVERS api。为了满足传感器自身的时序需求,我基于
I2Cdrivers的API自己编写了几个上层的传输函数。但结果显示我的每一次传输指令都没有成功。通过调试后我发现I2C_transfer()函数最后返回的状态都是
ERROR_STATE,我在自定义的传输函数之中每一次调用都进行了初始化I2C、关闭I2C,例化一个I2C句柄等操作,不知道这样可不可行。或者说是我调用API的
顺序有问题?
总之,烦请各位花一点时间看一下我的代码,进行一些指点,看函数看代码确实都非常的累,我也不愿意耽误大家太宝贵的时间,于是将代码简化如下请大家看看!
真心的感谢大家!
祝好!
Alan Xie
void *mainThread(void *arg0){uint8_t CheckInitiateState;/* Call driver init functions */// .....省略...../*=============================== Initiate MAG3110 e============================= */MAG3110_Init();//传感器驱动CheckInitiateState=I2C_Read(MAG3110_IIC_ADDRESS,WHO_AM_I_REG,1);//检查传输值if ( CheckInitiateState == MAG3110Q_ID){Display_printf(display, 0, 0, "ID:MAG3110Q,OK!\n ");}else // failed to initiate MAG3110{Display_printf(display, 0, 0, "ID not identified,FAILED!\n");}}//.....省略..../**************************传感器驱动函数********************/void MAG3110_Init (void){MAG3110_Standby();txBuffer[0] = CTRL_REG1;txBuffer[1] = DATA_RATE_5MS;I2C_WriteOneByte(MAG3110_IIC_ADDRESS, txBuffer);MAG3110_Active();}void MAG3110_Active (void){I2C_Read(MAG3110_IIC_ADDRESS,CTRL_REG1,1);txBuffer[0] = CTRL_REG1;txBuffer[1] = (rxBuffer[0]&0xFC|ACTIVE_MASK);I2C_WriteOneByte(MAG3110_IIC_ADDRESS,txBuffer);}void MAG3110_Standby (void){I2C_Read(MAG3110_IIC_ADDRESS,CTRL_REG1,1);txBuffer[0] = CTRL_REG1;txBuffer[1] = (rxBuffer[0]&0xFC|STANDBY_MASK);I2C_WriteOneByte(MAG3110_IIC_ADDRESS,txBuffer);}//.....省略..../******************自定义I2C传输函数**********//*=========================================== 自定义I2C读 ===================================================*/uint8_t I2C_Read(uint8_t I2C_Addr, uint8_t REG_Addr, uint16_t num){I2C_init();int ret;I2C_Handlei2c;I2C_Paramsi2cParams;I2C_Params_init(&i2cParams);i2cParams.bitRate = I2C_400kHz;i2cParams.transferMode = I2C_MODE_BLOCKING;i2c = I2C_open(Board_I2C0, &i2cParams);if (i2c == NULL) {Display_printf(display, 0, 0, "Error Initializing I2C\n");while (1);}else {Display_printf(display, 0, 0, "I2C Initialized!\n");}txBuffer[0] = REG_Addr;I2C_Transaction i2cTransaction;i2cTransaction.slaveAddress = I2C_Addr;i2cTransaction.writeBuf = txBuffer;i2cTransaction.writeCount = 1;i2cTransaction.readBuf = rxBuffer;i2cTransaction.readCount = num;ret = I2CCC26XX_transfer(i2c, &i2cTransaction);if (!ret)Display_printf(display, 0, 0, "Unsuccessful I2C read");elseDisplay_printf(display, 0, 0, "Successful I2C read");I2C_close(i2c);return *rxBuffer;} /************************************* 自定义I2C写 *********************/uint8_t I2C_WriteOneByte(uint8_t I2C_Addr, uint8_t *valuebuffer){I2C_init();int ret;I2C_Handlei2c;I2C_Paramsi2cParams;I2C_Params_init(&i2cParams);i2cParams.bitRate = I2C_400kHz;i2cParams.transferMode = I2C_MODE_BLOCKING;i2c = I2C_open(Board_I2C0, &i2cParams);if (i2c == NULL) {Display_printf(display, 0, 0, "Error Initializing I2C\n");while (1);}else {Display_printf(display, 0, 0, "I2C Initialized!\n");}I2C_Transaction i2cTransaction;i2cTransaction.slaveAddress = I2C_Addr;i2cTransaction.writeBuf = valuebuffer;i2cTransaction.writeCount = 2;//1 or 2? TODO: Needs to be figured out.i2cTransaction.readBuf = NULL;i2cTransaction.readCount = 0;ret=I2CCC26XX_transfer(i2c, &i2cTransaction);if (!ret){Display_printf(display, 0, 0, "Unsuccessful I2C write");}elseDisplay_printf(display, 0, 0, "Successful I2C write");I2C_close(i2c);return 0;}
Susan Yang:
回复 Alan Xie1:
谢谢分享!