STM32CubeMX, CMake and QtCreator

8 Feb

This is a short walk-through to setup a build configuration for STM32 projects using cmake and QtCreator.

STM32CubeMX on Linux

The STM32CubeMX software is available for Linux now! (last time I checked it was only for Windows). Download and install it. Then, click “New Project” and select the processor or board that you want to use. In my case, it’s the STM32F0 Discovery board.

Click OK and you will see a nice graphical display of your processor. You can change/tune a lot of stuff here, but for now leave it as it is. Go to Project->Settings and enter a project name. Also choose toolchain “Other Toolchains (GPDSC)”.

On the “Code Generator” tab, make sure to only add necessary library files.

After configuring these project settings, go to Project->Generate Code. If it is the first time, it will automatically download the required HAL package and install it to ~/STM32Cube/Repostory. The project folder will look similar to this:

Compiler

Previously I used the team-gcc-arm-embedded ppa, but that’s now suddenly at version 6 and I still want to use version 5. Version 5 is available from launchpad. I just copied the folder into my home directory, so I got /home/bart/gcc-arm-none-eabi-5_4-2016q3.

CMake

As you can see, there is no makefile or whatsoever in the project directory. This time, I’ll use cmake to build the project. There is an awesome project out on github which makes life super-easy: stm32-cmake. Clone the repository to your preferred location. I cloned it to /home/bart/git/stm32-cmake.

The next thing to do is add a file CMakeLists.txt in the main directory of your project, for example /home/bart/testdir/stm32f0_example_project/CMakeLists.txt. For me it has the following content:

SET(STM32_CHIP STM32F051R8)
SET(STM32Cube_DIR /home/bart/STM32Cube/Repository/STM32Cube_FW_F0_V1.7.0)
SET(TOOLCHAIN_PREFIX /home/bart/gcc-arm-none-eabi-5_4-2016q3)
SET(CMAKE_TOOLCHAIN_FILE /home/bart/git/stm32-cmake/cmake/gcc_stm32.cmake)
SET(CMAKE_MODULE_PATH /home/bart/git/stm32-cmake/cmake)

PROJECT(stm32f0_example_project)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
ENABLE_LANGUAGE(ASM)

FIND_PACKAGE(CMSIS REQUIRED)
FIND_PACKAGE(STM32HAL COMPONENTS gpio tim REQUIRED)

INCLUDE_DIRECTORIES(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMSIS_INCLUDE_DIRS}
    ${STM32HAL_INCLUDE_DIR}
    Inc
)

SET(PROJECT_SOURCES
    Src/main.c
    Src/stm32f0xx_hal_msp.c
    Src/stm32f0xx_it.c
    Inc/main.h
    Inc/stm32f0xx_hal_conf.h
    Inc/stm32f0xx_it.h
)

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} ${PROJECT_SOURCES} ${CMSIS_SOURCES} ${STM32HAL_SOURCES})

STM32_SET_TARGET_PROPERTIES(${CMAKE_PROJECT_NAME})
STM32_ADD_HEX_BIN_TARGETS(${CMAKE_PROJECT_NAME})

You may need to change some of the lines to match your processor, installation locations and source files.

QtCreator

The next and final step is configuring QtCreator. I assume you already have the newest version installed. The setup is very similar as in a previous post. However this time it is important to clean the CMake configuration field!

Then, when this is configured, go to File->Open File or Project. Browse to your project folder and select CMakeLists.txt. A screen pops up where you have to select the build configuration which you have just created:

Then hit “Configure Project” and you are all setup!

Hit the hammer in the bottom left corner to build. The default build directory will be one level higher than your project directory. In my case, the build directory is at /home/bart/testdir/build-stm32f0_example_project-STM32-Default.

OpenOCD

This process is still similar to the previous post. However there are some actions I had to take to make it work with the newest version of openocd (0.10).

  1. Download openocd from sourgeforce.
  2. Extract the archive to a directory of your choise.
  3. Install the libusb 1.0 dev package. Without this, the STLink did not work! “sudo apt install libusb-1.0-0-dev”
  4. Run “./configure” (not as root)
  5. Run “make”
  6. Run “sudo make install”
  7. “sudo cp contrib/60-openocd.rules /etc/udev/rules.d/”
  8. “sudo udevadm control –reload-rules”

It is also working in pipe mode now! So you don’t have to run openocd yourself in the background.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.