STM32创建静态库lib
创建静态库lib
- 1. 新建工程
- 1.1 创建工程文件夹
- 1.2 编写用户相关代码
- 1.2.1 stm32f4xx_it.h
- 1.2.2 stm32f4xx_it.c
- 1.2.3 标准库配置:stm32f4xx_conf.h
- 1.2.4 HAL库的配置:stm32f4xx_hal_conf.h
- 1.2.5 LL库配置:stm32f4xx_ll_conf.h
- 1.3 移植通用文件
- 1.3.1 标准外设库SPL
- 1.3.2 HAL库
- 1.3.3 LL库
- 1.4 修改相关文件(SPL/HAL/LL库)
- 1.4.1 启动文件 startup_stm32f40xx.s
- 1.5 将移植的文件添加到工程中
- 1.5.1 标准外设库SPL
- 1.5.2 HAL库
- 1.5.3 LL库
- 2. 创建lib库
- 2.1 创建标准外设库SPL库
- 2.2 创建HAL库
- 2.3 创建LL库
1. 新建工程
1.1 创建工程文件夹
在电脑的某个目录下建立一个 Template 文件夹,后面所建立的工程都放在这个文件夹下。在 Template 目录下新建下列目录:
新建工程:
1.2 编写用户相关代码
1.2.1 stm32f4xx_it.h
#ifndef __STM32F4xx_IT_H #define __STM32F4xx_IT_H#ifdef __cplusplus extern "C" { #endif void NMI_Handler(void); void HardFault_Handler(void); void MemManage_Handler(void); void BusFault_Handler(void); void UsageFault_Handler(void); void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void);#ifdef __cplusplus } #endif#endif /* __STM32F4xx_IT_H */
1.2.2 stm32f4xx_it.c
内核相关中断
#include "stm32f4xx_it.h"void NMI_Handler(void) { }void HardFault_Handler(void) {while (1) {} }void MemManage_Handler(void) {while (1) {} }void BusFault_Handler(void) {while (1) {} }void UsageFault_Handler(void) {while (1) {} }void SVC_Handler(void) { }void DebugMon_Handler(void) { }void PendSV_Handler(void) { }void SysTick_Handler(void) { }
1.2.3 标准库配置:stm32f4xx_conf.h
stm32f4xx.h
文件中会引用stm32f4xx_conf.h
,最重要的是实现assert_param函数
//同 标准库SPL中的实例一模一样 #ifndef __STM32F4xx_CONF_H #define __STM32F4xx_CONF_H#include "stm32f4xx_adc.h" #include "stm32f4xx_crc.h" #include "stm32f4xx_dbgmcu.h" #include "stm32f4xx_dma.h" #include "stm32f4xx_exti.h" #include "stm32f4xx_flash.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_i2c.h" #include "stm32f4xx_iwdg.h" #include "stm32f4xx_pwr.h" #include "stm32f4xx_rcc.h" #include "stm32f4xx_rtc.h" #include "stm32f4xx_sdio.h" #include "stm32f4xx_spi.h" #include "stm32f4xx_syscfg.h" #include "stm32f4xx_tim.h" #include "stm32f4xx_usart.h" #include "stm32f4xx_wwdg.h" #include "misc.h"#if defined (STM32F429_439xx) #include "stm32f4xx_cryp.h" #include "stm32f4xx_hash.h" #include "stm32f4xx_rng.h" #include "stm32f4xx_can.h" #include "stm32f4xx_dac.h" #include "stm32f4xx_dcmi.h" #include "stm32f4xx_dma2d.h" #include "stm32f4xx_fmc.h" #include "stm32f4xx_ltdc.h" #include "stm32f4xx_sai.h" #endif /* STM32F429_439xx */#if defined (STM32F427_437xx) #include "stm32f4xx_cryp.h" #include "stm32f4xx_hash.h" #include "stm32f4xx_rng.h" #include "stm32f4xx_can.h" #include "stm32f4xx_dac.h" #include "stm32f4xx_dcmi.h" #include "stm32f4xx_dma2d.h" #include "stm32f4xx_fmc.h" #include "stm32f4xx_sai.h" #endif /* STM32F427_437xx */#if defined (STM32F40_41xxx) #include "stm32f4xx_cryp.h" #include "stm32f4xx_hash.h" #include "stm32f4xx_rng.h" #include "stm32f4xx_can.h" #include "stm32f4xx_dac.h" #include "stm32f4xx_dcmi.h" #include "stm32f4xx_fsmc.h" #endif /* STM32F40_41xxx */#ifdef USE_FULL_ASSERT#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))void assert_failed(uint8_t* file, uint32_t line); #else#define assert_param(expr) ((void)0) #endif #endif
1.2.4 HAL库的配置:stm32f4xx_hal_conf.h
stm32f4xx_hal.h
文件中会引用stm32f4xx_hal_conf.h
,最重要的是实现assert_param函数
//同 HAL 库中的实例一模一样 #ifndef __STM32F4xx_HAL_CONF_H #define __STM32F4xx_HAL_CONF_H#ifdef __cplusplus extern "C" { #endif#define HAL_MODULE_ENABLED #define HAL_ADC_MODULE_ENABLED #define HAL_CAN_MODULE_ENABLED /* #define HAL_CAN_LEGACY_MODULE_ENABLED */ #define HAL_CRC_MODULE_ENABLED #define HAL_CRYP_MODULE_ENABLED #define HAL_DAC_MODULE_ENABLED #define HAL_DCMI_MODULE_ENABLED #define HAL_DMA_MODULE_ENABLED /* #define HAL_DMA2D_MODULE_ENABLED */ /* #define HAL_ETH_MODULE_ENABLED */ #define HAL_EXTI_MODULE_ENABLED #define HAL_FLASH_MODULE_ENABLED #define HAL_NAND_MODULE_ENABLED #define HAL_NOR_MODULE_ENABLED #define HAL_PCCARD_MODULE_ENABLED #define HAL_SRAM_MODULE_ENABLED /* #define HAL_SDRAM_MODULE_ENABLED */ #define HAL_HASH_MODULE_ENABLED #define HAL_GPIO_MODULE_ENABLED #define HAL_I2C_MODULE_ENABLED #define HAL_I2S_MODULE_ENABLED #define HAL_IWDG_MODULE_ENABLED /* #define HAL_LTDC_MODULE_ENABLED */ #define HAL_PWR_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED #define HAL_RNG_MODULE_ENABLED #define HAL_RTC_MODULE_ENABLED /* #define HAL_SAI_MODULE_ENABLED */ #define HAL_SD_MODULE_ENABLED #define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED #define HAL_USART_MODULE_ENABLED #define HAL_IRDA_MODULE_ENABLED #define HAL_SMARTCARD_MODULE_ENABLED #define HAL_WWDG_MODULE_ENABLED #define HAL_CORTEX_MODULE_ENABLED #define HAL_PCD_MODULE_ENABLED #define HAL_HCD_MODULE_ENABLED#if !defined (HSE_VALUE) #define HSE_VALUE (8000000U) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */#if !defined (HSE_STARTUP_TIMEOUT)#define HSE_STARTUP_TIMEOUT (100U) /*!< Time out for HSE start up, in ms */ #endif /* HSE_STARTUP_TIMEOUT */#if !defined (HSI_VALUE)#define HSI_VALUE (16000000U) /*!< Value of the Internal oscillator in Hz*/ #endif /* HSI_VALUE */#if !defined (LSI_VALUE) #define LSI_VALUE (32000U) #endif /* LSI_VALUE */ #if !defined (LSE_VALUE) #define LSE_VALUE (32768U) /*!< Value of the External Low Speed oscillator in Hz */ #endif /* LSE_VALUE */#if !defined (LSE_STARTUP_TIMEOUT)#define LSE_STARTUP_TIMEOUT (5000U) /*!< Time out for LSE start up, in ms */ #endif /* LSE_STARTUP_TIMEOUT */#if !defined (EXTERNAL_CLOCK_VALUE)#define EXTERNAL_CLOCK_VALUE (12288000U) /*!< Value of the External oscillator in Hz*/ #endif /* EXTERNAL_CLOCK_VALUE */#define VDD_VALUE (3300U) /*!< Value of VDD in mv */ #define TICK_INT_PRIORITY (0x0FU) /*!< tick interrupt priority */ #define USE_RTOS 0U #define PREFETCH_ENABLE 0U /* The prefetch will be enabled in SystemClock_Config(), depending on the used STM32F405/415/07/417 device: RevA (prefetch must be off) or RevZ (prefetch can be on/off) */ #define INSTRUCTION_CACHE_ENABLE 1U #define DATA_CACHE_ENABLE 1U#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */ #define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */ #define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */ #define USE_HAL_CRYP_REGISTER_CALLBACKS 0U /* CRYP register callback disabled */ #define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */ #define USE_HAL_DCMI_REGISTER_CALLBACKS 0U /* DCMI register callback disabled */ #define USE_HAL_DFSDM_REGISTER_CALLBACKS 0U /* DFSDM register callback disabled */ #define USE_HAL_DMA2D_REGISTER_CALLBACKS 0U /* DMA2D register callback disabled */ #define USE_HAL_DSI_REGISTER_CALLBACKS 0U /* DSI register callback disabled */ #define USE_HAL_ETH_REGISTER_CALLBACKS 0U /* ETH register callback disabled */ #define USE_HAL_HASH_REGISTER_CALLBACKS 0U /* HASH register callback disabled */ #define USE_HAL_HCD_REGISTER_CALLBACKS 0U /* HCD register callback disabled */ #define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */ #define USE_HAL_FMPI2C_REGISTER_CALLBACKS 0U /* FMPI2C register callback disabled */ #define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */ #define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */ #define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U /* LPTIM register callback disabled */ #define USE_HAL_LTDC_REGISTER_CALLBACKS 0U /* LTDC register callback disabled */ #define USE_HAL_MMC_REGISTER_CALLBACKS 0U /* MMC register callback disabled */ #define USE_HAL_NAND_REGISTER_CALLBACKS 0U /* NAND register callback disabled */ #define USE_HAL_NOR_REGISTER_CALLBACKS 0U /* NOR register callback disabled */ #define USE_HAL_PCCARD_REGISTER_CALLBACKS 0U /* PCCARD register callback disabled */ #define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */ #define USE_HAL_QSPI_REGISTER_CALLBACKS 0U /* QSPI register callback disabled */ #define USE_HAL_RNG_REGISTER_CALLBACKS 0U /* RNG register callback disabled */ #define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */ #define USE_HAL_SAI_REGISTER_CALLBACKS 0U /* SAI register callback disabled */ #define USE_HAL_SD_REGISTER_CALLBACKS 0U /* SD register callback disabled */ #define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */ #define USE_HAL_SDRAM_REGISTER_CALLBACKS 0U /* SDRAM register callback disabled */ #define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */ #define USE_HAL_SPDIFRX_REGISTER_CALLBACKS 0U /* SPDIFRX register callback disabled */ #define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U /* SMBUS register callback disabled */ #define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */ #define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */ #define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */ #define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */ #define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */#define USE_SPI_CRC 1U#ifdef HAL_RCC_MODULE_ENABLED#include "stm32f4xx_hal_rcc.h" #endif /* HAL_RCC_MODULE_ENABLED */#ifdef HAL_EXTI_MODULE_ENABLED#include "stm32f4xx_hal_exti.h" #endif /* HAL_EXTI_MODULE_ENABLED */#ifdef HAL_GPIO_MODULE_ENABLED#include "stm32f4xx_hal_gpio.h" #endif /* HAL_GPIO_MODULE_ENABLED */#ifdef HAL_DMA_MODULE_ENABLED#include "stm32f4xx_hal_dma.h" #endif /* HAL_DMA_MODULE_ENABLED */#ifdef HAL_CORTEX_MODULE_ENABLED#include "stm32f4xx_hal_cortex.h" #endif /* HAL_CORTEX_MODULE_ENABLED */#ifdef HAL_ADC_MODULE_ENABLED#include "stm32f4xx_hal_adc.h" #endif /* HAL_ADC_MODULE_ENABLED */#ifdef HAL_CAN_MODULE_ENABLED#include "stm32f4xx_hal_can.h" #endif /* HAL_CAN_MODULE_ENABLED */#ifdef HAL_CAN_LEGACY_MODULE_ENABLED#include "stm32f4xx_hal_can_legacy.h" #endif /* HAL_CAN_LEGACY_MODULE_ENABLED */#ifdef HAL_CRC_MODULE_ENABLED#include "stm32f4xx_hal_crc.h" #endif /* HAL_CRC_MODULE_ENABLED */#ifdef HAL_CRYP_MODULE_ENABLED#include "stm32f4xx_hal_cryp.h" #endif /* HAL_CRYP_MODULE_ENABLED */#ifdef HAL_DMA2D_MODULE_ENABLED#include "stm32f4xx_hal_dma2d.h" #endif /* HAL_DMA2D_MODULE_ENABLED */#ifdef HAL_DAC_MODULE_ENABLED#include "stm32f4xx_hal_dac.h" #endif /* HAL_DAC_MODULE_ENABLED */#ifdef HAL_DCMI_MODULE_ENABLED#include "stm32f4xx_hal_dcmi.h" #endif /* HAL_DCMI_MODULE_ENABLED */#ifdef HAL_ETH_MODULE_ENABLED#include "stm32f4xx_hal_eth.h" #endif /* HAL_ETH_MODULE_ENABLED */#ifdef HAL_FLASH_MODULE_ENABLED#include "stm32f4xx_hal_flash.h" #endif /* HAL_FLASH_MODULE_ENABLED */#ifdef HAL_SRAM_MODULE_ENABLED#include "stm32f4xx_hal_sram.h" #endif /* HAL_SRAM_MODULE_ENABLED */#ifdef HAL_NOR_MODULE_ENABLED#include "stm32f4xx_hal_nor.h" #endif /* HAL_NOR_MODULE_ENABLED */#ifdef HAL_NAND_MODULE_ENABLED#include "stm32f4xx_hal_nand.h" #endif /* HAL_NAND_MODULE_ENABLED */#ifdef HAL_PCCARD_MODULE_ENABLED#include "stm32f4xx_hal_pccard.h" #endif /* HAL_PCCARD_MODULE_ENABLED */ #ifdef HAL_SDRAM_MODULE_ENABLED#include "stm32f4xx_hal_sdram.h" #endif /* HAL_SDRAM_MODULE_ENABLED */#ifdef HAL_HASH_MODULE_ENABLED #include "stm32f4xx_hal_hash.h" #endif /* HAL_HASH_MODULE_ENABLED */#ifdef HAL_I2C_MODULE_ENABLED #include "stm32f4xx_hal_i2c.h" #endif /* HAL_I2C_MODULE_ENABLED */#ifdef HAL_I2S_MODULE_ENABLED #include "stm32f4xx_hal_i2s.h" #endif /* HAL_I2S_MODULE_ENABLED */#ifdef HAL_IWDG_MODULE_ENABLED #include "stm32f4xx_hal_iwdg.h" #endif /* HAL_IWDG_MODULE_ENABLED */#ifdef HAL_LTDC_MODULE_ENABLED #include "stm32f4xx_hal_ltdc.h" #endif /* HAL_LTDC_MODULE_ENABLED */#ifdef HAL_PWR_MODULE_ENABLED #include "stm32f4xx_hal_pwr.h" #endif /* HAL_PWR_MODULE_ENABLED */#ifdef HAL_RNG_MODULE_ENABLED #include "stm32f4xx_hal_rng.h" #endif /* HAL_RNG_MODULE_ENABLED */#ifdef HAL_RTC_MODULE_ENABLED #include "stm32f4xx_hal_rtc.h" #endif /* HAL_RTC_MODULE_ENABLED */#ifdef HAL_SAI_MODULE_ENABLED #include "stm32f4xx_hal_sai.h" #endif /* HAL_SAI_MODULE_ENABLED */#ifdef HAL_SD_MODULE_ENABLED #include "stm32f4xx_hal_sd.h" #endif /* HAL_SD_MODULE_ENABLED */#ifdef HAL_SPI_MODULE_ENABLED #include "stm32f4xx_hal_spi.h" #endif /* HAL_SPI_MODULE_ENABLED */#ifdef HAL_TIM_MODULE_ENABLED #include "stm32f4xx_hal_tim.h" #endif /* HAL_TIM_MODULE_ENABLED */#ifdef HAL_UART_MODULE_ENABLED #include "stm32f4xx_hal_uart.h" #endif /* HAL_UART_MODULE_ENABLED */#ifdef HAL_USART_MODULE_ENABLED #include "stm32f4xx_hal_usart.h" #endif /* HAL_USART_MODULE_ENABLED */#ifdef HAL_IRDA_MODULE_ENABLED #include "stm32f4xx_hal_irda.h" #endif /* HAL_IRDA_MODULE_ENABLED */#ifdef HAL_SMARTCARD_MODULE_ENABLED #include "stm32f4xx_hal_smartcard.h" #endif /* HAL_SMARTCARD_MODULE_ENABLED */#ifdef HAL_WWDG_MODULE_ENABLED #include "stm32f4xx_hal_wwdg.h" #endif /* HAL_WWDG_MODULE_ENABLED */#ifdef HAL_PCD_MODULE_ENABLED #include "stm32f4xx_hal_pcd.h" #endif /* HAL_PCD_MODULE_ENABLED */#ifdef HAL_HCD_MODULE_ENABLED #include "stm32f4xx_hal_hcd.h" #endif /* HAL_HCD_MODULE_ENABLED */#ifdef USE_FULL_ASSERT#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))void assert_failed(uint8_t* file, uint32_t line); #else#define assert_param(expr) ((void)0U) #endif /* USE_FULL_ASSERT */#ifdef __cplusplus } #endif#endif
1.2.5 LL库配置:stm32f4xx_ll_conf.h
stm32f4xx_hal.h
文件中会引用stm32f4xx_ll_conf.h
,最重要的是实现assert_param函数
//自行创建 #ifndef __STM32F4xx_LL_CONF_H #define __STM32F4xx_LL_CONF_H#ifdef __cplusplus extern "C" { #endif#define "stm32f4xx_ll_adc.h" #define "stm32f4xx_ll_crc.h" #define "stm32f4xx_ll_dac.h" #define "stm32f4xx_ll_dma.h" #define "stm32f4xx_ll_dma2d.h" #define "stm32f4xx_ll_exti.h" #define "stm32f4xx_ll_fmc.h" #define "stm32f4xx_ll_fmpi2c.h" #define "stm32f4xx_ll_fsmc.h" #define "stm32f4xx_ll_gpio.h" #define "stm32f4xx_ll_i2c.h" #define "stm32f4xx_ll_lptim.h" #define "stm32f4xx_ll_pwr.h" #define "stm32f4xx_ll_rcc.h" #define "stm32f4xx_ll_rng.h" #define "stm32f4xx_ll_rtc.h" #define "stm32f4xx_ll_sdmmc.h" #define "stm32f4xx_ll_spi.h" #define "stm32f4xx_ll_tim.h" #define "stm32f4xx_ll_usart.h" #define "stm32f4xx_ll_usb.h" #define "stm32f4xx_ll_utils.h"#ifdef USE_FULL_ASSERT#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))void assert_failed(uint8_t* file, uint32_t line); #else#define assert_param(expr) ((void)0U) #endif /* USE_FULL_ASSERT */#ifdef __cplusplus } #endif#endif
1.3 移植通用文件
1.3.1 标准外设库SPL
Drivers/SPL/BSP目录
下存放用户开发的相应驱动文件,如LED、Beep等。
Drivers/SPL/CMSIS目录
下存放下列文件 :
2.1 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>CMSIS–>Device–>ST–>STM32F4xx–>Include :
stm32f4xx.h、system_stm32f4xx.h
2.2 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates : system_stm32f4xx.c
2.3 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates–>arm : startup_stm32f40xx.s
2.4 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>CMSIS–>Include :
core_cm4.h、core_cmFunc.h、core_cmInstr.h、core_cmSimd.h
Drivers/SPL/STM32F4xx_SPL_Driver目录
下存放下列文件 :
3.1 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>STM32F4xx_StdPeriph_Driver–>inc : stm32f4xx_ppp.h
3.2 STM32F4xx_DSP_StdPeriph_Lib_V1.8.0–>Libraries–>STM32F4xx_StdPeriph_Driver–>src : stm32f4xx_ppp.c
User目录
下存放下列文件 :
用户自己编写源码文件, stm32f4xx_conf.h、stm32f4xx_it.c、stm32f4xx_it.h
1.3.2 HAL库
Drivers/HAL/BSP目录
下存放用户开发的相应驱动文件,如LED、Beep等。
Drivers/HAL/CMSIS目录
下存放下列文件:
2.1 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Include :
stm32f4xx.h、stm32f407xx.h、system_stm32f4xx.h
2.2 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates : system_stm32f4xx.c
2.3 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates–>arm : startup_stm32f407xx.s
2.4 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Include :
cmsis_armcc.h、cmsis_armclang.h、cmsis_compiler.h、cmsis_version.h、core_cm4.h、mpu_armv7.h
Drivers/HAL/STM32F4xx_HAL_Driver目录
下存放下列文件:
3.1 STM32Cube_FW_F4_V1.26.0–>Drivers–>STM32F4xx_HAL_Driver–>Inc : stm32f4xx_hal_ppp.h
3.2 STM32Cube_FW_F4_V1.26.0–>Drivers–>STM32F4xx_HAL_Driver–>Src : stm32f4xx_hal_ppp.c
User目录
下存放下列文件 :
用户自己编写源码文件, stm32f4xx_hal_conf.h、stm32f4xx_it.h、stm32f4xx_it.h
1.3.3 LL库
Drivers/LL/BSP目录
下存放用户开发的相应驱动文件,如LED、Beep等。
Drivers/LL/CMSIS目录
下存放下列文件 :
2.1 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Include :
stm32f4xx.h、stm32f407xx.h、system_stm32f4xx.h
2.2 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates : system_stm32f4xx.c
2.3 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Device–>ST–>STM32F4xx–>Source–>Templates–>arm : startup_stm32f407xx.s
2.4 STM32Cube_FW_F4_V1.26.0–>Drivers–>CMSIS–>Include :
cmsis_armcc.h、cmsis_armclang.h、cmsis_compiler.h、cmsis_version.h、core_cm4.h、mpu_armv7.h
Drivers/LL/STM32F4xx_LL_Driver目录
下存放下列文件 :
User目录
下存放下列文件 :
用户自己编写源码文件, stm32f4xx_ll_conf.h、stm32f4xx_it.h、stm32f4xx_it.h
1.4 修改相关文件(SPL/HAL/LL库)
1.4.1 启动文件 startup_stm32f40xx.s
屏蔽
SystemInit
函数的调用,在外部实现系统时钟
的配置。Reset_Handler PROCEXPORT Reset_Handler [WEAK];IMPORT SystemInitIMPORT __main//寄存器版本代码,因为没有用到 SystemInit 函数,所以注释掉//库函数版本代码,建议加上这里(外部必须实现 SystemInit 函数),以初始化 stm32 时钟等。;LDR R0, =SystemInit;BLX R0 LDR R0, =__mainBX R0ENDP
1.5 将移植的文件添加到工程中
1.5.1 标准外设库SPL
④选择AC5编译器,因为AC6编译器对中文支持和代码兼容较差,编译会出现很多告警。
全局宏变量:STM32F40_41xxx,USE_STDPERIPH_DRIVER
移植所需的所有文件:
Bsp 空 CMSIS system_stm32f4xx.c
、startup_stm32f40xx.s
Driver 只添加下列文件: stm32f4xx_fmc.c
不添加到工程,否则编译会报错。CMiddlewares 空 User stm32f4xx_it.c
、main.c
//main.c文件内容如下: #include "stm32f4xx.h" void Delay(__IO uint32_t nCount) {while(nCount--){} } int main(void) {GPIO_InitTypeDef GPIO_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOF, &GPIO_InitStructure);while(1){GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);Delay(0x7FFFFF);GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);Delay(0x7FFFFF);} }
1.5.2 HAL库
同标准外设库SPL类似,需要修改如下:
- 全局宏变量:
STM32F407xx,USE_HAL_DRIVER
- 头文件路径:
- 移植所需的所有文件
Bsp 空 CMSIS system_stm32f4xx.c
、startup_stm32f407xx.s
Driver 只添加下列文件:
或添加所有文件,除stm32f4xx_hal_timebase_rtc_alarm_template.c、stm32f4xx_hal_timebase_rtc_wakeup_template.c、stm32f4xx_hal_timebase_tim_template.c
不添加到工程,否则编译会报错。
添加LL库文件是由于HAL库文件中会引用一些LL库的C文件。CMiddlewares 空 User stm32f4xx_it.c
、main.c
//main.c文件内容如下: #include "stm32f4xx.h" #include "core_cm4.h" #include "stm32f4xx_hal.h" void Delay(__IO uint32_t nCount) {while(nCount--){} } int main(void) {HAL_Init(); GPIO_InitTypeDef gpio_init_struct;__HAL_RCC_GPIOF_CLK_ENABLE(); gpio_init_struct.Pin = GPIO_PIN_9; gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;gpio_init_struct.Pull = GPIO_PULLUP; gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOF, &gpio_init_struct); gpio_init_struct.Pin = GPIO_PIN_10;HAL_GPIO_Init(GPIOF, &gpio_init_struct); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9 ,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_SET);while(1){HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET); // LED0亮HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_SET); // LED1灭Delay(0x1FFFFF);HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_SET); // LED0灭HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_RESET); // LED1亮Delay(0x1FFFFF);} }
1.5.3 LL库
同标准外设库SPL类似,需要修改如下:
全局宏变量:
USE_FULL_LL_DRIVER,STM32F407xx
头文件路径:
移植所需的所有文件
Bsp 空 CMSIS system_stm32f4xx.c
、startup_stm32f407xx.s
Driver 添加下列文件: CMiddlewares 空 User stm32f4xx_it.c
、main.c
//main.c文件内容如下: #include "stm32f4xx_ll_rcc.h" #include "stm32f4xx_ll_bus.h" #include "stm32f4xx_ll_system.h" #include "stm32f4xx_ll_exti.h" #include "stm32f4xx_ll_cortex.h" #include "stm32f4xx_ll_utils.h" #include "stm32f4xx_ll_pwr.h" #include "stm32f4xx_ll_dma.h" #include "stm32f4xx.h" #include "stm32f4xx_ll_gpio.h"void SystemClock_Config(void) {LL_FLASH_SetLatency(LL_FLASH_LATENCY_5);if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_5){}LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);LL_RCC_HSI_SetCalibTrimming(16);LL_RCC_HSI_Enable();while(LL_RCC_HSI_IsReady() != 1) {}LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, LL_RCC_PLLM_DIV_8, 168,LL_RCC_PLLP_DIV_2);LL_RCC_PLL_Enable();while(LL_RCC_PLL_IsReady() != 1) {}LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_4);LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_2);LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL){}LL_Init1msTick(168000000);LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);LL_SetSystemCoreClock(168000000); }int main(void) {LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);NVIC_SetPriorityGrouping(0x00000003);SystemClock_Config();LL_GPIO_InitTypeDef GPIO_InitStruct = {0};LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH);LL_GPIO_ResetOutputPin(GPIOF, LL_GPIO_PIN_9|LL_GPIO_PIN_10);GPIO_InitStruct.Pin = LL_GPIO_PIN_9|LL_GPIO_PIN_10;GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(GPIOF, &GPIO_InitStruct);while (1){LL_GPIO_TogglePin (GPIOF,LL_GPIO_PIN_9);LL_GPIO_SetOutputPin(GPIOF,LL_GPIO_PIN_10);LL_mDelay(500);LL_GPIO_ResetOutputPin(GPIOF,LL_GPIO_PIN_10);LL_mDelay(500);} }
编译会报错,将文件
stm32f4xx_ll_fmc.c、stm32f4xx_ll_fsmc.c、stm32f4xx_ll_sdmmc.c、stm32f4xx_ll_usb.c
中的下列内容注释://#include "stm32f4xx_hal.h"
2. 创建lib库
2.1 创建标准外设库SPL库
将创建lib库所需的下列所有头文件存放到
C:\Keil_v5\ARM\inc
目录下,如果没有此目录可自行创建。
2.2 创建HAL库
将创建lib库所需的下列所有头文件存放到
C:\Keil_v5\ARM\hal_inc
目录下,如果没有此目录可自行创建。
添加步骤同 创建标准外设库SPL库
2.3 创建LL库
将创建lib库所需的下列所有头文件存放到
C:\Keil_v5\ARM\ll_inc
目录下,如果没有此目录可自行创建。
添加步骤同 创建标准外设库SPL库