关于 startup_stm32f10x_hd.s 这个文件的一些说明
阅读原文时间:2023年07月15日阅读:1

关于 startup_stm32f10x_hd.s 这个文件的一些说明

startup_stm32f10x_hd.s 是一个启动文件,里面是使用汇编语言写好的基本程序,当STM32 芯片上电启动的时候,受限会执行这里的汇编程序,从而建立起来C 语言的运行环境,所以我们把这个文件称为启动文件。改文件使用的汇编指令是 Cortex-M3 内核支持的指令,可以参考《Cortex-M3 权威指南中文》内指令集章节。

startup_stm32f10x_hd.s 文件是由ST官方提供的,该文件可以从KEIL5 安装目录中找到,也可以从ST库里面找到,找到该文件后吧启动文件添加到工程里面即可。不同型号的芯片以及不同编译环境使用的汇编文件是不一样的,但功能相同。

对于启动文件部分,我们主要总结功能,主要功能如下:

  • 初始化堆栈指针 SP
  • 初始化程序计数器指针 PC
  • 设置堆、栈的大小
  • 设置中断向量表的入口地址
  • 配置外部 SRAM 作为数据存储器
  • 调用 SystemInit() 函数配置 STM32 的系统时钟
  • 设置 C 库的分支入口 "__main” (最终用来调用 main 函数)

Reset_Hander

; Reset handler ; 汇编里面注释用的是";",相当于 C 语言的 “//” 注释符
Reset_Handler PROC ;PROC 是子程序定义伪指令。这里相当于 C 语言定义了一个函数为 Reset_Handler
EXPORT Reset_Handler [WEAK]   ;EXPORT 表示 Reset_Handler 这个子程序可供其他模块调用,相当于C的函数声明;[WEAK]表示弱定义
IMPORT __main        ;IMPORT 说明 __main 这个标号在其他文件,在链接时候需去其他文件中寻找。相当于c中引入函数声明
IMPORT SystemInit
LDR R0, =SystemInit    ;把 SystemInit 的地址加载到寄存器 R0 中
BLX R0   ;程序跳转到 R0 中地址执行程序,即执行 SystemInit 函数内容
LDR R0, =__main     ;把 __main 的地址加载到寄存器 R0
BX R0          ;程序跳转到 R0 中地址执行程序,即执行 __main 函数,执行完毕后即可进入 main 函数
ENDP

下面是 startup_stm32f10x_hd.s 源代码

;******************** (C) COPYRIGHT STMicroelectronics ********************
;* File Name : startup_stm32f10x_hd.s
;* Author : MCD Application Team
;* Version : V3.5.0
;* Date : -March-
;* Description : STM32F10x High Density Devices vector table for MDK-ARM
;* toolchain.
;* This module performs:
;* - Set the initial SP
;* - Set the initial PC == Reset_Handler
;* - Set the vector table entries with the exceptions ISR address
;* - Configure the clock system and also configure the external
;* SRAM mounted on STM3210E-EVAL board to be used as data
;* memory (optional, to be enabled by user)
;* - Branches to __main in the C library (which eventually
;* calls main()).
;* After Reset the CortexM3 processor is in Thread mode,
;* priority is Privileged, and the Stack is set to Main.
;* <<< Use Configuration Wizard in Context Menu >>>
;*******************************************************************************
; THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
; WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
; AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
; INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
; CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
; INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
;*******************************************************************************

; Amount of memory (in bytes) allocated for Stack
; Tailor this value to your application needs
; Stack Configuration ; Stack Size (in Bytes) <0x0-0xFFFFFFFF:> ;

Stack_Size EQU 0x00000400

             AREA    STACK, NOINIT, READWRITE, ALIGN=  

Stack_Mem SPACE Stack_Size
__initial_sp

; Heap Configuration ; Heap Size (in Bytes) <0x0-0xFFFFFFFF:> ;

Heap_Size EQU 0x00000200

             AREA    HEAP, NOINIT, READWRITE, ALIGN=  

__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit

             PRESERVE8  
             THUMB

; Vector Table Mapped to Address at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size

