首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

STM32固件库详解(6)

STM32固件库详解(6)

4. 使用步骤

前面几个小节已经详细介绍了标准外设库的组成结构以及部分主要文件的功能描述,那么如果在开发中使用标准外设库需要哪些描述呢?下面就进行简要的介绍,这儿介绍的使用方法是与开发环境无关的,在不同的开发环境中可能在操作方式上略有不同,但是总体的流程都是一样的,下一小节将介绍在MDK ARM开发环境下使用标准外设库的详细过程。
首先新建一个项目并设置工具链对应的启动文件,可以使用标准外设库中提供的模板,也可以自己根据自己的需求新建。标准外设库中已经提供了不同工具链对应的文件,位于STM32F10x_StdPeriph_Lib_V3.4.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup目录下。
其次按照使用产品的具体型号选择具体的启动文件,加入工程。文件主要按照使用产品的容量进行区分,根据产品容量进行选择即可。每个文件的具体含义可以在“stm32f10x.h”文件中找到对应的说明,摘录如下:
#if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)
/* #define STM32F10X_LD */ /*!< STM32F10X_LD: STM32 Low density devices */
/* #define STM32F10X_LD_VL */ /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */
/* #define STM32F10X_MD */ /*!< STM32F10X_MD: STM32 Medium density devices */
/* #define STM32F10X_MD_VL */ /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */ /* #define STM32F10X_HD */ /*!< STM32F10X_HD: STM32 High density devices */
/* #define STM32F10X_HD_VL */ /*!< STM32F10X_HD_VL: STM32 High density value line devices */
/* #define STM32F10X_XL */ /*!< STM32F10X_XL: STM32 XL-density devices */
/* #define STM32F10X_CL */ /*!< STM32F10X_CL: STM32 Connectivity line devices */
#endif
/* Tip: To avoid modifying this file each time you need to switch between these
devices, you can define the device in your toolchain compiler preprocessor.
- Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
where the Flash memory density ranges between 16 and 32 Kbytes.
- Low-density value line devices are STM32F100xx microcontrollers where the Flash
memory density ranges between 16 and 32 Kbytes.
- Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
where the Flash memory density ranges between 64 and 128 Kbytes.
- Medium-density value line devices are STM32F100xx microcontrollers where the
Flash memory density ranges between 64 and 128 Kbytes.
- High-density devices are STM32F101xx and STM32F103xx microcontrollers where
the Flash memory density ranges between 256 and 512 Kbytes.
- High-density value line devices are STM32F100xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
- XL-density devices are STM32F101xx and STM32F103xx microcontrollers where
the Flash memory density ranges between 512 and 1024 Kbytes.
- Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
*/
“stm32f10x.h”是整个标准外设库的入口文件,这个文件包含了STM32F10x全系列所有外设寄存器的定义(寄存器的基地址和布局)、位定义、中断向量表、存储空间的地址映射等。为了是这个文件适用于不同系列的产品,程序中是通过宏定义来实现不同产品的匹配的,上面这段程序的注释中已经详细给出了每个启动文件所对应的产品系列,与之对应,也要相应的修改这个入口文件,需要根据所使用的产品系列正确的注释/去掉相应的注释define。在这段程序的下方同样有这样的一个注释程序/*#define USE_STDPERIPH_DRIVER*/ 用于选择是否使用标准外设库,如果保留这个注释,则用户开发程序可以基于直接访问“stm32f10x.h”中定义的外设寄存器,所有的操作均基于寄存器完成,目前不使用固件库的单片机开发,如51、AVR、MSP430等其实都是采用此种方式,通过在对应型号的头文件中进行外设寄存器等方面的定义,从而在程序中对相应的寄存器操作完成相应的功能设计。
如果去掉/*#define USE_STDPERIPH_DRIVER*/的注释,则是使用标准外设库进行开发,用户需要使用在文件“stm32f10x_conf.h”中,选择要用的外设,外设同样是通过注释/去掉注释的方式来选择。示例程序如下:
/* Uncomment the line below to enable peripheral header file inclusion */
#include "stm32f10x_adc.h"
/* #include "stm32f10x_bkp.h" */
/* #include "stm32f10x_can.h" */
/* #include "stm32f10x_cec.h" */
/* #include "stm32f10x_crc.h" */
/* #include "stm32f10x_dac.h" */
/* #include "stm32f10x_dbgmcu.h" */
#include "stm32f10x_dma.h"
/* #include "stm32f10x_exti.h" */
/* #include "stm32f10x_flash.h" */
/* #include "stm32f10x_fsmc.h" */
#include "stm32f10x_gpio.h"
/* #include "stm32f10x_i2c.h" */
/* #include "stm32f10x_iwdg.h" */
/* #include "stm32f10x_pwr.h" */
#include "stm32f10x_rcc.h"
/* #include "stm32f10x_rtc.h" */
/* #include "stm32f10x_sdio.h" */
/* #include "stm32f10x_spi.h" */
/* #include "stm32f10x_tim.h" */
/* #include "stm32f10x_usart.h" */
/* #include "stm32f10x_wwdg.h" */
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
上面一段程序来自于例程中的AD采集程序,程序使用了AD和DMA,因此去掉相应的注释,同时几乎所有的应用都需要使用复位与时钟以及通用I/O,因此这两项是必须的,
而多数程序同样要使用NVIC中断IRQ设置和SysTick时钟源设置,那么 “misc.h”这一项也是必须的。
上面已经针对具体的产品信号和程序功能进行了针对性的配置,接下来需要配置系统所使用的时钟,系统时钟在“system_stm32f10x.c”同样通过注释的方式来配置,程序如下:
#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
#define SYSCLK_FREQ_24MHz 24000000
#else
/* #define SYSCLK_FREQ_HSE HSE_VALUE */
/* #define SYSCLK_FREQ_24MHz 24000000 */
/* #define SYSCLK_FREQ_36MHz 36000000 */
/* #define SYSCLK_FREQ_48MHz 48000000 */
/* #define SYSCLK_FREQ_56MHz 56000000 */
#define SYSCLK_FREQ_72MHz 72000000
#endif
如果这儿没有明确的定义那么HSI时钟将会作为系统时钟。
至此,已经配置了系统的主要外部参数,这些参数主要是通过更改相关的宏定义来实现的,有些开发环境,例如Keil支持在软件设置中加入全局宏定义,因此像芯片系列定义,是否使用固件库定义等也可以通过软件添加来实现。
完成了主要参数配置以后就可以进行程序的开发了,标准外设库开发就可以使用标准外设库中提供的方便的API函数进行相应的功能设计了。在4.2.2小节中已经介绍了基于标准外设库开发的优势,配置完成后,程序中仍然可以直接更改相应寄存器的配置,通过对寄存器的操作可以提高程序的效率,因此可以使用标准外设库和寄存器操作两种相结合的方式。
继承事业,薪火相传
返回列表