STM32 new project template based on library function

Based on library function version

prepare materials:

a) V3.5 firmware library package: STM32F10x_StdPeriph_Lib_V3.5.0 This is the full version of the firmware library downloaded from the ST official website
, our CD-ROM directory:
Software Information\STM32 Firmware Library Use References\STM32F10x_StdPeriph_Lib_V3.5.0
Our official forum download address: http:/ /openedv.com/posts/list/6054.htm
b) MDK5 development environment (the development environment of our board is currently using this version). There is an installation package under the software directory of our CD
: Software Information\Software\MDK5

Steps:

【1】Create a folder under a certain directory of the computer, and the projects created later can be placed under this folder. Here we create a folder called Template_library.
insert image description here

【2】Create a new folder under Template_library:
USER: Our code project files are placed in the USER directory. Many people like to create a new "Project" directory and put it below. This is also possible. This depends on personal preference. CORE:
use To store core files and startup files
OBJ: to store compilation process files and hex files
STM32F10x_FWLib: as the name suggests, to store library function source files officially provided by ST.
SYSTEM: Stores the common code that we use in each experiment
insert image description here

[3] Open the MDK menu: Project –> New Uvision Project, and then locate the directory under the folder Template_library/USER just created, name the project Template, and click Save.
insert image description here

Next there will be an interface for selecting CPU, which is to select our chip model. Because the STM32 model used by ALIENTEK elite STM32F103 is STM32F103ZET6, here we choose STMicroelectronics→STM32F1 Series→STM32F103→STM32F103ZET6 (if you use other series of chips, just select the corresponding model, special attention: must install the corresponding The device pack will display these contents!! If there is no choice, please close the MDK, and then install the CD: 6, software information\1, software\MDK5\Keil.STM32F1xx_DFP.1.0.5.pack (this installation package).
insert image description here

【4】Click OK, MDK will pop up the Manage Run-Time Environment dialog box, as shown in the figure:
insert image description here

This is a new function of MDK5. In this interface, we can add the components we need to facilitate the construction of the development environment, but we will not introduce it here. So we click Cancel directly to get the interface shown in the figure below:
insert image description here

So far, we have only built a framework, and we need to add startup code and .c files.

【5】Open the D:\code\stm32\Template_library\USER folder, the USER directory contains 2 folders and 2 files, as shown in the figure below:
insert image description here

Here we explain that Template.uvprojx is a project file, which is very critical and cannot be easily deleted. The Listings and Objects folders are folders automatically generated by MDK to store intermediate files generated during the compilation process. Here, we can delete the two folders, and we will create a new OBJ folder in the next step to store the intermediate files for compilation. Of course, it doesn't matter if we don't delete these two folders, it's just that we don't use it.

[6] 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, locate the directory of the firmware library package we prepared before: STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver, copy the src and inc folders under the directory to the STM32F10x_FWLib folder we just created.
src stores the .c file of the firmware library, and inc stores the corresponding .h file. You may wish to open these two file directories and have a look at the files inside. Each peripheral corresponds to a .c file and a .h header file .
insert image description here

【7】 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
STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport, and copy the files core_cm3.c and core_cm3.h to CORE.
Then navigate to the directory: STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm, copy the startup_stm32f10x_hd.s file inside to CORE. Here we have explained that chips with different capacities use different startup files. Our chip STM32F103ZET6 is a large-capacity chip, so choose this startup file.
Now look at the files under our CORE folder:
insert image description here

[8] Navigate to the directory: STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x Next, copy the three files inside stm32f10x.h, system_stm32f10x.c, system_stm32f10x.h to our USER directory.
Then copy the four files main.c, stm32f10x_conf.h, stm32f10x_it.c, stm32f10x_it.h under STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template to the USER directory.
insert image description here

[9] In the first 8 steps, we copied the required firmware library related files to our project directory, and then we will add these files to our project. Right click on Target1 and select Manage Components
insert image description here

【10】In the Targets column, we change the Target name to Template, then delete a Source Group1 in the Groups column, and create three Groups: USER, CORE, and FWLIB. Then click OK, you can see our Target name and Groups situation.
insert image description here
insert image description here

[11] Next, we add the files we need to the Group. We follow the method of step 10, right-click on Tempate, select Manage Components, and then select the Group that needs to add files. Here, we select FWLIB in the first step, then click Add Files on the right, and locate the directory we just created. STM32F1 0x_FWLib/ Under src, select all the files inside (Ctrl+A), then click Add, and then Close.
You can see that the files we added are included under the Files list. 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 the library files of the peripherals that are not used. For example, if I only use GPIO, I can only add stm32f10x_gpio.c and others do not need to be added. Here we add all of them for convenience later, and we don’t need to add them every time. Of course, the disadvantage of this is that the project is too large, and the compilation speed is slow, and users can choose by themselves.
insert image description here

[12] Use the same method to locate Groups under CORE and USER, and add the required files. Here, the files that need to be added under our CORE are core_cm3.c, startup_stm32f10x_hd.s (note that the file type is .c when added by default, that is, when adding the startup_stm32f10x_hd.s startup file, you need to select the file type as All files to You can see this file), the files that need to be added under the USER directory are main.c, stm32f10x_it.c, system_stm32f10x.c.
In this way, the files we need to add have been added to our project, and finally click OK to return to the main interface of the project .

insert image description here
insert image description here

insert image description here

[13] Next, we need to compile the project. Before compiling, we must first select the storage directory for the compiled intermediate files. The method is to click the magic wand, then select "Select folder for objects..." under the "Output" option, and then select the directory as the OBJ directory we created above. Please note here that if we do not set the Output path, then the default compilation intermediate file storage directory is the Objects directory and Listings directory automatically generated by MDK.

insert image description here

[14] Next, we click the compile button to compile the project, and we can see a lot of error reports because the header file cannot be found.
insert image description here

[15] Next, we will tell MDK which paths to search for the required header files, that is, the header file directory. Everyone should pay attention here, for any project, we need to include the paths of all header files referenced in the project. Go back to the project main menu, click the magic wand, a menu comes out, and then click the c/c++ option. Then click the button on the right of Include Paths. A dialog box for adding path pops up, and then we add the three directories above the picture. Remember, keil will only search in the first-level directory, so if there are sub-directories under your directory, remember that the path must locate the last-level sub-directory. Then click OK.
insert image description here

[16] Next, we compile the project again, and we can see that many of the same errors are reported again. Why? This is because the library functions of version 3.5 are selected through macro definitions when configuring and selecting peripherals, so we need to configure a global macro definition variable. According to step 16, locate the c/c++ interface, and then fill in "STM32F10X_HD, USE_STDPERIPH_DRIVER" in the Define input box.
Let me explain here, if you are using medium capacity, change STM32F10X_HD to STM32F10X_MD, and small capacity to STM32F10X_LD. Then click OK.
insert image description here

[17] Before compiling this time, we remember to open main.c under the project USER, copy the following code to main.c to overwrite the existing code, and then compile. (Remember to add a carriage return at the end of the code, otherwise there will be a warning), you can see that this compilation has been successful.

#include "stm32f10x.h"

 
/************************************************
 ALIENTEK 精英STM32F103开发板实验0
 工程模板
 注意,这是手册中的新建工程章节使用的main文件 
 技术支持:www.openedv.com
 淘宝店铺:http://eboard.taobao.com 
 关注微信公众平台微信号:"正点原子",免费获取STM32资料。
 广州市星翼电子科技有限公司  
 作者:正点原子 @ALIENTEK
************************************************/


 void Delay(u32 count)
 {
    
    
   u32 i=0;
   for(;i<count;i++);
 }
 int main(void)
 {
    
    	
  GPIO_InitTypeDef  GPIO_InitStructure;
	 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|
  RCC_APB2Periph_GPIOE, ENABLE);	    //使能PB,PE端口时钟
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;			    //LED0-->PB.5 端口配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	 //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	 //IO口速度为50MHz
  GPIO_Init(GPIOB, &GPIO_InitStructure);			     //初始化GPIOB.5
  GPIO_SetBits(GPIOB,GPIO_Pin_5);					//PB.5 输出高
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;	            //LED1-->PE.5推挽输出
  GPIO_Init(GPIOE, &GPIO_InitStructure);	  	       //初始化GPIO
  GPIO_SetBits(GPIOE,GPIO_Pin_5); 			 //PE.5 输出高 	  
  while(1)
	{
    
    
	  GPIO_ResetBits(GPIOB,GPIO_Pin_5);
	  GPIO_SetBits(GPIOE,GPIO_Pin_5);
		Delay(3000000);
		GPIO_SetBits(GPIOB,GPIO_Pin_5);
		GPIO_ResetBits(GPIOE,GPIO_Pin_5);
		Delay(3000000);
	}
 }

