第十六章:STM32处理器启动代码的理解

[cpp]  view plain  copy
  1. /** 
  2.   ****************************************************************************** 
  3.   * @file      startup_stm32f10x_md.s 
  4.   * @author    MCD Application Team 
  5.   * @version   V3.6.2 
  6.   * @date      28-February-2013 
  7.   * @brief     STM32F10x Medium Density Devices vector table for RIDE7 toolchain. 
  8.   *            This module performs: 
  9.   *                - Set the initial SP                                              -设置最初的SP寄存器值 
  10.   *                - Set the initial PC == Reset_Handler,                            -设置最初的PC寄存器值 == Reset_Handler 
  11.   *                - Set the vector table entries with the exceptions ISR address    -在有特殊的ISR(中断服务程序)地址的情况下设置向量表入口 
  12.   *                - Configure the clock system                                      -设置时钟系统 
  13.   *                - Branches to main in the C library (which eventually             -C库文件中由分支到主函数(最后调用了主函数) 
  14.   *                  calls main()). 
  15.   *            After Reset the Cortex-M3 processor is in Thread mode,                在线程模式下复位了CM3处理器后,此时优先级特权化,栈顶设置为主函数 
  16.   *            priority is Privileged, and the Stack is set to Main. 
  17.   ****************************************************************************** 
  18.   * @attention 
  19.   * 
  20.   * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2> 
  21.   * 
  22.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 
  23.   * You may not use this file except in compliance with the License. 
  24.   * You may obtain a copy of the License at: 
  25.   * 
  26.   *        http://www.st.com/software_license_agreement_liberty_v2 
  27.   * 
  28.   * Unless required by applicable law or agreed to in writing, software  
  29.   * distributed under the License is distributed on an "AS IS" BASIS,  
  30.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  31.   * See the License for the specific language governing permissions and 
  32.   * limitations under the License. 
  33.   * 
  34.   ****************************************************************************** 
  35.   */  
  36.       
  37.   .syntax unified                     ;表示使用了统一汇编语言语法  
  38.     .cpu cortex-m3                    ;MCU为CM3  
  39.     .fpu softvfp  
  40.     .thumb                            ;指令集  
  41.   
  42. .global g_pfnVectors                  ;global使得g_pfnVectors可以被其他目标文件使用  
  43. .global Default_Handler  
  44.   
  45. /* start address for the initialization values of the .data section.                初始化.data 块的起始地址,这个地址在链接脚本中被定义 
  46. defined in linker script */  
  47. .word   _sidata  
  48. /* start address for the .data section. defined in linker script                    .data块的起始地址,这个地址在链接脚本中被定义*/    
  49. .word   _sdata  
  50. /* end address for the .data section. defined in linker script                      .data块的结束地址,这个地址在链接脚本中被定义*/  
  51. .word   _edata  
  52. /* start address for the .bss section. defined in linker script                     .bss块的起始地址,这个地址在链接脚本中被定义*/  
  53. .word   _sbss  
  54. /* end address for the .bss section. defined in linker script                       .bss块的结束地址,这个地址在链接脚本中被定义*/  
  55. .word   _ebss  
  56.   
  57. .equ  BootRAM, 0xF108F85F                                                           /*这里跟c中的宏定义类似,即BootRAM = 0xF108F85F,不参与编译*/  
  58. /** 
  59.  * @brief  This is the code that gets called when the processor first 
  60.  *          starts execution following a reset event. Only the absolutely 
  61.  *          necessary set is performed, after which the application 
  62.  *          supplied main() routine is called.  
  63.  * @param  None 
  64.  * @retval : None 
  65. */  
  66.   
  67.     .section    .text.Reset_Handler  
  68.     .weak   Reset_Handler  
  69.     .type   Reset_Handler, %function  
  70. Reset_Handler:    
  71.   
  72. /* Copy the data segment initializers from flash to SRAM                             将初始化了的数据段从flash复制到SRAM*/    
  73.   movs  r1, #0                             ;将立即数0赋值给r1寄存器  
  74.   b LoopCopyDataInit                       ;程序转移到LoopCopyDataInit处  
  75.   
  76. CopyDataInit:                    ;从FLASH中拷贝地址在sdata和edata之间的代码到SRAM中  
  77.     ldr r3, =_sidata                       ;从存储器中将_sidata加载到寄存器r3中  
  78.     ldr r3, [r3, r1]                       ;从地址r3+r1处读取一个字(32bit)到r3中  
  79.     str r3, [r0, r1]             ;把寄存器r3的值存储到存储器中地址为r0+r1地址处  
  80.     adds    r1, r1, #4                     ;r1 = r1 + 4  
  81.       
  82. LoopCopyDataInit:  
  83.     ldr r0, =_sdata                        ;从存储器中将_sidata加载到寄存器r0中  
  84.     ldr r3, =_edata                        ;从存储器中将_edata加载到寄存器r3中  
  85.     adds    r2, r0, r1                     ;r2=r0+r1  
  86.     cmp r2, r3                             ;计算r2 - r3,若小于0,标志位为0,反之为1  
  87.     bcc CopyDataInit                       ;如果标志位为0(无借位)即r2<r3,则跳转到                                                CopyDataInit处  
  88.     ldr r2, =_sbss                         ;从存储器中将_sbss加载到寄存器r2中  
  89.     b   LoopFillZerobss                    ;无条件跳转到LoopFillZerobss  
  90. /* Zero fill the bss segment. */    
  91. FillZerobss:  
  92.     movs    r3, #0                         ;将立即数0存入寄存器r3  
  93.     str r3, [r2], #4                       ;将寄存器r3的值存储到地址为r2寄存器值得地址                                             处后,r2 = r2 + 4  
  94.       
  95. LoopFillZerobss:  
  96.     ldr r3, = _ebss                        ;从存储器中将_ebss加载到寄存器r3中  
  97.     cmp r2, r3                             ;比较r2,r3,然后更新标志位  
  98.     bcc FillZerobss                        ;如果标志位为0(无借位),则跳转到                                                      FillZerobss处  
  99. /* Call the clock system intitialization function.*/  
  100. /* bl  SystemInit */  
  101. /* Call the application's entry point.*/  
  102.     bl  main                               ;转移到main函数起始处  
  103.     bx  lr                                 ;转移到地址lr处  
  104. .size   Reset_Handler, .-Reset_Handler     ;相当于mov pc lr    lr即寄存器r14  
  105.   
  106. /** 
  107.  * @brief  This is the code that gets called when the processor receives an  
  108.  *         unexpected interrupt. This simply enters an infinite loop, preserving 
  109.  *         the system state for examination by a debugger. 
  110.  * @param  None      
  111.  * @retval None        
  112. */  
  113.     .section    .text.Default_Handler,"ax",%progbits  
  114. Default_Handler:  
  115. Infinite_Loop:                                                                             
  116.     b   Infinite_Loop                      ;无条件跳转到Infinite_Loop  
  117.     .size   Default_Handler, .-Default_Handler  
  118. /****************************************************************************** 
  119. * 
  120. * The minimal vector table for a Cortex M3.  Note that the proper constructs         Cortex M3的最小向量表 
  121. * must be placed on this to ensure that it ends up at physical address 
  122. * 0x0000.0000. 
  123. * 
  124. ******************************************************************************/      
  125.     .section    .isr_vector,"a",%progbits             ;定义中断向量表的数据段  
  126.     .type   g_pfnVectors, %object  
  127.     .size   g_pfnVectors, .-g_pfnVectors  
  128.       
  129.       
  130. g_pfnVectors:  
  131.     .word   _estack         ;在当前位置放置一个word型的值,这个值为_estack;后面同理  
  132.     .word   Reset_Handler   ;这一块程序的意思就是说以g_pfnVectors为初始地址,然后依次  
  133.     .word   NMI_Handler     ;以字为单位写入相应的数据  
  134.     .word   HardFault_Handler  
  135.     .word   MemManage_Handler  
  136.     .word   BusFault_Handler  
  137.     .word   UsageFault_Handler  
  138.     .word   0  
  139.     .word   0  
  140.     .word   0  
  141.     .word   0  
  142.     .word   SVC_Handler  
  143.     .word   DebugMon_Handler  
  144.     .word   0  
  145.     .word   PendSV_Handler  
  146.     .word   SysTick_Handler  
  147.     .word   WWDG_IRQHandler  
  148.     .word   PVD_IRQHandler  
  149.     .word   TAMPER_IRQHandler  
  150.     .word   RTC_IRQHandler  
  151.     .word   FLASH_IRQHandler  
  152.     .word   RCC_IRQHandler  
  153.     .word   EXTI0_IRQHandler  
  154.     .word   EXTI1_IRQHandler  
  155.     .word   EXTI2_IRQHandler  
  156.     .word   EXTI3_IRQHandler  
  157.     .word   EXTI4_IRQHandler  
  158.     .word   DMA1_Channel1_IRQHandler  
  159.     .word   DMA1_Channel2_IRQHandler  
  160.     .word   DMA1_Channel3_IRQHandler  
  161.     .word   DMA1_Channel4_IRQHandler  
  162.     .word   DMA1_Channel5_IRQHandler  
  163.     .word   DMA1_Channel6_IRQHandler  
  164.     .word   DMA1_Channel7_IRQHandler  
  165.     .word   ADC1_2_IRQHandler  
  166.     .word   USB_HP_CAN1_TX_IRQHandler  
  167.     .word   USB_LP_CAN1_RX0_IRQHandler  
  168.     .word   CAN1_RX1_IRQHandler  
  169.     .word   CAN1_SCE_IRQHandler  
  170.     .word   EXTI9_5_IRQHandler  
  171.     .word   TIM1_BRK_IRQHandler  
  172.     .word   TIM1_UP_IRQHandler  
  173.     .word   TIM1_TRG_COM_IRQHandler  
  174.     .word   TIM1_CC_IRQHandler  
  175.     .word   TIM2_IRQHandler  
  176.     .word   TIM3_IRQHandler  
  177.     .word   TIM4_IRQHandler  
  178.     .word   I2C1_EV_IRQHandler  
  179.     .word   I2C1_ER_IRQHandler  
  180.     .word   I2C2_EV_IRQHandler  
  181.     .word   I2C2_ER_IRQHandler  
  182.     .word   SPI1_IRQHandler  
  183.     .word   SPI2_IRQHandler  
  184.     .word   USART1_IRQHandler  
  185.     .word   USART2_IRQHandler  
  186.     .word   USART3_IRQHandler  
  187.     .word   EXTI15_10_IRQHandler  
  188.     .word   RTCAlarm_IRQHandler  
  189.     .word   USBWakeUp_IRQHandler      
  190.   .word 0  
  191.     .word   0  
  192.     .word   0  
  193.     .word   0  
  194.     .word   0  
  195.     .word   0  
  196.     .word   0  
  197.     .word   BootRAM          /* @0x108. This is for boot in RAM mode for             这个是为stm32f10系列中容量系列设备的boot中的RAM模式准备的 
  198.                             STM32F10x Medium Density devices. */  
  199.      
  200. /******************************************************************************* 
  201. * 
  202. * Provide weak aliases for each Exception handler to the Default_Handler.  
  203. * As they are weak aliases, any function with the same name will override  
  204. * this definition. 
  205. * 
  206. *******************************************************************************/  
  207.       
  208.   .weak NMI_Handler                                                                 ;定义一个弱别名NMI_Handler  
  209.    .thumb_set NMI_Handler,Default_Handler                                            ;如果这个不重写这个弱别名,那么默认执行Default_Handler,反之这执行重写过的NMI_Handler  
  210.       
  211.   .weak HardFault_Handler  
  212.   .thumb_set HardFault_Handler,Default_Handler  
  213.       
  214.   .weak MemManage_Handler  
  215.   .thumb_set MemManage_Handler,Default_Handler  
  216.       
  217.   .weak BusFault_Handler  
  218.   .thumb_set BusFault_Handler,Default_Handler  
  219.   
  220.     .weak   UsageFault_Handler  
  221.     .thumb_set UsageFault_Handler,Default_Handler  
  222.   
  223.     .weak   SVC_Handler  
  224.     .thumb_set SVC_Handler,Default_Handler  
  225.   
  226.     .weak   DebugMon_Handler  
  227.     .thumb_set DebugMon_Handler,Default_Handler  
  228.   
  229.     .weak   PendSV_Handler  
  230.     .thumb_set PendSV_Handler,Default_Handler  
  231.   
  232.     .weak   SysTick_Handler  
  233.     .thumb_set SysTick_Handler,Default_Handler  
  234.   
  235.     .weak   WWDG_IRQHandler  
  236.     .thumb_set WWDG_IRQHandler,Default_Handler  
  237.   
  238.     .weak   PVD_IRQHandler  
  239.     .thumb_set PVD_IRQHandler,Default_Handler  
  240.   
  241.     .weak   TAMPER_IRQHandler  
  242.     .thumb_set TAMPER_IRQHandler,Default_Handler  
  243.   
  244.     .weak   RTC_IRQHandler  
  245.     .thumb_set RTC_IRQHandler,Default_Handler  
  246.   
  247.     .weak   FLASH_IRQHandler  
  248.     .thumb_set FLASH_IRQHandler,Default_Handler  
  249.   
  250.     .weak   RCC_IRQHandler  
  251.     .thumb_set RCC_IRQHandler,Default_Handler  
  252.   
  253.     .weak   EXTI0_IRQHandler  
  254.     .thumb_set EXTI0_IRQHandler,Default_Handler  
  255.   
  256.     .weak   EXTI1_IRQHandler  
  257.     .thumb_set EXTI1_IRQHandler,Default_Handler  
  258.   
  259.     .weak   EXTI2_IRQHandler  
  260.     .thumb_set EXTI2_IRQHandler,Default_Handler  
  261.   
  262.     .weak   EXTI3_IRQHandler  
  263.     .thumb_set EXTI3_IRQHandler,Default_Handler  
  264.   
  265.     .weak   EXTI4_IRQHandler  
  266.     .thumb_set EXTI4_IRQHandler,Default_Handler  
  267.   
  268.     .weak   DMA1_Channel1_IRQHandler  
  269.     .thumb_set DMA1_Channel1_IRQHandler,Default_Handler  
  270.   
  271.     .weak   DMA1_Channel2_IRQHandler  
  272.     .thumb_set DMA1_Channel2_IRQHandler,Default_Handler  
  273.   
  274.     .weak   DMA1_Channel3_IRQHandler  
  275.     .thumb_set DMA1_Channel3_IRQHandler,Default_Handler  
  276.   
  277.     .weak   DMA1_Channel4_IRQHandler  
  278.     .thumb_set DMA1_Channel4_IRQHandler,Default_Handler  
  279.   
  280.     .weak   DMA1_Channel5_IRQHandler  
  281.     .thumb_set DMA1_Channel5_IRQHandler,Default_Handler  
  282.   
  283.     .weak   DMA1_Channel6_IRQHandler  
  284.     .thumb_set DMA1_Channel6_IRQHandler,Default_Handler  
  285.   
  286.     .weak   DMA1_Channel7_IRQHandler  
  287.     .thumb_set DMA1_Channel7_IRQHandler,Default_Handler  
  288.   
  289.     .weak   ADC1_2_IRQHandler  
  290.     .thumb_set ADC1_2_IRQHandler,Default_Handler  
  291.   
  292.     .weak   USB_HP_CAN1_TX_IRQHandler  
  293.     .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler  
  294.   
  295.     .weak   USB_LP_CAN1_RX0_IRQHandler  
  296.     .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler  
  297.   
  298.     .weak   CAN1_RX1_IRQHandler  
  299.     .thumb_set CAN1_RX1_IRQHandler,Default_Handler  
  300.   
  301.     .weak   CAN1_SCE_IRQHandler  
  302.     .thumb_set CAN1_SCE_IRQHandler,Default_Handler  
  303.   
  304.     .weak   EXTI9_5_IRQHandler  
  305.     .thumb_set EXTI9_5_IRQHandler,Default_Handler  
  306.   
  307.     .weak   TIM1_BRK_IRQHandler  
  308.     .thumb_set TIM1_BRK_IRQHandler,Default_Handler  
  309.   
  310.     .weak   TIM1_UP_IRQHandler  
  311.     .thumb_set TIM1_UP_IRQHandler,Default_Handler  
  312.   
  313.     .weak   TIM1_TRG_COM_IRQHandler  
  314.     .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler  
  315.   
  316.     .weak   TIM1_CC_IRQHandler  
  317.     .thumb_set TIM1_CC_IRQHandler,Default_Handler  
  318.   
  319.     .weak   TIM2_IRQHandler  
  320.     .thumb_set TIM2_IRQHandler,Default_Handler  
  321.   
  322.     .weak   TIM3_IRQHandler  
  323.     .thumb_set TIM3_IRQHandler,Default_Handler  
  324.   
  325.     .weak   TIM4_IRQHandler  
  326.     .thumb_set TIM4_IRQHandler,Default_Handler  
  327.   
  328.     .weak   I2C1_EV_IRQHandler  
  329.     .thumb_set I2C1_EV_IRQHandler,Default_Handler  
  330.   
  331.     .weak   I2C1_ER_IRQHandler  
  332.     .thumb_set I2C1_ER_IRQHandler,Default_Handler  
  333.   
  334.     .weak   I2C2_EV_IRQHandler  
  335.     .thumb_set I2C2_EV_IRQHandler,Default_Handler  
  336.   
  337.     .weak   I2C2_ER_IRQHandler  
  338.     .thumb_set I2C2_ER_IRQHandler,Default_Handler  
  339.   
  340.     .weak   SPI1_IRQHandler  
  341.     .thumb_set SPI1_IRQHandler,Default_Handler  
  342.   
  343.     .weak   SPI2_IRQHandler  
  344.     .thumb_set SPI2_IRQHandler,Default_Handler  
  345.   
  346.     .weak   USART1_IRQHandler  
  347.     .thumb_set USART1_IRQHandler,Default_Handler  
  348.   
  349.     .weak   USART2_IRQHandler  
  350.     .thumb_set USART2_IRQHandler,Default_Handler  
  351.   
  352.     .weak   USART3_IRQHandler  
  353.     .thumb_set USART3_IRQHandler,Default_Handler  
  354.   
  355.     .weak   EXTI15_10_IRQHandler  
  356.     .thumb_set EXTI15_10_IRQHandler,Default_Handler  
  357.   
  358.     .weak   RTCAlarm_IRQHandler  
  359.     .thumb_set RTCAlarm_IRQHandler,Default_Handler  
  360.   
  361.     .weak   USBWakeUp_IRQHandler  
  362.     .thumb_set USBWakeUp_IRQHandler,Default_Handler  
  363.   
  364. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/  

