STM32CubeMX Software Engineering Description_USART Configuration Process

Before I read this article, I can check out my previous article:

This article follows the STM32CubeMX article in conjunction with the USART example to describe the new USART project for STM32CubeMX and the newly completed software project. Finally, the software project for implementing USART serial communication is completed through the STM32CubeMX tool configuration.

This article uses Keil (MDK-ARM) V5 software for the compilation environment. If you do not have the latest version of the software installed, please install the Keil (MDK-ARM) V5 version of the software, the specific process can refer to one of my articles:

See below for more details on this article.

Ⅱ、STM32CubeMXNewUSARTengineering

In the previous article, I briefly described the steps of building a new project based on IO. This article is based on the USART and briefly describes the new UART serial port transmission project of STM32CubeMX.

(We still use the STM32F103E as an example. If your board is a STM32 other chip, you can select it when you select the chip.)

1.Basic steps for new construction

The basic steps in the STM32CubeMX new project are roughly as follows (see my previous article for details):

1. Open the STM32CubeMX software and click on "New Project"

2. Select MCU or select (ST official) development board

3. Engineering configuration

2.PinoutConfiguration

Here we configure IO (defining PF11 pin for LED), RCC (using HSE), USART (serial port).

A. Configure the PF11 pin as the output mode.

The first step: zoom in on the chip icon and find PF11 (you can find it by Find)

Step 2: Configure PF11 as the output -> Left -> Popup List -> Check "GPIO_Output"

B. Configure RCC

There are three PCC configurations, we only configure here.HSEHigh-speed external clock, the other twoLSE external low speed clock,MCO clock output Default configuration.

selectHSE high-speed external clock is "Crystal", the corresponding pin is automatically changed, as shown below:

C. Configure USART (serial port)

There are several configurations of the USART, and this article is based on the most basic and most common type of "UART asynchronous serial communication" configuration.

Select the pin corresponding to “asynchronous” in USART1 mode to be automatically converted to USART.

3.ClockConfiguration

It should be known to the system clock that it is the heart that drives the entire chip. If it is not, it means that people have no heartbeat. The speed of the clock also determines the speed of the program.

STM32's clock tree function is more powerful, frequency division, multiplier can be configured according to your needs a variety of clocks. STM32CubeMX this tool is also more user-friendly in this block, you can visually see the specific configuration, and there are error tips (Note: the clock tree of different models may be different, but similar), we configure HSE, as shown below:

4."comprehensive" configuration

The configuration here is equivalent to initializing some configurations, collecting the initial configuration corresponding to the previous pins, so it is called "comprehensive configuration".

Since we mainly configure IO and USART, the main concern in this option is these two.

A.IO (PF11) configuration

B.USART configuration

5.Finally generate code, compile and download

These steps are not the focus of today, the detailed process can refer to the previous article.

Generated raw code"STM32CubeMX_USART configuration (original version)" can be downloaded from my 360 cloud disk:

6.Add code

Add a "delay function" to the main.c file:

voidTestDelay(uint32_t cnt)

{

  while(cnt–);

}

Add the following code to the main function:

Adding the above code can be achieved: the interval (about 0.5S) LED lights up and changes once, and the serial port prints data.

STM32CubeMX_USART configuration (modified version), available to everyone to download the address (360 cloud disk):

III. Software Engineering Description

Let's talk about the new software project (MDK-ARM version) created with STM32CubeMX.

1.table of Contentsstructure

In the MDK-ARM project, you can see four directories, which are:

HAL library: This library is similar to the standard peripheral library (in the Drivers directory)

The startup file startup_stm32f103xe.s: This file is also similar to the startup_stm32f10x_hd.s file in the standard peripheral library.

User User Application: This directory is the directory where users add source code files. If you want to add new files, you can add them here.

CMSIS Microcontroller Software Interface Standard: This directory is also the underlying code (similar to the software project I used to build with the standard peripheral library, also in the Drivers directory).

2.Add user code area

Open the new software project using STM32CubeMX. You can see that there are pairs of comment codes "USER CODE BEGIN" and "USERCODE END" in many places. The two comments are the area where the user adds their own code. There are several user programming areas under the main.c file:

3.Initialize configuration code description

When we use STM32CubeMX to create a new software project, you can see that there is a lot of code in the software that is automatically generated by the STM32CubeMX tool (which is a feature of STM32CubeMX). The following is a general description of the code generated in this article.

A. Four main initialization codes:

SystemClock_Config();Clock configuration

MX_GPIO_Init();IO initialization (user requirements)

MX_USART1_UART_Init();USART initialization (user requirements)

Among the four main initialization configuration codes, the first two are basically generated code using the STM32CubeMX tool, and the latter two are the code configured by the user according to their own needs.

This is a standard for software engineering, mainly for initializing (default) FLASH and NVIC.

This function will make the program unable to access FLASH, which is why we can't download the code when the program is running. The solution is: 1. Do not call the function; 2. Make the MCU in the reset state to download the code.

When you use it beforeThe standard peripheral library has developed STM32 and knows the clock initialization code, then this function should be familiar. In fact, the configuration here is the RCC configuration (clock tree) mentioned above, and the parameter is the value corresponding to the clock tree. It is easy to understand the clock tree.

Located in the main.c file.

D. MX_GPIO_Init();

This function is our configured IO function (initializing the PF11 pin, very simple), located in the main.c file.

And previous useThe standard peripheral library development is similar, and the parameters are set in the "Integrated Configuration" above.

E. MX_USART1_UART_Init();

This function is the USART we configured and is located in the main.c file.

And previous useThe standard peripheral library development is similar, and the parameters are set in the "comprehensive configuration" above, baud rate, number of data bits, stop bits, and so on.

Description

The above briefly describes the software project generated by STM32CubeMX, and will periodically update the article on using STM32CubeMX to create various peripheral resource projects. Please wait.

If you are a STM32 beginner, it is not recommended to use this tool for development immediately. It is recommended to learn standard peripheral library development first, because the code produced by this tool (HAL library) is relatively difficult to understand compared to the previous standard peripheral library.

The above summary is for reference only, please understand if there is something wrong.

V, last

More exciting articles I will share in the WeChat public account for the first time. If you have any questions about this article, you can leave a message on WeChat.

In the principle of free sharing, it is convenient for everyone to learn knowledge on mobile phones and share technical knowledge on the WeChat platform on a regular basis. If you feel that the content you share is useful to you and would like to know more about the article, please use WeChat search.EmbeddDeveloper" Or scan the QR code below, follow, there will be more exciting content waiting for you.

        </div>
            </div>
        </article>