Embedded HandbookApps
All sections › I. Basics
STM32C5 · level basic · 19 min read
Open in the interactive handbook

4г. STM32CubeMX 2 and a HAL2 project: the folder layout, code generation, where to write your own code, the CMake build and debugging

Sections 4б and 115а worked with the classic STM32CubeMX: an .ioc file, the Core/ and Drivers/ folders, your own code between USER CODE markers. Section 4в built firmware with no environment at all. For the STM32C5 series ST has a different tool — STM32CubeMX 2 — and a different library, HAL2 (section 5б). The project they create is arranged differently: other folders, other rules on where to write your own code, and the build only through CMake presets, IAR projects and Open-CMSIS.

Here: what STM32CubeMX 2 is, how its project is arranged, what a repeated generation rewrites and what it does not, and how to build such a project from the command line: from an empty folder to .elf and .bin. The STM32CubeMX 2 program is not on the machine, so the tool itself is described from ST documentation, and the project from the templates and examples of the STM32CubeC5 2.1.0 package: they were created by this tool and carry its service files. The experiment is an LED blink on the NUCLEO-C562RE board (the hal_gpio_toggle example, pin PA5), built in Docker.

Sources: the STM32CubeC5 2.1.0 package (commit cadccf77 of 7 September 2026) with the stm32c5xx_drivers and stm32c5xx_dfp 2.1.0 submodules (licence BSD-3-Clause), arch/cmsis — CMSIS 6.3.0 (Apache 2.0), utilities/syscalls 2.0.0 (BSD-3-Clause); the README of the start_from_nucleo template; the ST Community articles "How to use the HAL2 structure and project format" (24 March 2026) and "How to generate code for VS Code from STM32CubeMX2" (17 March 2026); the STM32Cube 2.0.0 examples FAQ; the ST page on STM32CubeMX 2 and the STM32Cube 2.0.0 software distribution page — on STM32 Package Creator. The build uses arm-none-eabi-gcc 12.2.1, CMake 4.4.3, Ninja 1.11.1 and xPack GCC 14.2.1.

Terms used in the documentation

Term Meaning
STM32CubeMX2 the second generation of the configuration and code generation program, starting with STM32C5
.ioc2 the STM32CubeMX 2 project file: a set of embedded settings files
generated code the generated/ folder: code that generation writes anew
user_modifiable the folder of your copies of the startup code, the linker script and system_*.c
file group (generated, project_file, RTE, user_code, pack) a file's group: it decides what happens on repeated generation
conflict resolution what to do if a file was edited by hand: show the difference or overwrite
user label your own name for a pin or a block; it yields an alias of the init function
init hook (pre_system_init_hook, post_system_init_hook) a weak function you can replace with your own before and after the system setup
configure preset, build preset a CMake configure preset and a build preset
copy table, zero table tables in the image: what to copy to RAM and what to zero at startup
stack limit (MSPLIM) the stack boundary in the Cortex-M33 core
STM32 Package Creator an ST service: assemble your own HAL2 software package from the parts you need

In plain words. For new chips ST has a new configurator program, and it lays the project out in a new way: what is yours and what the program may rewrite live in different folders. The section shows what belongs to whom here, where to write your own code so that it is not erased, and how to build such a project with a couple of commands in a terminal, with no environment at all.


4г.1 STM32CubeMX 2: what it is and for which chips

STM32CubeMX 2 is a new ST program for the same tasks as CubeMX: pins, clocks, peripherals, middleware, code and project generation. According to ST, all new series starting with STM32C5 work only with it, while the old ones stay in the classic CubeMX; the two programs live side by side. This can be checked from the other side: the STM32CubeMX 6.18.1 database on this machine has 2240 chip descriptions, and not a single STM32C5.

Since version 1.2.0 the list has grown: besides STM32C5, CubeMX 2 has STM32G5 and STM32H7P, and only these. Everything else — F0…F7, G0, G4, H5, H7, L0…L5, U0, U5, WB, WL — is still configured in the classic CubeMX 6. The rule is simple: if a chip is missing in one program, look in the other.

