Should I use xSemaphoreGiveFromISR() or xSemaphoreGive() in STM32 HAL interrupt callbacks?

1k Views Asked by At

I've implemented a fault task in the FreeRTOS which is taking a binary semaphore used as a fault flag. That fault flag is triggered by the STM32 HAL error callback functions, such as HAL_I2C_ErrorCallback or HAL_UART_ErrorCallback. If an error occurs, error callbacks functions will call signalFault() function, which will raise the fault flag by giving the binary semaphore.

My question is: is my signalFault() function treated as an interrupt service routine (ISR) or not, because it is being called in the HAL ISR error callbacks? Do I need to call xSemaphoreGiveFromISR() or xSemaphoreGive() function in signalFault() function to raise the fault flag?

2

There are 2 best solutions below

5
HS2 On BEST ANSWER

Sure, you should use xSemaphoreGiveFromISR if the calling context is an ISR. A function called from an ISR is still part of the ISR.

2
0___________ On

If you do not trust us then simple check if you are in the interrupt. It will be 100% safe.

int isInISR( void ) 
{
    return ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK );
}

and in your code:

if(isInISR()) xSemaphoreGiveFromISR(...);
else xSemaphoreGive(...);