Chapter 4. API Guides
Note: Understanding all stages of ESP-IDF app initialization is often not necessary. To understand initialization
from the application developer’s perspective only, skip forward to Running the main task.
Port Initialization
ESP-IDF application entry point is call_start_cpu0 function found in compo-
nents/esp_system/port/cpu_start.c. This function is executed by the second stage bootloader, and never returns.
This port-layer initialization function initializes the basic C Runtime Environment (“CRT”) and performs initial
configuration of the SoC’s internal hardware:
• Reconfigure CPU exceptions for the app (allowing app interrupt handlers to run, and causing Fatal Errors to
be handled using the options configured for the app rather than the simpler error handler provided by ROM).
• If the option CONFIG_BOOTLOADER_WDT_ENABLE is not set then the RTC watchdog timer is disabled.
• Initialize internal memory (data & bss).
• Finish configuring the MMU cache.
• Enable PSRAM if configured.
• Set the CPU clocks to the frequencies configured for the project.
• Initialize memory protection if configured.
Once call_start_cpu0 completes running, it calls the “system layer”initialization function start_cpu0
found in components/esp_system/startup.c.
System Initialization
The main system initialization function is start_cpu0. By default, this function is weak-linked to the function
start_cpu0_default. This means that it’s possible to override this function to add some additional initial-
ization steps.
The primary system initialization stage includes:
• Log information about this application (project name, App version, etc.) if default log level enables this.
• Initialize the heap allocator (before this point all allocations must be static or on the stack).
• Initialize newlib component syscalls and time functions.
• Configure the brownout detector.
• Setup libc stdin, stdout, and stderr according to the serial console configuration.
• Perform any security-related checks, including burning efuses that should be burned for this configuration
(including permanently limiting ROM download modes).
• Initialize SPI flash API support.
• Call global C++ constructors and any C functions marked with __attribute__((constructor)).
Secondary system initialization allows individual components to be initialized. If a component has an initialization
function annotated with the ESP_SYSTEM_INIT_FN macro, it will be called as part of secondary initialization.
Running the main task
After all other components are initialized, the main task is created and the FreeRTOS scheduler starts running.
After doing some more initialization tasks (that require the scheduler to have started), the main task runs the
application-provided function app_main in the firmware.
The main task that runs app_main has a fixed RTOS priority (one higher than the minimum) and a configurable
stack size.
Unlike normal FreeRTOS tasks (or embedded C main functions), the app_main task is allowed to return. If this
happens, The task is cleaned up and the system will continue running with other RTOS tasks scheduled normally.
Therefore, it is possible to implement app_main as either a function that creates other application tasks and then
returns, or as a main application task itself.
Espressif Systems 1265
Submit Document Feedback
Release v4.4