Microcontrollers STM32 from STMicroelectronics have long become the standard for embedded systems due to their flexibility, performance and rich set of peripherals. However, working with them requires not only knowledge of architecture ARM Cortex-M, but also the ability to quickly configure a project. This is where it comes to the rescue STM32CubeMX is a graphical tool that automates register settings, initialization code generation, and even offers ready-made examples for popular development environments.

This article will not just tell you how to install and run CubeMX, but will also reveal nuances that are kept silent in the official documentation. You'll learn how to avoid common timing configuration mistakes, why the code generator sometimes creates extra files, and how to integrate your project into Keil, IAR or STM32CubeIDE without manual editing. And for experienced developers, we have prepared a section on fine tuning low-layer (LL) and hardware abstraction layer (HAL) drivers — there are no template tips here, only specific cases from practice.

What is STM32CubeMX and why is it needed?

STM32CubeMX is not just a configurator, but a full-fledged ecosystem for development on STM32. The tool allows you to:

  • 🔧 Visually configure peripheral devices (timers, ADC, UART, SPI, etc.) without manually editing registers.
  • 📥 Automatically generate initialization code on C for the selected microcontroller.
  • 🔄 Maintain backward compatibility of projects when moving to new versions STM32Cube.
  • 🛠️ Integrate with popular IDEs: Keil MDK, IAR Embedded Workbench, STM32CubeIDE and even VS Code (with plugins).

Main advantage CubeMX before manually writing code - minimizing configuration errors. For example, when setting up clocking, the system automatically checks the compatibility of the selected parameters with the microcontroller datasheet and warns about possible conflicts (for example, exceeding the maximum core frequency). This is especially valuable for beginners who have not yet memorized all the restrictions. STM32F4 or STM32H7.

However, the tool also has pitfalls. For example, the generated code often contains redundant checks and comments, which increases the size of the firmware. Experienced developers prefer to use CubeMX only for initial setup, and then optimize the code manually. It is also worth remembering that the tool does not replace knowledge of architecture ARM - it only speeds up routine operations.

📊 Which STM32 microcontroller do you use most often?
  • STM32F1
  • STM32F4
  • STM32G4
  • STM32H7
  • STM32L4
  • Other

Installing STM32CubeMX: step-by-step instructions

Official website STMicroelectronics offers two versions CubeMX: Online tool (limited functionality) and desktop application. For serious work, it is recommended to install the desktop version, as it supports offline work and has advanced settings.

Download the installer from official page (choose the version for your OS: Windows, Linux or macOS). The installation is standard, but there are several nuances:

  • 📂 Default CubeMX installed in C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX. If you have limited rights, choose a different path (for example, C:\STM32Tools\).
  • 🔄 When updating an old version do not delete the folder with the previous installation — the new version automatically imports settings and projects.
  • 🔌 After installation, run CubeMX and wait for the microcontroller database to load (may take several minutes).

The first launch may take time due to synchronization with ST servers. If the process is frozen, check your proxy settings in Help → Proxy Settings. For Russia and CIS countries, it is sometimes necessary to use a VPN, since access to ST servers may be limited.

Install Java (version 8 or 11)|Download the latest version of STM32CubeMX|Run the installer as administrator|Check proxy settings (if there are connection problems)|Update the microcontroller database via Help → Check for Updates-->

Creating the first project: from choosing a microcontroller to generating code

Let's look at the process of creating a project using the example of a popular microcontroller STM32F407G-DISC1 (debug board STM32F4 Discovery). This chip is equipped with a core Cortex-M4 with a clock speed of up to 168 MHz and a rich set of peripherals, making it ideal for training.

