STM32CubeMX

Last modified on Tue Jan 11 20:47:18 2022 UTC.   Improve this page

Overview

STM32CubeMX is a graphical tool provided by ST Microelectornics, that allows a very easy configuration of STM32 microcontrollers, as well as the generation of the corresponding initialization C code for the Arm® Cortex-M.

This page documents the procedure to create the initialisation code for the STM32F4DISCOVERY blinky projects.

For other boards/devices, adjust the steps accordingly.

Note: STM32CubeMX has a very weird implementation, and you may face many small issues with the interface;

Prerequisites

The application requires Java. Install OpenJava or any Java distribution.

Download & install

Register to the STM site and download the latest version.

The result is a file like en.stm32cubemx_v6-1-1.zip. Unpack it and follow the instructions.

Note: for macOS, if the user account has no administrative rights (as it should not have!), run the install manually, with sudo:

sudo java -jar SetupSTM32CubeMX-6.1.1.exe

On macOS, the application is installed as /Applications/STMicroelectronics/STM32CubeMX and the packages repository is stored at ${HOME}/tmp/STM32Cube

Start

Start the application. The current top window looks like:

STM32CubeMX

Install the F4 package

STM provides separate packages for each family. Install the F4 package.

  • in the top page, click the INSTALL/REMOVE button
  • scroll to the STM32F4 family
  • enable the latest available version
  • click the Install Now button
  • click the Close button

Install STM32F4

Create a new project

Before creating a new project, create a new folder in the project, for example stm32cubemx.

To create a new project, start by selecting the board:

  • click the ACCESS TO BOARD SELECTOR
  • in the Commercial Part Number, enter STM32F407G-DISC1
  • in the list, select the board
  • click the Start Project button

Select STM32F4DISCO

  • at the Initialize all peripherals with their default Mode click the No button for a minimal configuration
  • in CubeMX, File -> Save Project as -> select the destination (like <project>/stm32cubemx), Save
  • check if a file called stm32cubemx.ioc was created in the destination folder

Configure the project

  • in the top window, click the Project Manager tab
  • select the Project category
    • check the Project Name: stm32cubemx
    • check the Project Location: platform-stm32f407g-disc1
    • check the Application Structure: Advanced
    • enable Do not generate the main()
    • ignore the Toolchain Folder Location (no idea what it might be)
    • in the Toolchain/IDE combo, select Other Toolchains (GPDSC)
    • in the Mcu and Firmware Package area
      • enable Use latest available version
      • enable Use Default Firmware Location

Project settings

  • select the Code Generation category
    • select the Copy only the necessary library files
    • in the Generated files group
      • enable Generate peripheral initialization as a pair of .c/.h files per peripheral
      • enable Keep User Code when re-generating
      • enable Delete previously gnerated files when not re-generated
    • in the HAL Settings group
      • disable Set all free pins as analog
      • enable Enable Full Assert

Code Generator settings

  • select the Advanced Settings
    • enable MX_GPIO_Init, with visibility (static)
    • enable SystemClock_Config
    • disable all register callbacks (on the right)

Advanced settings

  • in the top window, click the Pinout & Configuration tab
    • expand the System Core category
    • select NVIC
    • in the NVIC tab
      • enable all system interrupts, up to System tick timer
    • in the Code generation tab
      • disable Generate IRQ handler

NVIC NVIC Code

Generate code

Click the top right GENERATE CODE button.

The result is a file system hierarchy like:


