STM32启动文件分析

时间:2022-09-20 16:42:23
 1 ;先在RAM中分配系统使用的栈,RAM的起始地址为0x2000_0000
2 ;然后在RAM中分配变量使用的堆
3 ;然后在CODE区(flash)分配中断向量表,flash的起始地址为0x0800_0000,该中断向量表就从这个起始地址开始分配
4 ;分配完成后,再定义和实现相应的中断函数,
5 ;所有的中断函数全部带有[weak]特性,即弱定义,如果编译器发现在别处文件中定义了同名函数,在链接时用别处的地址进行链接。
6 ;中断函数仅仅实现了Reset_Handler,其他要么是死循环,要么仅仅定义了函数名称
7
8 ;STM32被设置为从内部FLASH启动时(这也是最常见的一种情况),当STM32遇到复位信号后,
9 ;从0x0800_0000处取出栈顶地址存放于MSP寄存器,从0x0800_0004处取出复位中断服务入口地址放入PC寄存器,
10 ;继而执行复位中断服务程序Reset_Handler,
11 ;Reset_Handler仅仅执行了两个函数调用,一个是SystemInit,另一个__main,
12 ;SystemInit定义在system_stm32f10x.c中,主要初始化了STM的时钟系统:HSI,HSE,LSI,LSE,PLL,SYSCLK,USBCLK,APECLK等等.
13 ;__main函数由编译器生成,负责初始化栈、堆等,并在最后跳转到用户自定义的main()函数,来到C的世界。
14
15
16 Stack_Size EQU 0x00000400 ;//定义堆栈大小
17 AREA STACK, NOINIT, READWRITE, ALIGN=3 ;//定义一个数据段 按8字节对齐 ;AREA 伪指令用于定义一个代码段或数据段 NOINIT:指定此数据段仅仅保留了内存单元,而没有将各初始值写入内存单元,或者将各个内存单元值初始化为0
18 Stack_Mem SPACE Stack_Size ;//保留Stack_Size大小的堆栈空间 分 配连续 Stack_Size 字节的存储单元并初始化为 0
19 __initial_sp ;//标号,代表堆栈顶部地址,后面有用
20
21 ; <h> Heap Configuration
22 ; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
23 ; </h>
24 Heap_Size EQU 0x00000200 ;//定义堆空间大小
25 AREA HEAP, NOINIT, READWRITE, ALIGN=3 ;//定义一个数据段,8字节对齐
26 __heap_base ;//标号,代表堆末底部地址,后面有用
27 Heap_Mem SPACE Heap_Size ;//保留Heap_Size的堆空间
28 __heap_limit ;//标号,代表堆界限地址,后面有用
29 ;PRESERVE8 指令指定当前文件保持堆栈八字节对齐。 它设置 PRES8 编译属性以通知链接器。
30 ;链接器检查要求堆栈八字节对齐的任何代码是否仅由保持堆栈八字节对齐的代码直接或间接地调用。
31 PRESERVE8 ;//指示编译器8字节对齐
32 THUMB ;//指示编译器以后的指令为THUMB指令
33
34 ;中断向量表定义
35 ; Vector Table Mapped to Address 0 at Reset
36 AREA RESET, DATA, READONLY ;//定义只读数据段,其实放在CODE区,位于0地址
37 EXPORT __Vectors ;EXPORT:在程序中声明一个全局的标号__Vectors,该标号可在其他的文件中引用
38 EXPORT __Vectors_End
39 EXPORT __Vectors_Size
40 __Vectors DCD __initial_sp ; Top of Stack ;给__initial_sp分配4字节32位的地址0x0
41 DCD Reset_Handler ; Reset Handler ; 给标号Reset Handler分配地址为0x00000004
42 DCD NMI_Handler ; NMI Handler ; 给标号NMI Handler分配地址0x00000008
43 DCD HardFault_Handler ; Hard Fault Handler
44 DCD MemManage_Handler ; MPU Fault Handler
45 DCD BusFault_Handler ; Bus Fault Handler
46 DCD UsageFault_Handler ; Usage Fault Handler
47 DCD 0 ; Reserved ; 这种形式就是保留地址,不给任何标号分配
48 DCD 0 ; Reserved
49 DCD 0 ; Reserved
50 DCD 0 ; Reserved
51 DCD SVC_Handler ; SVCall Handler
52 DCD DebugMon_Handler ; Debug Monitor Handler
53 DCD 0 ; Reserved
54 DCD PendSV_Handler ; PendSV Handler
55 DCD SysTick_Handler ; SysTick Handler
56 ; External Interrupts
57 DCD WWDG_IRQHandler ; Window Watchdog
58 DCD PVD_IRQHandler ; PVD through EXTI Line detect
59 DCD TAMPER_IRQHandler ; Tamper
60 DCD RTC_IRQHandler ; RTC
61 DCD FLASH_IRQHandler ; Flash
62 DCD RCC_IRQHandler ; RCC
63 DCD EXTI0_IRQHandler ; EXTI Line 0
64 DCD EXTI1_IRQHandler ; EXTI Line 1
65 DCD EXTI2_IRQHandler ; EXTI Line 2
66 DCD EXTI3_IRQHandler ; EXTI Line 3
67 DCD EXTI4_IRQHandler ; EXTI Line 4
68 DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
69 DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
70 DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
71 DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
72 DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
73 DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
74 DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
75 DCD ADC1_2_IRQHandler ; ADC1 & ADC2
76 DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX
77 DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0
78 DCD CAN1_RX1_IRQHandler ; CAN1 RX1
79 DCD CAN1_SCE_IRQHandler ; CAN1 SCE
80 DCD EXTI9_5_IRQHandler ; EXTI Line 9..5
81 DCD TIM1_BRK_IRQHandler ; TIM1 Break
82 DCD TIM1_UP_IRQHandler ; TIM1 Update
83 DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
84 DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
85 DCD TIM2_IRQHandler ; TIM2
86 DCD TIM3_IRQHandler ; TIM3
87 DCD TIM4_IRQHandler ; TIM4
88 DCD I2C1_EV_IRQHandler ; I2C1 Event
89 DCD I2C1_ER_IRQHandler ; I2C1 Error
90 DCD I2C2_EV_IRQHandler ; I2C2 Event
91 DCD I2C2_ER_IRQHandler ; I2C2 Error
92 DCD SPI1_IRQHandler ; SPI1
93 DCD SPI2_IRQHandler ; SPI2
94 DCD USART1_IRQHandler ; USART1
95 DCD USART2_IRQHandler ; USART2
96 DCD USART3_IRQHandler ; USART3
97 DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
98 DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line
99 DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend
100 DCD TIM8_BRK_IRQHandler ; TIM8 Break
101 DCD TIM8_UP_IRQHandler ; TIM8 Update
102 DCD TIM8_TRG_COM_IRQHandler ; TIM8 Trigger and Commutation
103 DCD TIM8_CC_IRQHandler ; TIM8 Capture Compare
104 DCD ADC3_IRQHandler ; ADC3
105 DCD FSMC_IRQHandler ; FSMC
106 DCD SDIO_IRQHandler ; SDIO
107 DCD TIM5_IRQHandler ; TIM5
108 DCD SPI3_IRQHandler ; SPI3
109 DCD UART4_IRQHandler ; UART4
110 DCD UART5_IRQHandler ; UART5
111 DCD TIM6_IRQHandler ; TIM6
112 DCD TIM7_IRQHandler ; TIM7
113 DCD DMA2_Channel1_IRQHandler ; DMA2 Channel1
114 DCD DMA2_Channel2_IRQHandler ; DMA2 Channel2
115 DCD DMA2_Channel3_IRQHandler ; DMA2 Channel3
116 DCD DMA2_Channel4_5_IRQHandler ; DMA2 Channel4 & Channel5
117 __Vectors_End
118 __Vectors_Size EQU __Vectors_End - __Vectors
119 AREA |.text|, CODE, READONLY ;代码段定义
120
121 ; Reset Handler
122 ;利用PROC、ENDP这一对伪指令把程序段分为若干个过程,使程序的结构加清晰
123 Reset_Handler PROC ;过程的开始
124 EXPORT Reset_Handler [WEAK];[WEAK] 弱定义,意思是如果在别处也定义该标号(函数),在链接时用别处的地址。
125 ;如果没有其它定方定义,编译器也不报错,以此处地址进行链接。
126 ;EXPORT提示编译器该标号可以为外部文件引用。
127 IMPORT __main ;通知编译器要使用的标号在其他文件
128 IMPORT SystemInit
129 LDR R0, =SystemInit
130 BLX R0 ;BX是ARM指令集和THUMB指令集之间程序的跳转
131 LDR R0, =__main ;使用“=”表示LDR目前是伪指令不是标准指令。这里是把__main的地址给RO。
132 BX R0 ;BX是ARM指令集和THUMB指令集之间程序的跳转
133 ENDP ;过程的结束
134
135 ; Dummy Exception Handlers (infinite loops which can be modified)
136 NMI_Handler PROC
137 EXPORT NMI_Handler [WEAK]
138 B . ;原地跳转(即无限循环),
139 ENDP
140 HardFault_Handler\
141 PROC
142 EXPORT HardFault_Handler [WEAK]
143 B .
144 ENDP
145 MemManage_Handler\
146 PROC
147 EXPORT MemManage_Handler [WEAK]
148 B .
149 ENDP
150 BusFault_Handler\
151 PROC
152 EXPORT BusFault_Handler [WEAK]
153 B .
154 ENDP
155 UsageFault_Handler\
156 PROC
157 EXPORT UsageFault_Handler [WEAK]
158 B .
159 ENDP
160 SVC_Handler PROC
161 EXPORT SVC_Handler [WEAK]
162 B .
163 ENDP
164 DebugMon_Handler\
165 PROC
166 EXPORT DebugMon_Handler [WEAK]
167 B .
168 ENDP
169 PendSV_Handler PROC
170 EXPORT PendSV_Handler [WEAK]
171 B .
172 ENDP
173 SysTick_Handler PROC
174 EXPORT SysTick_Handler [WEAK]
175 B .
176 ENDP
177 Default_Handler PROC
178 EXPORT WWDG_IRQHandler [WEAK]
179 EXPORT PVD_IRQHandler [WEAK]
180 EXPORT TAMPER_IRQHandler [WEAK]
181 EXPORT RTC_IRQHandler [WEAK]
182 EXPORT FLASH_IRQHandler [WEAK]
183 EXPORT RCC_IRQHandler [WEAK]
184 EXPORT EXTI0_IRQHandler [WEAK]
185 EXPORT EXTI1_IRQHandler [WEAK]
186 EXPORT EXTI2_IRQHandler [WEAK]
187 EXPORT EXTI3_IRQHandler [WEAK]
188 EXPORT EXTI4_IRQHandler [WEAK]
189 EXPORT DMA1_Channel1_IRQHandler [WEAK]
190 EXPORT DMA1_Channel2_IRQHandler [WEAK]
191 EXPORT DMA1_Channel3_IRQHandler [WEAK]
192 EXPORT DMA1_Channel4_IRQHandler [WEAK]
193 EXPORT DMA1_Channel5_IRQHandler [WEAK]
194 EXPORT DMA1_Channel6_IRQHandler [WEAK]
195 EXPORT DMA1_Channel7_IRQHandler [WEAK]
196 EXPORT ADC1_2_IRQHandler [WEAK]
197 EXPORT USB_HP_CAN1_TX_IRQHandler [WEAK]
198 EXPORT USB_LP_CAN1_RX0_IRQHandler [WEAK]
199 EXPORT CAN1_RX1_IRQHandler [WEAK]
200 EXPORT CAN1_SCE_IRQHandler [WEAK]
201 EXPORT EXTI9_5_IRQHandler [WEAK]
202 EXPORT TIM1_BRK_IRQHandler [WEAK]
203 EXPORT TIM1_UP_IRQHandler [WEAK]
204 EXPORT TIM1_TRG_COM_IRQHandler [WEAK]
205 EXPORT TIM1_CC_IRQHandler [WEAK]
206 EXPORT TIM2_IRQHandler [WEAK]
207 EXPORT TIM3_IRQHandler [WEAK]
208 EXPORT TIM4_IRQHandler [WEAK]
209 EXPORT I2C1_EV_IRQHandler [WEAK]
210 EXPORT I2C1_ER_IRQHandler [WEAK]
211 EXPORT I2C2_EV_IRQHandler [WEAK]
212 EXPORT I2C2_ER_IRQHandler [WEAK]
213 EXPORT SPI1_IRQHandler [WEAK]
214 EXPORT SPI2_IRQHandler [WEAK]
215 EXPORT USART1_IRQHandler [WEAK]
216 EXPORT USART2_IRQHandler [WEAK]
217 EXPORT USART3_IRQHandler [WEAK]
218 EXPORT EXTI15_10_IRQHandler [WEAK]
219 EXPORT RTCAlarm_IRQHandler [WEAK]
220 EXPORT USBWakeUp_IRQHandler [WEAK]
221 EXPORT TIM8_BRK_IRQHandler [WEAK]
222 EXPORT TIM8_UP_IRQHandler [WEAK]
223 EXPORT TIM8_TRG_COM_IRQHandler [WEAK]
224 EXPORT TIM8_CC_IRQHandler [WEAK]
225 EXPORT ADC3_IRQHandler [WEAK]
226 EXPORT FSMC_IRQHandler [WEAK]
227 EXPORT SDIO_IRQHandler [WEAK]
228 EXPORT TIM5_IRQHandler [WEAK]
229 EXPORT SPI3_IRQHandler [WEAK]
230 EXPORT UART4_IRQHandler [WEAK]
231 EXPORT UART5_IRQHandler [WEAK]
232 EXPORT TIM6_IRQHandler [WEAK]
233 EXPORT TIM7_IRQHandler [WEAK]
234 EXPORT DMA2_Channel1_IRQHandler [WEAK]
235 EXPORT DMA2_Channel2_IRQHandler [WEAK]
236 EXPORT DMA2_Channel3_IRQHandler [WEAK]
237 EXPORT DMA2_Channel4_5_IRQHandler [WEAK]
238 WWDG_IRQHandler
239 PVD_IRQHandler
240 TAMPER_IRQHandler
241 RTC_IRQHandler
242 FLASH_IRQHandler
243 RCC_IRQHandler
244 EXTI0_IRQHandler
245 EXTI1_IRQHandler
246 EXTI2_IRQHandler
247 EXTI3_IRQHandler
248 EXTI4_IRQHandler
249 DMA1_Channel1_IRQHandler
250 DMA1_Channel2_IRQHandler
251 DMA1_Channel3_IRQHandler
252 DMA1_Channel4_IRQHandler
253 DMA1_Channel5_IRQHandler
254 DMA1_Channel6_IRQHandler
255 DMA1_Channel7_IRQHandler
256 ADC1_2_IRQHandler
257 USB_HP_CAN1_TX_IRQHandler
258 USB_LP_CAN1_RX0_IRQHandler
259 CAN1_RX1_IRQHandler
260 CAN1_SCE_IRQHandler
261 EXTI9_5_IRQHandler
262 TIM1_BRK_IRQHandler
263 TIM1_UP_IRQHandler
264 TIM1_TRG_COM_IRQHandler
265 TIM1_CC_IRQHandler
266 TIM2_IRQHandler
267 TIM3_IRQHandler
268 TIM4_IRQHandler
269 I2C1_EV_IRQHandler
270 I2C1_ER_IRQHandler
271 I2C2_EV_IRQHandler
272 I2C2_ER_IRQHandler
273 SPI1_IRQHandler
274 SPI2_IRQHandler
275 USART1_IRQHandler
276 USART2_IRQHandler
277 USART3_IRQHandler
278 EXTI15_10_IRQHandler
279 RTCAlarm_IRQHandler
280 USBWakeUp_IRQHandler
281 TIM8_BRK_IRQHandler
282 TIM8_UP_IRQHandler
283 TIM8_TRG_COM_IRQHandler
284 TIM8_CC_IRQHandler
285 ADC3_IRQHandler
286 FSMC_IRQHandler
287 SDIO_IRQHandler
288 TIM5_IRQHandler
289 SPI3_IRQHandler
290 UART4_IRQHandler
291 UART5_IRQHandler
292 TIM6_IRQHandler
293 TIM7_IRQHandler
294 DMA2_Channel1_IRQHandler
295 DMA2_Channel2_IRQHandler
296 DMA2_Channel3_IRQHandler
297 DMA2_Channel4_5_IRQHandler
298 B .
299 ENDP
300 ALIGN ;填充字节使地址对齐
301 ;*******************************************************************************
302 ; User Stack and Heap initialization
303 ;*******************************************************************************
304 ;堆和栈的初始化
305 IF :DEF:__MICROLIB ;“DEF”的用法——:DEF:X 就是说X定义了则为真,否则为假
306 ;如果定义了MICORLIB,
307 EXPORT __initial_sp ;则将栈顶地址,
308 EXPORT __heap_base ;堆起始地址赋予全局属性,
309 EXPORT __heap_limit ;堆末端界限地址赋予全局属性,使外部程序可调用
310
311 ELSE ;如果没定义__MICROLIB,则使用默认的C运行时库
312
313 IMPORT __use_two_region_memory ;;通知编译器要使用的标号在其他文件__use_two_region_memory
314 EXPORT __user_initial_stackheap ;声明全局标号__user_initial_stackheap,这样外程序也可调用此标号
315 ;则进行堆栈和堆的赋值,在__main函数执行过程中调用
316
317 ;如果使用默认的C库,程序启动过程中就不会执行该标号下的代码
318 __user_initial_stackheap ;标号__user_initial_stackheap,表示用户堆栈初始化程序入口
319 ;//则进行堆栈和堆的赋值,在__main函数执行过程中调用。
320 LDR R0, = Heap_Mem ;保存堆始地址
321 LDR R1, =(Stack_Mem + Stack_Size) ;保存栈的大小
322 LDR R2, = (Heap_Mem + Heap_Size) ;保存堆的大小
323 LDR R3, = Stack_Mem ;保存栈顶指针
324 BX LR
325 ALIGN ;填充字节使地址对齐
326
327 ENDIF
328
329 END