Program Series
STM32CubeMX 2 C5, G5, H7P
STM32CubeMX 6 all the others

The project file is .ioc2, not .ioc. For the hal_gpio_toggle example it weighs 106376 bytes and consists of 22 embedded files: clocks, pins, NVIC, DMA, the component set, the export settings — each in its own JSON. The series of the two programs do not overlap, so a project for F1 or F4 stays in CubeMX 6, and for C5 it is started anew in CubeMX 2.

The project is exported in three formats; they are listed in the .ioc2 itself:

Format Folder Compiler in the example
CMake cmake/ GCC
EWARM (IAR) ewarm/ IAR
Open-CMSIS (.csolution.yml) open-cmsis/ Arm Compiler 6

There is no CubeIDE project in this list: for C5 ST offers STM32CubeIDE for VS Code, which opens the CMake project (4г.6).


4г.2 A HAL2 project: the folders and who owns what

The HAL2 project of the hal_gpio_toggle example for NUCLEO-C562RE: on the left the shared parts of the STM32CubeC5 package — stm32c5xx_drivers, stm32c5xx_dfp, arch/cmsis, utilities/syscalls; in the middle the project folder — application and user_modifiable belong to you, generated/hal, cmake and .ioc2 are updated by generation; on the right CMake 3.30 and Ninja build hal_gpio_toggle.elf in 20 steps, Flash 4312 bytes, RAM 1840 bytes

The example lies in the package as STM32CubeMX 2 exported it. Without the IAR and Open-CMSIS projects the board folder and the shared application/ folder hold 44 files:

application/main.c, main.h, example.c, example.h
NUCLEO-C562RE/
  hal_gpio_toggle.ioc2
  cmake/CMakeLists.txt, CMakePresets.json, .settings/exported-files.json
  cmake/cmake/GCC.14.3.1.cmake, components.cmake, files.cmake, flags.cmake, target.cmake
  generated/hal/mx_system.c, mx_rcc.c, mx_gpio_default.c, mx_icache.c, mx_cortex_mpu.c, mx_cortex_nvic.c (+ .h)
  generated/hal/mx_def.h, mx_hal_def.h, stm32c5xx_hal_conf.h, stm32_external_env.h, Pre_Include_Global.h
  generated/hal/STM32_HAL_Driver_codegen.cmake, .gpdsc, CMakeLists.txt
  generated/utilities/syscalls/CMakeLists.txt, STMicroelectronics.syscalls.cmake, .gpdsc
  user_modifiable/Device/STM32C562RET6/startup_stm32c562xx.c, stm32c562xe_flash.ld, system_stm32c5xx.c

In HAL1 everything lay mixed in Core/: main.c with USER CODE markers next to stm32f1xx_it.c and stm32f1xx_hal_conf.h, which generation rewrites outside the markers (section 115а). Here the folders are split by owner:

Folder Owner What is in it
application/ yours main.c and the example code; one for all three boards of the example
user_modifiable/Device/ yours copies of the startup file, .ld for GCC, .icf for IAR, .sct for Arm and system_stm32c5xx.c
generated/hal/ generation mx_*.c/h — peripheral setup, stm32c5xx_hal_conf.h
cmake/ generation, except CMakeLists.txt presets, toolchain, lists of files and options
package: stm32c5xx_drivers, stm32c5xx_dfp, arch/cmsis ST HAL2 and LL, chip headers, CMSIS — shared by all projects

stm32c5xx_hal_conf.h is now created by generation: of the 42 HAL modules in it 6 are enabled — CORTEX, FLASH, GPIO, ICACHE, PWR, RCC. No copy of HAL2 is put into the project: CMake takes it from the package via relative paths like ../../../../../../stm32c5xx_drivers.

Generation learns what counts as yours from the file cmake/.settings/exported-files.json. Its first line: "Do not modify this file as it is used by STM32CubeMX2 to determine what and how to update your project with new configuration". Inside — 7 files with a group and a SHA-256 sum: CMakeLists.txt in the project_file group, the other six in generated. The sum is computed over the file with LF line endings: that way 6 sums of 7 matched. The seventh, for flags.cmake, did not match even with LF — the file was edited after the export. In the copy of the package taken by git with core.autocrlf=true on Windows, none of the 7 matched: git replaced LF with CRLF.

