11 Tutorial 1: From pinout to project C code generation using an MCU of the STM32F4 series ¶
Support of additional software components using CMSIS-Pack standard
Figure 590. Project generated with CMSIS-Pack software component ¶
11 Tutorial 1: From pinout to project C code generation
using an MCU of the STM32F4 series ¶
This section describes the configuration and C code generation process. It takes as an example a simple LED toggling application running on the STM32F4DISCOVERY board.
11.1 Creating a new STM32CubeMX project ¶
Select File > New project from the main menu bar or New project from the Home page.
Select the MCU/MPU Selector tab and filter down the STM32 portfolio by selecting STM32F4 as ‘Series’, STM32F407 as ‘Lines’, and LQFP100 as ‘Package’ (see Figure 591).
Select the STM32F407VGTx from the MCU list and click OK.
Figure 591. MCU selection
11.2 Configuring the MCU pinout ¶
For a detailed description of menus, advanced actions and conflict resolutions, refer to Section 4 and Appendix A.
By default, STM32CubeMX shows the Pinout view.
2. By default, is unchecked, allowing STM32CubeMX to .. image:: ../../../assets/media/image1174.webp
- width :
1.78333in
- height :
0.2in
move the peripheral functions around and to find the optimal pin allocation (the one that accommodates the maximum number of peripheral modes).
Since the MCU pin configurations must match the STM32F4DISCOVERY board,
enable for STM32CubeMX to maintain the peripheral
Select the required peripherals and peripheral modes:
Configure the GPIO to output the signal on the STM32F4DISCOVERY green LED by right-clicking PD12 from the Pinout view, then select GPIO_output:
Figure 594. GPIO pin configuration
Enable a timer to be used as timebase for toggling the LED. This is done by selecting Internal Clock as TIM3 clock source from the peripheral tree (see Figure 595).
You can also configure the RCC to use an external oscillator as clock source (see Figure 596).
Note: Starting with STM32CubeMX 4.2, the user can skip the pinout configuration by directly loading ST Discovery board configuration from the Board selector tab.
11.3 Saving the project ¶
1. Click to save the project. .. image:: ../../../assets/media/image1180.webp
- width :
0.16667in
- height :
0.16667in
When saving for the first time, select a destination folder and filename for the project. The .ioc extension is added automatically to indicate that this is an STM32CubeMX configuration file.
11.4 Generating the report ¶
Reports can be generated at any time during the configuration:
1. Click to generate .pdf and .txt reports. .. image:: ../../../assets/media/image1184.webp
- width :
0.16667in
- height :
0.16667in
If a project file has not been created yet, a warning prompts the user to save the project first and requests a project name and a destination folder (see Figure 598). An .ioc file is then generated for the project along with a .pdf and .txt reports with the same name.
11.5 Configuring the MCU clock tree ¶
The following sequence describes how to configure the clocks required by the application based on an STM32F4 MCU.
STM32CubeMX automatically generates the system, CPU and AHB/APB bus frequencies from the clock sources and prescalers selected by the user. Wrong settings are detected and highlighted in fuchsia through a dynamic validation of minimum and maximum conditions. Useful tooltips provide a detailed description of the actions to undertake when the settings are unavailable or wrong. User frequency selection can influence some peripheral parameters (e.g. UART baud rate limitation).
STM32CubeMX uses the clock settings defined in the Clock tree view to generate the initialization C code for each peripheral clock. Clock settings are performed in the generated C code as part of RCC initialization within the project main.c and in stm32f4xx_hal_conf.h (HSE, HSI and external clock values expressed in Hertz).
Follow the sequence below to configure the MCU clock tree:
Click the Clock Configuration tab to display the clock tree (see Figure 600).
The internal (HSI, LSI), system (SYSCLK) and peripheral clock frequency fields cannot be edited. The system and peripheral clocks can be adjusted by selecting a clock source, and optionally by using the PLL, prescalers and multipliers.
Figure 600. Clock tree view
Select the clock source (HSE, HSI or PLLCLK) that will drive the system clock.
In the example taken for the tutorial, select HSI to use the internal 16 MHz clock (see Figure 601).
Select the external HSE source and enter 8 in the HSE input frequency box since an 8 MHz crystal is connected on the discovery board:
Figure 603. HSE clock source enabled
Select the external PLL clock source and the HSI or HSE as the PLL input clock source.
Figure 604. External PLL clock source enabled
Keep the core and peripheral clocks to 16 MHz using HSI, no PLL and no prescaling.
Note: Optionally, further adjust the system and peripheral clocks using PLL, prescalers and multipliers:
Other clock sources independent from the system clock can be configured as follows:
USB OTG FS, RNG and SDIO clocks are driven by an independent PLL output.
I2S peripherals come with their own internal clock (PLLI2S), alternatively derived by an independent external clock source.
USB OTG HS and Ethernet clocks are derived from an external source.
Optionally, configure the prescaler for the Microcontroller Clock Output (MCO) pins that allow to output two clocks to the external circuit.
5. Click to save the project. .. image:: ../../../assets/media/image1180.webp
- width :
0.16667in
- height :
0.16667in
Go to the Configuration tab to proceed with the project configuration.
11.6 Configuring the MCU initialization parameters ¶
Caution: The C code generated by STM32CubeMX covers the initialization of the MCU peripherals and middlewares using the STM32Cube firmware libraries.
From the Pinout & Configuration tab, select and configure (one by one) every component (peripheral, middleware, additional software) required by the application using the Mode and Configuration panels (see Figure 605).
Tooltips and warning messages are displayed when peripherals are not properly configured (see Section 4 for details).
Note: The RCC peripheral initialization uses the parameter configuration done in this view as well as the configuration done in the Clock tree view (clock source, frequencies, prescaler values).
Figure 605. Pinout & Configuration view
Click the peripheral button to open the corresponding configuration window.
In our example
click TIM3 to open the timer configuration window.
Figure 607. Timer 3 configuration window
with a 16 MHz APB clock (Clock tree view), set the prescaler to 16000 and the counter period to 1000 to make the LED blink every millisecond.
Figure 608. Timer 3 configuration
Optionally, and when available, select:
The NVIC Settings tab to display the NVIC configuration and enable interruptions for this peripheral.
The DMA Settings tab to display the DMA configuration and to configure DMA transfers for this peripheral.
In the tutorial example, the DMA is not used and the GPIO settings remain unchanged. The interrupt is enabled, as shown in Figure 609.
The GPIO Settings tab to display the GPIO configuration and to configure the GPIOs for this peripheral.
Insert an item:
The User Constants tab to specify constants to be used in the project.
Figure 609. Enabling Timer 3 interrupt
Click the GPIO button in the Configuration view to open the Pin Configuration window.
The first tab shows pins that have been assigned a GPIO mode, but not for a dedicated peripheral and middleware. Select Pin Name to open the configuration for that pin.
In the tutorial example, select PD12 and configure it in output push-pull mode to drive the STM32F4DISCOVERY LED (see Figure 611).
This is not required for this example. It is recommended to use DMA transfers to offload the CPU. The DMA Configuration window provides a fast and easy way to configure the DMAs (see Figure 612):
add a new DMA request and select among a list of possible configurations.
select among the available streams.
select the Direction: Memory to Peripheral or Peripheral to Memory.
select a Priority.
enable the FIFO.
Note: Configuring the DMA for a given peripheral and middleware can also be performed using the Peripheral and Middleware configuration window.
Figure 612. DMA parameters configuration window
Configure the USB peripheral from the Pinout view.
Select MSC_FS class from USB Host middleware.
Select the checkbox to enable FatFs USB mode in the tree panel.
Figure 615. FatFs over USB mode enabled
Select the Configuration view. FatFs and USB buttons are then displayed.
5. FatFs and USB using default settings are already marked as configured . Click FatFs and USB buttons to display default configuration settings. You can also change them by following the guidelines provided at the bottom of the window. .. image:: ../../../assets/media/image1211.webp
- width :
0.26667in
- height :
0.175in
Figure 617. FatFs define statements
11.7 Generating a complete C project ¶
Default project settings can be adjusted prior to C code generation as shown in Figure 618.
Select the Project Manager view to update project settings and generation options.
Select the Project Tab and choose a Project name, location, a toolchain and a toolchain version to generate the project (see Figure 618).
Select the Code Generator tab to choose various C code generation options:
The library files copied to Projects folder.
C code regeneration (e.g. what is kept or backed up during C code regeneration).
HAL specific action (for example, set all free pins as analog I/Os to reduce power consumption).
In the tutorial example, select the settings as displayed in Figure 619, and click OK.
Note: A dialog window appears when the firmware package is missing. Go to next section for explanation on how to download the firmware package.
1. Click to generate the C code. .. image:: ../../../assets/media/image1215.webp
- width :
1.14583in
- height :
0.20833in
During the generation, STM32CubeMX copies files from the relevant STM32Cube MCU package into the project folder so that the project can be compiled. When generating a project for the first time, the firmware package is not available on the user PC and a warning is displayed.
STM32CubeMX offers to download the relevant firmware package or to go on. Click Download to obtain a complete project, ready to use in the selected IDE.
By clicking Continue, only Inc and Src folders are created, holding STM32CubeMX generated initialization files. The necessary firmware and middleware libraries must be copied manually to obtain a complete project. If the download fails, an error message is displayed.
Figure 621. Error during download
Select Help > Connection & Updates, a new window appears.
Go to the Connection Parameters tab and adjust the parameters to match your network configuration.
Click Check connection. The check mark turns green once the connection is established.
Figure 622. Updated settings with connection
6. Once the connection is functional, click to generate the C code. .. image:: ../../../assets/media/image1215.webp
- width :
1.14583in
- height :
0.20833in
The progress is displayed (see next figures).
Figure 623. Downloading the firmware package
Finally, a confirmation message is displayed to indicate that the C code generation has been successful.
The STM32CubeMX .ioc project file located in the root folder. It contains the project user configuration and settings generated through STM32CubeMX user interface.
The Drivers and Middlewares folders hold copies of the firmware package files relevant for the user configuration.
The Projects folder contains IDE specific folders with all the files required for the project development and debug within the IDE.
The Inc and Src folders contain STM32CubeMX generated files for middleware, peripheral and GPIO initialization, including the main.c file. The STM32CubeMX generated files contain user-dedicated sections allowing to insert user-defined C code.
Caution: C code written within the user sections is preserved at next C code generation, while C code written outside these sections is overwritten.
User C code is lost if user sections are moved or if user sections delimiters are renamed.
STM32CubeMX includes a feature that allows users to pause and resume the download actions. This capability is particularly useful in scenarios where network connectivity is unstable or frequently interrupted. This also offers the ability to verify network issue, and connection speed.
The user does not need to restart large downloads from scratch after a brief Internet hiccup. To stop the download, click the Pause button.
Figure 627. Pause button
If the user clicks Yes, the system retains the temporary file to facilitate the download.
If the user clicks No, the system deletes the temporary file.
Figure 631. Closing the download window without clicking the Resume button
11.8 Building and updating the C code project ¶
This example explains how to use the generated initialization C code and complete the project, within IAR™ EWARM toolchain, to have the LED blink according to the TIM3 frequency.
A folder is available for the toolchains selected for C code generation: the project can be generated for more than one toolchain by choosing a different toolchain from the Project Manager menu and clicking Generate code once again.
Open the project directly in the IDE toolchain by clicking Open Project from the dialog window or by double-clicking the relevant IDE file available in the toolchain folder under STM32CubeMX generated project directory (see Figure 625).
As an example, select .eww file to load the project in the IAR™ EWARM IDE.
Select the main.c file to open in editor.
In the IAR™ IDE, right-click the project name and select Options.
Figure 636. IAR™ options
Click the ST-LINK category and make sure SWD is selected to communicate with the STM32F4DISCOVERY board. Click OK.
Select Project > Rebuild all. Check if the project building has succeeded.
Figure 638. Project building log
Add user C code in the dedicated user sections only.
Note: The main while(1) loop is placed in a user section.
For example:
Edit the main.c file.
To start timer 3, update User Section 2 with the following C code:
Figure 639. User Section 2
Then, add the following C code in User Section 4:
Figure 640. User Section 4
8. Rebuild and program your board using . Make sure the SWD ST-LINK option is checked as a Project options otherwise board programming will fail. .. image:: ../../../assets/media/image1241.webp
- width :
0.26667in
- height :
0.2in
9. Launch the program using . The green LED on the STM32F4DISCOVERY board will blink every second. .. image:: ../../../assets/media/image1242.webp
- width :
0.26667in
- height :
0.19167in
To change the MCU configuration, go back to STM32CubeMX user interface, implement the changes and regenerate the C code. The project will be updated,
preserving the C code in the user sections if option in
11.9 Switching to another MCU ¶
STM32CubeMX allows loading a project configuration on an MCU of the same series.
Proceed as follows:
Select File > New Project.
Select an MCU belonging to the same series. As an example, you can select the STM32F429ZITx that is the core MCU of the 32F429IDISCOVERY board.
Select File > Import project. In the Import project window, browse to the .ioc file to load. A message warns you that the currently selected MCU (STM32F429ZITx) differs from the one specified in the .ioc file (STM32F407VGTx). Several import options are proposed (see Figure 641).
Click the Try Import button and check the import status to verify if the import has been successful.
Click OK to really import the project. An output tab is then displayed to report the import results.
The green LED on 32F429IDISCOVERY board is connected to PG13: CTRL+ right click PD12 and drag and drop it on PG13.
From Project Manager project tab configure the new project name and folder location. Click Generate icon to save the project and generate the code.
Select Open the project from the dialog window, update the user sections with the user code, making sure to update the GPIO settings for PG13. Build the project and flash the board. Launch the program and check that LED blinks once per second.