Project creation steps:

  1. Selecting a microcontroller: Click New Project, then in the search field enter STM32F407G. Select the desired model from the list (note the suffix, e.g. STM32F407GTx different from STM32F407VTx number of pins).
  2. Clock setting: Go to the tab Clock Configuration. Here you can select the clock source (internal HSI, external HSE or PLL). For STM32F4 Discovery typical configuration: HSE = 8 MHz (outer quartz) → PLL with output frequency 168 MHz.
  3. Peripheral Configuration: In the tab Pinout & Configuration add the necessary modules. For example, to work with USART2 (for debugging via printf) select mode Asynchronous and adjust the speed 115200 baud.
  4. Code generation: On the menu Project Manager select the target IDE (for example, STM32CubeIDE) and press Generate Code. The tool will create a project folder including files main.c, stm32f4xx_hal_conf.h and others.

Important: when generating code CubeMX creates a file STM32CubeMX.ioc is a project file that stores all the settings. Never edit it manually (for example, in Notepad) as this may corrupt the configuration. If you need to transfer the project to another PC, copy the entire folder.

What to do if CubeMX does not see the microcontroller?

If your STM32 model is not listed, please update the database via Help → Check for Updates. If the problem persists, download the latest support package for your series (for example, STM32CubeF4 for STM32F4) from the ST website and install it manually via Help → Install New Libraries.

Fine tuning: clocking, interrupts and code optimization

One of the most difficult tasks when working with STM32 — correct timing setting. Errors here can lead to unstable operation of the device or even damage to the chip. B CubeMX this is done in the tab Clock Configuration, but there are several critical moments:

  • ⚡ If you are using PLL as a source for SYSCLK, make sure that the output frequency does not exceed the maximum allowed for your core (for example, for Cortex-M4 this is usually 168–180 MHz).
  • 🔄 When using external quartz (HSE) check that the quartz of the required frequency is installed on the board (for example, on STM32F4 Discovery this is 8 MHz). If there is no quartz, select HSI (internal 16 MHz oscillator).
  • ⏱️ For energy efficient applications (e.g. STM32L4) configure Clock Security System (CSS) - this will allow you to switch to a backup source if the main one fails.

Another common problem is interrupt conflicts. If multiple peripherals use the same interrupt (for example, TIM1 And USART1 can share IRQn), CubeMX does not always warn about this. To avoid problems:

  1. Check the interrupt vector table in your microcontroller datasheet.
  2. B NVIC Settings (tab System Core → NVIC) manually assign priorities to interrupts.
  3. Use HAL_NVIC_SetPriority() in the code if you need to dynamically change priorities.

Optimizing the generated code is a separate topic. Default CubeMX includes all possible checks and debugging macros, which increases the size of the firmware. To reduce the code:

  • B Project Manager → Code Generator disable the option Generate peripheral initialization as a pair of '.c/.h' files - this will reduce the number of files.
  • Remove unnecessary peripheral modules from the configuration (for example, if you are not using I2C, uncheck Pinout).
  • Replace HAL-drivers for LL (Low Layer) where maximum performance is needed (for example, to work with DMA or TIM).
💡

If you need to quickly test a configuration without generating a full project, use the tab Live Expression in the STM32CubeIDE debugger. It allows you to monitor the values of registers and variables in real time without stopping the program.

IDE integration: Keil, IAR and STM32CubeIDE

STM32CubeMX supports exporting projects to most popular development environments. Let's look at the features of integration with the three most common IDEs.

IDE Benefits Disadvantages Settings Features
STM32CubeIDE Free, tight integration with CubeMX, built-in debugger Heavy, sometimes slow on weak PCs When generating a project, select STM32CubeIDE in Toolchain/IDE. For debugging, use the built-in ST-Link.
Keil MDK High compilation speed, convenient debugger Paid (32 KB code limit in free version) B CubeMX select MDK-ARM. After generation, open the project .uvprojx in Keil. Make sure the compiler path is correct.
IAR Embedded Workbench Powerful code analysis tools, J-Link support Expensive license, complex interface for beginners When generating, select IAR EWARM. In IAR, import the project via File → Open → Workspace. Check the linker settings in Options → Linker → Config.

General advice for all IDEs: after generating a project in CubeMX do not edit files in the folder Drivers — they are automatically updated when re-generated. If you need to add your own functions, create separate files in the folder Src or Inc.

