Chapter 4. API Guides
• Wi-Fi wpa_supplicant component may create dedicated tasks while the Wi-Fi Protected Setup (WPS), WPA2
EAP-TLS, Device Provisioning Protocol (DPP) or BSS Transition Management (BTM) features are in use.
These tasks all have low priority (2).
• The Ethernet driver creates a task for the MAC to receive Ethernet frames. If using the default config
ETH_MAC_DEFAULT_CONFIG then the priority is medium-high (15). This setting can be changed by pass-
ing a custom eth_mac_config_t struct when initializing the Ethernet MAC.
• If using the mDNS component, it creates a task with default low priority 1 (configurable.
• If using the MQTT component, it creates a task with default priority 5 (configurable, depends
on CONFIG_MQTT_USE_CUSTOM_CONFIG (also configurable runtime by task_prio field in the
esp_mqtt_client_config_t)
Choosing application task priorities In general, it’s not recommended to set task priorities higher than the built-
in Wi-Fi operations as starving them of CPU may make the system unstable. For very short timing-critical operations
that don’t use the network, use an ISR or a very restricted task (very short bursts of runtime only) at highest priority
(24). Choosing priority 19 will allow lower layer Wi-Fi functionality to run without delays, but still preempts the
lwIP TCP/IP stack and other less time-critical internal functionality - this is the best option for time-critical tasks that
don’t perform network operations. Any task that does TCP/IP network operations should run at lower priority than
the lwIP TCP/IP task (18) to avoid priority inversion issues.
Note: Task execution is always completely suspended when writing to the built-in SPI flash chip. Only IRAM-Safe
Interrupt Handlers will continue executing.
Improving Interrupt Performance ESP-IDF supports dynamic Interrupt allocation with interrupt preemption.
Each interrupt in the system has a priority, and higher priority interrupts will preempt lower priority ones.
Interrupt handlers will execute in preference to any task (provided the task is not inside a critical section). For this
reason, it’s important to minimize the amount of time spent executing in an interrupt handler.
To obtain the best performance for a particular interrupt handler:
• Assign more important interrupts a higher priority using a flag such as ESP_INTR_FLAG_LEVEL2 or
ESP_INTR_FLAG_LEVEL3 when calling esp_intr_alloc().
• If you’re sure the entire interrupt handler can run from IRAM (see IRAM-Safe Interrupt Handlers) then set the
ESP_INTR_FLAG_IRAM flag when calling esp_intr_alloc() to assign the interrupt. This prevents it
being temporarily disabled if the application firmware writes to the internal SPI flash.
• Even if the interrupt handler is not IRAM safe, if it is going to be executed frequently then consider moving the
handler function to IRAM anyhow. This minimizes the chance of a flash cache miss when the interrupt code is
executed (see Targeted Optimizations). It’s possible to do this without adding the ESP_INTR_FLAG_IRAM
flag to mark the interrupt as IRAM-safe, if only part of the handler is guaranteed to be in IRAM.
Improving Network Speed
• For Wi-Fi, see How to improve Wi-Fi performance and Wi-Fi Buffer Usage
• For lwIP TCP/IP (Wi-Fi and Ethernet), see Performance Optimization
• The wifi/iperf example contains a configuration that is heavily optimized for Wi-Fi TCP/IP through-
put. Append the contents of the files wifi/iperf/sdkconfig.defaults, wifi/iperf/sdkconfig.defaults.esp32s2 and
wifi/iperf/sdkconfig.ci.99 to your project sdkconfig file in order to add all of these options. Note that some
of these options may have trade-offs in terms of reduced debuggability, increased firmware size, increased
memory usage, or reduced performance of other features. To get the best result, read the documentation pages
linked above and use this information to determine exactly which options are best suited for your app.
Minimizing Binary Size
The ESP-IDF build system compiles all source files in the project and ESP-IDF, but only functions and variables that
are actually referenced by the program are linked into the final binary. In some cases, it is necessary to reduce the
total size of the firmware binary (for example, in order to fit it into the available flash partition size).
Espressif Systems 1443
Submit Document Feedback
Release v4.4