XANTH-IO / stm32-cmake

CMake for stm32 developing.
Other
0 stars 0 forks source link

About

Tests

This project is used to develop applications for the STM32 - ST's ARM Cortex-Mx MCUs. It uses cmake and GCC, along with newlib (libc), STM32Cube. Supports C0 F0 F1 F2 F3 F4 F7 G0 G4 H7 L0 L1 L4 L5 U0 U5 WB WL device families.

Requirements

Project contains

Examples

Usage

First of all you need to configure toolchain and library paths using CMake variables. There are generally three ways to do this:

  1. Pass the variables through command line during cmake run with passed to CMake with -D<VAR_NAME>=...
  2. Set the variables inside your CMakeLists.txt
  3. Pass these variables to CMake by setting them as environmental variables.

The most important set of variables which needs to be set can be found in the following section.

Configuration

These configuration options need to be set for the build process to work properly:

These configuration variables are optional:

Helper script on Unix shells

If you have access to a Unix shell, which is the default terminal on Linux, or tools like MinGW64 or git bash on Windows, you can write a small path_helper.sh script like this:

export STM32_TOOLCHAIN_PATH="<ToolchainPath>"
export STM32_TARGET_TRIPLET=arm-none-eabi
export STM32_CUBE_<FAMILY>_PATH="<PathToCubeRoot>"

and then use . path_helper.sh to set up the environment for the local terminal instance in one go.

Helper script in Powershell

On Windows, you can use a Powershell script path_helper.ps1to set up the environment:

$env:STM32_TOOLCHAIN_PATH = "<ToolchainPath>"
$env:STM32_TARGET_TRIPLET = arm-none-eabi
$env:STM32_CUBE_<FAMILY>_PATH="<PathToCubeRoot>"

Common usage

First thing that you need to do after toolchain configuration in your CMakeLists.txt script is to find CMSIS package:

find_package(CMSIS [CMSIS_version] COMPONENTS STM32F4 REQUIRED)

