STM32 Start-up code explore
Eslam Shahin
Embedded Software Engineer at SwiftAct || ARM || STM32 || AVR || PIC || IOT || FOTA || Bootloader || PCB Design
If you an Embedded Software engineer, you should know what is start-up code and what it’s job
It’s a .c or .s file that executed before main()
It responsible for
- copy the initialised data region(s)
- clear the BSS region(s)
- initialise the system
- run the preinit/init array (for the C++ static constructors)
- initialise the arc/argv
- branch to main()
- run the fini array (for the C++ static destructors)
- call _exit(), directly or via exit()
Frist, we start by assign the start and end addresses of the memory or its size from the linker files
Then declaration of main() function and exit() , actually you could change the entry point to any other name .
Then the declaration of functions for hardware Initialise hardware right after reset, to switch clock to higher frequency and have the rest of the initialisations run faster.
This is the place where Cortex-M core will go immediately after reset, via a call or jump from the Reset_Handler.
Inside _start() we call __initialize_hardware_early () then Call the CMSIS system initialisation routine SystemInit() that config the RCC with high frequency to boost the startup process
Then we need to initialize .data and .bss sections
Early we know that always the .bss is initialized by 0 , but we could easly change it in line #158
If you write in c++ the compiler will use these two functions for the C++ static constructors and static destructors
Now we need to restore the clock source to default
Then Get the argc/argv (useful in semihosting configurations)
After that we call __run_init_array () to execute the constructors for the static objects).
Now call your program to be executed and save the return in case you want to check on it
After exit from main Run the C++ static destructors.
Finally we want to stop the PC from increment to prevent to fetch old or bad instructions outside our actual program space in flash
And its implementation only have simple infinite loop
That was the startup code of STM32F1XX built with newlib
Hope it helps .
Lead CoC Engineer
3 年It's very important information about stm32. And even booting sequence also was a nice reading. Keep it up ??