Updated May 2025
taskENTER_CRITICAL_FROM_ISR(), taskEXIT_CRITICAL_FROM_ISR()
1task. h23UBaseType_t taskENTER_CRITICAL_FROM_ISR( void );4void taskEXIT_CRITICAL_FROM_ISR( UBaseType_t uxSavedInterruptStatus );
Versions of taskENTER_CRITICAL() and taskEXIT_CRITICAL() that can be used in an interrupt service routine (ISR).
In an ISR critical sections are entered by calling taskENTER_CRITICAL_FROM_ISR(), and subsequently exited by calling taskEXIT_CRITICAL_FROM_ISR().
The taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() macros provide a basic critical section implementation that works by simply disabling interrupts, either globally, or up to a specific interrupt priority level.
If the FreeRTOS port being used supports interrupt nesting then calling taskENTER_CRITICAL_FROM_ISR() will disable interrupts at and below the interrupt priority set by the configMAX_SYSCALL_INTERRUPT_PRIORITY (or configMAX_API_CALL_INTERRUPT_PRIORITY) kernel configuration constant, and leave all other interrupt priorities enabled. If the FreeRTOS port being used does not support interrupt nesting then taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() will have no effect.
Calls to taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() are designed to nest, but the semantics of how the macros are used is different to the taskENTER_CRITICAL() and taskEXIT_CRITICAL() equivalents.
Critical sections must be kept very short, otherwise they will adversely affect the response times of higher priority interrupts that would otherwise nest. Every call to taskENTER_CRITICAL_FROM_ISR() must be closely paired with a call to taskEXIT_CRITICAL_FROM_ISR().
FreeRTOS API functions must not be called from within a critical section.
Parameters:
-
uxSavedInterruptStatus
taskEXIT_CRITICAL_FROM_ISR() takes uxSavedInterruptStatus as its only parameter. The value used as the uxSavedInterruptStatus parameter must be the value returned from the matching call to taskENTER_CRITICAL_FROM_ISR().
taskENTER_CRITICAL_FROM_ISR() does not take any parameters.
Returns:
taskENTER_CRITICAL_FROM_ISR() returns the interrupt mask state as it was before the macro was called. The value returned by taskENTER_CRITICAL_FROM_ISR() must be used as the uxSavedInterruptStatus parameter in the matching call to taskEXIT_CRITICAL_FROM_ISR().
taskEXIT_CRITICAL_FROM_ISR() does not return a value.
Example usage:
1/* A function called from an ISR. */2void vDemoFunction( void )3{4UBaseType_t uxSavedInterruptStatus;56 /* Enter the critical section. In this example, this function is itself called from7 within a critical section, so entering this critical section will result in a nesting8 depth of 2. Save the value returned by taskENTER_CRITICAL_FROM_ISR() into a local9 stack variable so it can be passed into taskEXIT_CRITICAL_FROM_ISR(). */10 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();1112 /* Perform the action that is being protected by the critical section here. */1314 /* Exit the critical section. In this example, this function is itself called from a15 critical section, so interrupts will have already been disabled before a value was16 stored in uxSavedInterruptStatus, and therefore passing uxSavedInterruptStatus into17 taskEXIT_CRITICAL_FROM_ISR() will not result in interrupts being re-enabled. */18 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );19}2021/* A task that calls vDemoFunction() from within an interrupt service routine. */22void vDemoISR( void )23{24UBaseType_t uxSavedInterruptStatus;2526 /* Call taskENTER_CRITICAL_FROM_ISR() to create a critical section, saving the27 returned value into a local stack variable. */28 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();293031 /* Execute the code that requires the critical section here. */323334 /* Calls to taskENTER_CRITICAL_FROM_ISR() can be nested so it is safe to call a35 function that includes its own calls to taskENTER_CRITICAL_FROM_ISR() and36 taskEXIT_CRITICAL_FROM_ISR(). */37 vDemoFunction();3839 /* The operation that required the critical section is complete so exit the40 critical section. Assuming interrupts were enabled on entry to this ISR, the value41 saved in uxSavedInterruptStatus will result in interrupts being re-enabled.*/42 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );43}