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

No alt text provided for this image


Vector table of ARM Cortex_M4

vector table contain all the locations of the exceptions and interrupts "ISR"

No alt text provided for this image


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. 

No alt text provided for this image


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 ,


Osama Mahmoud

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)

回复

要查看或添加评论,请登录

Eslam Shahin的更多文章

  • Interrupt Handling Techniques

    Interrupt Handling Techniques

    Hello all Today we will talk about Interrupt Handling techniques , 1 . Frist type is The Fixed Priority Interrupt…

    1 条评论
  • USB protocol

    USB protocol

    Introduction Universal Serial Bus (USB) is a widely-used protocol in most devices Its a industry standard way to…

  • STM32 Start-up code explore

    STM32 Start-up code explore

    If you an Embedded Software engineer, you should know what is start-up code and what it’s job It’s a .c or .

    2 条评论

社区洞察

其他会员也浏览了