Search code examples
sdkesp8266esp32rtos

RTOS SDK Where is app_main defined as startup function?


Using rtos SDK i was able to develop and run successfully some simple examples, but I need to understand.

Normally a c / c++ program starts with main(...) (I don't remember the exact signature)

RTOS projects seem to start almost all with app_main() and some examples online with user_init()

A text search throughout all sources did not help me. It seems that there is a startup.c that in turn calls app_main but this does not explain why some other examples (https://github.com/espressif/esp8266-rtos-sample-code/blob/master/03Wifi/Soft_AP_DEMO/user/user_main.c that I did not try) have another entrypoint.

Can somebody explain how it is structured? "Who" is calling app_main?


Solution

  • The ESP32 ESP-IDF SDK startup procedure is fairly thoroughly described in Application Startup Flow - Application startup. ESP8266 RTOS SDK startup is similar.

    ESP-IDF application entry point is call_start_cpu0 function found in components/esp_system/port/cpu_start.c. This function is executed by the second stage bootloader, and never returns.

    . . . <skip> . . .

    Once call_start_cpu0 completes running, it calls the “system layer” initialization function start_cpu0 found in components/esp_system/startup.c. Other cores will also complete port-layer initialization and call start_other_cores found in the same file.

    . . . <skip> . . .

    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 initialization steps.

    . . . <skip> . . .

    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 last part has been refactored recently. Here's a link to the app_main call in an older IDF-SDK v4.2.