Article Directory
Preface
This article analyzes the STM32F103 startup file
1. STM32F103 system startup steps
The startup file is startup_stm32f10x_hd.s, which is written by assembly files and is the first program executed after the system is powered on.
The process is:
1. Set the heap and stack size, set the vector table
2, initialize the stack pointer
3, initialize the PC pointer = Rest_Handler
4, initialize the interrupt vector table
5, configure the system clock
6, call the C library function _main to initialize the user stack, thus Implement the main function
2. Start-up file code analysis
Stack stack size setting
Stack_Size EQU 0x00000400
AREA STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem SPACE Stack_Size
__initial_sp
1. Set the stack size to 0x00000400 (1M size)
2. The function of the stack: save local variables, function calls, function parameters and other overheads. The size of the stack cannot exceed the internal SRAM.
3. AREA: Tell the assembler that it is a code segment or a data segment. STACK represents the stack name. It can be named arbitrarily; NOINIT means no initialization; READWRITE means readable and writable; ALIGN=3 alignment 2^3 power alignment, that is, 8-byte alignment, SPACE is used to allocate a certain size of memory, and the specified here is Stack_Size
4 , __Initial_sp, after SPACE, represents the end address of the stack, that is, the address of the top of the stack, the stack grows from high to low
Heap
Heap_Size EQU 0x00000200
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
PRESERVE8
THUMB
1. Heap_Size is set to 0x00000200 (512 bytes)
2. __heap_base represents the start address of the heap, and __heap_limit represents the end address.
3. The heap grows from low to high.
4. The heap is used to dynamically allocate memory. The memory allocated by a series of malloc functions is on the heap.
5, PRESERVE8: indicates that the stack is aligned according to 8 bytes
6, THUMB: indicates that the thumb instruction set is used
Vector table
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
Define a data segment named RESET, which is only readable. And EXPORT declares __Vectors, __Vectors_End, __Vectors_Size three attributes for external use.
Interrupt vector
When the kernel responds to an exception, the corresponding exception service will be executed. The kernel uses the interrupt vector table to indicate the entry address.
__Vectors DCD __initial_sp ; Top of Stack 申请的栈顶,__Vectors对外的向量起始地址
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
; External Interrupts
DCD WWDG_IRQHandler ; Window Watchdog
DCD PVD_IRQHandler ; PVD through EXTI Line detect
DCD TAMPER_IRQHandler ; Tamper
__Vectors_End
__Vectors_Size EQU __Vectors_End - __Vectors
The vector table has system interrupts and peripheral interrupts. There are many vectors, and many are omitted here.
__Vectors is the start address of the vector, __Vectors_End is the end address, and __Vectors_Size is the size of the stack. The vector table is stored starting from address 0 of FLASH, with 4 bytes as a unit. The interrupt table stores the names of interrupt functions.
Code snippet
AREA |.text|, CODE, READONLY
Define a code segment, readable
Reset program, entry function
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT __main
IMPORT SystemInit
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP
The reset program is the first program executed. It initializes the system clock and jumps to the main function. __main is a standard C function library, which is mainly used to initialize the user stack and jump to the world of main C language.
IF :DEF:__MICROLIB
EXPORT __initial_sp
EXPORT __heap_base
EXPORT __heap_limit
ELSE
IMPORT __use_two_region_memory
EXPORT __user_initial_stackheap
__user_initial_stackheap;用户初始化堆栈
LDR R0, = Heap_Mem
LDR R1, =(Stack_Mem + Stack_Size)#计算栈大小
LDR R2, = (Heap_Mem + Heap_Size)#计算堆大小
LDR R3, = Stack_Mem
BX LR
ALIGN
ENDIF
END