__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
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 ; Reserved
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  
             DCD     EXTI1\_IRQHandler           ; EXTI Line  
             DCD     EXTI2\_IRQHandler           ; EXTI Line  
             DCD     EXTI3\_IRQHandler           ; EXTI Line  
             DCD     EXTI4\_IRQHandler           ; EXTI Line  
             DCD     DMA1\_Channel1\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel2\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel3\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel4\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel5\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel6\_IRQHandler   ; DMA1 Channel  
             DCD     DMA1\_Channel7\_IRQHandler   ; DMA1 Channel  
             DCD     ADC1\_2\_IRQHandler          ; ADC1 & ADC2  
             DCD     USB\_HP\_CAN1\_TX\_IRQHandler  ; USB High Priority or CAN1 TX  
             DCD     USB\_LP\_CAN1\_RX0\_IRQHandler ; USB Low  Priority or CAN1 RX0  
             DCD     CAN1\_RX1\_IRQHandler        ; CAN1 RX1  
             DCD     CAN1\_SCE\_IRQHandler        ; CAN1 SCE  
             DCD     EXTI9\_5\_IRQHandler         ; EXTI Line ..  
             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 ..  
             DCD     RTCAlarm\_IRQHandler        ; RTC Alarm through EXTI Line  
             DCD     USBWakeUp\_IRQHandler       ; USB Wakeup from suspend  
             DCD     TIM8\_BRK\_IRQHandler        ; TIM8 Break  
             DCD     TIM8\_UP\_IRQHandler         ; TIM8 Update  
             DCD     TIM8\_TRG\_COM\_IRQHandler    ; TIM8 Trigger and Commutation  
             DCD     TIM8\_CC\_IRQHandler         ; TIM8 Capture Compare  
             DCD     ADC3\_IRQHandler            ; ADC3  
             DCD     FSMC\_IRQHandler            ; FSMC  
             DCD     SDIO\_IRQHandler            ; SDIO  
             DCD     TIM5\_IRQHandler            ; TIM5  
             DCD     SPI3\_IRQHandler            ; SPI3  
             DCD     UART4\_IRQHandler           ; UART4  
             DCD     UART5\_IRQHandler           ; UART5  
             DCD     TIM6\_IRQHandler            ; TIM6  
             DCD     TIM7\_IRQHandler            ; TIM7  
             DCD     DMA2\_Channel1\_IRQHandler   ; DMA2 Channel1  
             DCD     DMA2\_Channel2\_IRQHandler   ; DMA2 Channel2  
             DCD     DMA2\_Channel3\_IRQHandler   ; DMA2 Channel3  
             DCD     DMA2\_Channel4\_5\_IRQHandler ; DMA2 Channel4 & Channel5  

__Vectors_End

__Vectors_Size EQU __Vectors_End - __Vectors

             AREA    |.text|, CODE, READONLY

; Reset handler
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT __main
IMPORT SystemInit
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP

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

NMI_Handler 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  DMA1\_Channel1\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel2\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel3\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel4\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel5\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel6\_IRQHandler   \[WEAK\]  
             EXPORT  DMA1\_Channel7\_IRQHandler   \[WEAK\]  
             EXPORT  ADC1\_2\_IRQHandler          \[WEAK\]  
             EXPORT  USB\_HP\_CAN1\_TX\_IRQHandler  \[WEAK\]  
             EXPORT  USB\_LP\_CAN1\_RX0\_IRQHandler \[WEAK\]  
             EXPORT  CAN1\_RX1\_IRQHandler        \[WEAK\]  
             EXPORT  CAN1\_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\]  
             EXPORT  TIM8\_BRK\_IRQHandler        \[WEAK\]  
             EXPORT  TIM8\_UP\_IRQHandler         \[WEAK\]  
             EXPORT  TIM8\_TRG\_COM\_IRQHandler    \[WEAK\]  
             EXPORT  TIM8\_CC\_IRQHandler         \[WEAK\]  
             EXPORT  ADC3\_IRQHandler            \[WEAK\]  
             EXPORT  FSMC\_IRQHandler            \[WEAK\]  
             EXPORT  SDIO\_IRQHandler            \[WEAK\]  
             EXPORT  TIM5\_IRQHandler            \[WEAK\]  
             EXPORT  SPI3\_IRQHandler            \[WEAK\]  
             EXPORT  UART4\_IRQHandler           \[WEAK\]  
             EXPORT  UART5\_IRQHandler           \[WEAK\]  
             EXPORT  TIM6\_IRQHandler            \[WEAK\]  
             EXPORT  TIM7\_IRQHandler            \[WEAK\]  
             EXPORT  DMA2\_Channel1\_IRQHandler   \[WEAK\]  
             EXPORT  DMA2\_Channel2\_IRQHandler   \[WEAK\]  
             EXPORT  DMA2\_Channel3\_IRQHandler   \[WEAK\]  
             EXPORT  DMA2\_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

;*******************************************************************************
; User Stack and Heap initialization
;*******************************************************************************
IF :DEF:__MICROLIB

              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

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

startup_stm32f10x_hd.s

要操作STM32寄存器,我们就需要使用C 语言对其封装,这部分程序代码都放在 stm32f10x.h

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章