insert image description here

[18] Such a project template is established. The following also needs to be configured so that the hex file can be generated after compilation. Also click the magic wand to enter the configuration menu and select Output. Then tick the next three options. Among them, Create HEX file is to compile and generate a hex file, and Browser Information is to view variable and function definitions.
insert image description here

[19] Recompile the code, you can see that the generated hex file is under the OBJ directory, and we can use flymcu to download this file to the mcu (refer to Section 3.4.2). So far, a project template based on firmware library V3.5 has been established.

【20】In fact, after the previous 19 steps, our project template has been established. However, in the experiments provided by ALIENTEK, each experiment has a SYSTEM folder, and there are 3 subdirectories under it: sys, usart, and delay, which store the common code that we need to use in each experiment. The code is It is written by ALIENTEK. The principle of this code will be explained in detail in our later chapters. We just introduce it into the project here to facilitate the establishment of the project in later experiments.
Using the method we explained in step 13 before, create a new group in the project, name it SYSTEM, and then add the .c files under these three folders as sys.c, delay.c, and usart.c, as shown in the figure below:
insert image description here

Next, we will add the corresponding three directories (sys, usart, delay) to the PATH, because there are corresponding .h header files under each directory, please refer to step 15, the screenshot after adding is
insert image description here

Finally click OK. In this way, our project template is completely completed, so that we can call the functions in the SYSTEM folder provided by ALIENTEK.

Guess you like

Origin blog.csdn.net/weixin_42581177/article/details/131158233