What to do with a changed file is written in the .ioc2:

"conflict": {
    "generated": "DIFF",
    "all": "OVERWRITE_WITHOUT_BACKUP",
    "pack": "DIFF",
    "project_file": "DIFF",
    "RTE": "DIFF",
    "user_code": "DIFF"
  }

Judging by the names, DIFF means show the difference, OVERWRITE_WITHOUT_BACKUP — overwrite without a copy; the ST article also mentions a window with the difference. The RTE group is user_modifiable/: in components.cmake it is named as the CMSIS_RTE_FOLDER folder.


4г.3 Your own code without USER CODE

There are almost no USER CODE BEGIN/END markers in HAL2 projects: among the 24800 .c and .h files of the package examples they were found in five — these are USBX files (mx_usbx_device_ccid.c, mx_usbx_host.c). ST says the same in its answers about the examples: the user section concept is not needed in HAL2 examples. Your own code lives in your own files and is tied to generation in three ways.

Your own names instead of pins. In CubeMX 2 a pin is given a label — here mx_example_gpio. Generation turns it into aliases in mx_hal_def.h and mx_gpio_default.h:

#define mx_example_gpio_init mx_gpio_default_init
#define MX_EXAMPLE_GPIO_PORT                            HAL_GPIOA
#define MX_EXAMPLE_GPIO_PIN                             HAL_GPIO_PIN_5

The example code uses only the aliases and does not know which pin is on which board:

  HAL_GPIO_TogglePin(MX_EXAMPLE_GPIO_PORT, MX_EXAMPLE_GPIO_PIN);

That is why application/ is one for three boards: moving to another board means another generated/, not an edit of your own code.

Weak functions before and after the setup. mx_system_init() from generated/hal/mx_system.c calls in order pre_system_init_hook, mx_cortex_mpu_init, HAL_Init, mx_cortex_nvic_init, mx_icache_init, mx_icache_gethandle, HAL_ICACHE_Start, mx_rcc_init, mx_rcc_peripherals_clock_config and post_system_init_hook — ten calls according to the disassembly. The first and the last are empty weak functions. Your own version goes into your own file, and the linker takes it:

#include "mx_system.h"

volatile uint32_t boot_marker;

system_status_t post_system_init_hook(void)
{
    boot_marker = 0x4C454421u;            /* "LED!" — видно в отладчике */
    return SYSTEM_OK;
}

Your own files go into CMakeLists.txt. The top-level CMakeLists.txt is in the project_file group, and it has empty places "Add additional source files here", "Add additional include directories here" and five more. The file above was added to the first of them with one line:

  # Add additional source files here
  ../../my/app_hooks.c

The build passed without warnings, the code grew by 16 bytes (4200 → 4216), and in the symbol table post_system_init_hook went from W (weak) to T:

20000120 B boot_marker
080005c8 T post_system_init_hook
0800073c W pre_system_init_hook

Sometimes you will still have to change generated files — for example, stm32c5xx_hal_conf.h. Then on the next generation CubeMX 2 shows the difference (generated=DIFF). According to the ST article, user files are not rewritten on repeated generation; in the comments to it readers of STM32CubeMX 2 1.1.1 report the opposite — their main.c was rewritten. This is not checked: the program is not available. The reliable rule is your code in your own files and under git, and look at git diff after generation.


4г.4 A CMake build from the command line: from an empty folder to .elf and .bin

The experiment runs in the stm-arm:1 image. The package is mounted read-only at /pkg, the build goes in the empty folder /work. The example refers to the package via the paths ../../../../../../, so the package tree is repeated in /work — only the parts that are needed:

mkdir -p /work && cd /work
for d in arch/cmsis stm32c5xx_dfp stm32c5xx_drivers utilities/syscalls; do
  mkdir -p "$(dirname $d)" && cp -r /pkg/$d $d
done
mkdir -p examples/hal/gpio/toggle && cp -r /pkg/examples/hal/gpio/toggle/application /pkg/examples/hal/gpio/toggle/NUCLEO-C562RE examples/hal/gpio/toggle/
cd /work/examples/hal/gpio/toggle/NUCLEO-C562RE/cmake

2971 files were copied: CMSIS — 2221, stm32c5xx_dfp — 249, stm32c5xx_drivers — 424, syscalls — 26, the example — 51. Of the 26 submodules of the package this example needs four.

The first attempt uses the CMake from the image, 3.25.1:

CMake 3.30 or higher is required.  You are running version 3.25.1

The line cmake_minimum_required(VERSION 3.30) is in the CMakeLists.txt of the stm32c5xx_dfp package; the other parts have 3.20. CMake 4.4.3 was taken from the stm-pio:1 image (the cmake package for Python) and put alongside:

export PATH=/t/cmake/bin:$PATH
cmake --preset release_GCC_NUCLEO-C562RE
cmake --build --preset release_GCC_NUCLEO-C562RE
arm-none-eabi-size build/release_GCC_NUCLEO-C562RE/hal_gpio_toggle.elf
arm-none-eabi-objcopy -O binary build/release_GCC_NUCLEO-C562RE/hal_gpio_toggle.elf app.bin

There are two presets — release_GCC_NUCLEO-C562RE (-Os -g) and debug_GCC_NUCLEO-C562RE (-g3 -O0), both with the Ninja generator and the folder build/<preset>. The preset name is also CMAKE_BUILD_TYPE: by it files.cmake, flags.cmake and components.cmake pick their lists. The toolchain file is called GCC.14.3.1.cmake, but it does not check the version: it takes arm-none-eabi-gcc from PATH and, from the core variables in target.cmake (Cortex-M33, SP_FPU, DSP), sets -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mthumb.

Ninja made 20 steps: 19 object files and the link. Warnings — 0.

From C files
application/ main.c, example.c
generated/hal/ mx_system.c, mx_rcc.c, mx_gpio_default.c, mx_icache.c, mx_cortex_mpu.c, mx_cortex_nvic.c
user_modifiable/Device/ startup_stm32c562xx.c, system_stm32c5xx.c
HAL2 package stm32c5xx_hal.c, _hal_cortex.c, _hal_flash_itf.c, _hal_gpio.c, _hal_icache.c, _hal_pwr.c, _hal_rcc.c
utilities/syscalls syscalls.c, sysmem.c
   text    data     bss     dec     hex filename
   4200     112    1728    6040    1798 build/release_GCC_NUCLEO-C562RE/hal_gpio_toggle.elf

Flash — 4312 bytes: 4200 of code and constants and 112 of initial values of .data; app.bin is also 4312 bytes. RAM — 1840 bytes: 112 of .data, 192 of .bss, 512 of heap and 1024 of stack. The debug preset gives 7472 bytes of code with the same 112 and 1728. The same example built with xPack GCC 14.2.1 (the stm-arduino:1 image) gives 3952, 96 and 2048 bytes, also without warnings.

For comparison: the HAL1 blink for the F103 from section 0ж in the debug build took 4884 bytes of Flash, while the debug build here is 7584 (7472 + 112). But this is another chip, another clock tree and an instruction cache, so the difference is not a measure of the libraries; a fair comparison of HAL1 and HAL2 is in section 5б.


4г.5 What is inside the image: startup code, linker script, SystemInit

The linker script was chosen not by CubeMX 2 but by the package: the CMakeLists.txt of stm32c5xx_dfp checks whether there is a file in user_modifiable/Device/<chip>/. If there is, it takes it; if not, its own template from Source/Templates/gcc/linker/. The same goes for the startup file and system_stm32c5xx.c. In the example the copies are byte-for-byte identical to the package templates; you edit them in place, in user_modifiable.

