STM32CubeMXA installation and project creation

STM32CubeMXA installation and project creation

Install STM32CubeMXA

The construction of the STM32CubeMX operating environment consists of two parts. The first is the Java runtime environment installation, followed by
the STM32CubeMX software installation.

Install JAVA environment

For the Java operating environment, you can download the latest Java software from the Java official website www.java.com.
After the installation is complete, the prompt interface is shown in Figure 10.2.1.1 below.
insert image description here
After installing the Java runtime environment, in order to check whether the installation is normal, we can open the Windows command input
box and input: java –version command, if the Java version information is displayed, the installation is successful. The prompt information is as shown in Figure 10.2.1.2:
insert image description here

Install STM32CubeMX

After installing the Java runtime environment, next we install the STM32CubeMX graphical tool and
download it directly from the ST official website. The download address is: https://www.st.com/en/development-tools/stm32cubemx.html.
Next, we directly double-click SetupSTM32CubeMX-6.3.0.exe, the installation steps are as follows.
insert image description here
insert image description here
insert image description here
insert image description here

Use STM32CubeMX to create a new project

Step 1: Open STM32CubeMX

Step 2: Download and associate the STM32Cube firmware package

We know that there is only one STM32CubeMX graphics tool, but there are many STM32Cube firmware packages. We need to choose the
firmware package corresponding to our project.
For convenience, before creating a new project, we first download and associate the STM32Cube firmware package, click Help->Manage
embedded software packages, as shown in Figure 10.3.2.1.
insert image description here
Then pop up the management interface, find the STM32F4 list option in this window, and check the 1.26.0 version. Here we choose version 1.26.0
because the firmware package of our CD is of this version. There are two methods for associating the STM32Cube firmware package, as
shown in Figure 10.3.2.2
insert image description here
Method 2: After downloading, it will be automatically associated, so there is no need to say anything.
Method 1: After clicking, the following window will pop up, and then select the corresponding firmware package in the CD. Note that this is in the
form of a compressed package, as shown in Figure 10.3.2.3.
insert image description here
insert image description here

Step 3: New Construction

The general steps to configure a project using STM32CubeMX are:

  1. Preliminary establishment of the project
  2. HSE and LSE clock source settings
  3. Clock System (Clock Tree) Configuration
  4. GPIO function pin configuration
  5. Cortex-M4 core basic configuration (limited items)
  6. Generate project source code
  7. User program
    Next, we will teach you to use the STM32CubeMX tool to generate a complete project according to the above 7 steps
    .

Preliminary establishment of the project

Method 1: Click "File" and then "New Project" to create a new project. If it has been opened before,
there will be a list of open projects in the recently opened process column on the left, and you can also open them by directly clicking on these projects.
Method 2: Click ACCESS TO MCU SELECTOR directly.
The specific operation is shown in Figure 10.3.3.1.
insert image description here
After clicking the new project, some files may be downloaded from the Internet for the first time, and the waiting time may be relatively long, so you can directly choose to
cancel. insert image description here
Then you can enter the chip selection interface, as shown in Figure 10.3.3.3. insert image description here
Select the specific chip model, as shown in Figure 10.3.3.4.
insert image description here
After selecting the chip model, the main design interface will pop up, as shown in Figure 10.3.3.5.insert image description here

HSE and LSE clock source settings

After entering the project main design interface, first set the clock source HSE and LSE. As shown in Figure 10.3.3.6.
insert image description here
For labels 3 and 4 in Figure 10.3.3.6, we have selected Crystal/Ceramic Resonator, indicating that external crystal oscillators are used as
their clock sources. The external high-speed crystal oscillator and external low-speed crystal oscillator of our development board are: 8MHz and 32.768KHz respectively, so the
HSE clock frequency is 8MHz, and the LSE clock frequency is 32.768KHz.
The option Master Clock Output 1 is used to select whether to enable the clock output of the MCO1 pin, the option Master Clock
Output 2 is used to select whether to enable the clock output of the MCO2 pin, and the last option Audio Clock Input (I2S_CKIN)
is used to select whether to output from the I2S_CKIN( PC9) Input I2S clock. Everyone should pay attention here, because the option Master Clock Output
2 and the option Audio Clock Input (I2S_CKIN) are both PC9 pins used, so if we enable one of them, the other
option will automatically display in red, that is, it is not allowed configuration, this is the automatic conflict detection
function of STM32CubeMX.

