This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/LAUNCHCC3220MODASF: I2C initialization failed

Part Number: LAUNCHCC3220MODASF
Other Parts Discussed in Thread: TMP006

Tool/software: TI C/C++ Compiler

Hi,

I have combined the timerled and i2ctemp examples to get the temperature value on every second but it is giving me error : I2C bus fault.

I have tried to resolve the issue but I couldn't understand why is occurring, please suggest me how could I resolve this issue ?

Here is the code and screenshot of the debug page:

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>

#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/display/Display.h>
#include <ti/drivers/Timer.h>

#include "Board.h"

#define TMP006_DIE_TEMP 0x0001
uint16_t temperature;
uint8_t txBuffer[1];
uint8_t rxBuffer[2];
I2C_Transaction i2cTransaction;
I2C_Handle i2c;

I2C_Params i2cParams;

int flag;

void timerCallback();


void *mainThread(void *arg0)
{

Timer_Handle timer0;
Timer_Params params;


GPIO_init();
Timer_init();

I2C_init();

I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;

GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);

Timer_Params_init(&params);
params.period = 1000000;
params.periodUnits = Timer_PERIOD_US;
params.timerMode = Timer_CONTINUOUS_CALLBACK;
params.timerCallback = timerCallback;

timer0 = Timer_open(Board_TIMER0, &params);

if (timer0 == NULL) {

while (1);
}

if (Timer_start(timer0) == Timer_STATUS_ERROR) {

while (1);
}

return (NULL);
}


void timerCallback()
{
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
printf( "Error Initializing I2C\n");
while (1);
}
else {
printf( "I2C Initialized!\n");
}
txBuffer[0] = TMP006_DIE_TEMP;
i2cTransaction.slaveAddress = Board_TMP_ADDR;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 1;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 2;
flag = I2C_transfer(i2c, &i2cTransaction);

if (flag) {

temperature = (rxBuffer[0] << 6) | (rxBuffer[1] >> 2);

if (rxBuffer[0] & 0x80) {
temperature |= 0xF000;
}

temperature /= 32;

printf( "Sample %d (C)\n", temperature);
}
else {
printf("I2C Bus fault\n");
}

I2C_close(i2c);
printf("I2C closed!\n");

}

  • Hey SAGAR,

    I reviewed your code and here is the reason your I2C transfer params its in blocking and not Callback mode so your I2C transfer calls were all being blocked so switch the transfer modes, however that will not full fix the problem you also need to added a callback function to your I2C to call your transfer function otherwise your value will be zero. I attached the fix down below; also there is a simpler way to replicate this without callbacks just add a sleep call (if not already added) and an infinite while loop (where the for loop is) to the tempature example.

    Code :
    #include <stdint.h>
    #include <stddef.h>
    #include <unistd.h>
    #include <stdio.h>

    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/I2C.h>
    #include <ti/display/Display.h>
    #include <ti/drivers/Timer.h>

    /* Example/Board Header files */
    #include "Board.h"

    #define TMP006_DIE_TEMP 0x0001 /* Die Temp Result Register*/

    unsigned int i = NULL;
    uint16_t temperature;
    uint8_t txBuffer[1];
    uint8_t rxBuffer[2];
    I2C_Handle i2c;
    I2C_Params i2cParams;
    I2C_Transaction i2cTransaction;
    Timer_Handle timer0;
    Timer_Params params;


    void timerCallback();

    void *mainThread(void *arg0){

    GPIO_init();
    Timer_init();
    I2C_init();

    GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);

    txBuffer[0] = TMP006_DIE_TEMP;
    i2cTransaction.slaveAddress = Board_TMP_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;



    Timer_Params_init(&params);
    params.period = 1500000;
    params.periodUnits = Timer_PERIOD_US;
    params.timerMode = Timer_CONTINUOUS_CALLBACK;
    params.timerCallback = timerCallback;

    timer0 = Timer_open(Board_TIMER0, &params);

    if (timer0 == NULL)
    {

    while (1)
    ;
    }

    if (Timer_start(timer0) == Timer_STATUS_ERROR)
    {

    while (1)
    ;
    }

    return(0);
    }
    void transferCallbackFxn(I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus){ // for I2C Callback



    if (transferStatus) {

    temperature = (rxBuffer[0] << 6) | (rxBuffer[1] >> 2);

    if (rxBuffer[0] & 0x80)
    {
    temperature |= 0xF000;
    }

    temperature /= 32;

    printf("Sample %d (C)\n", temperature);
    }

    else
    {
    printf("I2C Bus fault\n");
    }

    I2C_close(i2c);
    printf("I2C closed!\n");
    }
    void timerCallback()
    {

    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2cParams.transferMode = I2C_MODE_CALLBACK; //Switched
    i2cParams.transferCallbackFxn = transferCallbackFxn; // Add this
    i2c = I2C_open(Board_I2C_TMP, &i2cParams);

    if (i2c == NULL)
    {
    printf("Error Initializing I2C\n");
    while (1)
    ;
    }
    else
    {
    printf("I2C Initialized!\n");
    }

    I2C_transfer(i2c, &i2cTransaction);
    }


     
     
    Have a wonderful day.

    Sincerely,
    Tyisheam J.

  • Thanks Tyisheam,
    As it has resolved the issue I have clicked on this resolve my issue.