1. Install the pack support package
pack is a support package file. When your board is connected to the computer, how does keil5 know which model your board is? This requires the use of pack files. Keil official download address of pack file: download | device pack
What I use here is the STM324 series
Then download it directly with one click. After the download is completed, double-click to install. It will automatically detect the installation directory of the keil software on your computer and then install it automatically.
When you create a new project later, there will be a series of options
2. Download the peripheral library
When there was no peripheral library, we needed to manually configure the registers while reading the development manual. Now with the peripheral library, the peripheral library encapsulates the operation of the peripheral registers. There is no need to manually configure the registers, which greatly improves development efficiency.
ST official peripheral library download: STM32 Standard Peripheral Libraries - Products
However, you may need to register first. The registration process is relatively simple. Just enter your name and email address, and then verify it by email. The whole process doesn't take long.
After downloading, you will see a compressed package similar to the one below. It not only contains peripheral library functions, but also provides operation examples for each peripheral.
3. Construction project
The key to building a project is to introduce some necessary files into our project. As for the specific files required for our project, you can refer to the project structure diagram below.
1. Start file
The operating environment needs to be initialized before running the program.
- Stack initialization
- Initialize interrupt vector table
- Call the reset interrupt service function
- Call the SyStemInit function for other initialization
- main function jump
All these operations are completed by the startup file startup_stm32f40xx.s . Generally, .s files are written in assembly. Among them, the SyStemInit function is in the system_stm32f4xx.h file, and the corresponding source file system_stm32f4xx.c can only be found in the official Example.
file name | Location |
startup_stm32f40xx.s | Libraries—CMSIS—Device—ST—STM32F4xx—Source—Templates—arm |
system_stm32f4xx.h | Libraries—CMSIS—Device—ST—STM32F4xx—Include |
system_stm32f4xx.c | Project—STM32F4xx_StdPeriph_Examples—ADC—ADC_DMA |
Note: If it is the stm32f10x series, the startup file will have a suffix. The meaning of the suffix is as follows
2. Peripheral registers and peripheral libraries
The register addresses required for STM32 to operate each peripheral are saved in stm32f4xx.h . Part of the content contained in stm32f4xx.h :
The peripheral library contains specific function implementations for operating peripherals. Among them, misc.c contains some kernel operations, such as modifying the interrupt vector table offset, and the rest of the files are specific implementations of some common peripheral operations.
In order to use the peripheral library more conveniently, ST provides us with a file stm32f4xx_conf.h in the routine , which can introduce all peripherals at once. (If you want to use this header file later, you need to add the macro USE_STDPERIPH_DRIVER)
file name | Location |
stm32f4xx.h | Libraries—CMSIS—Device—ST—STM32F4xx—Include |
Peripheral library | Source file: Libraries—STM32F4xx_StdPeriph_Driver—src Header file: Libraries—STM32F4xx_StdPeriph_Driver—inc |
stm32f4xx_conf.h | Project—STM32F4xx_StdPeriph_Examples—ADC—ADC_DMA |
3. Kernel registers
core_cm4.h mainly involves some kernel configurations. core_cmFunc.h, core_cmInstr.h, core_cmSimd.h are header files related to core_cm4.h
file name | Location |
core_cm4.h | Libraries—CMSIS—Include |
core_cmFunc.h core_cmInstr.h core_cmSimd.h |
Libraries—CMSIS—Include |
4. Interrupt processing function file
The stm32f4xx_it.c file contains specific implementations of some interrupt processing functions, such as SVC interrupts and bus error interrupts.
file name | Location |
Header file: stm32f4xx_it.h Source file: stm32f4xx_it.c |
Project—STM32F4xx_StdPeriph_Examples—ADC—ADC_DMA |
4. Organize
Now that we have a general understanding of what files are needed for a basic project, we need to store these files separately. It would be too messy to pile them all together.
1. Start directory
This directory saves some files required for startup, such as startup_stm32f40xx.s, system_stm32f4xx.h. In addition, we also put some files that we usually do not change here.
2. Library directory
This directory is used to save the header files and source files of the peripheral library. The original inc and src directories are copied directly here.
3. User directory
This directory stores the main function and some files that may need to be used or modified.
5. Add macro definitions to the new Keil project
If you want to use the header file stm32f4xx_conf.h , you need to add the macro USE_STDPERIPH_DRIVER
If an error is reported, you can double-click to jump. The following error is the macro STM32F40_41xxx that needs to be added to the current development board model.
Reference article: