Boot Sequence in ARM Cortex-M4
what happens when you press the reset button?
your main code does not start immediately as you think , lets know how .
Frist thing we should know the memory map of ARM Cortex-M4
we interested only in 3 regions
- code region
Place in which the machine code is burned .and it consist of ".v , .text , .rodata , .data" .program will be ordered in this way by using linker stage in compiler toolchain .
- SRAM
stack, heap, global RW variables, and Static variables
- Peripheral region
Vector table of ARM Cortex_M4
vector table contain all the locations of the exceptions and interrupts "ISR"
As in photo the first address contain the MSP (main stack pointer ) which store the initial stack pointer value
The second one is the reset handler
So now what happen exactly when you press the reset button ?
1- If the MCU contains an internal bootloader like STM32 , this Bootloader is come with the MCU that does not stored in your available code region
The hardware check any 2 pins that specified by the vendor to determine the boot mode , most times pins on your board with jumpers.
2 - Program Counter "PC" will be loaded with 0x00000000. Because the 0x00000000 address contain the initial stack pointer value , it will be loaded to the MSP register
3- PC will be loaded with Reset handler’s address.
The reset handler address is your code , but it isn't just your main function (or whatever you name it in the linker script )
It contains also the startup code that initialize the memory segments (Copy Initialized global , static variables , Un-initialized data SRAM )
Then call the main() function .
So actually you can imagine the reset handler code as
void ResetHandler(){
//Startup code ...
..................
...................
//your main function
main();
while(1);// some compilers add it for safety in case you forgot to add while 1 in you main function
}
Thanks ,
Embedded Software Engineer@VxLabs [Cybersecurity, penetration test, AUTOSAR, UDS, CAPL]
3 年why startup.c not startup.s ? is that depend on the processor (M4 different M3 or the same)