Clock System (Clock Tree) Configuration

Click the Clock Configuration tab to enter the clock system configuration bar, as shown in Figure 10.3.3.7 below: insert image description here
After entering the Clock Configuration configuration bar, you can see that the interface shows a complete STM32F4 clock system block diagram
. It can be seen from this clock tree configuration diagram that the main configuration is the size of the external crystal oscillator, frequency division coefficient, frequency multiplication coefficient and selector
. In our configured project, the clock value will be updated dynamically. If a certain clock value exceeds the allowable value during the configuration process, the
corresponding option box will be prompted in red. Here, we will configure a process that
takes HSE as the clock source, configures PLL related parameters, then selects PLLCLK as the clock source for the system clock , and finally configures the system clock as 168MHz. At the same time, the relevant frequency division coefficients of
AHB, APB1, APB and Systick are also configured .
Because the picture is relatively large, we divide the main configuration part into two parts to explain, the first part is to configure the system clock, and the second part is to configure the frequency division coefficients of SYSTICK, AHB, APB1 and APB2. First, let's take a look at
the configuration of the first part, as shown in Figure 10.3.3.8 below:
insert image description here

We divide the system clock configuration into seven steps, which are represented by labels ①~⑦. The detailed process is as follows:
① Clock source parameter setting: we choose HSE as the clock source, so we need to base on the actual high-speed crystal oscillator frequency of the hardware
(here we are 8MHz) to fill in.
③ Clock source selection: we configure the selector to select HSE.
④ PLL1 frequency division factor M configuration. The frequency division factor M is set to 8.
④ PLL1 multiplication factor N configuration. We set the multiplication factor N to 336.
⑤ PLL1 frequency division coefficient P configuration. The frequency division factor P is configured as 2.
⑥ System clock source selection: PLL, HSI or HSE. We choose PLL, and the selector selects PLLCLK.
⑦ After the above configuration, SYSCLK=168MHz at this time.
After the above 7 steps, the system clock of STM32F4 is configured to be 168MHz. Next, we also need to configure
the frequency division coefficients of AHB, APB1, APB2 and Systick to set the corresponding working
clock for STM32 on-chip peripherals or M4 core, and prepare for the subsequent use of these hardware functions. The configuration is shown in Figure 10.3.3.9 below:
insert image description here
AHB, APB1 and APB2 bus clocks and Systick clocks come from the system clock SYSCLK. The AHB
bus clock HCLK is obtained from SYSCLK after passing through the AHB prescaler. If we want to set HCLK to 168MHz
(maximum 168Mz), then we only need to configure the marked place in the figure to be 1. After getting HCLK, next I
We will sequentially configure Systick, APB1 and APB2 frequency division coefficients to be 1, 4 and 2 in the same way at icon numbers ⑨~⑪.
Notice! The systick is fixed at 168MHz. After the configuration is completed, then HCLK=168MHz, Systick=168MHz,
PCLK1=42MHz, PCLK2=84MHz, which is the same as the clock frequency configured in the previous routine.
The above method is a manual calculation method, which is to help us better understand the configuration method of the STM32 clock. Of course,
CubeMX also provides a simpler method: in the position of "HCLK(MHz)" in Figure 10.3.3.9, it is actually can be edited.
We directly input the clock frequency we want, here is 168MHz, press the Enter key, CubeMX will help us provide a suggestion
to set clock frequency and other clocks, after selecting Yes, it will be automatically configured by the software, of course, only the external The 168MHz clock can only be configured after the crystal oscillator.

GPIO function pin configuration

