Working with family microcontrollers STM32 may seem like a daunting task, especially if you're just getting started with embedded electronics. This is where it comes to the rescue STM32CubeMX - graphical tool from STMicroelectronics, which simplifies peripheral configuration, initialization code generation, and even helps with board pinouts. But how to use this tool correctly to avoid common mistakes and speed up development?
In this article we will look at STM32CubeMX from installation to fine-tuning projects for specific tasks. You will learn how to select a suitable microcontroller, configure clocking, configure peripherals (from GPIO to USB), and also export the project to popular development environments - Keil, IAR or STM32CubeIDE. We will pay special attention hidden settings that are rarely mentioned in the documentation but can save hours of debugging.
What is STM32CubeMX and why is it needed?
STM32CubeMX is not just a utility for generating code, but a full-fledged graphic configurator, which solves several key problems:
- 🔧 Visual setup of peripherals: Instead of manually writing register settings, you work with an intuitive interface where each pin of the microcontroller can be assigned to
UART,SPI,ADCorGPIOliterally a couple of clicks. - 📡 Automatic generation of initialization code: The tool creates files
main.c,stm32fxxx_hal_conf.hand others, already configured for the selected chip and peripherals. - 🔄 Supports all STM32 families: from budget STM32F0 to powerful STM32H7 with kernels Cortex-M7.
- 🛠️ Integration with popular IDEs: export projects to Keil MDK, IAR Embedded Workbench, STM32CubeIDE, as well as generation
Makefilefor manual assembly.
Main advantage STM32CubeMX before manual setup - minimizing errors. For example, when configuring clocking, the system automatically checks frequency compatibility and prevents the selection of impossible combinations (for example, an attempt to overclock STM32F103 up to 100 MHz with 3.3V power supply). However, the tool is not without pitfalls: incorrect settings Clock Configuration can lead to unstable operation of the device, and incorrect assignment of pins can lead to peripheral conflicts.
- STM32CubeIDE
- Keil MDK
- IAR Embedded Workbench
- PlatformIO
- Other
Installation and first launch of STM32CubeMX
Before installation, make sure your computer meets the minimum requirements:
| Parameter | Minimum Requirements | Featured |
|---|---|---|
| Operating system | Windows 7/10/11, Linux, macOS | Windows 10/11 (64-bit) |
| RAM | 2 GB | 4 GB or more |
| Free disk space | 500 MB | 1 GB (including packages for all families) |
| Java | JRE 8 or later | OpenJDK 11+ |
Download the latest version STM32CubeMX from the official website STMicroelectronics (section Tools & Software → Development Tools → STM32CubeMX). Install the program following the installation wizard. After the first launch:
- Accept the license agreement.
- Select a folder to store projects (by default this is
C:\Users\<Name>\STM32Cube\Repository). - Update packages for microcontroller families via
Help → Manage embedded software packages.
⚠️ Attention: If you are running Linux, run STM32CubeMX via scriptSetupSTM32CubeMX-<version>.linuxwith superuser rights (sudo). Otherwise, you may have problems accessing USB ports when working with debuggers.
Microcontroller selection and basic configuration
When creating a new project (File → New Project) you will be greeted by a microcontroller selection window. Here you can filter chips based on several criteria:
- 🔍 Family: STM32F0, STM32F1, STM32F4, STM32H7 etc.
- 📦 Housing:
LQFP-48,BGA-176,WLCSP-36. - 🔌 Periphery: availability
USB,Ethernet,CAN,ADCwith 12/16 bits. - 💡 Features: support DSP, FPU, TrustZone.
After selecting a chip (for example, STM32F407G-DISC1 for development board Discovery) the main configuration window will open. What's important here is:
- Check
Pinout & Configurationfor peripheral conflicts (red exclamation marks on pins). - Customize
Clock Configuration(clocking) - this is critical for stable operationUART,SPIand timers. - Activate the necessary modules in the section
Middleware(For example,FreeRTOSorFatFS).
Clock configuration: avoiding common mistakes
Incorrect setting Clock Configuration - one of the main reasons for unstable work STM32. For example, an attempt to clock STM32F103 from internal generator HSI (8 MHz) with a multiplier x9 (72 MHz) will lead to failures if not configured PLL correct. Let's look at the step-by-step setup for STM32F407:
- Tab
Clock Configuration→ select clock source:HSE (8 MHz)(external quartz) orHSI (16 MHz)(internal). - Set up
PLL:PLL Source: HSEPLLM (делитель): 8 (для HSE 8 МГц → 1 МГц)
PLLN (умножитель): 336 (→ 336 МГц)
PLLP (делитель для системной шины): 2 (→ 168 МГц) - Install tire dividers:
AHB Prescaler: /1 (168 MHz),APB1 Prescaler: /4 (42 MHz),APB2 Prescaler: /2 (84 MHz).
After applying the settings STM32CubeMX will show a warning if the selected frequencies exceed the maximum for a given chip. For example, for STM32F4 maximum frequency AHB - 168 MHz, and APB1 - 42 MHz.
If you are using external quartz (HSE), make sure the board has appropriate load capacitors (usually 12-22 pF). Their absence or incorrect value will lead to unstable operation of the clock generator.
Peripheral setup: from GPIO to USB
Let's consider the configuration of the most popular modules:
1. GPIO (General Purpose Input/Output)
- 🔌 Assign pin as
Input,Output,Alternate FunctionorAnalog. - 🔄 For
Outputselect mode:Push-Pull(standard),Open-Drain(for tireI2C) orPull-Up/Pull-Down. - ⚡ Set speed:
Low,Medium,HighorVery High(affects consumption and noise level).
2. UART (Asynchronous Receiver)
- 📡 Select mode:
Asynchronous,SynchronousorSingle-Wire. - ⚙️ Configure the baud rate, number of stop bits and parity.
- 🔄 Activate receive interrupts (
RXNE) or transfer (TC).
3. SPI (Serial Peripheral Interface)
- 🔄 Select mode:
MasterorSlave. - 📏 Customize word length (8 or 16 bits), bit order (
MSB FirstorLSB First). - ⚡ Set the clock frequency (do not exceed the maximum for the connected device!).
⚠️ Attention: When settingSPIin modeMastermake sure the frequencySCKdoes not exceed the maximum for the slave device. For example, a sensor MPU6050 supports a maximum of 1 MHz, and attempting to set it to 10 MHz will result in data read errors.
How to check peripheral conflicts?
Tab Pinout & Configuration displays red exclamation marks next to pins if:
- The same pin is assigned to two different functions (for example, USART2_TX And SPI1_MOSI).
- The peripheral module requires an alternative function, but the pin is configured as GPIO.
- Module clocking is disabled (for example, USART1 will not work unless activated USART1 Clock in Clock Configuration).
Code generation and export to IDE
After configuration is complete, click Project → Generate Code. Here select:
- 📁 Toolchain/IDE: STM32CubeIDE, Keil MDK-ARM, IAR EWARM or
Makefile. - 🔧 Generate Under Root: Creates the project in a separate folder (recommended to avoid confusion).
- 📝 Copy only the necessary library files: Reduces project size by copying only used libraries.
For STM32CubeIDE the process is as simple as possible: after generation, open the project file (.project) through File → Open Projects from File System. B Keil import .uvprojx, and in IAR — .eww.
The generated code includes:
- 📄
main.c— entry point with initialization of HAL libraries. - 📄
stm32fxxx_hal_msp.c— configuration of low-level peripherals (clocking, GPIO). - 📄
stm32fxxx_it.c- interrupt handlers.
Always check the file stm32fxxx_hal_conf.h after generating the code. Here you can disable unnecessary modules (for example, #define HAL_I2C_MODULE_DISABLED) to reduce the size of the firmware and speed up compilation.
Debugging and optimization of the project
Even after generating the code in STM32CubeMX problems may arise. Let's look at typical scenarios and their solutions:
1. The microcontroller does not start
- 🔋 Check the power: STM32 requires a stable voltage (usually 3.3V or 5V depending on the model).
- ⚡ Make sure that clocking is configured correctly (see section above).
- 🔌 Check your connection
BOOT0(must be in0to run with Flash).
2. Peripherals don't work
- 📡 For
UART: check connectionTX/RXand speed settings (baud rate). - 🔄 For
SPI: Make sure the slave device supports the selected frequency and mode (CPOL/CPHA). - 💡 For
ADC: Set the sampling time (Sampling Time) - too short will lead to inaccurate readings.
3. The project does not compile
- 📂 Check the paths to the libraries in the IDE settings (especially if you transferred the project).
- 🔧 Update packages STM32Cube through
Help → Manage embedded software packages. - 📝 Make sure that
main.cthere are no conflicting definitions (for example, duplicateHAL_UART_MspInit).
For debugging use:
- 🛠️ ST-Link (built into most development boards) or J-Link.
- 📊
printfthroughUART(set upRetargetin Keil). - 🔍 Logic Analyzer for signal analysis
SPI/I2C.
FAQ: answers to frequently asked questions
Can STM32CubeMX be used for microcontrollers from other manufacturers?
No, STM32CubeMX works only with microcontrollers of the family STM32 from STMicroelectronics. For other chips (eg NXP or Microchip) use similar tools: MCUXpresso Config Tools or MPLAB Code Configurator.
How to transfer a project from STM32CubeMX to PlatformIO?
Export the project in format Makefile, then:
- Create a new project in PlatformIO for your board (eg
stm32f407g-disc1). - Copy the generated files (
Src/,Inc/,Drivers/) to the project folder. - Set up
platformio.iniby specifying the frameworkstm32cube.
Why doesn't the project compile after generating code in Keil?
Common reasons:
- There is not enough memory in the chip (check
.map-file). - Library version conflict (update STM32CubeMX and packages).
- Incorrect paths to header files (check
Include Pathsin the project settings).
How to add FreeRTOS to a project via STM32CubeMX?
Activate FreeRTOS in section Middleware:
- Specify the version (usually the latest stable version).
- Configure kernel parameters: stack size, task priorities, timers.
- B
Project Managermake sure the checkbox is tickedGenerate FreeRTOSincluded.
After generating the code in main.c Examples of task creation will appear.
Is it possible to use STM32CubeMX without generating code?
Yes! The tool is useful even if you write the code by hand:
- To visualize pinouts and check conflicts.
- For calculating clocking and checking frequencies.
- To export the configuration in format
.ioc, which you can open later.