Skip to content

A small set of helper functions for integrating a STM32 CubeMX project with CMake.

License

Notifications You must be signed in to change notification settings

skull132/cubemx_cmake_helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is this?

This is a set of CMake functions and files to help you compile the output of CubeMX's generator. It's made to be as little intrusive as possible. Most of the arguments you can provide to the functions yourself.

This tool does not invoke CubeMX for you. It doesn't even read the .ioc file for the time being. It doesn't require Python.

It does depend on the structure of the generated ST project, though.

This tool also comes with a toolchain file which adds necessary compiler definitions and flags to the compiler. This is necessary when integrating libraries that get compiled with the same toolchain.

What is supported?

Both LL and HAL projects are supported. Also enabling both should work fine.

Having the CubeMX code in a different subdirectory is also supported.

Supported and "does it compile?" tested MCU families:

  • STM32F4
  • STM32F3
  • STM32F2
  • STM32F1
  • STM32F0

What does this not support?

Libraries downloaded from CubeMX (like embedTLS, FreeRTOS, or such) are not tested currently. Probably don't work.

Feel free to PR support for'em. Just requires testing.

Why is this?

Easy enough: I didn't find a decent CMake tool which would allow me to wrap code generated by CubeMX into a CMake project. There are alternatives, like cubemx.cmake, but I've found that they try to do a little too much.

How do I use this?

First, clone or copy the repo or its CMake files into a place where you can include() them into your main CMakeLists.txt.

This guide will assume you cloned it into a subfolder cubemx_cmake_helpers.

Next, configure the CubeMX project.

CubeMX Settings

The key settings are all in the "Project Manager" tab. Set the:

  • "Toolchain / IDE" to "Makefile". Might work with others as well, but only tested with this.
  • Select "Copy only the necessary library files" checkbox from "Code Generator". Compile errors will ensue otherwise.

The rest shouldn't affect the tool.

Generate the code.

Toolchain configuration

You can use the toolchain file, arm-none-eabi.cubemx.cmake, to help configure the arm-none-eabi toolchain to use proper flags and defines for your CubeMX project. To do this, minimally, you have to pass the following argument to your cmake command:

-DCMAKE_TOOLCHAIN_FILE=cubemx_cmake_helpers/arm-none-eabi.cubemx.cmake

However, the tooling does not depend on this toolchain file being used.

CMakeLists.txt configuration

(See the example folder for an example CMakeLists.txt.)

Configure your CMake target as an executable, and note that the tool assumes that the target's name ends with the suffix .out. Add your custom source files, flags, etc. The tool shouldn't interfere with them.

Note that both C and ASM must be enabled as languages for the compiler, otherwise the startup file is not going to be compiled and the application will not boot properly.

Following this, invoke the cube_configure_compiler_flags() and cube_configure_target() functions in your CMakeLists.txt.

A very minimal example would look like this:

enable_language(C CXX ASM)
# C and ASM are required. Otherwise startup.s isn't getting compiled.

include(cubemx_cmake_helpers/cubemx_target.cmake)

cube_configure_compiler_flags()

add_executable(myapp.out
  app/main.cpp # your own files
  app/my_class.cpp
)

cube_configure_target(myapp)

This will:

  • Attempt to determine the MCU name again.
  • Append MCU specific compiler flags to your already existing C, CXX, ASM, and linker compiler flags.
  • Glob the ST source files, add them to your target along with the include dirs. Also adds the startup file.
  • Configure the linker with the linker script.
  • Adds a custom command to generate myapp.bin and myapp.hex. Along with an appropriate clean configuration.

Library Components

Configure Compiler Flags

cube_configure_compiler_flags modifies the CMAKE_CXX_FLAGS, CMAKE_C_FLAGS, CMAKE_ASM_FLAGS, and CMAKE_EXE_LINKER_FLAGS by adding common defines, MCU specifier, and floating point arguments.

Will also add the defines required for HAL and LL to compile properly to the toolchain. This is necessary if you plan to use other libraries with add_subdirectory which need to reference the HAL's or LL's headers.

cube_configure_compiler_flags(
  MCU "STM32F401xE"                   # MCU name as generated by CubeMX.
                                      # Can be omitted, the tool will then attempt auto-discovery of this parameter.
  ROOT_PATH ${CMAKE_CURRENT_LIST_DIR} # The path for the cube generated project.
                                      # Defaults to ${CMAKE_CURRENT_LIST_DIR}
  USE_HAL ON                          # Controls whether or not the -DUSE_HAL_DRIVER flag is added to the compiler and linker flags.
                                      # Defaults ON
  USE_LL OFF                          # Controls whether or not the -DUSE_LL_DRIVER flag is added to the compiler and linker flags.
                                      # Defaults OFF
  FLOAT_ABI "hard"                    # Controls the -ffloat-abi flag for the compiler and linker. Variants are "hard", "soft", "softfp".
                                      # Defaults to "hard"
)

Configure Target

cube_configure_target will add the relevant HAL & LL files to the target, configure relevant include directories, and add some convience files to be generated or cleaned up.

cube_configure_target(
  myapp                               # The root name of the executable. Assumes that the actual application ends with the suffic ".out".
  MCU "STM32F401xE"                   # MCU name as generated by CubeMX.
                                      # Can be omitted, the tool will then attempt auto-discovery of this parameter.
  ROOT_PATH ${CMAKE_CURRENT_LIST_DIR} # The path for the cube generated project.
                                      # Default ${CMAKE_CURRENT_LIST_DIR}
  USE_HAL ON                          # Controls whether or not the HAL source files and include directories are added to the project sources.
                                      # Defaults ON
  USE_LL OFF                          # Controls whether or not the LL source files and include directories are added to the project sources.
                                      # Defaults OFF
)

About

A small set of helper functions for integrating a STM32 CubeMX project with CMake.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published