The stm32c562xe_flash.ld script is the same language as in section 4в, but with differences. Memory — ROM 0x80000 (512 KB) at 0x08000000 and RAM 0x20000 (128 KB) at 0x20000000. The heap HEAP_SIZE = 0x200 and the stack STACK_SIZE = 0x400 are separate sections right after .bss, not the end of RAM:

.vectors 00000188 08000000 08000000
.text 00000e98 08000188 08000188
.copy.table 0000000c 08001050 08001050
.data 00000070 20000000 08001068
.zero.table 0000000c 0800105c 0800105c
.bss 000000c0 20000070 080010d8
.heap 00000200 20000130 080010d8
.stack 00000400 20000330 080010d8

The first two words of the image:

000000 20000730 080007d5

The initial SP is 0x20000730, which is __StackTop, not 0x20020000: the stack ends where the data end, the remaining 126 KB of RAM are free. Reset_Handler is 0x080007d5, the low bit 1 means Thumb (section 4в.6). The vector table is 0x188 = 392 bytes, 98 words.

Reset_Handler is written in C. It writes PSP and the stack limits MSPLIM and PSPLIM (__StackLimit = 0x20000330), calls SystemInit() and jumps to __cmsis_start from CMSIS 6. That walks the tables from the script: .copy.table — from where, to where and how many words to copy (.data), .zero.table — what to zero (.bss) — and calls _start of the C library, which calls main. The loops are the same as in the startup code of section 4в, only the addresses lie not in symbols but in a table. The MSPLIM limit is new in Cortex-M33: core_cm33.h has the STKOF bit in CFSR for a fault on it.

SystemInit() from system_stm32c5xx.c does two things: it enables the FPU (SCB_EnableFPU) and writes the table address to VTOR. It does not touch the clocks: after reset the core runs from HSI divided by 3 — 144 MHz / 3 = 48 MHz, which is also the initial SystemCoreClock. The frequency is raised by main: first mx_system_init(), and inside it mx_rcc_init() — HSE 24 MHz, the PSI multiplier up to 144 MHz, a Flash latency of 4 cycles, then the switch to PSIS.

Step Where What
Reset_Handler startup_stm32c562xx.c PSP, MSPLIM, PSPLIM
SystemInit system_stm32c5xx.c FPU, VTOR; the core at 48 MHz
__cmsis_start CMSIS 6 copy of .data, zeroing of .bss, _start
mx_system_init generated/hal/mx_system.c MPU, HAL_Init, NVIC, instruction cache, 144 MHz
app_init, app_process application/ pin PA5 and toggling it every 100 ms

4г.6 Debugging, flashing, your own package and a comparison with the classic CubeMX

There is no board, so here is only what can be checked without it, and what ST writes in the documentation.

ST-LINK and SWD. On the NUCLEO-C562RE the ST-LINK sits on the board; the core is debugged over SWD, as in section 35д. STM32CubeProgrammer 2.23.0 on this machine already knows the series: its database has three STM32C5 chip codes — 0x44E (STM32C5x), 0x44F (STM32C53x/542) and 0x45A (STM32C59x/5A3). QEMU 7.2 from the image does not model C5: it has three STM32 boards, all of them on Cortex-M3 and M4. There is nowhere to run the example.

GDB on the image. gdb-multiarch reads the debug .elf without a board:

Line 33 of "examples/hal/gpio/toggle/NUCLEO-C562RE/generated/hal/mx_system.c" starts at address 0x8000836 <mx_system_init> and ends at 0x800083a <mx_system_init+4>.
type = const VECTOR_TABLE_Type []
Symbol "Reset_Handler" is a function at address 0x800097c.

With a board this is the same session as in section 35б, only the server is the ST-LINK GDB server or OpenOCD, if its version knows C5 (there is no OpenOCD in the image; not checked).

VS Code. According to the ST article the order is (from ST documentation, not checked on hardware): in CubeMX 2 the export format is CMake, the compiler GCC; in VS Code with the STM32CubeIDE extension and CMake Tools open the project folder, select the debug_GCC_NUCLEO-C562RE preset, run "STM32Cube: Setup STM32Cube project(s)", build (F7) and in "Run and Debug" choose "STM32Cube: STLink GDB Server"; F5 starts debugging. The presets are the same as in 4г.4, so the build from VS Code and from the command line is one and the same.