You can specify STM32 family or even specific device (STM32F407VG) in COMPONENTS or omit COMPONENTS totally - in that case stm32-cmake will find ALL sources for ALL families and ALL chips (you'll need ALL STM32Cube packages somewhere).

[CMSIS_version] is an optional version requirement. See find_package documentation. This parameter does not make sense if multiple STM32 families are requested.

Each STM32 device can be categorized into family and device type groups, for example STM32F407VG is device from F4 family, with type F407xx.

*Note: Some devices have two different cores (e.g. STM32H7 has Cortex-M7 and Cortex-M4). For those devices the name used must include the core name e.g STM32H7_M7 and STM32H7_M4. STM32WB is a multi-cores device even if the second core is not accessible by end user.

CMSIS consists of three main components:

stm32-cmake uses modern CMake features notably imported targets and target properties. Every CMSIS component is CMake's target (aka library), which defines compiler definitions, compiler flags, include dirs, sources, etc. to build and propagate them as dependencies. So in a simple use-case all you need is to link your executable with library CMSIS::STM32::<device>:

add_executable(stm32-template main.c)
target_link_libraries(stm32-template CMSIS::STM32::F407VG)

That will add include directories, peripheral layer files, startup source, linker script and compiler flags to your executable.

CMSIS creates the following targets:

So, if you don't need linker script, you can link only CMSIS::STM32::<TYPE> library and provide your own script using stm32_add_linker_script function

Note: Because of some families multi-cores architecture, all targets also have a suffix (e.g. STM32H7 has ::M7 or ::M4). For example, targets created for STM32H747BI will look like CMSIS::STM32::H7::M7, CMSIS::STM32::H7::M4, CMSIS::STM32::H747BI::M7, CMSIS::STM32::H747BI::M4, etc.

The GCC C/C++ standard libraries are added by linking the library STM32::NoSys. This will add the --specs=nosys.specs to compiler and linker flags. If you want to use C++ on MCUs with little flash, you might instead want to link the newlib-nano to reduce the code size. You can do so by linking STM32::Nano, which will add the --specs=nano.specs flags to both compiler and linker. Keep in mind that when using STM32::Nano, by default you cannot use floats in printf/scanf calls, and you have to provide implementations for several OS interfacing functions (_sbrk, _close, _fstat, and others). You can enable printf/scanf floating point support with newlib-nano by linking against STM32::Nano::FloatPrint and/or STM32::Nano::FloatScan. It is also possible to combine STM32::Nano and STM32::NoSys to have the benefits of reduced code size while not being forced to implement system calls.

HAL

STM32 HAL can be used similar to CMSIS.

find_package(HAL [HAL_version] COMPONENTS STM32F4 REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

CMAKE_INCLUDE_CURRENT_DIR here because HAL requires stm32<family>xx_hal_conf.h file being in include headers path.

[HAL_version] is an optional version requirement. See find_package documentation. This parameter does not make sense if multiple STM32 families are requested.

HAL module will search all drivers supported by family and create the following targets:

Note: Targets for multi-cores devices will look like HAL::STM32::<FAMILY>::<CORE>, HAL::STM32::<FAMILY>::<CORE>::<DRIVER>, etc.

Here is typical usage:

add_executable(stm32-blinky-f4 blinky.c stm32f4xx_hal_conf.h)
target_link_libraries(stm32-blinky-f4
    HAL::STM32::F4::RCC
    HAL::STM32::F4::GPIO
    HAL::STM32::F4::CORTEX
    CMSIS::STM32::F407VG
    STM32::NoSys
)

Building

    $ cmake -DCMAKE_TOOLCHAIN_FILE=<path_to_gcc_stm32.cmake> -DCMAKE_BUILD_TYPE=Debug <path_to_sources>
    $ make

Linker script & variables

CMSIS package will generate linker script for your device automatically (target CMSIS::STM32::<DEVICE>). To specify a custom linker script, use stm32_add_linker_script function.

Useful CMake functions

In the following functions, you can also specify mutiple families.

Additional CMake modules

stm32-cmake contains additional CMake modules for finding and configuring various libraries and RTOSes used in the embedded world.

FreeRTOS

cmake/FindFreeRTOS - finds FreeRTOS sources in location specified by FREERTOS_PATH (default: /opt/FreeRTOS) variable and format them as IMPORTED targets. FREERTOS_PATH can be either the path to the whole FreeRTOS/FreeRTOS github repo, or the path to FreeRTOS-Kernel (usually located in the subfolder FreeRTOS on a downloaded release). FREERTOS_PATH can be supplied as an environmental variable as well.

It is possible to either use the FreeRTOS kernel provided in the Cube repositories, or a separate FreeRTOS kernel. The Cube repository also provides the CMSIS RTOS and CMSIS RTOS V2 implementations. If the CMSIS implementations is used, it is recommended to also use the FreeRTOS sources provided in the Cube repository because the CMSIS port might be incompatible to newer kernel versions. The FreeRTOS port to use is specified as a FreeRTOS component. A list of available ports can be found below. If the FreeRTOS sources provided in the Cube repository are used, the device family also has to be specified as a component for the FreeRTOS package.

CMSIS RTOS can be used by specifying a CMSIS target and by finding the CMSIS RTOS package. The following section will show a few example configurations for the H7 and F4 family. You can also find example code for several devices in the examples folder.

Typical usage for a H7 device when using the M7 core, using an external kernel without CMSIS support. The FreeRTOS namespace is set to FreeRTOS and the ARM_CM7 port is used:

find_package(CMSIS COMPONENTS STM32H743ZI STM32H7_M7 REQUIRED)
find_package(FreeRTOS ARM_CM7 REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE
    ...
    FreeRTOS::ARM_CM7
)

Typical usage for a F4 device, using an external kernel without CMSIS support. The FreeRTOS namespace is set to FreeRTOS and the ARM_CM4F port is used:

find_package(FreeRTOS COMPONENTS ARM_CM4F REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE
    ...
    FreeRTOS::ARM_CM4F
)

For ARMv8-M architecture (CM23 and CM33) you can choose "No Trust Zone" port:

find_package(FreeRTOS COMPONENTS ARM_CM33_NTZ REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE
    ...
    FreeRTOS::ARM_CM33_NTZ
)

Or you can use the trust zone with:

find_package(FreeRTOS COMPONENTS ARM_CM33 REQUIRED)
target_link_libraries(${SECURE_TARGET_NAME} PRIVATE
    ...
    FreeRTOS::ARM_CM33::SECURE
)
target_link_libraries(${NON_SECURE_TARGET_NAME} PRIVATE
    ...
    FreeRTOS::ARM_CM33::NON_SECURE
)

Another typical usage using the FreeRTOS provided in the Cube repository and the CMSIS support. The FreeRTOS namespace is set to FreeRTOS::STM32::<FAMILY>, the ARM_CM7 port is used and the device family is specified as a FreeRTOS component with STM32H7:

find_package(CMSIS COMPONENTS STM32H743ZI STM32H7_M7 RTOS REQUIRED)
find_package(FreeRTOS COMPONENTS ARM_CM7 STM32H7 REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE
    ...
    FreeRTOS::STM32::H7::M7::ARM_CM7
    CMSIS::STM32::H7::M7::RTOS
)

The following CMSIS targets are available in general:

The following additional FreeRTOS targets are available in general to use the FreeRTOS provided in the Cube repository

For the multi-core architectures, both family and core need to be specified like shown in the example above.

The following FreeRTOS ports are supported in general: ARM_CM0, ARM_CM3, ARM_CM3_MPU, ARM_CM4F, ARM_CM4_MPU, ARM_CM7, ARM_CM7_MPU, ARM_CM23, ARM_CM23_NTZ, ARM_CM33, ARM_CM33_NTZ.

Other FreeRTOS libraries, with FREERTOS_NAMESPACE being set as specified in the examples above: