Files CCore/src/libc/__std_init.cpp
Three functions __std_init(), __std_main() and __std_exit() are called by the startup code to drive the application. They are implemented in the target file __std_init.cpp.
using namespace CCore; /* init/exit */ void __std_init(void) { Dev::Init_CPU(); Init_SpecialMem(); // constructors are called here __std_init_t *ptr=__std_get_init_base(); __std_init_t *lim=__std_get_init_lim(); for(; ptr<lim ;ptr++) { if( __std_init_t func=*ptr ) func(); } } extern void before_main(); void __std_main(void) { Task::Internal::Enable(); try { before_main(); } catch(...) { Abort("Fatal error : exception from before_main()"); } Task::Internal::Disable(); } void __std_exit(void) { Exit_atexit(); // call atexit functions, destructors are called here Exit_SpecialMem(); Dev::Exit_CPU(); }
__std_init() performs initializations. The first step is the basic CPU initialization, the function Dev::Init_CPU() is responsible for it. For example, on BeagleBoneBlack this function setups CPU clock and core clocks, then initialize the memory address translation table and setup MMU and some other CPU control registers for proper operation.
The next urgent step is the special heap initialization by the function Init_SpecialMem(). The interrupt heap must be ready ASAP for the proper C++ code execution.
Finally, constructors of global objects are called.
__std_main() is the first main-function. It enables XCore and calls main() or before_main() functions to run the main code. You may introduce in your target the function before_main() to do additional HW and SW initialization. Between Task::Internal::Enable() and Task::Internal::Disable() XCore is fully operational and console is switched to the packet mode. New tasks can be spawned here. Task::Internal::Enable() spawns special tasks and switches the console to the packet mode. Task::Internal::Disable() waits for extra task completion and stops the special tasks, console is switched back from the packet mode to the polling mode.
__std_exit() calls Exit_atexit() first, it calls global object destructors also. Then Exit_SpecialMem() and Dev::Exit_CPU() to shutdown the system.