STM32启动代码|STM32启动代码分析

STM32启动代码分析
复位序列
硬件复位之后,CPU内的时序逻辑电路首先完成如下两个工作(程序代码下载到内部flash)
1.将0x08000000位置存放的堆栈栈顶地址存放在SP中(MSP);
2.将0x08000004位置存放的向量地址装入PC程序计数器。
CPU从PC寄存器指向的物理地址取出第一条指令开始执行程序,也就是开始执行复位中断服务程序Reset_Handler; 复位中断服务程序会调用SystemInit()函数(C语言)来配置系统时钟,配置FSMC总线上的外部SRAM,然后跳转到C库中__main函数。由C库中的__main函数完成用户程序的初始化工作(比如:变量赋初值等),最后由__main函数调用用户写的main()函数开始执行C程序。

; * Version: V3.5.0

(EQU表示宏定义的伪指令,类似与#define;伪指令的意思是指这个“指令”并不会生成二进制程序代码,也不会引起变量空间分配)
(栈空间用于局部变量、函数调用、函数的参数等)
Stack_SizeEQU0x00000400; 定义栈大小,1Kb
(AREA命令指示汇编器汇编一个新的代码段或数据段)
AREASTACK,NOINIT,READWRITE, ALIGN=3
段的名字(任意)、未初始化、允许读写、8字节边界对齐


Stack_MemSPACEStack_Size; 分配栈空间,并把首地址赋给Stack_Mem
__initial_sp; 初始化堆栈指针,指向栈顶位置

(开辟堆空间,主要用于动态内存分配,也就是malloc、calloc、realloc等函数分配的变量空间是在堆上)
Heap_SizeEQU0x00000200; 定义堆的大小512Byte

AREAHEAP, NOINIT, READWRITE, ALIGN=3
; 堆段、未初始化、允许读写、堆数据段8字节边界对齐
__heap_base; 堆的首地址
Heap_MemSPACEHeap_Size; 分配堆空间
__heap_limit; 堆的尾地址

PRESERVE8; 命令指定当前文件保持堆栈的八字节对齐
THUMB; 指令集,THUMB必须位于使用新语法的任何Thumb代码之前

; EXPORT命令声明一个符号,,EXPORT语句声明为可被外部引用,主要提供链接器链接
; 库文件或其他文件
; 以下为向量表,在复位时被映射到FLASH的0地址
; Vector Table Mapped to Address 0 at Reset
AREARESET, DATA, READONLY; 复位段,只包含数据,只读
EXPORT__Vectors; 标号输出,中断向量表开始
EXPORT__Vectors_End; 中断向量表结束
EXPORT__Vectors_Size; 中断向量表大小

; DCD命令表示分配1个4字节的空间,每行DCD都会生成一个4字节的二进制代码,中
; 断向量表存放的实际上是中断服务程序的入口地址。中断向量表一般存放在Falsh的0地址
__VectorsDCD__initial_sp; Top of Stack
; 栈顶指针,被放在向量表的开始,FLASH的0地址,复位后首先装在栈顶指针
DCDReset_Handler; Reset Handler
; 复位异常,装载栈顶后,第一个执行的,并且不返回
DCDNMI_Handler; NMI Handler
DCDHardFault_Handler; Hard Fault Handler
DCDMemManage_Handler; MPU Fault Handler
DCDBusFault_Handler; Bus Fault Handler
; 总线错误中断,一般发生在数据访问异常,比如fsmc访问不当
DCDUsageFault_Handler; UsageFault Handler
; 用法错误中断,一般是预取值,或者位置指令,数据处理等错误
DCD0; Reserved
DCD0; Reserved
DCD0; Reserved
DCD0; Reserved
DCDSVC_Handler; SVCall Handler
; 系统调用异常,主要是为了调用操作系统内核服务(服务请求)
DCDDebugMon_Handler; Debug Monitor Handler
; 调试监视异常
DCD0; Reserved
DCDPendSV_Handler; PendSV Handler
; 挂起异常,此处可以看见用作了FreeRTOS的上下文切换异常
DCDSysTick_Handler; SysTick Handler
; 滴答定时器,操作系统内核时钟

; 以下为外部中断向量表
; External Interrupts
DCDWWDG_IRQHandler; Window Watchdog
DCDPVD_IRQHandler; PVD through EXTI Line detect
DCDTAMPER_IRQHandler; Tamper
DCDRTC_IRQHandler; RTC
DCDFLASH_IRQHandler; Flash
DCDRCC_IRQHandler; RCC
DCDEXTI0_IRQHandler; EXTI Line 0
DCDEXTI1_IRQHandler; EXTI Line 1
DCDEXTI2_IRQHandler; EXTI Line 2
DCDEXTI3_IRQHandler; EXTI Line 3
DCDEXTI4_IRQHandler; EXTI Line 4
DCDDMA1_Channel1_IRQHandler; DMA1 Channel 1
DCDDMA1_Channel2_IRQHandler; DMA1 Channel 2
DCDDMA1_Channel3_IRQHandler; DMA1 Channel 3
DCDDMA1_Channel4_IRQHandler; DMA1 Channel 4
DCDDMA1_Channel5_IRQHandler; DMA1 Channel 5
DCDDMA1_Channel6_IRQHandler; DMA1 Channel 6
DCDDMA1_Channel7_IRQHandler; DMA1 Channel 7
DCDADC1_2_IRQHandler; ADC1 & ADC2
DCDUSB_HP_CAN1_TX_IRQHandler; USB High Priority or CAN1 TX
DCDUSB_LP_CAN1_RX0_IRQHandler; USB LowPriority or CAN1 RX0
DCDCAN1_RX1_IRQHandler; CAN1 RX1
DCDCAN1_SCE_IRQHandler; CAN1 SCE
DCDEXTI9_5_IRQHandler; EXTI Line 9..5
DCDTIM1_BRK_IRQHandler; TIM1 Break
DCDTIM1_UP_IRQHandler; TIM1 Update
DCDTIM1_TRG_COM_IRQHandler; TIM1 Trigger and Commutation
DCDTIM1_CC_IRQHandler; TIM1 Capture Compare
DCDTIM2_IRQHandler; TIM2
DCDTIM3_IRQHandler; TIM3
DCDTIM4_IRQHandler; TIM4
DCDI2C1_EV_IRQHandler; I2C1 Event
DCDI2C1_ER_IRQHandler; I2C1 Error
DCDI2C2_EV_IRQHandler; I2C2 Event
DCDI2C2_ER_IRQHandler; I2C2 Error
DCDSPI1_IRQHandler; SPI1
DCDSPI2_IRQHandler; SPI2
DCDUSART1_IRQHandler; USART1
DCDUSART2_IRQHandler; USART2
DCDUSART3_IRQHandler; USART3
DCDEXTI15_10_IRQHandler; EXTI Line 15..10
DCDRTCAlarm_IRQHandler; RTC Alarm through EXTI Line
DCDUSBWakeUp_IRQHandler; USB Wakeup from suspend
DCDTIM8_BRK_IRQHandler; TIM8 Break
DCDTIM8_UP_IRQHandler; TIM8 Update
DCDTIM8_TRG_COM_IRQHandler; TIM8 Trigger and Commutation
DCDTIM8_CC_IRQHandler; TIM8 Capture Compare
DCDADC3_IRQHandler; ADC3
DCDFSMC_IRQHandler; FSMC
DCDSDIO_IRQHandler; SDIO
DCDTIM5_IRQHandler; TIM5
DCDSPI3_IRQHandler; SPI3
DCDUART4_IRQHandler; UART4
DCDUART5_IRQHandler; UART5
DCDTIM6_IRQHandler; TIM6
DCDTIM7_IRQHandler; TIM7
DCDDMA2_Channel1_IRQHandler; DMA2 Channel1
DCDDMA2_Channel2_IRQHandler; DMA2Channel2
DCDDMA2_Channel3_IRQHandler; DMA2 Channel3
DCDDMA2_Channel4_5_IRQHandler; DMA2 Channel4& Channel5
__Vectors_End; 向量表结束标志

__Vectors_SizeEQU__Vectors_End- __Vectors; 计算向量表地址空间大小

; |.text|用于表示由C编译程序产生的代码段,或用于以某种方式与C库关联的代码段
AREA|.text|, CODE, READONLY; 定义C编译器源代码的代码段,只读

; IMPORT:伪指令用于通知编译器要使用的标号在其他的源文件中定义。
; [WEAK]表示弱定义,优先执行其他文件的定义。就是说如果外面声明了的话先会调用外面
; 的,这就让我们可以在c文件中放置中断服务程序,只要保证C函数名和向量表一致就行
; 利用PROC、ENDP这一对伪指令把程序段分为若干个过程,是程序的结构更加清晰
; Reset handler
Reset_HandlerPROC
EXPORTReset_Handler[WEAK]
IMPORT__main
IMPORTSystemInit
LDRR0, =SystemInit; 装载寄存器指令
BLXR0; 带链接的跳转,切换指令集
LDRR0, =__main
BXR0; 切换指令集main函数不返回
ENDP

; Dummy Exception Handlers (infinite loops which can be modified)

NMI_HandlerPROC
EXPORTNMI_Handler[WEAK]
B.
ENDP
HardFault_Handler\
PROC
EXPORTHardFault_Handler[WEAK]
B.
ENDP
MemManage_Handler\
PROC
EXPORTMemManage_Handler[WEAK]
B.
ENDP
BusFault_Handler\
PROC
EXPORTBusFault_Handler[WEAK]
B.
ENDP
UsageFault_Handler\
PROC
EXPORTUsageFault_Handler[WEAK]
B.
ENDP
SVC_HandlerPROC
EXPORT SVC_Handler[WEAK]
B.
ENDP
DebugMon_Handler\
PROC
EXPORTDebugMon_Handler[WEAK]
B.
ENDP
PendSV_HandlerPROC
EXPORTPendSV_Handler[WEAK]
B.
ENDP
SysTick_HandlerPROC
EXPORTSysTick_Handler[WEAK]
B.
ENDP

Default_HandlerPROC

EXPORTWWDG_IRQHandler[WEAK]
EXPORTPVD_IRQHandler[WEAK]
EXPORTTAMPER_IRQHandler[WEAK]
EXPORTRTC_IRQHandler[WEAK]
EXPORTFLASH_IRQHandler[WEAK]
EXPORTRCC_IRQHandler[WEAK]
EXPORTEXTI0_IRQHandler[WEAK]
EXPORTEXTI1_IRQHandler[WEAK]
EXPORTEXTI2_IRQHandler[WEAK]
EXPORTEXTI3_IRQHandler[WEAK]
EXPORTEXTI4_IRQHandler[WEAK]
EXPORTDMA1_Channel1_IRQHandler[WEAK]
EXPORTDMA1_Channel2_IRQHandler[WEAK]
EXPORTDMA1_Channel3_IRQHandler[WEAK]
EXPORT DMA1_Channel4_IRQHandler[WEAK]
EXPORTDMA1_Channel5_IRQHandler[WEAK]
EXPORTDMA1_Channel6_IRQHandler[WEAK]
EXPORTDMA1_Channel7_IRQHandler[WEAK]
EXPORTADC1_2_IRQHandler[WEAK]
EXPORTUSB_HP_CAN1_TX_IRQHandler[WEAK]
EXPORTUSB_LP_CAN1_RX0_IRQHandler[WEAK]
EXPORTCAN1_RX1_IRQHandler[WEAK]
EXPORTCAN1_SCE_IRQHandler[WEAK]
EXPORTEXTI9_5_IRQHandler[WEAK]
EXPORTTIM1_BRK_IRQHandler[WEAK]
EXPORTTIM1_UP_IRQHandler[WEAK]
EXPORTTIM1_TRG_COM_IRQHandler[WEAK]
EXPORTTIM1_CC_IRQHandler[WEAK]
EXPORTTIM2_IRQHandler[WEAK]
EXPORTTIM3_IRQHandler[WEAK]
EXPORTTIM4_IRQHandler[WEAK]
EXPORT I2C1_EV_IRQHandler[WEAK]
EXPORTI2C1_ER_IRQHandler[WEAK]
EXPORTI2C2_EV_IRQHandler[WEAK]
EXPORTI2C2_ER_IRQHandler[WEAK]
EXPORTSPI1_IRQHandler[WEAK]
EXPORTSPI2_IRQHandler[WEAK]
EXPORTUSART1_IRQHandler[WEAK]
EXPORTUSART2_IRQHandler[WEAK]
EXPORTUSART3_IRQHandler[WEAK]
EXPORTEXTI15_10_IRQHandler[WEAK]
EXPORTRTCAlarm_IRQHandler[WEAK]
EXPORTUSBWakeUp_IRQHandler[WEAK]
EXPORTTIM8_BRK_IRQHandler[WEAK]
EXPORTTIM8_UP_IRQHandler[WEAK]
EXPORTTIM8_TRG_COM_IRQHandler[WEAK]
EXPORTTIM8_CC_IRQHandler[WEAK]
EXPORTADC3_IRQHandler[WEAK]
EXPORTFSMC_IRQHandler[WEAK]
EXPORTSDIO_IRQHandler[WEAK]
EXPORTTIM5_IRQHandler[WEAK]
EXPORTSPI3_IRQHandler[WEAK]
EXPORTUART4_IRQHandler[WEAK]
EXPORTUART5_IRQHandler[WEAK]
EXPORTTIM6_IRQHandler[WEAK]
EXPORTTIM7_IRQHandler[WEAK]
EXPORTDMA2_Channel1_IRQHandler[WEAK]
EXPORTDMA2_Channel2_IRQHandler[WEAK]
EXPORTDMA2_Channel3_IRQHandler[WEAK]
EXPORTDMA2_Channel4_5_IRQHandler[WEAK]

; 如下只是定义了一个空函数
WWDG_IRQHandler
PVD_IRQHandler
TAMPER_IRQHandler
RTC_IRQHandler
FLASH_IRQHandler
RCC_IRQHandler
EXTI0_IRQHandler
EXTI1_IRQHandler
EXTI2_IRQHandler
EXTI3_IRQHandler
EXTI4_IRQHandler
DMA1_Channel1_IRQHandler
DMA1_Channel2_IRQHandler
DMA1_Channel3_IRQHandler
DMA1_Channel4_IRQHandler
DMA1_Channel5_IRQHandler
DMA1_Channel6_IRQHandler
DMA1_Channel7_IRQHandler
ADC1_2_IRQHandler
USB_HP_CAN1_TX_IRQHandler
USB_LP_CAN1_RX0_IRQHandler
CAN1_RX1_IRQHandler
CAN1_SCE_IRQHandler
EXTI9_5_IRQHandler
TIM1_BRK_IRQHandler
TIM1_UP_IRQHandler
TIM1_TRG_COM_IRQHandler
TIM1_CC_IRQHandler
TIM2_IRQHandler
TIM3_IRQHandler
TIM4_IRQHandler
I2C1_EV_IRQHandler
I2C1_ER_IRQHandler
I2C2_EV_IRQHandler
I2C2_ER_IRQHandler
SPI1_IRQHandler
SPI2_IRQHandler
USART1_IRQHandler
USART2_IRQHandler
USART3_IRQHandler
EXTI15_10_IRQHandler
RTCAlarm_IRQHandler
USBWakeUp_IRQHandler
TIM8_BRK_IRQHandler
TIM8_UP_IRQHandler
TIM8_TRG_COM_IRQHandler
TIM8_CC_IRQHandler
ADC3_IRQHandler
FSMC_IRQHandler
SDIO_IRQHandler
TIM5_IRQHandler
SPI3_IRQHandler
UART4_IRQHandler
UART5_IRQHandler
TIM6_IRQHandler
TIM7_IRQHandler
DMA2_Channel1_IRQHandler
DMA2_Channel2_IRQHandler
DMA2_Channel3_IRQHandler
DMA2_Channel4_5_IRQHandler
B.

ENDP

ALIGN; 默认是字对齐方式,也说明了代码是4字节对齐

; *******************************************************************************
; UserStack and Heap initialization
; *******************************************************************************
IF:DEF:__MICROLIB; 如果勾选了USE MicroLIB

EXPORT__initial_sp
EXPORT__heap_base
EXPORT__heap_limit

ELSE

IMPORT__use_two_region_memory
; 两区堆栈空间,堆和栈有各自的空间地址
EXPORT__user_initial_stackheap
; 此处是初始化两区的堆栈空间,堆是向上生长,栈是向下生长。两个是互相独立的数据段,
; 并不能交叉使用

__user_initial_stackheap

LDRR0, = Heap_Mem
LDRR1, =(Stack_Mem + Stack_Size)
LDRR2, = (Heap_Mem +Heap_Size)
LDRR3, = Stack_Mem
BXLR

ALIGN

ENDIF

END; 命令指示汇编器,已到达一个源文件的末尾

; *******************(C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE*****


此源码出自于startup_stm32f10x_hd,本博文的注释是参考了安富莱和网上一些文章。有错误的地方还望各网友指教!
【STM32启动代码|STM32启动代码分析】

    推荐阅读