基本知识框架
课堂笔记
正常复位后的启动流程
STM32在复位上电后,会立即执行main函数吗?
并不是,在执行main函数前,STM32会依次经历:
- 硬件选择启动模式
- 读取启动模式对应存储器的中断向量表
- 初始化栈
- 初始化PC指针,指向Reset Handler
- 初始化系统时钟
- 执行C库函数__main,进入C语言的世界,执行main函数
在main函数之前执行的这些流程,被称为STM32的启动流程 。虽然这个流程较难理解,但是是必须要掌握
硬件
STM32单片机引出的引脚中有BOOT0和BOOT1两个引脚,通过在这两个引脚上加上不同的电平,可以使得STM32进入不同的启动模式
BOOT0 | BOOT1 | 存储器 | 存储器对应地址 |
---|
0 | X | 内部Flash(常用) | 0x0800 0000 | 1 | 0 | 系统存储器(存储厂家编写的ISP程序,只读,运行ISP程序可以串口传入的数据保存到内部Flash中) | 0x1FFF 0000 | 1 | 1 | 内部sRam(断电后代码数据会丢失,可用于执行临时代码) | 0x2000 0000 |
软件
经过硬件判断后,STM32会选择对应存有代码的存储介质(如内部Flash),并开始从存储器基地址开始读取数据,寻找中断向量表
中断 中断指的是STM32在执行当前任务时,遇到异常情况,需要保存当前任务的进度,去处理其他任务,处理完异常情况再返回处理原任务的这种行为。 例如一个人在吃饭(当前任务),此时他的快递到了(异常情况),他放下碗筷(保存进度),去拿了快递(执行异常任务),再返回家中吃饭(返回执行原任务),这就是“中断”
中断向量表 对于机器而言,遇到的异常情况不会过于复杂,可以通过中断向量表将所有的突发情况汇总 中断向量表本质上是一个32位整数数组,它存储了发生异常时,需要执行的对应任务函数的地址,当发生异常情况时,会将对应的地址赋值给PC寄存器,进而执行对应任务
以下是Flash中存储的中断向量表 优先级:优先级标号越小,中断优先级越高 MSP:Main Stack Pointer,指的是主堆栈指针
以选择了Flash存储器为例,在找到中断向量表后,STM32由地址0x0000 0000开始执行代码,首先要执行的任务是初始化主堆栈指针MSP,下一步是执行复位后的中断Reset(复位后重新上电也是一种异常情况),对应的处理函数是Reset Handler(),Reset Handler要做的事主要有两个:
- 通过SystemInit()函数,初始化系统时钟
- 通过C库函数__main,执行用户函数main
以上就是STM32单片机的大致启动流程
Flash启动文件
STM32的启动流程中的软件部分,可以通过修改启动文件stm32f10x_xx.s去配置,根据不同的机型和可用资源,xx可以分为: 不同的启动文件,内容都大同小异。其主要的任务是
- 初始化堆
- 初始化栈
- 配置中断处理函数Reset Handler
- 配置系统时钟
- 通过C库函数__main,运行用户函数main
- 用户堆栈配置
以中容量的startup_stm32f10x_hd.s文件为例,以下是文件内容:
;******************** (C) COPYRIGHT 2011 STMicroelectronics ********************
;* File Name : startup_stm32f10x_md.s
;* Author : MCD Application Team
;* Version : V3.5.0
;* Date : 11-March-2011
;* Description : STM32F10x Medium 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
;* - 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
; <h> Stack Configuration
; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Stack_Size EQU 0x00000400 ; 定义栈的大小为1KB
AREA STACK, NOINIT, READWRITE, ALIGN=3 ; 分配数据段,数据段名称为`STACK`,该数据段不进行初始化,可以读写,8(2^3)字节对齐
Stack_Mem SPACE Stack_Size ; 为栈分配1KB的空间
__initial_sp ; 栈顶地址,栈是由高地址向低地址生长的
; 堆初始化
; <h> Heap Configuration
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>
Heap_Size EQU 0x00000200 ; 定义堆的大小为256B
AREA HEAP, NOINIT, READWRITE, ALIGN=3 ; 分配数据段,数据段名称为`HEAP`,该数据段不进行初始化,可以读写,8(2^3)字节对齐
__heap_base ; 堆的起始地址
Heap_Mem SPACE Heap_Size ; 为堆分配256B的空间
__heap_limit ; 堆的结束地址
PRESERVE8 ; 当前文件的堆栈,按照8字节对齐
THUMB ; 表示后面指令兼容 THUMB 指令。THUBM 是 ARM 以前的指令集,16bit,现在Cortex-M系列的都使用 THUMB-2 指令集,THUMB-2 是 32 位的,兼容 16 位和 32 位的指令,是 THUMB的超集
; 配置中断向量表,从地址0x0000 0000开始
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY ; 分配数据段,数据段名称为`RESET`,该数据段只读
EXPORT __Vectors ; 定义外部标号__Vectors,从外部文件读取
EXPORT __Vectors_End ; 定义外部标号__Vectors_End,从外部文件读取
EXPORT __Vectors_Size ; 定义外部标号__Vectors_Size,从外部文件读取
;为中断处理函数的地址分配内存空间,存到向量表中(注意:存储的是地址),具体内容可以到STM32中文参考手册中查看
__Vectors DCD __initial_sp ; Reserve
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 0 ; Reserve
DCD 0 ; Reserve
DCD 0 ; Reserve
DCD 0 ; Reserve
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserve
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 DMA1_Channel1_IRQHandler ; DMA1 Channel 1
DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
DCD ADC1_2_IRQHandler ; ADC1_2
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 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
__Vectors_End ; 中断向量表结束
__Vectors_Size EQU __Vectors_End - __Vectors ; 计算向量表大小
; 代码段
AREA |.text|, CODE, READONLY ; 分配代码段,数据段名称为`|.text|`,定义代码段,该数据段只读
; 复位中断函数
; Reset handler
Reset_Handler PROC ; 定义函数,PROC...ENDP表示函数的开始和收尾,中间是函数内容
EXPORT Reset_Handler [WEAK] ; 使得Reset Handler函数拥有全局属性,外部其他函数也可以使用此函数
IMPORT __main ; 说明此函数是由外部定义的
IMPORT SystemInit
LDR R0, =SystemInit ; 外部SystemInit函数,初始化系统时钟
BLX R0
LDR R0, =__main ; 通过C库函数__main,可以进入
BX R0
ENDP
; Dummy Exception Handlers (infinite loops which can be modified)
; [WEAK]指令会优先使用外部的链接符号,但是没有原型,编译器也不会出现报错
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]
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
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 2011 STMicroelectronics *****END OF FILE*****
基本知识框架Xmind文件下载
链接:资源下载
|