The PF9 and PF10 pins of the Explorer STM32F407 development board are each connected to an LED light. Let's learn to configure the relevant parameters of these two IO ports
. Here we return to the Pinout&Configuration option of STM32CubeMX, enter PF9 in the search bar and press Enter
, the position can be displayed in the pin diagram, as shown in Figure 10.3.3.11 below: insert image description here
Next, we click PF9 in the pin diagram of Figure 10.3.3.11 , in the pop-up drop-down menu, select the function of the IO port
as GPIO_Output. The operation method is shown in Figure 10.3.3.11 below: insert image description here
In the same way, we configure the PF10 selection function as GPIO_Oput. After setting, you can see that the pin turns from gray
to green, indicating that the pin has been enabled. Here we need to explain that if we want to configure the IO port as an external interrupt pin
or other multiplexed functions, we can select the corresponding option. After configuring the IO port function, you need to configure the speed,
pull-up and pull-down parameters of the IO port. We configure these parameters through the GPIO option under System Core, as shown in Figure 10.3.3.12.
insert image description here
Let's configure PF9 first, the configuration method of PF9 and PF10 is the same. Click PF9 in the box ④ in Figure 10.3.3.12, and the configuration
is shown in Figure 10.3.3.13. insert image description here
GPIO output level is the initial value of IO. Since one end of the LED is connected to VCC and the other end is connected to GPIO, when the LED is to be lit
, the GPIO output low level is sufficient. In order to turn off the LED light at the beginning, we set the initial value to output high level.
GPIO mode has been configured as a push-pull output in the view, and there is no need to modify it here.
GPIO Pull-up/Pull-down is no pull-up by default, we use the default configuration here.
Maximum output speed Output speed configuration, the default is low speed, we set it to high speed.
User Label User symbol, we can give PF9 an alias LED0.
PF10 is also configured in this way, give PF10 an alias LED1.

Configure Debug options

Because CubeMX turns off the Debug option by default, this will cause us trouble: after
compiling and downloading the project generated by CubeMX once, an error will be prompted when the next download is made again, so we need to turn on the Debug option. There are many
options here, we can set it to the situation shown in Figure 10.3.3.14.
insert image description here
If you have accidentally turned off the Debug option, then press and hold the reset button when downloading next time, and release the reset button
when , because the Debug pin function of the STM32 chip is turned on by default when it is reset and powered on .
It is recommended to use swd mode, because the io interface is saved

Project Manager

Next, we learn how to set up and generate a project, as shown in Figure 10.3.3.15. Select the Project Manager-> Project
option to configure the project options, let's take a look at the information inside.
Project Name: project name, fill in the project name (half-width, no Chinese characters)
Project Location: project save path, click Browse to select the save location (half-width, no Chinese characters)
Toolchain Folder Location: toolchain folder location, default That's it.
Application Structure: The structure of the application, select Basic (basic), uncheck Do not generate the main(),
because we want it to generate the main function.
Toolchain/IDE: Toolchain/integrated development environment, we use Keil, so choose MDK-ARM, Min Version choose
V5.27, here may be different according to the version of CubeMX, we use the version above V5 by default.
Linker Settings Linker settings:
Minimum Heap Size Minimum heap size, default (large projects need to be adjusted as needed).
Minimum Stack Size The minimum stack size, the default (large projects need to be adjusted as needed).
MCU and Firmware Package is the setting of MCU and firmware package:
MCU Reference: The name of the target MCU series.
Firmware Package Name and Version: Firmware package name and version.
Check Use Default Firmware Location, the path in the text box is the storage address of the firmware package, we
can use the default address. In this way, the settings generated by the project are set.
insert image description here
Next we configure the configuration options for the generated code. Open Project Manager->Code Generator option,
Generated files generate file option, check Generate peripheral initialization as a pir of '.c/.h' files per
peripheral, if you check this option, each peripheral will be separated into a separate Group .c and .h files to make the code structure
clearer, as shown in Figure 10.3.3.16.
insert image description here
So far, the most basic configuration of the project has been completed. Click the blue button (SENERATE CODE) to generate the project.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45172119/article/details/129972522