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.

RTOS: I2C TRANSFER IN A CALLBACK FUNCTION

Other Parts Discussed in Thread: CC2650

Tool/software: TI-RTOS

Hi,

I´m using the CC2650 launchpad and I,ve found and issue I can´t resolve. I´m trying to do some I2c readings controlling them with a timer. The fact is that when I try to do a I2cparams transfer at the timer´s callback it never gets succesfull. Here is the code:

static void Lecturas_Handler(UArg arg)  //The callback function from the timer
{

lecturas();

}

//////////////***********************////////////////////

static float lecturas(void)
{
I2C_Transaction i2cTrans;
uint8_t rxBuf[32] = {0}; //Receive buffer
uint8_t txBuf[32] = {0}; //Transmit buffer
uint8_t error1=0;
coordenadas acel;
RAW raw;

float modulo=0.0;


handle = I2C_open(Board_I2C, &params);
if(!handle) {
printf("clsdbvjlbsvklb");
//Error
}

//Read Acel
//Address = 0x28
txBuf[0] = 0x28;
i2cTrans.writeCount = 1;
i2cTrans.writeBuf = txBuf;
i2cTrans.readCount = 6;
i2cTrans.readBuf = rxBuf;
i2cTrans.slaveAddress = 0x6B;

do
{
error1=I2C_transfer(handle, &i2cTrans);

}while(!error1);

error1=0;

}

When the programme arrives at the transfer, the returning value never becomes "TRUE".

Is this the right way to do it or is there another solution?

  • Hi Alex,

    What are your I2C params setup to? If you are using the I2C in blocking mode, you can not perform any transfers from within a callback function as this will lock the system up. You can however perform transfers in callback mode.

    Also you should avoid doing any kind of busy spins in the callbacks.
  • Hi,
    I´m using the blocking mode, but if I use the callback mode, it doesn´t return raw data
  • What do you mean with "but if I use the callback mode, it doesn´t return raw data"?

    If you are doing a transfer in callback mode, the results will be ready when the I2C callback arrive.
  • rxBuf is one of the Params I transfer. If I make a transfer and I want to read some registers value, that data should be placed in the rxBuf. In this case, if I want that, the value returned to rxBuf is 0.
  • It is hard to say anything about this without knowing how to you implement this when using callback mode for the I2C transfer.

    Based on the assumption that you used the above code with simple modifications: Change to callback, register callback function, I would expect rxBuf to be either 0 or unreachable. As rxBuf is local, the chance that it has gone out of scope when the I2C driver tries to use it is big as "lecturas" would return right away. If you check rxBuf right after I2C_transfer return when running callback mode, the I2C driver will most likely not be done with the transfer (thus no data).

    If you have not already done this, can you try make the rx and tx buffers global and then check the value of them again inside the I2C callback function?