请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:CC2640 工具/软件:TI-RTOS
这是正确的代码吗?:
bool reg_write (uint32_t reg_address、uint32_t reg_value){
uint8_t txBuffer[4];
txBuffer[0]= reg_address;
txBuffer[1]=(REG_VALUE & 0x00FF0000)>> 16;// MSB
txBuffer[2]=(REG_VALUE & 0x0000FF00)>> 8;// MID
txBuffer[3]=(REG_VALUE & 0x000000FF); // LSB
I2C_Handle I2C;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
bool transfer_Success;
//创建 I2C 以供使用
I2C_Params_init (&i2cParams);
i2cParams.bitrate = I2C_400kHz;
I2C = I2C_open (Board_I2C、&i2cParams);
if (i2c == NULL){
System_abort ("初始化 I2C\n 时出错");
}
否则{
// system_printf ("I2C 已初始化!\n");
}
//设置事务
i2cTransaction.slaveAddress = SLAVEADDR;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 4;
i2cTransaction.ReadCount = 0;
//进行传输
transfer_Success = I2C_transfer (i2c、&i2cTransaction);
如果(!transfer_Success){
System_printf ("I2C 总线故障\n");
}
//取消初始化 I2C
I2C_Close (i2c);
// System_printf ("I2C closed!\n");
// System_flush ();
返回 TRANSFER_SUCCESS
;}
或者、我是否创建 I2C 句柄的一个实例并在每个 REG_WRITE 之间共享它、即每次使用 I2C 时不要初始化和取消初始化 I2C?