0. The difference between library functions and registers
Essentially the same. You can directly manipulate the registers in the library function template, because the official library-related header files have register definitions. But it is not possible to call library functions in the register template, because the definition of library functions is not introduced.
The purpose of understanding the basic principles of registers is to give us a more in-depth understanding of STM32 related knowledge, so that it can be handy during the development process. If there is a problem with the configuration of the low-level code and need to be debugged, you must have a certain understanding of the register to find the problem. Because the code is debugged, the bottom layer can only view the configuration of the register.
1. Tools used
Development environment: MDK5
firmware library version: V1.4 (for F4)
The download address of the firmware library: https://download.csdn.net/download/mahoon411/13457727
2. New project template
- First create a folder as Template. This is the root folder of the project. In order to store some other files needed by the project conveniently, create the following 5 subfolders: CORE, FWLIB, OBJ, SYSTEM, USER.
As for the names of these folders, they can actually be chosen arbitrarily, and they are just for easy identification.
- Next, open Keil, click Keil's menu: Project -> New Uvision Project, and then locate the directory to the USER subdirectory under the folder Template just created. At the same time, name the project as Template and click Save. Our project The files are saved in the USER folder. The operation process is as follows:
- Next, an interface for selecting Device will appear, which is to select our chip model. Here we locate STM32F407ZG under STMicroelectronics. Select STMicroelectronics→STM32F4 Series→STM32F407→STM32F407ZG (if you are using other series of chips, just select the corresponding model).
Special attention: The content of the corresponding chip will be displayed only after the corresponding device pack is installed in advance. The device pack can be downloaded directly from the official website.
Click OK, MDK will pop up the Manage Run-Time Environment dialog box.
This is a new feature of MDK5. In this interface, you can add the components you need to facilitate the construction of the development environment, but it will not be introduced here. Just click Cancel.
After completing the above steps, the interface of keil's project should be as shown in the figure below.
- The USER directory contains four files, as shown in the figure below.
To explain here, Template.uvprojx is a project file, which is very critical and cannot be easily deleted. The project file generated by MDK51.4 has the suffix .uvprojx.
The Listings and Objects folders are automatically generated by MDK and used to store intermediate files generated during the compilation process. Here, we delete the two folders, and we will use the newly created OBJ folder in the next step to store the intermediate files generated by the compilation. Of course, it doesn't matter if we don't delete these two folders, we just don't use them.
- Next, we will copy the source code files in the official firmware library package to our project directory folder.
Open the official firmware library package and locate the directory of the firmware library package we prepared earlier: \STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Libraries\STM32F4xx_StdPeriph_Driver, copy the src and inc folders under the directory to the FWLib folder we just created.
Src stores the .c file of the firmware library, and inc stores the corresponding .h file. Each peripheral corresponds to a .c file and a .h header file. The content of the file is shown in the figure below.
- Next, we will copy the relevant startup files in the firmware library package to our project directory CORE.
Open the official firmware library package. Navigate to the directory \STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Libraries\CMSIS\Device\ST\STM32F4xx\Source\Templates\arm, and copy the file startup_stm32f40_41xxx.s to the CORE directory.
Then navigate to the directory \STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Libraries\CMSIS\Includ, and copy the four header files inside: core_cm4.h, core_cm4_simd.h, core_cmFunc.h and core_cmInstr.h to the CORE directory as well.
- Next, we need to copy some other header files and source files needed by the project template to our project.
First locate the directory: STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Libraries\CMSIS\Device\ST\STM32F4xx\Include. Copy the two header files stm32f4xx.h and system_stm32f4xx.h to the USER directory. These two header files are two very critical header files for STM32F4 project.
Then enter the directory \STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Project\STM32F4xx_StdPeriph_Templates, and copy the 5 files main.c, stm32f4xx_conf.h, stm32f4xx_it.c, stm32f4xx_it.c, stm32f4xx_it.h, USER_stm32f under the 4xx directory under the directory 4xx.
-
In the previous 7 steps, we copied the required firmware library related files to our project directory.
Next we will add these files to our project.
Open keil, right-click Target1, and select Manage Project Items, as shown in the figure below.
-
In the Project Targets column, we modify the Target name to Template, then delete a SourceGroup1 in the Groups column, and create three Groups: USER, CORE, FWLIB. Then click OK, you can see our Target name and Groups situation as shown below.
Click OK, the project status is shown in the figure below.
-
Next, we add the files we need to the Group.
We follow the method in step 9, right-click and click Tempate, and select Manage Project Items. Then select the group that needs to add files.
In the first step here, we select FWLIB, then click Add Files on the right, navigate to the directory \FWLIB\src we just created, select all the files inside (Ctrl+A), then click Add, and then Close. You can see the files we added under the Files list, as shown in the figure below.
Here is a special file stm32f4xx_fmc.c. This file is only used by STM32F42 and STM32F43 series,
so we need to delete it here (note that stm32f4xx_fmc.c should be deleted, don't delete stm32f4xx_fsmc.c). As
shown in the figure below: After deleting, click OK. Go to the next step
It needs to be explained here that for us to write code, if we only use one of the peripherals, we don't need to add library files for the peripherals that are not used. For example, if I only use GPIO, I can just add stm32f4xx_gpio.c and others don't need to add. Here we add all of them for the convenience of the later, and there is no need to add them each time. Of course, the disadvantage is that the project is too large, the compilation speed is slow, and the user can choose by himself.
- In the same way, locate Groups under CORE and USER, and add the required files.
The file to be added under CORE is startup_stm32f40_41xxx.s (note that the file type is .c when added by default, that is, when adding startup_stm32f40_41xxx.s startup files, you need to select the file type as All files to see this file).
The files to be added under the USER directory are main.c, stm32f4xx_it.c, and system_stm32f4xx.c. In this way, the files we need to add have been added to our project.
Finally, click OK to return to the main project interface.
- Next, we need to set the header file storage path in the MDK.
That is to tell MDK to look for included header files in those directories. This step is very important. If the header file path is not set, then the project will report an error: the header file path cannot be found .
The specific operation is as shown in the figure below. After 5 steps, add the corresponding header file path.
Here we need to add the header file path including: \CORE, \USER\ and \FWLIB\inc. Everyone must be careful here. The header file subdirectory of the firmware library is \FWLIB\inc, not FWLIB\src.
After clicking OK, the C/C++ page changes to.
- Next, for the STM32F40 series of projects, a global macro definition identifier needs to be added.
The method to add is to click the magic wand, enter the C/C++ tab, and then enter the Define input box:
STM32F40_41xxx,USE_STDPERIPH_DRIVER
Note that there are two identifiers STM32F40_41xxx and USE_STDPERIPH_DRIVER, which are separated by commas.
- Next, we need to compile the project. Before compiling, we must first select the directory where the intermediate files are compiled and then compiled.
The method is to click the magic wand, and then select "Select folder for objects..." under the "Output" option, and then select the directory as the OBJ directory we created above.
Then tick all the three option boxes below. Selecting the Create HEX File option requires that HEX files be generated after compilation. The Browse Information option is selected to facilitate us to view some function variable definitions in the project.
- Before compiling, we first replace the contents of the main.c file with the following:
#include "stm32f4xx.h"
void Delay(__IO uint32_t nCount);
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);
}
}
At the same time, delete the 32nd line in the stm32f4xx_it.c file, the introduction of the main.h header file and the 144 line SysTick_Handler function content.
- Next, we click the compile button to compile the project, and we can see that the project compiles through without any errors or warnings.
- At this point, a project template based on firmware library V1.4 is created, and the corresponding hex file is generated under the project's OBJ directory.
- There is also a very important key point here, which is the configuration of the system clock. Here we need to modify the System_stm32f4xx.c file to modify the first-stage frequency division factor M of the PLL to 8, so that the main clock frequency is 168MHz.
PLL_M Here we need to modify it to 8, so that our system clock is 168MHz.
At the same time, we need to modify the external clock HSE_VALUE value in stm32f4xx.h to 8MHz, because our external high-speed clock uses a crystal oscillator of 8M, the specific modification method is as follows:
if there is no plus sign on the left side of your stm32f4xx_it.c file after compilation, Please close keil and reopen the project (this is a bug of keil).
At this point, the project template has been established.