When I was knee-high to Kernigan and Ritchie.......

wevets
Posts: 112
Joined: Sat Mar 09, 2019 2:56 am

When I was knee-high to Kernigan and Ritchie.......

Postby wevets » Sat Aug 24, 2019 4:25 pm

When I first started coding decades ago, every program in C began with main(). Having gotten back in after decades off and working in the ESP IDF, all applications begin with app_main().
Is there a main() that calls app_main(), and if not, what calls app_main()? What does this caller do? I've grepped for "main()" in the IDF and was surprised at how many results there were. Which is the main(), if that is the first routine called that calls app_main(), and what does it do? I'm guessing it starts the scheduler as I see "Scheduler started" for both CPUs in my log traces. Is app_main just another task within FreeRTOS?

Is there anywhere where all this pre-application run-time setup is explained?

Thanks for any enlightenment.

vonnieda
Posts: 145
Joined: Tue Nov 07, 2017 3:42 pm

Re: When I was knee-high to Kernigan and Ritchie.......

Postby vonnieda » Sat Aug 24, 2019 6:05 pm

https://docs.espressif.com/projects/esp ... notes.html covers the boot process in pretty good detail.

Jason

wevets
Posts: 112
Joined: Sat Mar 09, 2019 2:56 am

Re: When I was knee-high to Kernigan and Ritchie.......

Postby wevets » Sat Aug 24, 2019 8:44 pm

Jason,

Thank you. Your link put me on exactly the right path. Do you know off the top if all tasks created in app_main() will continue to run if app_main() returns? This was implied but not explicitly stated at the link you steered me to.

vonnieda
Posts: 145
Joined: Tue Nov 07, 2017 3:42 pm

Re: When I was knee-high to Kernigan and Ritchie.......

Postby vonnieda » Sun Aug 25, 2019 5:57 pm

wevets wrote:
Sat Aug 24, 2019 8:44 pm
Jason,

Thank you. Your link put me on exactly the right path. Do you know off the top if all tasks created in app_main() will continue to run if app_main() returns? This was implied but not explicitly stated at the link you steered me to.
Yep, they continue to run. My app, for instance, starts several tasks in app_main() and then returns.

In general, any FreeRTOS task that is started is expected to run forever. Returning is normally "bad", but the ESP initialization code handles a return from app_main() correctly by calling vTaskDelete on it. If you have tasks that need to exit, make sure to call vTaskDelete on them to free memory allocated when you xTaskCreate.

Jason

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: When I was knee-high to Kernigan and Ritchie.......

Postby ESP_Angus » Mon Aug 26, 2019 12:23 am

Thanks Jason for the great explanation.

The decision to name the "main" routine "app_main" is because it doesn't behave the same as C standard main() - it runs in an RTOS task, and it is safe to return from. Will add a specific section about these differences on the "general notes" page that Jason linked to.

Who is online

Users browsing this forum: Bing [Bot], dzungpv, ESP_Roland, Google [Bot] and 100 guests