STM32 manually transplants HAL firmware library

1. Three development methods of STM32

The essence of microcontroller development is the operation of registers, and the firmware library is a further encapsulation of registers.

Direct register development: The execution efficiency is the highest, but the development is difficult and inconvenient for transplantation. You need to consult the data manual frequently.

Standard peripheral library development: Encapsulating a complete set of standardized function interfaces in registers greatly reduces the difficulty of development and makes transplantation more convenient. However, maintenance has been stopped at present.

HAL library development: The encapsulation of registers is more abstract, so it is more portable and convenient, and can run on more different platforms, but the execution efficiency is lower

2.HAL library development

STM32CubeMX: It is a graphical development tool that automatically generates code related to stm32 chip initialization. The pins can be configured to generate c code, which greatly saves the time of transplanting the HAL firmware library and configuring various peripherals. It is very Convenient development tools

Transplanting the HAL firmware library: Manually transplanting the HAL firmware library is tedious and time-consuming. You can cut it by yourself. The customized project directory folder structure is clearer, and the understanding of the HAL library is deeper.

3.HAL firmware library package acquisition

Obtain from the official website: STM32CubeMX: Graphical Tools – STMicroelectronics

Baidu network disk acquisition: https://pan.baidu.com/s/1EYSi4A_agQO8rC9t6voaPA?pwd=777w
Extraction code: 777w

Enter the official website interface, click search and enter STM32CubeF1, because the development board used this time is STM32F103C8T6 minimum system board. If it is the f4 series development board, search for STM32CubeF4, and for the h7 series, search for STM32CubeH7

Click on STM32CudeF1

Choose the latest version to download. The download is free, but you need to enter your personal email address to register. If you don’t want to register and log in, you can choose other download methods provided above.

4.HAL firmware package

Unzip the downloaded compressed package to get a folder like this

Simple transplantation only requires two core folders in Drivers, the CMSIS folder and the content files of the STM32F1xx_HAL_Driver folder. CMSIS includes: DSP library, Cortex-M core and its device files, microcontroller Special header files, startup files, special system files, etc. STM32F1xx_HAL_Driver contains: It is the HAL peripheral driver source code, including F1 series HAL library source files and header files. If other series of development boards need to replace the firmware package

Device folder and Include folder are retained in CMSIS

Device can only leave these five files

startup_stm32f103xb.s:The startup file depends on the development board. If it is the STM31F103C8T6 development board, select startup_stm32f103xb.s. For the STM31F103ZET6 development board, select startup_stm32f103xe.s. , select the corresponding startup file according to the development board

stm32f1xx.h and stm32f103xb.h: Microcontroller-specific header files, similarly select the corresponding startup file according to the development board

system_stm32f1xx.c:Specialized system file

Include can only leave these six key files, the Cortex-M core and its device files, and compiler-related header files

The STM32F1xx_HAL_Driver folder contains the function interface (API) that encapsulates all registers. It is recommended to keep all peripheral driver source codes to prevent them from being found when needed later.

Based on CMSIS application file description

STM32 development file structure distribution

5. Start external file migration

DRIVERS:Hardware related and HAL library driver source code

HARDWARE: Peripheral files, such as LED, KEY, BEEP, etc.

MDK-PROJET: MDK project files

SYSTEM: System-level driver code, such as sys.c, delay.c, usart.c, etc.

USERS:main.c, stm32f1xx_hal_conf.h, stm32f1xx_it.c, etc.

Put the STM32Cude firmware package driver source code Drivers into DRIVERS

The three files stm32f1xx_it.c, stm32f1xx_it.h, and stm32f1xx_hal_conf.h must also be transplanted in USERS. stm32f1xx_hal_conf.h can be found in STM32F1xx_HAL_Driver. You need to rename it according to the instructions and put it in. The other two files are in the sample project of the STM32Cude firmware package. middle

The effect of the transplantation is as follows. In the Inc folder above, there is also stm32f1xx_hal_conf.h which can be transplanted directly here. You can also get it by renaming stm32f1xx_hal_conf_template.h in the previous STM32F1xx_HAL_Driver folder.

Create a new text file and rename it to main.c file

6. Add project files

Create a new project

Generate MDK project file, select the project path, store it in the MDK_PROJET folder, and customize the name

Select the chip model. This time we use STM32F103C8T6, so choose STM32F103C8. If it is different, select your corresponding model and click OK.

View already created projects

Manage project files and add customized file directories to the project. Here, create one less MDK-PROJET folder and one more STARTUP project folder to store the startup file startup_stm32f103xb.s. Click OKSave

Add the startup file to the STARTUP project folder. In the DRIVERS folder, in CMSIS, in Device, select All file(*.*) and add startup_stm32f103xb.s

DRIVRS project folder Add system_stm32f1xx.c and a series of hal library interfaces that will be used roughly. You can also add them all.

Add USERS project folder, main.c, stm32f1xx_it.c, add one more stm32f1xx_hal_conf.h

Add the file path

7. Improve the project

Although the project is added, there are still many errors in compilation. Start improving the project, click the magic wand, select Target, configure as follows, then select c/c++ to add macros, STM32F103xB, USE_HAL_DRIVER, indicating the use of chips with this capacity and usage HAL library firmware package, separated by English commas, Note: If the development board is not STM32F103C8T6, other macros must be used; such as STM32F103ZET6, STM32F103xE

Compile again, there is only one error, indicating that there is no main.h header file. Just delete it.

Compile again, there is still a warning, cannot find where this function comes from, add the hal library header file stm32f1xx_hal.h

Compile again, and there is another error, indicating that the program entry cannot be found and the main function is missing. Just add it in main.c. There are no errors in compilation and the transplantation is successful.

8.Writing code

Write code, compile without errors, burn into program

Code:

int main(void)
{
HAL_Init();//Initialize HAL library

GPIO_InitTypeDef gpio_init_struct;
\t
__HAL_RCC_GPIOC_CLK_ENABLE();//Turn on the GPIOC clock
\t
gpio_init_struct.Pin = GPIO_PIN_13; //Select pin 13
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; //Push-pull output
gpio_init_struct.Pull = GPIO_PULLUP; //Pull-up
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; //High speed
\t
HAL_GPIO_Init(GPIOC, & amp;gpio_init_struct);//Initialize pin
\t
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);//Turn on the LED
\t
while(1) {
\t\t
\t
}
}
#include <stdio.h>
#include "stm32f1xx_hal.h"

void Mydelay_ms(int ms)//rough delay function
{
int i,j,k;
for(i=0; i<ms; i + + )
for(j=0; j<100; j + + )
for(k=0; k<100; k + + );
\t
}

int main(void)
{
HAL_Init();//Initialize HAL library

GPIO_InitTypeDef gpio_init_struct;
\t
__HAL_RCC_GPIOC_CLK_ENABLE();//Turn on the GPIOC clock
\t
gpio_init_struct.Pin = GPIO_PIN_13; //Select pin 13
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; //Push-pull output
gpio_init_struct.Pull = GPIO_PULLUP; //Pull-up
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; //High speed
\t
HAL_GPIO_Init(GPIOC, & amp;gpio_init_struct);//Initialize pin
\t
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);//Turn on the LED
\t
while(1) {
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
Mydelay_ms(100);
\t
}
}

Phenomena:

Test completed! ! !