STM32
直播中

李鑫

7年用户 1271经验值
私信 关注
[问答]

单片机启动过程是怎样的?

单片机启动过程是怎样的?

回帖(1)

张奥

2021-11-30 14:14:34
[cpp]    view plain    copy   
     



  • /**
  •   ******************************************************************************
  •   * @file      startup_stm32f10x_md.s
  •   * @author    MCD Application Team
  •   * @version   V3.6.2
  •   * @date      28-February-2013
  •   * @brief     STM32F10x Medium Density Devices vector table for RIDE7 toolchain.
  •   *            This module performs:
  •   *                - Set the initial SP                                              -设置最初的SP寄存器值
  •   *                - Set the initial PC == Reset_Handler,                            -设置最初的PC寄存器值 == Reset_Handler
  •   *                - Set the vector table entries with the exceptions ISR address    -在有特殊的ISR(中断服务程序)地址的情况下设置向量表入口
  •   *                - Configure the clock system                                      -设置时钟系统
  •   *                - Branches to main in the C library (which eventually             -C库文件中由分支到主函数(最后调用了主函数)
  •   *                  calls main()).
  •   *            After Reset the Cortex-M3 processor is in Thread mode,                在线程模式下复位了CM3处理器后,此时优先级特权化,栈顶设置为主函数
  •   *            priority is Privileged, and the Stack is set to Main.
  •   ******************************************************************************
  •   * @attention
  •   *
  •   *

    © COPYRIGHT 2013 STMicroelectronics


  •   *
  •   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  •   * You may not use this file except in compliance with the License.
  •   * You may obtain a copy of the License at:
  •   *
  •   *        http://www.st.com/software_license_agreement_liberty_v2
  •   *
  •   * Unless required by applicable law or agreed to in writing, software  
  •   * distributed under the License is distributed on an "AS IS" BASIS,  
  •   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  •   * See the License for the specific language governing permissions and
  •   * limitations under the License.
  •   *
  •   ******************************************************************************
  •   */  
  •       
  •   .syntax unified                     ;表示使用了统一汇编语言语法  
  •     .cpu cortex-m3                    ;MCU为CM3  
  •     .fpu softvfp  
  •     .thumb                            ;指令集  
  •   
  • .global g_pfnVectors                  ;global使得g_pfnVectors可以被其他目标文件使用  
  • .global Default_Handler  
  •   
  • /* start address for the initialization values of the .data section.                初始化.data 块的起始地址,这个地址在链接脚本中被定义
  • defined in linker script */  
  • .word   _sidata  
  • /* start address for the .data section. defined in linker script                    .data块的起始地址,这个地址在链接脚本中被定义*/   
  • .word   _sdata  
  • /* end address for the .data section. defined in linker script                      .data块的结束地址,这个地址在链接脚本中被定义*/  
  • .word   _edata  
  • /* start address for the .bss section. defined in linker script                     .bss块的起始地址,这个地址在链接脚本中被定义*/  
  • .word   _***ss  
  • /* end address for the .bss section. defined in linker script                       .bss块的结束地址,这个地址在链接脚本中被定义*/  
  • .word   _ebss  
  •   
  • .equ  BootRAM, 0xF108F85F                                                           /*这里跟c中的宏定义类似,即BootRAM = 0xF108F85F,不参与编译*/  
  • /**
  • * @brief  This is the code that gets called when the processor first
  • *          starts execution following a reset event. Only the absolutely
  • *          necessary set is performed, after which the application
  • *          supplied main() routine is called.  
  • * @param  None
  • * @retval : None
  • */  
  •   
  •     .section    .text.Reset_Handler  
  •     .weak   Reset_Handler  
  •     .type   Reset_Handler, %function  
  • Reset_Handler:   
  •   
  • /* Copy the data segment initializers from flash to SRAM                             将初始化了的数据段从flash复制到SRAM*/   
  •   movs  r1, #0                             ;将立即数0赋值给r1寄存器  
  •   b LoopCopyDataInit                       ;程序转移到LoopCopyDataInit处  
  •   
  • CopyDataInit:                    ;从FLASH中拷贝地址在sdata和edata之间的代码到SRAM中  
  •     ldr r3, =_sidata                       ;从存储器中将_sidata加载到寄存器r3中  
  •     ldr r3, [r3, r1]                       ;从地址r3+r1处读取一个字(32bit)到r3中  
  •     str r3, [r0, r1]             ;把寄存器r3的值存储到存储器中地址为r0+r1地址处  
  •     adds    r1, r1, #4                     ;r1 = r1 + 4  
  •       
  • LoopCopyDataInit:  
  •     ldr r0, =_sdata                        ;从存储器中将_sidata加载到寄存器r0中  
  •     ldr r3, =_edata                        ;从存储器中将_edata加载到寄存器r3中  
  •     adds    r2, r0, r1                     ;r2=r0+r1  
  •     cmp r2, r3                             ;计算r2 - r3,若小于0,标志位为0,反之为1  
  •     bcc CopyDataInit                       ;如果标志位为0(无借位)即r2
  •     ldr r2, =_***ss                         ;从存储器中将_***ss加载到寄存器r2中  
  •     b   LoopFillZerobss                    ;无条件跳转到LoopFillZerobss  
  • /* Zero fill the bss segment. */   
  • FillZerobss:  
  •     movs    r3, #0                         ;将立即数0存入寄存器r3  
  •     str r3, [r2], #4                       ;将寄存器r3的值存储到地址为r2寄存器值得地址                                             处后,r2 = r2 + 4  
  •       
  • LoopFillZerobss:  
  •     ldr r3, = _ebss                        ;从存储器中将_ebss加载到寄存器r3中  
  •     cmp r2, r3                             ;比较r2,r3,然后更新标志位  
  •     bcc FillZerobss                        ;如果标志位为0(无借位),则跳转到                                                      FillZerobss处  
  • /* Call the clock system intitialization function.*/  
  • /* bl  SystemInit */  
  • /* Call the application's entry point.*/  
  •     bl  main                               ;转移到main函数起始处  
  •     bx  lr                                 ;转移到地址lr处  
  • .size   Reset_Handler, .-Reset_Handler     ;相当于mov pc lr    lr即寄存器r14  
  •   
  • /**
  • * @brief  This is the code that gets called when the processor receives an  
  • *         unexpected interrupt. This simply enters an infinite loop, preserving
  • *         the system state for examination by a debugger.
  • * @param  None      
  • * @retval None        
  • */  
  •     .section    .text.Default_Handler,"ax",%progbits  
  • Default_Handler:  
  • Infinite_Loop:                                                                             
  •     b   Infinite_Loop                      ;无条件跳转到Infinite_Loop  
  •     .size   Default_Handler, .-Default_Handler  
  • /******************************************************************************
  • *
  • * The minimal vector table for a Cortex M3.  Note that the proper constructs         Cortex M3的最小向量表
  • * must be placed on this to ensure that it ends up at physical address
  • * 0x0000.0000.
  • *
  • ******************************************************************************/      
  •     .section    .isr_vector,"a",%progbits             ;定义中断向量表的数据段  
  •     .type   g_pfnVectors, %object  
  •     .size   g_pfnVectors, .-g_pfnVectors  
  •       
  •       
  • g_pfnVectors:  
  •     .word   _estack         ;在当前位置放置一个word型的值,这个值为_estack;后面同理  
  •     .word   Reset_Handler   ;这一块程序的意思就是说以g_pfnVectors为初始地址,然后依次  
  •     .word   NMI_Handler     ;以字为单位写入相应的数据  
  •     .word   HardFault_Handler  
  •     .word   MemManage_Handler  
  •     .word   BusFault_Handler  
  •     .word   UsageFault_Handler  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   SVC_Handler  
  •     .word   DebugMon_Handler  
  •     .word   0  
  •     .word   PendSV_Handler  
  •     .word   SysTick_Handler  
  •     .word   WWDG_IRQHandler  
  •     .word   PVD_IRQHandler  
  •     .word   TAMPER_IRQHandler  
  •     .word   RTC_IRQHandler  
  •     .word   FLASH_IRQHandler  
  •     .word   RCC_IRQHandler  
  •     .word   EXTI0_IRQHandler  
  •     .word   EXTI1_IRQHandler  
  •     .word   EXTI2_IRQHandler  
  •     .word   EXTI3_IRQHandler  
  •     .word   EXTI4_IRQHandler  
  •     .word   DMA1_Channel1_IRQHandler  
  •     .word   DMA1_Channel2_IRQHandler  
  •     .word   DMA1_Channel3_IRQHandler  
  •     .word   DMA1_Channel4_IRQHandler  
  •     .word   DMA1_Channel5_IRQHandler  
  •     .word   DMA1_Channel6_IRQHandler  
  •     .word   DMA1_Channel7_IRQHandler  
  •     .word   ADC1_2_IRQHandler  
  •     .word   USB_HP_CAN1_TX_IRQHandler  
  •     .word   USB_LP_CAN1_RX0_IRQHandler  
  •     .word   CAN1_RX1_IRQHandler  
  •     .word   CAN1_SCE_IRQHandler  
  •     .word   EXTI9_5_IRQHandler  
  •     .word   TIM1_BRK_IRQHandler  
  •     .word   TIM1_UP_IRQHandler  
  •     .word   TIM1_TRG_COM_IRQHandler  
  •     .word   TIM1_CC_IRQHandler  
  •     .word   TIM2_IRQHandler  
  •     .word   TIM3_IRQHandler  
  •     .word   TIM4_IRQHandler  
  •     .word   I2C1_EV_IRQHandler  
  •     .word   I2C1_ER_IRQHandler  
  •     .word   I2C2_EV_IRQHandler  
  •     .word   I2C2_ER_IRQHandler  
  •     .word   SPI1_IRQHandler  
  •     .word   SPI2_IRQHandler  
  •     .word   USART1_IRQHandler  
  •     .word   USART2_IRQHandler  
  •     .word   USART3_IRQHandler  
  •     .word   EXTI15_10_IRQHandler  
  •     .word   RTCAlarm_IRQHandler  
  •     .word   USBWakeUp_IRQHandler      
  •   .word 0  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   0  
  •     .word   BootRAM          /* @0x108. This is for boot in RAM mode for             这个是为stm32f10系列中容量系列设备的boot中的RAM模式准备的
  •                             STM32F10x Medium Density devices. */  
  •      
  • /*******************************************************************************
  • *
  • * Provide weak aliases for each Exception handler to the Default_Handler.  
  • * As they are weak aliases, any function with the same name will override  
  • * this definition.
  • *
  • *******************************************************************************/  
  •       
  •   .weak NMI_Handler                                                                 ;定义一个弱别名NMI_Handler  
  •    .thumb_set NMI_Handler,Default_Handler                                            ;如果这个不重写这个弱别名,那么默认执行Default_Handler,反之这执行重写过的NMI_Handler  
  •       
  •   .weak HardFault_Handler  
  •   .thumb_set HardFault_Handler,Default_Handler  
  •       
  •   .weak MemManage_Handler  
  •   .thumb_set MemManage_Handler,Default_Handler  
  •       
  •   .weak BusFault_Handler  
  •   .thumb_set BusFault_Handler,Default_Handler  
  •   
  •     .weak   UsageFault_Handler  
  •     .thumb_set UsageFault_Handler,Default_Handler  
  •   
  •     .weak   SVC_Handler  
  •     .thumb_set SVC_Handler,Default_Handler  
  •   
  •     .weak   DebugMon_Handler  
  •     .thumb_set DebugMon_Handler,Default_Handler  
  •   
  •     .weak   PendSV_Handler  
  •     .thumb_set PendSV_Handler,Default_Handler  
  •   
  •     .weak   SysTick_Handler  
  •     .thumb_set SysTick_Handler,Default_Handler  
  •   
  •     .weak   WWDG_IRQHandler  
  •     .thumb_set WWDG_IRQHandler,Default_Handler  
  •   
  •     .weak   PVD_IRQHandler  
  •     .thumb_set PVD_IRQHandler,Default_Handler  
  •   
  •     .weak   TAMPER_IRQHandler  
  •     .thumb_set TAMPER_IRQHandler,Default_Handler  
  •   
  •     .weak   RTC_IRQHandler  
  •     .thumb_set RTC_IRQHandler,Default_Handler  
  •   
  •     .weak   FLASH_IRQHandler  
  •     .thumb_set FLASH_IRQHandler,Default_Handler  
  •   
  •     .weak   RCC_IRQHandler  
  •     .thumb_set RCC_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI0_IRQHandler  
  •     .thumb_set EXTI0_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI1_IRQHandler  
  •     .thumb_set EXTI1_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI2_IRQHandler  
  •     .thumb_set EXTI2_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI3_IRQHandler  
  •     .thumb_set EXTI3_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI4_IRQHandler  
  •     .thumb_set EXTI4_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel1_IRQHandler  
  •     .thumb_set DMA1_Channel1_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel2_IRQHandler  
  •     .thumb_set DMA1_Channel2_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel3_IRQHandler  
  •     .thumb_set DMA1_Channel3_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel4_IRQHandler  
  •     .thumb_set DMA1_Channel4_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel5_IRQHandler  
  •     .thumb_set DMA1_Channel5_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel6_IRQHandler  
  •     .thumb_set DMA1_Channel6_IRQHandler,Default_Handler  
  •   
  •     .weak   DMA1_Channel7_IRQHandler  
  •     .thumb_set DMA1_Channel7_IRQHandler,Default_Handler  
  •   
  •     .weak   ADC1_2_IRQHandler  
  •     .thumb_set ADC1_2_IRQHandler,Default_Handler  
  •   
  •     .weak   USB_HP_CAN1_TX_IRQHandler  
  •     .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler  
  •   
  •     .weak   USB_LP_CAN1_RX0_IRQHandler  
  •     .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler  
  •   
  •     .weak   CAN1_RX1_IRQHandler  
  •     .thumb_set CAN1_RX1_IRQHandler,Default_Handler  
  •   
  •     .weak   CAN1_SCE_IRQHandler  
  •     .thumb_set CAN1_SCE_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI9_5_IRQHandler  
  •     .thumb_set EXTI9_5_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM1_BRK_IRQHandler  
  •     .thumb_set TIM1_BRK_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM1_UP_IRQHandler  
  •     .thumb_set TIM1_UP_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM1_TRG_COM_IRQHandler  
  •     .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM1_CC_IRQHandler  
  •     .thumb_set TIM1_CC_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM2_IRQHandler  
  •     .thumb_set TIM2_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM3_IRQHandler  
  •     .thumb_set TIM3_IRQHandler,Default_Handler  
  •   
  •     .weak   TIM4_IRQHandler  
  •     .thumb_set TIM4_IRQHandler,Default_Handler  
  •   
  •     .weak   I2C1_EV_IRQHandler  
  •     .thumb_set I2C1_EV_IRQHandler,Default_Handler  
  •   
  •     .weak   I2C1_ER_IRQHandler  
  •     .thumb_set I2C1_ER_IRQHandler,Default_Handler  
  •   
  •     .weak   I2C2_EV_IRQHandler  
  •     .thumb_set I2C2_EV_IRQHandler,Default_Handler  
  •   
  •     .weak   I2C2_ER_IRQHandler  
  •     .thumb_set I2C2_ER_IRQHandler,Default_Handler  
  •   
  •     .weak   SPI1_IRQHandler  
  •     .thumb_set SPI1_IRQHandler,Default_Handler  
  •   
  •     .weak   SPI2_IRQHandler  
  •     .thumb_set SPI2_IRQHandler,Default_Handler  
  •   
  •     .weak   USART1_IRQHandler  
  •     .thumb_set USART1_IRQHandler,Default_Handler  
  •   
  •     .weak   USART2_IRQHandler  
  •     .thumb_set USART2_IRQHandler,Default_Handler  
  •   
  •     .weak   USART3_IRQHandler  
  •     .thumb_set USART3_IRQHandler,Default_Handler  
  •   
  •     .weak   EXTI15_10_IRQHandler  
  •     .thumb_set EXTI15_10_IRQHandler,Default_Handler  
  •   
  •     .weak   RTCAlarm_IRQHandler  
  •     .thumb_set RTCAlarm_IRQHandler,Default_Handler  
  •   
  •     .weak   USBWakeUp_IRQHandler  
  •     .thumb_set USBWakeUp_IRQHandler,Default_Handler  
  •   
  • /************************ (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),反之则执行重写了的中断处理函数
举报

更多回帖

发帖
×
20
完善资料,
赚取积分