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/TM4C1294NCPDT: I2C write and read call inside ISR function

Part Number: TM4C1294NCPDT


Tool/software: TI-RTOS

Hi,

I am calling a i2c_write function inside a ISR function, But its showing some error like below...

ssertion failure: A_badContext: bad calling context. Must be called from a Task.
xdc.runtime.Error.raise: terminating execution

/* ISR function call */

ISRHandlerFunction()

{

/*clearing flags and led toggling is done*/

(* (volatile uint32_t *)clearFlagaddr) = bit_shift;
 GPIO_toggle(Board_LED0);

I2cWrite();

}

/* I2C write funcation */

I2cWrite()

{

uint8_t         vui8Count;


    System_printf("i2c write\n");
    //System_flush();

    /* Create I2C for usage */
    I2C_Params_init(&stI2cParams);
    stI2cParams.bitRate = I2C_400kHz;

    stI2cHandle = I2C_open(stI2cConfig->vui8I2cBus, &stI2cParams);
    if (stI2cHandle == NULL) {
        System_abort("Error Initializing I2C\n");
    }
    else {
        System_printf("I2C Initialized!\n");
    }


    for (vui8Count = 0; vui8Count < (stI2cConfig->vui8writeCount - 1); vui8Count++) {
        if (I2C_transfer(stI2cHandle, &stI2cTransaction)) {
            System_printf("Send data %d : %d \n", vui8Count+1, stI2cConfig->pui8txbuffer);
        }
        else {
            System_printf("I2C Bus fault\n");
        }

        //System_flush();

    }

    /* Deinitialized I2C */
    I2C_close(stI2cHandle);

}

the above is the actual code, its just a format of code.

if I comment down the i2cWrite function, ISR function working fine. If I use inside ISR, Its showing the above error.

Want to know why this error is coming.

Regards,

Manohar

  • Manohar B said:
    I am calling a i2c_write function inside a ISR function, But its showing some error like below...

    ssertion failure: A_badContext: bad calling context. Must be called from a Task

    That error is because an interrupt handler (SWI or HWI) is not allow to call an SYS/BIOS function which blocks.

    To remove the error move your i2c_write code into a Task function surrounded by a Semaphore_pend(), and have the ISRHandlerFunction post the Semaphore that the Task is blocked on.

    See https://e2e.ti.com/support/embedded/tirtos/f/355/t/376920 for an example.

  • Manohar,

    It is a good thing that the compiler won't let you move on with that attempt, or your project would start getting less reliable.

    Think of it like this: you want your interrupt service routines to be as short as possible. It should do the very minimum required when the hardware interrupt event happens. A task such as writing stuff over I2C can be done "later on".

    The logic inside a non-os project should be: inside the interrupt, set a control flag that says that the i2CWrite must be executed; back on the main program, check for flags and execute the enabled tasks.

    With TIRTOS, the concept is pretty much the same, but easier - inside the ISR, you should just post a semaphore to a task, and the OS will take care of running the i2CWrite as soon as possible.

    Bruno

  • Hi,

    Even if I post a SWI and inside SWI function I am calling the i2cWrite() function. Even though I am getting the same error.

    /*Sample code*/
    /*HWI Handler function */
    HWIHandlerFunc()
    {
    //clearing flags//
    Swi_Post(SwiHandle);
    }

    /*Swi Handler function */
    SwihandleFunc()
    {
    while(1)
    {
    Swi_pend(SwiHandle);
    i2cWrite();
    }
    }

    The above i2cWrite function is also giving the same error. Why?


    Regards,
    Manohar
  • Manohar B said:
    Even if I post a SWI and inside SWI function I am calling the i2cWrite() function. Even though I am getting the same error.

    A SWI is subject to the same constraints a HWI, in that neither can make SYS/BIOS calls which block.

    The i2cWrite() function call needs to be made in a task, where the task can pend on a semaphore posted by a SWI or HWI.

  • Chester,
    I had misleaded poster, sorry. Have just corrected my previous message replacing the SWI by a TASK.
    Bruno