ilg@wks % tree stm32cubemx
stm32cubemx
├── Core
│   ├── Inc
│   │   ├── gpio.h
│   │   ├── main.h
│   │   ├── stm32f4xx_hal_conf.h
│   │   └── stm32f4xx_it.h
│   └── Src
│       ├── gpio.c
│       ├── main.c
│       ├── stm32f4xx_hal_msp.c
│       ├── stm32f4xx_it.c
│       └── system_stm32f4xx.c
├── Drivers
│   ├── CMSIS
│   │   ├── Device
│   │   │   └── ST
│   │   │       └── STM32F4xx
│   │   │           ├── Include
│   │   │           │   ├── stm32f407xx.h
│   │   │           │   ├── stm32f4xx.h
│   │   │           │   └── system_stm32f4xx.h
│   │   │           └── Source
│   │   │               └── Templates
│   │   └── Include
│   │       ├── cmsis_armcc.h
│   │       ├── cmsis_armclang.h
│   │       ├── cmsis_compiler.h
│   │       ├── cmsis_gcc.h
│   │       ├── cmsis_iccarm.h
│   │       ├── cmsis_version.h
│   │       ├── core_armv8mbl.h
│   │       ├── core_armv8mml.h
│   │       ├── core_cm0.h
│   │       ├── core_cm0plus.h
│   │       ├── core_cm1.h
│   │       ├── core_cm23.h
│   │       ├── core_cm3.h
│   │       ├── core_cm33.h
│   │       ├── core_cm4.h
│   │       ├── core_cm7.h
│   │       ├── core_sc000.h
│   │       ├── core_sc300.h
│   │       ├── mpu_armv7.h
│   │       ├── mpu_armv8.h
│   │       └── tz_context.h
│   └── STM32F4xx_HAL_Driver
│       ├── Inc
│       │   ├── Legacy
│       │   │   └── stm32_hal_legacy.h
│       │   ├── stm32f4xx_hal.h
│       │   ├── stm32f4xx_hal_cortex.h
│       │   ├── stm32f4xx_hal_def.h
│       │   ├── stm32f4xx_hal_dma.h
│       │   ├── stm32f4xx_hal_dma_ex.h
│       │   ├── stm32f4xx_hal_exti.h
│       │   ├── stm32f4xx_hal_flash.h
│       │   ├── stm32f4xx_hal_flash_ex.h
│       │   ├── stm32f4xx_hal_flash_ramfunc.h
│       │   ├── stm32f4xx_hal_gpio.h
│       │   ├── stm32f4xx_hal_gpio_ex.h
│       │   ├── stm32f4xx_hal_pwr.h
│       │   ├── stm32f4xx_hal_pwr_ex.h
│       │   ├── stm32f4xx_hal_rcc.h
│       │   ├── stm32f4xx_hal_rcc_ex.h
│       │   ├── stm32f4xx_hal_tim.h
│       │   └── stm32f4xx_hal_tim_ex.h
│       └── Src
│           ├── stm32f4xx_hal.c
│           ├── stm32f4xx_hal_cortex.c
│           ├── stm32f4xx_hal_dma.c
│           ├── stm32f4xx_hal_dma_ex.c
│           ├── stm32f4xx_hal_exti.c
│           ├── stm32f4xx_hal_flash.c
│           ├── stm32f4xx_hal_flash_ex.c
│           ├── stm32f4xx_hal_flash_ramfunc.c
│           ├── stm32f4xx_hal_gpio.c
│           ├── stm32f4xx_hal_pwr.c
│           ├── stm32f4xx_hal_pwr_ex.c
│           ├── stm32f4xx_hal_rcc.c
│           ├── stm32f4xx_hal_rcc_ex.c
│           ├── stm32f4xx_hal_tim.c
│           └── stm32f4xx_hal_tim_ex.c
├── stm32cubemx.gpdsc
└── stm32cubemx.ioc

16 directories, 68 files

Source folders

Add the following source folders to the build:

  • stm32cubemx/Core/Src
  • stm32cubemx/Drivers/STM32F4xx_HAL_Driver/Src

Include folders

Add the following include folders to the build:

  • stm32cubemx/Core/Inc
  • stm32cubemx/Drivers/CMSIS/Device/ST/STM32F4xx/Include
  • stm32cubemx/Drivers/CMSIS/Include
  • stm32cubemx/Drivers/STM32F4xx_HAL_Driver/Inc

Customisations

Although not critical, for a better error reporting, some small changes can be performed to the stm32cubemx/Core/Src/main.c file.

Being braced by the special comments, these changes survive regenerations with CubeMX.

Include trace header

/* USER CODE BEGIN Includes */

#if defined(OS_INCLUDE_MICRO_OS_PLUS_DIAG_TRACE)
#include <micro-os-plus/diag/trace.h>
#endif

/* USER CODE END Includes */

Implement Error_Handler()

  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

#if defined(OS_INCLUDE_MICRO_OS_PLUS_DIAG_TRACE)
  trace_printf("Error_Handler()\r\n");
#endif

  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */

Implement assert_failed()

  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

#if defined(OS_INCLUDE_MICRO_OS_PLUS_DIAG_TRACE)
  trace_printf("Wrong parameters value: file %s on line %d\r\n", file, line);
#endif

    while(1) {}

  /* USER CODE END 6 */