I do the following step to use I2C in sensor example. I2C_open is no problem but I2C_transfer always return false. Can anyone use I2C successfully in sensor example?
1. Add the following header files to sensor.c
#include <ti/drivers/I2C.h> //YK
#include <ti/drivers/i2c/I2CCC26XX.h> //YK
#include "board.h" //YK
#include "board_lcd.h" //YK
2. Add the following I2C code in red to processSensorMsgEvt().
static void processSensorMsgEvt(void)
{
Smsgs_sensorMsg_t sensor;
uint32_t stat;
memset(&sensor, 0, sizeof(Smsgs_sensorMsg_t));
ApiMac_mlmeGetReqUint32(ApiMac_attribute_diagRxSecureFail, &stat);
Sensor_msgStats.rxDecryptFailures = (uint16_t)stat;
ApiMac_mlmeGetReqUint32(ApiMac_attribute_diagTxSecureFail, &stat);
Sensor_msgStats.txEncryptFailures = (uint16_t)stat;
ApiMac_mlmeGetReqArray(ApiMac_attribute_extendedAddress,
sensor.extAddress);
/* fill in the message */
sensor.frameControl = configSettings.frameControl;
if(sensor.frameControl & Smsgs_dataFields_tempSensor)
{
memcpy(&sensor.tempSensor, &tempSensor,
sizeof(Smsgs_tempSensorField_t));
}
if(sensor.frameControl & Smsgs_dataFields_lightSensor)
{
memcpy(&sensor.lightSensor, &lightSensor,
sizeof(Smsgs_lightSensorField_t));
}
if(sensor.frameControl & Smsgs_dataFields_humiditySensor)
{
memcpy(&sensor.humiditySensor, &humiditySensor,
sizeof(Smsgs_humiditySensorField_t));
}
if(sensor.frameControl & Smsgs_dataFields_msgStats)
{
memcpy(&sensor.msgStats, &Sensor_msgStats,
sizeof(Smsgs_msgStatsField_t));
}
if(sensor.frameControl & Smsgs_dataFields_configSettings)
{
sensor.configSettings.pollingInterval = configSettings.pollingInterval;
sensor.configSettings.reportingInterval = configSettings
.reportingInterval;
}
/* inform the user interface */
Ssf_sensorReadingUpdate(&sensor);
//YK
{
// Locals
I2C_Handle handle;
I2C_Params params;
I2C_Transaction i2cTrans;
uint8_t rxBuf[32]; // Receive buffer
uint8_t txBuf[32]; // Transmit buffer
I2C_init();
// Configure I2C parameters.
I2C_Params_init(¶ms);
// Initialize master I2C transaction structure
i2cTrans.writeCount = 0;
i2cTrans.writeBuf = txBuf;
i2cTrans.readCount = 10;
i2cTrans.readBuf = rxBuf;
i2cTrans.slaveAddress = 0x3C;
// Open I2C
handle = I2C_open(CC1350_LAUNCHXL_I2C0, ¶ms);
if (handle == NULL) {
LCD_WRITE_STRING("Error Initializing I2C",8);
}
else {
LCD_WRITE_STRING("I2C Initialized!",8);
}
// Do I2C transfer receive
if (I2C_transfer(handle, &i2cTrans)){
LCD_WRITE_STRING("Device ID read OK!",8);
} else {
LCD_WRITE_STRING("Device ID fail!",8);
}
/* Deinitialized I2C */
I2C_close(handle);
LCD_WRITE_STRING("I2C closed!",8);
//System_flush();
}
/* send the data to the collector */
sendSensorMessage(&collectorAddr, &sensor);
}