AN2953
应用笔记
如何从 STM32F10xxx 固件库 V2.0.3
升级为 STM32F10xxx 标准外设库 V3.0.0
介绍
本文的目的是介绍如何把基于STM32F10xxx固件库V2.0.3(FWLib)开发的应用程序升级到
STM32F10xxx标准外设库V3.0.0。本文的目的不是提供2个版本的详细信息,而是重点介绍2个
版本之间的差异。
注意:
文中,除非特别指明,固件库
(FWLib)
代表
STM32F10xxx
固件库
V2.0.3
,而标准外设库
(StdPeriph_Lib)
代表
STM32F10xxx
标准外设库
V3.0.0
。
术语表
小容量产品是指闪存存储器容量在16K至32K字节之间的STM32F101xx,STM32F102xx和
STM32F103xx微控制器。
中容量产品是指闪存存储器容量在64K至128K字节之间的STM32F101xx,STM32F102xx和
STM32F103xx微控制器。
大容量产品是指闪存存储器容量在256K至512K字节之间的STM32F101xx和STM32F103xx微控
制器。
译注:
本译文的英文版下载地址为:
http://www.st.com/stonline/products/literature/an/15531.pdf
STM32F10xxx标准外设库V3.0.0下载地址为:
http://www.st.com/stonline/products/support/micro/files/stm32f10x_stdperiph_lib_v3.0.0.zip
自动升级脚本下载地址为:
http://www.st.com/stonline/products/support/micro/files/an2953.zip
参照2009年4月 AN2953 英文第1版
本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本
如何从STM32F10xxx固件库V2.0.3升级为STM32F10xxx标准外设库V3.0.0
目录
1 为什么把STM32F10xxx固件库(FWLib)V2.0.3 升级为标准外设库(StdPeriph_Lib)V3.0.0 ····· 3
1.1 兼容ARM
®
Cortex-M3
TM
微控制器软件接口标准(CMSIS)···························································· 3
1.1.1 CMSIS描述·························································································································· 3
1.1.2 CMSIS结构·························································································································· 4
1.1.3 STM32固件库V2.0.3和CMSISV1.10对比············································································ 5
1.2 STM32F10xxx标准外设库:Doxygen格式 ················································································· 5
1.3 STM32F10xxx标准外设库体系结构 ···························································································· 6
1.4 STM32F10xxx标准外设库体系结构:文件包含关系··································································· 6
1.5 STM32F10xxx固件库(FWLib)V2.0.3存档 ··················································································· 7
2 STM32F10xxx标准外设库包·································································································· 8
3 STM32F10xxx标准外设库变动列表 ····················································································· 11
3.1 STM32F10xxx标准外设库文件 ································································································· 11
3.1.1 库的内核文件······················································································································11
3.1.2 库的外设驱动······················································································································11
3.1.3 库的用户和工具链专用文件 ······························································································· 12
3.1.4 库的例程 ···························································································································· 12
3.2 代码的书写规则和惯例·············································································································· 12
3.2.1 数据类型和IO类型限定词 ·································································································· 12
3.2.2 异常的命名 ························································································································ 13
3.3 外设驱动更新 ···························································································································· 14
3.3.1 NVIC·································································································································· 14
3.3.2 SysTick ······························································································································ 15
3.3.3 CAN··································································································································· 16
3.4 如何使用STM32F10xxx标准外设库 ·························································································· 16
4 使用自动脚本的升级示例 ···································································································· 19
4.1 如何使用自动脚本 ····················································································································· 19
4.2 使用自动脚本的升级步骤 ·········································································································· 19
附录A 固件库(FWLib)V2.0.3 升级到标准外设库(StdPeriph_Lib)V3.0.0 的具体步骤·············· 21
2/22
参照2009年4月 AN2953 英文第1版
本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本
如何从STM32F10xxx固件库V2.0.3升级为STM32F10xxx标准外设库V3.0.0
1 为什么把STM32F10xxx固件库(FWLib)V2.0.3升级为标
准外设库(StdPeriph_Lib)V3.0.0
STM32F10xxx固件库(FWLib)V2.0.3是一个完整的固件包,它适用于STM32F10xxx小容量,中
容量和大容量产品。固件库由程序,数据结构和宏组成,覆盖了所有外设的特征,还包括了全
部标准外设的驱动和一系列示例程序。
STM32F10xxx标准外设库(StdPeriph_Lib)V3.0.0由固件库(FWLib)V2.0.3升级而来:
● 它使库与Cortex
TM
微控制器软件接口标准(CMSIS)兼容
● 改进了库包的体系结构
● 源代码符合Doxygen格式
● 升级不影响STM32外设驱动的API(应用编程接口)
注意:
标准外设库
(StdPeriph_Lib)V3.0.0
只对
STM32F10xxx CAN
驱动进行了升级,目的是支持即将面
世的
STM32F10xxx
连接型产品
(
带双
CAN)
。
要升级到STM32F10xxx标准外设库V3.0.0,用户只需要更新:
● 与工具链相关的文件
● 项目(project)设置
● 库文件的位置
● 用户无需改变或者更新应用程序的代码
下文详细描述了标准外设库(StdPeriph_Lib)的所有更新细节。
1.1 兼容ARM
®
Cortex-M3
TM
微控制器软件接口标准(CMSIS)
CMSIS可以解决用户在基于Cortex-M0/Cortex-M1或者Cortex-M3内核的微控制器上进行软件开
发时可能遇到的种种问题。CMSIS还可以扩展,应用在将来的Cortex-M系列处理器内核上(标准
称为Cortex-Mx)。CMSIS是ARM公司与多家不同的芯片和软件供应商一起紧密合作定义的,提
供了内核与外设、实时操作系统和中间设备之间的通用接口。可以访问网站www.onarm.com
获
取更多细节。
1.1.1 CMSIS描述
CMSIS可以分为多个软件层次,ARM提供了下列部分,可用于多种编译器:
● 内核设备访问层:包含了用来访问内核的寄存器设备的名称定义,地址定义和助手函数。
同时也为RTOS(实时操作系统)定义了独立于微控制器的接口,该接口包括调试通道定义。
● 中间设备访问层:为软件提供了访问外设的通用方法。芯片供应商应当修改中间设备访问
层,以适应中间设备组件用到的微控制器上的外设。目前中间设备访问层仍处于开发过程
中,本文不做详述。
芯片供应商扩展下列软件层:
● 微控制器外设访问层:提供片上所有外设的定义。
● 外设的访问函数(可选):为外设提供额外的助手函数。
CMSIS为Cortex-Mx微控制器系统定义了:
● 访问外设寄存器的通用方法和定义异常向量的通用方法。
● 内核设备的寄存器名称和内核异常向量的名称
● 独立于微控制器的RTOS接口,带调试通道
● 中间设备组件接口(TCP/IP协议栈,闪存文件系统)
3/22
参照2009年4月 AN2953 英文第1版
本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本
如何从STM32F10xxx固件库V2.0.3升级为STM32F10xxx标准外设库V3.0.0
1.1.2 CMSIS结构
图1展示了基于CMSIS的应用程序的基本结构。
图1 CMSIS层结构
CMSIS-外设访问层的文件
独立于编译器的文件:
● Cortex-M3内核及其设备文件(core_cm3.h + core_cm3.c)
─ 访问 Cortex-M3 内核及其设备:NVIC,SysTick 等
─ 访问 Cortex-M3 的 CPU 寄存器和内核外设的函数
● 微控制器专用头文件(device.h)
─ 指定中断号码(与启动文件一致)
─ 外设寄存器定义(寄存器的基地址和布局)
─ 控制微控制器其他特有的功能的函数(可选)
● 微控制器专用系统文件(system_device.c)
─ 函数 SystemInit,用来初始化微控制器
─ 函数 Sysem_ExtMemCtl,用来配置外部存储器控制器。它位于文件
startup_stm32f10x_xx.s /.c,在跳转到 main 前调用
─ SystemFrequncy,该值代表系统时钟频率
─ 微控制器的其他功能(可选)
编译器供应商
+
微控制器专用启动文件
● 编译器启动代码(汇编或者C)(startup_device.s)
─ 微控制器专用的中断处理程序列表(与头文件一致)
─ 弱定义(Weak)的中断处理程序默认函数(可以被用户代码覆盖)
注意:
关键字
weak
指示编译器弱导出符号。该关键字可以用于函数和变量的声明,以及函数的定义。
使用
__weak
定义的函数将弱导出其符号。除非将相同名称的非弱定义函数链接到相同映像
上,否则弱定义函数的行为与正常定义的函数类似。
如果非弱定义函数和弱定义函数位于相同
4/22
参照2009年4月 AN2953 英文第1版
本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本
如何从STM32F10xxx固件库V2.0.3升级为STM32F10xxx标准外设库V3.0.0
映像中,则会将对弱定义函数的所有调用解析为对非弱函数的调用。如果有多个可用的弱定
义,链接器将选择其中的一个弱定义供所有调用使用。
1.1.3 STM32 固件库V2.0.3 和CMSISV1.10 对比
CMSIS对STM32F10xxx固件库(FWLib)的某些功能采用了不同的实现方法。下面列出主要的区
别:
● 使用标准C类型,文件<stdint.h>
● 对每一个Cortex-M3异常和STM32的IRQ,有:
─ 异常服务程序带后缀_Handler,中断服务程序带后缀_IRQHandler。
─ 弱定义(Weak)的默认异常/中断服务程序,包含一个无限循环
─ 带_IRQn 后缀的中断号码“#define”
● 启动文件更名为”startup_stm32f10x_xx.s/.c”,其中xx可以是hd,md或者ld,分别对应大容
量,中容量,小容量产品。
● 只提供精简的NVIC和SysTick函数,其他一些常用函数作为一个新的驱动加入
STM32F10xxx标准外设库,文件命名为misc.h/.c。
● 某些宏的名字与STM32F10xxx固件库V2.0.3中的相同功能宏不同(见表1)
表1 STM32F10xxx固件库V2.0.3与CMSIS宏对比
(1)
STM32宏 CMSIS宏 STM32宏 CMSIS宏
- __NOP
__RESETPRIMASK __enable_irq
__WFI __WFI
__SETPRIMASK __disable_irq
__WFE __WFE
__get_PRIMASK
__SEV __SEV
__READ_PRIMASK
__set_PRIMASK(val)
__ISB __ISB
__RESETFAULTMASK __enable_fault_irq
__DSB __DSB
__SETFAULTMASK __disable_fault_irq
__DMB __DMB
__get_FAULTMASK
__SVC -
__READ_FAULTMASK
__set_FAULTMASK(val)
__MRS_CONTROL __get_CONTROL __BASEPRICONFIG __set_BASEPRI
__MSR_CONTROL __set_CONTROL __GetBASEPRI __get_BASEPRI
__MRS_PSP __get_PSP __REV_HalfWord __REV16
__MSR_PSP __set_PSP __REV_Word __REV
__MRS_MSP __get_MSP - __REVSH
__MSR_MSP __set_MSP - __RBIT
1. 加粗的字体表示这些宏有变化。灰色的充填色表示这些改动影响到固件库(FWLib)的驱动或者例程。
1.2 STM32F10xxx标准外设库:Doxygen格式
STM32F10xxx标准外设库的源代码采用了新的格式,所有源文件都按照doxygen格式书写,用
这种书写格式的代码能够很便利地生成更加规范且内在关联性更强的文档。
ST将从意法半导体微控制器网站(www.st.com/mcu
)撤下现有的STM32F10xxx固件库用户手册
UM0427,而由Doxygen生成的CHM文件取而代之。该CHM文件完整地描述了STM32F10xxx标
准外设库的全部组件。
Doxygen例程:
/**
* @brief Reads the specified input port pin.
* @param GPIOx: where x can be (A..G) to select the GPIO
peripheral.
* @param GPIO_Pin: specifies the port bit to read.
* This parameter can be GPIO_Pin_x where x can be (0..15).
5/22
参照2009年4月 AN2953 英文第1版
本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本