Master STM32CubeMX: From Installation to a Complete LED Blinking Project
This step‑by‑step guide walks you through preparing the Java runtime, installing STM32CubeMX, configuring clocks, GPIOs, and core settings, and finally generating and flashing code for a blinking‑LED demo on an STM32F103 board, while highlighting best practices for beginners.
0. Prerequisite
STM32CubeMX runs on Java. Install a Java Runtime Environment (JRE) version ≥ 1.7.0_45. A bundled JRE 1.8.0_271 works, or you can download a newer JRE from https://www.oracle.com/java/technologies/downloads/. Verify the installation with: java -version If the command prints the version, the JRE is correctly installed.
1. STM32CubeMX Overview
STM32CubeMX is a graphical configuration tool from STMicroelectronics that generates peripheral‑initialisation C code for the entire STM32 family. It runs on Windows and macOS and works together with the STM32Cube firmware packages (HAL, LL, middleware) for each series.
2. Installation
2.1 Install JRE
Download and run the JRE installer. After installation, confirm the version with java -version. Ensure the JRE version meets the minimum requirement (≥ 1.7.0_45).
2.2 Install STM32CubeMX
Download the STM32CubeMX installer from the official ST page:
https://www.st.com/content/st_com/zh/stm32cubemx.html
Run the installer and follow the wizard. Choose an installation directory that contains only ASCII characters (no Chinese or other non‑ASCII symbols).
2.3 Download STM32 Firmware Packages
Launch STM32CubeMX, open Preferences → STM32Cube Packages , and set a repository path that also avoids non‑ASCII characters. Click “Download Packages” and select the firmware pack that matches your target MCU (e.g., STM32F1, F4, F7, H7 series).
3. STM32CubeMX Interface
3.1 Startup Screen
The initial panel “ACCESS TO MCU SELECTOR” lets you choose the target chip and creates a new project.
3.2 Project Workspace
The left pane configures core parameters and peripherals (CPU, timers, UART, DMA, etc.). The right pane shows a graphical pin‑out where you can assign functions and labels to pins.
4. Practical Example – Dual‑LED Blinking
This example uses a 正点原子 “战舰” development board with an STM32F103ZET6 (72 MHz, 512 KB Flash, 64 KB SRAM).
4.1 Create a New Project
File → New Project → select STM32F103ZET6 , choose the HAL library, and set a project location without Chinese characters.
4.2 Clock Configuration
Open the Clock Configuration tab and set HCLK to 72 MHz (for F1 series). For other series, set HCLK to the maximum frequency of the device (e.g., 180 MHz for F4, 216 MHz for F7, 400 MHz for H7).
4.3 GPIO Configuration
According to the board schematic, LED0 is connected to PB5 and LED1 to PE5 . Set both pins to GPIO_Output mode and give them appropriate labels.
4.4 Cortex Core Settings (Optional)
Configure debug interface and NVIC priority grouping as needed under the “System Core” tab.
4.5 Generate Code
Click “Generate Code”. The tool creates a project directory containing the HAL initialization code and a main.c skeleton.
4.6 Write User Application
Replace the default while‑loop in main.c with a simple toggle routine:
#include "main.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5); // LED0
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_5); // LED1
HAL_Delay(500); // 500 ms
}
}The MX_GPIO_Init() function is generated by CubeMX and configures PB5 and PE5 as outputs.
4.7 Flashing the Firmware
Connect the board to the PC via USB (or an ST‑Link programmer). Use your preferred flashing tool (e.g., STM32CubeProgrammer, ST‑Link Utility, or the IDE’s built‑in programmer) to upload the generated binary.
4.8 Expected Result
The two LEDs alternate every 500 ms, confirming that the peripheral configuration and code generation were successful.
5. Remarks
STM32CubeMX dramatically reduces the time required to configure clocks, GPIOs, and peripheral drivers, making it ideal for rapid prototyping and learning. However, over‑reliance on the tool can limit deeper understanding of the underlying hardware. After initial experiments, developers are encouraged to inspect and modify the generated initialization code to solidify their STM32 expertise.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