如果有注释不准确的地方还希望能够给我指出,感谢感谢~~


这里以STM32程序运行过程为轴线,依次介绍单片机启动过程中的流程。

  在给单片机上电前,先通过给BOOT0和BOOT1连接高低电平来设置程序启动的初始位置。STM32程序启动的位置有三种:

  1:Main Flash memory

  是STM32内置的Flash,一般我们使用JTAG或者SWD模式下载程序时,就是下载到这个里面,重启后也直接从这启动程序。

  2:System memory

  从系统存储器启动,这种模式启动的程序功能是由厂家设置的。一般来说,这种启动方式用的比较少。

  3:Embedded Memory

  内置SRAM,既然是SRAM,自然也就没有程序存储的能力了,这个模式一般用于程序调试。

 

  在这三种方式里面用的最多的是第一种,下面以第一种启动方式为例进行介绍:

  在重启芯片时,SYSCLK的第4个上升沿,BOOT引脚的值将被锁存。然后进入启动文件,具体的启动文件为startup_stm32f10x_md.s。

  首先启动文件中有一个Reset_Handler,这个汇编函数的作用有两个:

   a.将FLASH中的程序代码拷贝到SRAM中

   b.将系统堆栈初始化(清0)

  然后进入main,从而进入c的世界。


但是启动文件到这里还没有结束,后面还有一个部分。

这个部分的作用是定义了一个段来存放中断向量表,然后以字的形式分别填入了中断的指针。

在这以后还有一个部分,后面这个部分的作用是给中断服务定义了一个弱别名,这个弱别名的作用就是在有中断被触发后,如果没有重写对应的弱别名,那么程序就默认执行默认的中断处理函数(Default_Handler),反之则执行重写了的中断处理函数


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值