1

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?

Ivan Vnucec
  • 604
  • 1
  • 5
  • 17

2 Answers2

1

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(...);
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks. I've looked up what is portNVIC_INT_CTRL_REG register and VECTACTIVE bit here https://interrupt.memfault.com/blog/arm-cortex-m-exceptions-and-nvic#icsr-register and indeed with your solution I can be certain that I'm in ISR. Once I check my code, I will approve your answer. – Ivan Vnucec Nov 09 '21 at 09:54
  • For the interested: I've stumbled on the xPortIsInsideInterrupt() defined inside portmacro.h header file under FreeRTOS directory. Function returns if we are inside interrupt by checking Interrupt Program Status Register (IPSR). – Ivan Vnucec Nov 09 '21 at 11:10
0

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

HS2
  • 164
  • 1
  • 1
  • 4
  • Do you have a reference to your statement? – Ivan Vnucec Nov 08 '21 at 09:14
  • Definately the FromISR version. Clearly stated in the user manual that only FromISR functions can be called from an ISR. – Realtime Rik Nov 08 '21 at 10:16
  • Let me rephrase: Do you have a reference to your statement that "A function called from an ISR is still part of the ISR."? – Ivan Vnucec Nov 08 '21 at 11:01
  • 2
    @IvanVnucec Why should just calling a function change anything to the calling context ? Same applies to code executed by a function call from a task, which is still part of the task context, right ? – HS2 Nov 08 '21 at 12:29
  • 3
    @IvanVnucec the reference is knowledge. If you call a function from the interrupt handler you are still in the handler. Only return from the handler changes the context. – 0___________ Nov 08 '21 at 23:53