Cortex-m3启动代码分析笔记
启动代码文件名是STM32F10X.S,它的作用先总结下,然后再分析。
启动代码作用一般是:
1)堆和栈的初始化;
2)中断向量表定义;
3)地址重映射及中断向量表的转移;
4)设置系统时钟频率;
5)中断寄存器的初始化;
6)进入C应用程序。
(1)按启动代码的次序,先看堆和栈的初始化:
Stack_Size EQU 0x00000200 ;定义Stack_Size为0x00000200 AREA STACK, NOINIT, READWRITE, ALIGN= ;定义栈,可初始为0,8字节对齐 ;声明一个名为STACK的可读写段,8字节对齐 Stack_Mem SPACE Stack_Size ;分配0x200个连续字节,并初始化为0 ;开辟一段大小为Stack_Size的内存空间 __initial_sp ;汇编代码地址标号 ;地址标号,即STACK段空间顶地址 Heap_Size EQU 0x00000000 AREA HEAP, NOINIT, READWRITE, ALIGN= __heap_base Heap_Mem SPACE Heap_Size __heap_limit PRESERVE8 ;指定当前文件堆栈8字节对齐 THUMB ;告诉汇编器下面是32为的Thumb指令,如果需要汇编器将插入位以保证对齐
由于Cortex m3设定每次上电复位后都从0x00地址处加载中断向量表,并且将中断向量表的第0个地址的数据加载入MSP作为栈顶地址。所以在中断向量表的第0个地址出填写__initial_sp即STACK段空间顶地址。
同时在MDK的工程属性页面里设定“片上只读地址的范围”0x00至0x80000;在 LINK选项卡里面选择使用从Target里面得到内存布局。这样RESET段都可以被链接至0x00地址处,而其中最开始的__Vectors地址标号所指定的__initial_sp,即STACK段空间顶地址,至此芯片就在上电后通过中断向量表将MSP的值设置为__initial_sp的值了。
(2)中断向量表定义
AREA RESET, DATA, READONLY ; 定义复位向量段,只读 EXPORT __Vectors ; 定义一个可以在其他文件中使用的全局标号。此处表示中断地址 __Vectors DCD __initial_sp ; 给__initial_sp分配4字节32位的地址0x0 DCD Reset_Handler ; 给标号Reset Handler分配地址为0x00000004 DCD NMI_Handler ; 给标号NMI Handler分配地址0x00000008 DCD HardFault_Handler ; Hard Fault Handler DCD MemManage_Handler ; MPU Fault Handler DCD BusFault_Handler ; Bus Fault Handler DCD UsageFault_Handler ; Usage Fault Handler DCD ; 这种形式就是保留地址,不给任何标号分配 DCD ; Reserved DCD ; Reserved DCD ; Reserved DCD SVC_Handler ; SVCall Handler DCD DebugMon_Handler ; Debug Monitor Handler DCD ; Reserved DCD PendSV_Handler ; PendSV Handler DCD SysTick_Handler ; SysTick Handler ; External Interrupts DCD WWDG_IRQHandler ; Window Watchdog DCD PVD_IRQHandler ; PVD through EXTI Line detect DCD TAMPER_IRQHandler ; Tamper DCD RTC_IRQHandler ; RTC DCD FLASH_IRQHandler ; Flash DCD RCC_IRQHandler ; RCC DCD EXTI0_IRQHandler ; EXTI Line 0 DCD EXTI1_IRQHandler ; EXTI Line 1 DCD EXTI2_IRQHandler ; EXTI Line 2 DCD EXTI3_IRQHandler ; EXTI Line 3 DCD EXTI4_IRQHandler ; EXTI Line 4 DCD DMAChannel1_IRQHandler ; DMA Channel 1 DCD DMAChannel2_IRQHandler ; DMA Channel 2 DCD DMAChannel3_IRQHandler ; DMA Channel 3 DCD DMAChannel4_IRQHandler ; DMA Channel 4 DCD DMAChannel5_IRQHandler ; DMA Channel 5 DCD DMAChannel6_IRQHandler ; DMA Channel 6 DCD DMAChannel7_IRQHandler ; DMA Channel 7 DCD ADC_IRQHandler ; ADC DCD USB_HP_CAN_TX_IRQHandler ; USB High Priority or CAN TX DCD USB_LP_CAN_RX0_IRQHandler ; USB Low Priority or CAN RX0 DCD CAN_RX1_IRQHandler ; CAN RX1 DCD CAN_SCE_IRQHandler ; CAN SCE DCD EXTI9_5_IRQHandler ; EXTI Line 9..5 DCD TIM1_BRK_IRQHandler ; TIM1 Break DCD TIM1_UP_IRQHandler ; TIM1 Update DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare DCD TIM2_IRQHandler ; TIM2 DCD TIM3_IRQHandler ; TIM3 DCD TIM4_IRQHandler ; TIM4 DCD I2C1_EV_IRQHandler ; I2C1 Event DCD I2C1_ER_IRQHandler ; I2C1 Error DCD I2C2_EV_IRQHandler ; I2C2 Event DCD I2C2_ER_IRQHandler ; I2C2 Error DCD SPI1_IRQHandler ; SPI1 DCD SPI2_IRQHandler ; SPI2 DCD USART1_IRQHandler ; USART1 DCD USART2_IRQHandler ; USART2 DCD USART3_IRQHandler ; USART3 DCD EXTI15_10_IRQHandler ; EXTI Line 15..10 DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend
(3)中断向量表的转移
AREA |.text|, CODE, READONLY ;代码段定义 ; Reset Handler Reset_Handler PROC ;标记一个函数的开始 EXPORT Reset_Handler [WEAK] ;[WEAK] 选项表示当所有的源文件都没有定义这样一个标号时,编译器也不给出错误信息,在多数情况下将该标号置为 0 , ;若该标号为 B 或 BL 指令引用,则将 B 或 BL指令置为 NOP 操作。EXPORT提示编译器该标号可以为外部文件引用。 IMPORT __main ;通知编译器要使用的标号在其他文件 LDR R0, =__main ;使用“=”表示LDR目前是伪指令不是标准指令。这里是把__main的地址给RO。 BX R0 ;BX是ARM指令集和THUMB指令集之间程序的跳转 ENDP ; Dummy Exception Handlers (infinite loops which can be modified) NMI_Handler m ;"m"其实就是PROC表示汇编函数的开始 EXPORT NMI_Handler [WEAK] B . ENDP HardFault_Handler\ ;"\"是换行的意思 PROC EXPORT HardFault_Handler [WEAK] B . ;"."号到底是什么含义呢,目前还没查到资料。可能是保留地址,供以后修改的吧 ENDP MemManage_Handler\ PROC EXPORT MemManage_Handler [WEAK] B . ENDP BusFault_Handler\ PROC EXPORT BusFault_Handler [WEAK] B . ENDP UsageFault_Handler\ PROC EXPORT UsageFault_Handler [WEAK] B . ENDP SVC_Handler PROC EXPORT SVC_Handler [WEAK] B . ENDP DebugMon_Handler\ PROC EXPORT DebugMon_Handler [WEAK] B . ENDP PendSV_Handler PROC EXPORT PendSV_Handler [WEAK] B . ENDP SysTick_Handler PROC EXPORT SysTick_Handler [WEAK] B . ENDP Default_Handler PROC EXPORT WWDG_IRQHandler [WEAK] EXPORT PVD_IRQHandler [WEAK] EXPORT TAMPER_IRQHandler [WEAK] EXPORT RTC_IRQHandler [WEAK] EXPORT FLASH_IRQHandler [WEAK] EXPORT RCC_IRQHandler [WEAK] EXPORT EXTI0_IRQHandler [WEAK] EXPORT EXTI1_IRQHandler [WEAK] EXPORT EXTI2_IRQHandler [WEAK] EXPORT EXTI3_IRQHandler [WEAK] EXPORT EXTI4_IRQHandler [WEAK] EXPORT DMAChannel1_IRQHandler [WEAK] EXPORT DMAChannel2_IRQHandler [WEAK] EXPORT DMAChannel3_IRQHandler [WEAK] EXPORT DMAChannel4_IRQHandler [WEAK] EXPORT DMAChannel5_IRQHandler [WEAK] EXPORT DMAChannel6_IRQHandler [WEAK] EXPORT DMAChannel7_IRQHandler [WEAK] EXPORT ADC_IRQHandler [WEAK] EXPORT USB_HP_CAN_TX_IRQHandler [WEAK] EXPORT USB_LP_CAN_RX0_IRQHandler [WEAK] EXPORT CAN_RX1_IRQHandler [WEAK] EXPORT CAN_SCE_IRQHandler [WEAK] EXPORT EXTI9_5_IRQHandler [WEAK] EXPORT TIM1_BRK_IRQHandler [WEAK] EXPORT TIM1_UP_IRQHandler [WEAK] EXPORT TIM1_TRG_COM_IRQHandler [WEAK] EXPORT TIM1_CC_IRQHandler [WEAK] EXPORT TIM2_IRQHandler [WEAK] EXPORT TIM3_IRQHandler [WEAK] EXPORT TIM4_IRQHandler [WEAK] EXPORT I2C1_EV_IRQHandler [WEAK] EXPORT I2C1_ER_IRQHandler [WEAK] EXPORT I2C2_EV_IRQHandler [WEAK] EXPORT I2C2_ER_IRQHandler [WEAK] EXPORT SPI1_IRQHandler [WEAK] EXPORT SPI2_IRQHandler [WEAK] EXPORT USART1_IRQHandler [WEAK] EXPORT USART2_IRQHandler [WEAK] EXPORT USART3_IRQHandler [WEAK] EXPORT EXTI15_10_IRQHandler [WEAK] EXPORT RTCAlarm_IRQHandler [WEAK] EXPORT USBWakeUp_IRQHandler [WEAK] WWDG_IRQHandler PVD_IRQHandler TAMPER_IRQHandler RTC_IRQHandler FLASH_IRQHandler RCC_IRQHandler EXTI0_IRQHandler EXTI1_IRQHandler EXTI2_IRQHandler EXTI3_IRQHandler EXTI4_IRQHandler DMAChannel1_IRQHandler DMAChannel2_IRQHandler DMAChannel3_IRQHandler DMAChannel4_IRQHandler DMAChannel5_IRQHandler DMAChannel6_IRQHandler DMAChannel7_IRQHandler ADC_IRQHandler USB_HP_CAN_TX_IRQHandler USB_LP_CAN_RX0_IRQHandler CAN_RX1_IRQHandler CAN_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 B . ENDP ALIGN
(4)堆和栈的初始化
IF :DEF:__MICROLIB ;“DEF”的用法——:DEF:X 就是说X定义了则为真,否则为假 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
==============================================================================
“.”代表 address of current instruction 也就是当前指令地址
文中了(B .)表示循环,有点像C里面的while(1);语句。