Home | Projects | Notes > Bootloader > Reset Sequence
When you reset the processor (e.g., press the reset button on the boad), the PC is loaded with the value 0x0000_0000. (Memory location 0x0000_0000 belongs to a Flash memory, or ROM memory, or Program memory.)
Then the processor reads the value at the memory location 0x0000_0000 into the MSP.
MSP = value at 0x0000_0000
MSP is the Main Stack Pointer register. This means that the processor, upon reset, first initializes the Stack Pointer.
Then the processor reads the value at the memory location 0x000_0004 into PC. That value is actually the address of the reset handler. (In other words, the address of the reset handler gets stored into PC.)
PC jumps to the reset handler.
A reset handler is just a C or assembly function written by you to carry out any initialization required.
From the reset handler, main()
function of the application gets called.
This is how the control reaches your program's main()
function after reset.
All ARM Cortex-M based MCUs, upon reset, does
Load the value stored at 0x0000_0000 into MSP
Load the value stored at 0x000_0004 into PC (Value: the address of the reset handler)
In STM32 microcontroller,
MSP value is stored at 0x0800_0000
Vector table starts from 0x0800_0004
Address of the reset handler found at 0x0800_0004
So, there must be something that links 0x0800_0000 to 0x0000_0000?
[Answer] Both addresses can be linked with the technique called "memory aliasing" and it depends on the MCU.
The base address of the user Flash (i.e., 0x0800_0000; depends on the MCU) is mapped to 0x0000_0000 via memory aliasing. So, when the processor references the memory location 0x0000_0000, the address is converted into 0x0800_0000 and the processor ends up reading 0x0800_0000.
This can be verified by comparing the contents of the both of the memory regions using the memory browser on your IDE. They will show the same contents.
Reset handler does the early initialization after which the call to the user program (main()
) is made:
Processor reset
__libc_init_array();
)
main()
)
Remember! The main()
function cannot be called without these important early initializations.
xxxxxxxxxx
431/* a section of startup file (startup_stm32f407vgtx.s) */
2
3Reset_Handler:
4 ldr r0, =_estack
5 mov sp, r0 /* set stack pointer */
6/* Call the clock system initialization function.*/
7 bl SystemInit
8
9/* Copy the data segment initializers from flash to SRAM */
10 ldr r0, =_sdata
11 ldr r1, =_edata
12 ldr r2, =_sidata
13 movs r3, #0
14 b LoopCopyDataInit
15
16CopyDataInit:
17 ldr r4, [r2, r3]
18 str r4, [r0, r3]
19 adds r3, r3, #4
20
21LoopCopyDataInit:
22 adds r4, r0, r3
23 cmp r4, r1
24 bcc CopyDataInit
25
26/* Zero fill the bss segment. */
27 ldr r2, =_sbss
28 ldr r4, =_ebss
29 movs r3, #0
30 b LoopFillZerobss
31
32FillZerobss:
33 str r3, [r2]
34 adds r2, r2, #4
35
36LoopFillZerobss:
37 cmp r2, r4
38 bcc FillZerobss
39
40/* Call static constructors */
41 bl __libc_init_array
42/* Call the application's entry point.*/
43 bl main