Chapter 4. API Guides
Disabling Interrupts
Vanilla FreeRTOS allows interrupts to be disabled and enabled by calling taskDISABLE_INTERRUPTS and
taskENABLE_INTERRUPTS respectively.
ESP-IDF FreeRTOS provides the same API, however interrupts will only disabled or enabled on the current core.
Warning: Disabling interrupts is a valid method of achieve mutual exclusion in Vanilla FreeRTOS (and single
core systems in general). However, in an SMP system, disabling interrupts is NOT a valid method ensuring
mutual exclusion. Refer to Critical Sections for more details.
Startup and Termination
ESP-IDF FreeRTOS does not require users to call vTaskStartScheduler() to start the scheduler. The startup
flow of an ESP-IDF application will already call this automatically. The entry point for user code is a user defined
void app_main(void) function. For more details regarding the startup of ESP-IDF FreeRTOS applications,
see ESP-IDF FreeRTOS Applications.
ESP-IDF FreeRTOS does not support scheduler termination. Calling vTaskEndScheduler() will simply cause
the application to abort.
4.14.5 Critical Sections
API Changes
Vanilla FreeRTOS implements critical sections by disabling interrupts, This prevents preemptive context switches
and the servicing of ISRs during a critical section. Thus a task/ISR that enters a critical section is guaranteed to be
the sole entity to access a shared resource. Critical sections in Vanilla FreeRTOS have the following API:
• taskENTER_CRITICAL() enters a critical section by disabling interrupts
• taskEXIT_CRITICAL() exits a critical section by reenabling interrupts
• taskENTER_CRITICAL_FROM_ISR() enters a critical section from an ISR by disabling interrupt nesting
• taskEXIT_CRITICAL_FROM_ISR() exits a critical section from an ISR by reenabling interrupt nesting
However, in an SMP system, merely disabling interrupts does not constitute a critical section as the presence of
other cores means that a shared resource can still be concurrently accessed. Therefore, critical sections in ESP-IDF
FreeRTOS are implemented using spinlocks. To accommodate the spinlocks, the ESP-IDF FreeRTOS critical section
APIs contain an additional spinlock parameter as shown below:
• Spinlocks are of portMUX_TYPE (not to be confused to FreeRTOS mutexes)
• taskENTER_CRITICAL(&mux) enters a critical from a task context
• taskEXIT_CRITICAL(&mux) exits a critical section from a task context
• taskENTER_CRITICAL_ISR(&mux) enters a critical section from an interrupt context
• taskEXIT_CRITICAL_ISR(&mux) exits a critical section from an interrupt context
Note: The critical section API can be called recursively (i.e., nested critical sections). Entering a critical section
multiple times recursively is valid so long as the critical section is exited the same number of times it was entered.
However, given that critical sections can target different spinlocks, users should take care to avoid dead locking when
entering critical sections recursively.
Implementation
In ESP-IDF FreeRTOS, the process of a particular core entering and exiting a critical section is as follows:
• For taskENTER_CRITICAL(&mux) (or taskENTER_CRITICAL_ISR(&mux))
Espressif Systems 1364
Submit Document Feedback
Release v4.4