STM32 Package Creator. The STM32CubeC5 package has 26 submodules; on this machine 7 are downloaded, and four were enough for the example. ST offers a service that assembles such a set for a project: according to ST, in STM32 Package Creator you pick the HAL2 software parts you need and get your own package, including as packs for STM32CubeMX 2 and STM32CubeIDE for VS Code (from ST documentation, not checked). The classic STM32PackCreator from CubeMX 6 is something else: it makes a CMSIS pack to extend CubeMX.

CubeMX 6 + CubeIDE (4б, 115а) CubeMX 2 + HAL2 (here)
series all series before STM32C5 STM32C5 and newer
project file .ioc, key=value lines .ioc2, embedded JSON
library HAL1, a copy in Drivers/ HAL2 from the package via paths, no copy
export STM32CubeIDE, Makefile, CMake and others CMake, IAR, Open-CMSIS
your own code between USER CODE BEGIN/END your own files, weak functions, places in CMakeLists.txt
what counts as edited markers in the text SHA-256 sums in exported-files.json, conflict rules
startup file assembly startup_*.s C, startup_*.c with __cmsis_start
CMake from 3.22 in a CubeMX 6.18.1 project from 3.30 because of stm32c5xx_dfp
debugging the Debug button in CubeIDE STM32CubeIDE for VS Code or GDB

4г.7 Pitfalls

CMake 3.30 or higher is required. The stm32c5xx_dfp package requires CMake 3.30, while the stm-arm:1 image has 3.25.1. A newer one is needed — here CMake 4.4.3 was taken from the Python cmake package.

add_subdirectory given source "../../../../../../utilities/syscalls" which is not an existing directory. The package was downloaded without submodules or without the one needed: the experiment with the utilities/syscalls folder removed gave exactly this. The package is taken with git clone --recursive and updated with git submodule update --init --recursive; "Download ZIP" on GitHub does not contain submodules — so says the package README.

Regeneration erases an edit. Everything in generated/ and the six files cmake/cmake/*.cmake are written by generation. An edit there survives generation only if you keep it in the difference window. Your own CMake lines go into the top-level CMakeLists.txt, your own code into your own files.

An old project cannot be moved to CubeMX 2. .ioc and .ioc2 are different formats, and the series of the programs do not overlap. Moving to C5 means a new project, and the code is moved by hand (section 5б on the differences between HAL1 and HAL2). Whether CubeMX 2 opens an .ioc file even for reading is not checked.

HAL1 and HAL2 packages mixed up. HAL_GPIOA and HAL_GPIO_PIN_5 are HAL2 names; in HAL1 they are GPIOA and GPIO_PIN_5. A HAL1 example will not build with HAL2, and the STM32CubeU5 V1 package and the HAL2 preview for U5 are different things. Check which package stm32*_hal.h and stm32*_hal_conf.h come from.

Long paths on Windows. The startup file lies outside the CMake folder, and CMake repeats its full path inside the object folder: with the root /work the longest path in the build is 214 characters, and the root enters it twice. On Windows a path to the package in the user profile easily pushes such paths past the old limit of 260 characters (not checked on Windows). Put the package in a short folder and write paths in CMake with /.

Sums do not match after git. With core.autocrlf=true git on Windows changes LF to CRLF, and none of the 7 sums in exported-files.json matches the file. What CubeMX 2 then shows is not checked; it is safer to keep * text eol=lf in .gitattributes for the project.

The stack is not at the end of RAM. The initial SP is 0x20000730: a 1 KB stack lies right after the data. Recursion or a large array on the stack will hit MSPLIM, although almost all of RAM is free. The size is STACK_SIZE in user_modifiable/.../stm32c562xe_flash.ld.


4г.8 Summary

Next: section 4д "A map of the STM32CubeMX windows: what is configured where".

Open in the interactive handbook