patrislav1 / cubemx.cmake

Script collection to build CubeMX projects with CMake and debug them with VSCode
The Unlicense
34 stars 10 forks source link
arm-gcc cmake cubemx pyocd stm32 vscode

cubemx.cmake

This is a collection of lightweight CMake and Python scripts that can build STM32 CubeMX projects with CMake and set up VSCode for editing/building/debugging.

It is a more lightweight / modular replacement of ioc2cmake, taking advantage of VSCode CMake tools, avoiding the need to pass compiler flags / include paths etc directly to VSCode.

Features

How to use

How to set up the CMake target

Set up your executable target with a regular add_executable(<target_name> <target_sources>) and then call cubemx_target(<target_name> <options...>) to add the relevant sources and compile/link flags to it.

cubemx_target() supports following options (TARGET and IOC are mandatory, the rest is optional):

Name Default value Description
TARGET - CMake target name for the executable. The TARGET keyword can also be omitted.
IOC - CubeMX .ioc project file.
CUBEMX_SOURCE_DIR $CMAKE_CURRENT_SOURCE_DIR Directory of the CubeMX generated code. It's possible to build the same application for different targets, by pointing to a different CUBEMX_SOURCE_DIR per target.
CUBEMX_CORE_DIR $CUBEMX_SOURCE_DIR Directory of the custom code generated by CubeMX for the particular target (Core folder).
CUBEMX_LIB_DIR $CUBEMX_SOURCE_DIR Directory of the generic platform code used by CubeMX (Drivers and Middlewares). It's possible to keep it separate from the source tree, instead of letting CubeMX copy it to the project. See e.g. STM32CubeL4
STARTUP startup_stmXYZ.s By default the startup file provided by CubeMX is used, but it can be overridden if a custom one is needed (e.g. for a bootloader).
LDSCRIPT STMXYZ_FLASH.ld By default the linker script provided by CubeMX is used, but it can be overridden if a custom one is needed (e.g. for a bootloader and/or custom memory layouts).
FLASH_TARGET_NAME flash CMake target name for the "flash to target" operation. By default, make flash will program the target. To avoid name collision, it is necessary to assign different names, if multiple cubemx_targets are created.
IMG_ADDR 0x08000000 Start address of flash image. It can be overridden e.g. when a bootloader is used.
ELF2BIN_OPT - Additional options to be passed to objcopy, e.g. for padding of the .bin file.
ELF2LST_OPT - Additional options to be passed to objdump, e.g. for prefixing of intermixed source in the .lst file.

How to build a project on the command line

mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../cubemx.cmake/arm-gcc.cmake .. && make

How to build a project in VSCode

Caveats

Tested with following chips / boards