STM32F4的相关资料:http://www.stmcu.org/document/list/index/category-523
一、标准固件库简介
本文下载的是STM32F4xx_DSP_StdPeriph_Lib_V1.5.0,其文件夹如下图所示:
其中Project文件夹为各个开发环境(MDK-ARM、EWARM、TrueSTUDIO)的模板工程,Utilities文件夹为官方评估板的标准固件库应用例程,而真正的标准固件库在Libraries文件夹中。在Libraries文件夹中:CMSIS文件夹主要包含于内核相关的文件;STM32F4xx_StdPeriph_Driver文件夹为STM32F4xx处理器外设相关的底层驱动。
以下为固件库移植时CMSIS文件夹中的重要源文件:
core_cm4.h :内核功能的定义,比如NVIC相关寄存器的结构体和Systick配置。在CMSIS/Include中
core_cmFunc.h :内核核心功能接口头文件。在CMSIS/Include中
core_cmInstr.h :包含一些内核核心专用指令。在CMSIS/Include中
core_cmSimd.h :包含与编译器相关的处理。在CMSIS/Include中
stm32f4xx.h :包含了stm32f4的寄存器结构体的定义(类似于c51的reg52.h)。在CMSIS\Device\ST\STM32F4xx\Include中
system_stm32f4xx.h :system_stm32f4xx.c的头文件。在CMSIS\Device\ST\STM32F4xx\Include中
system_stm32f4xx.c :stm32f4的系统时钟配置。在CMSIS\Device\ST\STM32F4xx\Source\Templates中
startup_stm32f40_41xxx.s:启动文件:设定SP的初始值;设置PC的初始值;设置中断向量表的地址;配置时钟;设置堆栈;调用main。
这个 启动文件先调用system_stm32f4xx.c里面的systeminit()在调用main()之前。
在CMSIS\Device\ST\STM32F4xx\Source\Templates\arm中。
对于STM32F4xx_StdPeriph_Driver,其重要源文件为:
stm32f4xx_ppp.h:外设头文件。这里的ppp只是一个代码,在实际上是具体的外设名字,如ADC,DMA等。在实际使用时根据所需的外设选择性移植。
在STM32F4xx_StdPeriph_Driver\inc中
stm32f4xx_ppp.c:外设源文件。这里的ppp只是一个代码,在实际上是具体的外设名字,如ADC,DMA等。在实际使用时根据所需的外设选择性移植。
在STM32F4xx_StdPeriph_Driver\src中
stm32f4xx_conf.h:外设驱动配置文件。通过修改该文件中所包含的外设头文件,用户启动或禁用外设驱动。
此外,在此文件夹打开宏定义USE_FULL_ASSERT,通过预处理启 用或禁用标准外设库运行时的故障检测。
在Project\STM32F4xx_StdPeriph_Templates中。
stm32f4xx_it.h :头文件。包括所有中断处理程序原型。在Project\STM32F4xx_StdPeriph_Templates中。
stm32f4xx_it.c :中断源程序模板,中断函数的名称要与启动文件中中断向量表的名称一致。在Project\STM32F4xx_StdPeriph_Templates中。
此外,对于stm32f4的标准外设固件库,在CMSIS文件夹中还有相应的DSP库:DSP_Lib和Lib。DSP_Lib主要为DSP函数库的实例和源码;Lib为编译好的,且对于不同内核的STM32系列MCU所使用的lib文件。
二、标准固件库移植过程中的注意事项(这里以仅以Keil-MDK为例)
1、在stm32f4xx.h文件中选择芯片以及宏定义USE_STDPERIPH_DRIVER。
#if !defined(STM32F40_41xxx) && !defined(STM32F427_437xx) && !defined(STM32F429_439xx) && !defined(STM32F401xx) && !defined(STM32F411xE) && \ !defined(STM32F446xx) #define STM32F40_41xxx /*!< STM32F405RG, STM32F405VG, STM32F405ZG, STM32F415RG, STM32F415VG, STM32F415ZG, STM32F407VG, STM32F407VE, STM32F407ZG, STM32F407ZE, STM32F407IG, STM32F407IE, STM32F417VG, STM32F417VE, STM32F417ZG, STM32F417ZE, STM32F417IG and STM32F417IE Devices */ /* #define STM32F427_437xx */ /*!< STM32F427VG, STM32F427VI, STM32F427ZG, STM32F427ZI, STM32F427IG, STM32F427II, STM32F437VG, STM32F437VI, STM32F437ZG, STM32F437ZI, STM32F437IG, STM32F437II Devices */ /* #define STM32F429_439xx */ /*!< STM32F429VG, STM32F429VI, STM32F429ZG, STM32F429ZI, STM32F429BG, STM32F429BI, STM32F429NG, STM32F439NI, STM32F429IG, STM32F429II, STM32F439VG, STM32F439VI, STM32F439ZG, STM32F439ZI, STM32F439BG, STM32F439BI, STM32F439NG, STM32F439NI, STM32F439IG and STM32F439II Devices */ /* #define STM32F401xx */ /*!< STM32F401CB, STM32F401CC, STM32F401RB, STM32F401RC, STM32F401VB, STM32F401VC STM32F401CD, STM32F401RD, STM32F401VD, STM32F401CExx, STM32F401RE and STM32F401VE Devices */ /* #define STM32F411xE */ /*!< STM32F411CD, STM32F411RD, STM32F411VD, STM32F411CE, STM32F411RE and STM32F411VE Devices */ /* #define STM32F446xx */ /*!< STM32F446MC, STM32F446ME, STM32F446RC, STM32F446RE, STM32F446VC, STM32F446VE, STM32F446ZC and STM32F446ZE Devices */ #endif /* Old STM32F40XX definition, maintained for legacy purpose */ #ifdef STM32F40XX #define STM32F40_41xxx #endif /* STM32F40XX */ /* Old STM32F427X definition, maintained for legacy purpose */ #ifdef STM32F427X #define STM32F427_437xx #endif /* STM32F427X */ /* 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. */ #if !defined(STM32F40_41xxx) && !defined(STM32F427_437xx) && !defined(STM32F429_439xx) && !defined(STM32F401xx) && !defined(STM32F411xE) && \ !defined(STM32F446xx) #error "Please select first the target STM32F4xx device used in your application (in stm32f4xx.h file)" #endif #if !defined (USE_STDPERIPH_DRIVER) /** * @brief Comment the line below if you will not use the peripherals drivers. In this case, these drivers will not be included and the application code will be based on direct access to peripherals registers */ #define USE_STDPERIPH_DRIVER #endif /* USE_STDPERIPH_DRIVER */
2、根据实际的外部晶振修改stm32f4xx.h文件中默认的系统外部晶振时钟HSE_VALU。在本例中使用了16MHz的外部晶振,因此设为16000000。
#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) || defined(STM32F411xE) #if !defined (HSE_VALUE) #define HSE_VALUE ((uint32_t)16000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ #elif defined(STM32F446xx) #if !defined (HSE_VALUE) #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F411xE */ /** * @brief In the following line adjust the External High Speed oscillator (HSE) Startup Timeout value */ #if !defined (HSE_STARTUP_TIMEOUT) #define HSE_STARTUP_TIMEOUT ((uint16_t)0x05000) /*!< Time out for HSE start up */ #endif /* HSE_STARTUP_TIMEOUT */ #if !defined (HSI_VALUE) #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ #endif /* HSI_VALUE */
3、修改system_stm32f4xx.c文件中PLL_M,PLL_Q,PLL_N和PLL_P。
对于STM32F40_41xxx,系统时钟为168MHZ,一般只需修改PLL_M的值,PLL_N默认为336(若主时钟为168MHz则N值默认336,若主时钟为180MHz则N值默认360),PLL_P默认为2,PLL_Q默认为7。
/************************* PLL Parameters *************************************/ #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F401xx) /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */ #define PLL_M 16 #elif defined (STM32F446xx) #define PLL_M 8 #elif defined (STM32F411xE) #if defined(USE_HSE_BYPASS) #define PLL_M 8 #else #define PLL_M 16 #endif /* USE_HSE_BYPASS */ #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */ /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ #define PLL_Q 7 #if defined(STM32F446xx) /* PLL division factor for I2S, SAI, SYSTEM and SPDIF: Clock = PLL_VCO / PLLR */ #define PLL_R 7 #endif /* STM32F446xx */ #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) #define PLL_N 336 /* SYSCLK = PLL_VCO / PLL_P */ #define PLL_P 2 #endif /* STM32F40_41xxx || STM32F427_437x || STM32F429_439xx || STM32F446xx */ #if defined(STM32F401xx) #define PLL_N 336 /* SYSCLK = PLL_VCO / PLL_P */ #define PLL_P 4 #endif /* STM32F401xx */ #if defined(STM32F411xE) #define PLL_N 400 /* SYSCLK = PLL_VCO / PLL_P */ #define PLL_P 4 #endif /* STM32F411xx */ /******************************************************************************/
#if defined(STM32F40_41xxx) uint32_t SystemCoreClock = 168000000; #endif /* STM32F40_41xxx */ #if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F446xx) uint32_t SystemCoreClock = 180000000; #endif /* STM32F427_437x || STM32F429_439xx || STM32F446xx */ #if defined(STM32F401xx) uint32_t SystemCoreClock = 84000000; #endif /* STM32F401xx */ #if defined(STM32F411xE) uint32_t SystemCoreClock = 100000000; #endif /* STM32F401xx */
4、若需要使用FPU,则还需要在system_stm32f4xx.c中的SystemInit函数开头添加相关语句,并在keil工程配置中选择use FPU(Use Single Precision)
注意:__FPU_PRESENT == 1在stm32f4xx.h中已默认设置,__FPU_USED == 1在core_cm4.h中已默认设置。
/* FPU settings ------------------------------------------------------------ * If no this settings,it maybe enter HardFault_Handler() interrupt when mdk complier chose "Use FPU"(or "Use Single Precision") to use FPU instruction. */ #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ #endif