For debugging via ST-Link (built into most development boards) make sure the correct interface is selected in the project settings. B STM32CubeIDE this is done through Run → Debug Configurations → Debugger. B Keil check the settings in Options for Target → Debug.

1) The correctness of the selected crystal in the debugger settings.

2) Jumper status BOOT0 (must be in position 0 for normal operation).

3) Presence of conflicts in the clock configuration (for example, incorrect frequency HSE).-->

Common mistakes and their solutions

Even experienced developers face problems when working with STM32CubeMX. Here are the most common errors and how to fix them:

⚠️ Attention: If after generating the code the project does not compile with an error undefined reference to `HAL_Init', check if the option is enabled Generate HAL driver code in Project Manager → Code Generator. Also make sure the file stm32f4xx_hal_conf.h not empty - sometimes CubeMX does not update it correctly when the configuration changes.

Problem 1: The microcontroller does not start after flashing the firmware

  • 🔌 Check the power supply to the board (especially if you are using an external source).
  • 🔄 Make sure that the correct interface is selected in the debugger settings (SWD instead of JTAG, if used ST-Link).
  • 📡 Check if the fuses have reset (for example, Option Bytes) - this may block access to flash memory.

Problem 2: DMA Conflicts

If you are setting up DMA for data transfer (for example, between USART and memory), make sure that:

  • 🔀 In settings DMA in CubeMX the correct channel and stream is selected (for example, DMA2 Stream2 for USART1_RX).
  • 🚫 The same channel is not used DMA for multiple peripheral devices.
  • 🔄 Initialization functions are called in the code HAL_UART_Receive_DMA() or HAL_TIM_Base_Start_DMA().

Issue 3: CubeMX update errors

When upgrading to a new version CubeMX old projects may no longer open. To avoid this:

  • 📂 Create a backup copy of your project folder before updating.
  • 🔄 After updating, open the project and wait until CubeMX will automatically update the configuration.
  • 🔧 If the project does not open, try creating a new one and manually transferring the settings from the old file .ioc.
How to restore a project if CubeMX gives an error when opening?

1. Create a new project in the current version of CubeMX.

2. Copy the contents of the old file .ioc (open it in a text editor) to a new file.

3. Manually check the compatibility of the settings (for example, some clocking parameters may have changed in new versions of support packages).

4. Regenerate the code and compare it with the old project (pay special attention to the files stm32f4xx_hal_conf.h And main.c).

Advanced features: using Middleware and generating code for RTOS

STM32CubeMX can not only configure peripherals, but also integrate middleware And real-time operating systems (RTOS). This is especially useful for complex projects that require file system manipulation, a TCP/IP stack, or multitasking.

1. Adding Middleware

In the tab Middleware you can add ready-made components:

  • 📁 FatFS — for working with the file system on SD cards or flash memory.
  • 🌐 LwIP — TCP/IP stack for network applications (for example, for Ethernet or Wi-Fi modules).
  • 🔒 FreeRTOS or Azure RTOS - for multitasking.
  • 🎮 USB Device/Host — for the implementation of USB devices (HID, CDC, MSC).

For example, to add support FatFS:

  1. Go to Middleware → FatFS.
  2. Select operating mode (Read/Write or Read Only).
  3. Customize the interface (for example, SDIO for SD card or SPI for external flash memory).
  4. Generate the code - CubeMX will add the necessary files and initialization to main.c.

2. Integration with FreeRTOS

To add FreeRTOS:

  1. Enable the option FreeRTOS in Middleware.
  2. Configure kernel parameters (eg stack size per task, priorities).
  3. B Pinout & Configuration add tasks via FreeRTOS → Tasks and Queues.
  4. Generate the code - in main.c blanks for tasks will appear (StartDefaultTask etc.).

Important: when using FreeRTOS make sure the core clock speed is SysTick Compatible with RTOS settings. Default FreeRTOS uses SysTick for task management, but you can change it to a different timer if high precision is required.

💡

When using Middleware (for example, LwIP or FatFS), always check the stack and heap size in the linker settings. These components require significant RAM resources, and if there is insufficient memory, the project may not start.

STM32CubeMX Alternatives: When to Use Other Tools

Although STM32CubeMX - the most popular tool for working with STM32, in some cases it is advisable to consider alternatives:

Tool When to use Pros Cons
STM32CubeIDE (built-in configurator) For quick edits in existing projects No need to switch between programs Limited features compared to CubeMX
PlatformIO For cross-platform development (Windows/Linux/macOS) Multi-platform support, easy dependency management More difficult to set up for beginners
Mbed OS For prototyping IoT devices High-level API, support for cloud services Limited support for specific peripherals STM32
Manual register setting For maximum code optimization Full control over the hardware High risk of errors, long development time

For example, PlatformIO Convenient for team development thanks to built-in version control and support CI/CD. A Mbed OS suitable if you need to quickly deploy a device with support LoRaWAN or BLE.

However, for most tasks STM32CubeMX remains the optimal choice thanks to the balance between convenience and flexibility. If you need something in between full automation and manual control, try a combination CubeMX with manual editing of the generated code.

💡

If you work in a team, export the project settings in the format .ioc and store it in a version control system (eg Git). This will allow you to quickly synchronize the configuration between developers and avoid conflicts.

FAQ: answers to frequently asked questions

Can STM32CubeMX be used for microcontrollers from other manufacturers?

No, STM32CubeMX only works with microcontrollers STM32 from STMicroelectronics. For other chips (eg NXP, Microchip or Espressif) use similar tools:

  • 🔹 MCUXpresso Config Tools for NXP (for example, for LPC or i.MX RT).
  • 🔹 MPLAB Code Configurator (MCC) for Microchip PIC And AVR.
  • 🔹 ESP-IDF for Espressif ESP32.
How to transfer a project from STM32CubeMX to another IDE, for example, to VS Code?

To transfer a project to VS Code:

  1. Generate the project in CubeMX for any supported IDE (eg Makefile).
  2. Copy the project folder to your working directory VS Code.
  3. Install extensions for C/C++ (For example, C/C++ Extension Pack) and Makefile Tools.
  4. Create a file tasks.json to build the project via make.
  5. For debugging, configure launch.json using OpenOCD or ST-Link GDB Server.

Example of minimal configuration for tasks.json:

{

"version": "2.0.0",

"tasks": [

{

"label": "Build STM32",

"type": "shell",

"command": "make",

"group": {

"kind": "build",

"isDefault": true

}

}

]

}

Why does the generated code take up so much space?

Generated CubeMX the code often includes:

  • 📄 All possible peripheral drivers (even if they are not used).
  • 📝 Debugging macros and comments.
  • 🔄 Duplicate error checks (e.g. in HAL-drivers).

To reduce the size:

  • 🗑️ Remove unnecessary files from the folder Drivers (but don't edit them!).
  • 🔧 In the generator settings (Project Manager → Code Generator) disable the option Generate comments.
  • 🔄 Replace HAL on LL where possible.
How to update STM32CubeMX and support packages without losing projects?

Update CubeMX and packages (STM32CubeF4, STM32CubeH7 etc.) should not affect your projects if you follow these steps:

  1. Make a backup copy of your projects folder.
  2. Update CubeMX through the official installer.
  3. Run CubeMX and wait for the microcontroller database to automatically update.
  4. Update support packages via Help → Manage embedded software packages.
  5. Open old project − CubeMX will offer to update it to the new version. Agree, but check your settings carefully after updating.

If after updating the project no longer compiles, compare the new generated code with the old one (pay special attention to the files stm32f4xx_hal_conf.h And system_stm32f4xx.c).

Can STM32CubeMX be used for Zephyr RTOS development?

Direct integration STM32CubeMX with Zephyr RTOS no, but you can:

  1. Generate project in