STM32CubeMX

Table Of Contents

- STM32CubeMX documentation
- CubeMX User Manual
  - 1 STM32Cube overview
  - 2 Getting started with STM32CubeMX
  - 3 Installing and running STM32CubeMX
  - 4 STM32CubeMX user interface
  - 5 STM32CubeMX tools
  - 6 STM32CubeMX C Code generation overview
  - 7 Code generation for dual-core MCUs (STM32H7 dual-core product lines only)
  - 8 Code generation with TrustZone enabled (STM32L5 series only)
  - 9 Device tree generation (STM32MPUs only)
  - 10 Support of additional software components using CMSIS-Pack standard
  - 11 Tutorial 1: From pinout to project C code generation using an MCU of the STM32F4 series
  - 12 Tutorial 2 - Example of FatFs on an SD card using STM32429I-EVAL evaluation board
  - 13 Tutorial 3 - Using the Power Consumption Calculator to optimize the embedded application consumption and more
  - 14 Tutorial 4 - Example of UART communications with an STM32L053xx Nucleo board
  - 15 Tutorial 5: Exporting current project configuration to a compatible MCU
  - 16 Tutorial 6 - Adding embedded software packs to user projects
  - 17 Tutorial 7 - Using the X-Cube-BLE1 software pack
  - 18 Creating LPBAM projects
  - Appendix A STM32CubeMX pin assignment rules
  - Appendix B STM32CubeMX C code generation design choices and limitations
  - Appendix C STM32 microcontrollers power consumption parameters
  - Appendix D STM32Cube embedded software packages
  - Revision history
- Release Notes
  - Release details
  - Release fixes
  - Known limitations
    - Miscellaneous
    - 2.1. When selecting microcontrollers in the STM32C0 series
    - 2.2. When selecting microcontrollers in the STM32G0 series
    - 2.3. When selecting microcontrollers in the STM32G4 series
    - 2.4. When selecting microcontrollers in the STM32H5 series
    - 2.5. When selecting microcontrollers in the STM32H7 series
    - 2.6. When selecting dual-core microcontrollers in the STM32H7 series
    - 2.7. When selecting microcontrollers in the STM32L5 series
    - 3.4. How to improve the wake-up from Stop sequences without relying on the global SystemClock_Config()
    - 3.3. List pinout-compatible microcontrollers
    - 3.2. DDR binaries for the STM32MP1 boards
    - 3.1. Command line authentication required
    - 2.21. Symbolic links on Ubuntu
    - 2.20. User manual
    - 2.19. DDR test suite
    - 2.18. STM32CubeIDE toolchain
    - 2.13. When selecting microcontrollers in the STM32WL series
    - 2.11. When selecting microcontrollers in the STM32WB series
    - 2.17. When using additional software packs
    - 2.16. When using the Example Selector
    - 2.15. When using the Memory Management Tool
    - 2.14. When selecting microprocessors in the STM32MP1 series
    - 2.11.1. When selecting microcontrollers in the STM32WB0 product lines
    - 2.12. When selecting microcontrollers in the STM32WBA series
    - 2.13.1. When selecting microcontrollers in the STM32WL33 product lines
    - 2.10. When selecting microcontrollers in the STM32U5 series
    - 2.9. When selecting microcontrollers in the STM32U3 series
    - 2.8. When selecting microcontrollers in the STM32N6 series
- Running STM32CubeMX in command-line mode
- FAQ
- Community

### On this page

- Figure 590. Project generated with CMSIS-Pack software component
  - using an MCU of the STM32F4 series
- 11.1 Creating a new STM32CubeMX project
- 11.2 Configuring the MCU pinout
- 11.3 Saving the project
- 11.4 Generating the report
- 11.5 Configuring the MCU clock tree
- 11.6 Configuring the MCU initialization parameters
- 11.7 Generating a complete C project
- 11.8 Building and updating the C code project
- 11.9 Switching to another MCU

# 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

1. Select File > New project from the main menu bar or New project from the Home page.

2. 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).

3. 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.

1. 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

3. Select the required peripherals and peripheral modes:

1. 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

2. 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).

3. 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:

1. 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

2. 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

3. 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.

4. 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

6. 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

1. Click the peripheral button to open the corresponding configuration window.

> In our example

1. click TIM3 to open the timer configuration window.

> Figure 607. Timer 3 configuration window

2. 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

2. 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

1. Click the GPIO button in the Configuration view to open the Pin Configuration window.

2. 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):

1. add a new DMA request and select among a list of possible configurations.

2. select among the available streams.

3. select the Direction: Memory to Peripheral or Peripheral to Memory.

4. select a Priority.

5. 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

1. Configure the USB peripheral from the Pinout view.

2. Select MSC_FS class from USB Host middleware.

3. Select the checkbox to enable FatFs USB mode in the tree panel.

> Figure 615. FatFs over USB mode enabled

4. 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.

1. Select the Project Manager view to update project settings and generation options.

2. Select the Project Tab and choose a Project name, location, a toolchain and a toolchain version to generate the project (see Figure 618).

3. 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.

2. 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

3. Select Help > Connection & Updates, a new window appears.

4. Go to the Connection Parameters tab and adjust the parameters to match your network configuration.

5. 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

7. 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.

> 1. 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).

2. As an example, select .eww file to load the project in the IAR™ EWARM IDE.

3. Select the main.c file to open in editor.

4. In the IAR™ IDE, right-click the project name and select Options.

> Figure 636. IAR™ options

5. Click the ST-LINK category and make sure SWD is selected to communicate with the STM32F4DISCOVERY board. Click OK.

6. Select Project > Rebuild all. Check if the project building has succeeded.

> Figure 638. Project building log

7. Add user C code in the dedicated user sections only.

Note: The main while(1) loop is placed in a user section.

> For example:

1. Edit the main.c file.

2. To start timer 3, update User Section 2 with the following C code:

> Figure 639. User Section 2

3. 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

10. 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:

1. Select File > New Project.

2. 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.

3. 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).

4. Click the Try Import button and check the import status to verify if the import has been successful.

5. Click OK to really import the project. An output tab is then displayed to report the import results.

6. The green LED on 32F429IDISCOVERY board is connected to PG13: CTRL+ right click PD12 and drag and drop it on PG13.

7. From Project Manager project tab configure the new project name and folder location. Click Generate icon to save the project and generate the code.

8. 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.