CLion 2020.1 Help

STM32CubeMX Projects

For embedded projects that target STM32 boards, CLion provides integration with STM32CubeMX. You can open an .ioc file generated by CubeMX as project in CLion, and the CMake project structure will be created automatically. Or you can start a new project from scratch in CLion and the project wizard will help you configure it in CubeMX.

Required tools

  1. STM32CubeMX

    CubeMX is a graphical tool to configure STM32 microcontrollers and generate the corresponding C code. In CubeMX, you can set up both CPU and peripherals, and adjust frequencies and pin modes. It generates a stub project that CLion then converts into a CMake project.

  2. GNU ARM toolchain

    GNU ARM toolchain is a cross-platform set of tools that launches on desktop, while the build result can only run under the target ARM MCU environment.

    For CLion to detect the toolchain, it should be presented in system PATH. You can check it by running arm-none-eabi-gcc from the command line - your system should recognize this command.

In Settings/Preferences | Build, Execution, Deployment | Embedded Development, you can check whether the toolchain was detected correctly and set the path manually if needed. If you don't plan to use OpenOCD, leave the OpenOCD Location field empty.

  • For GNU ARM Toolchain, use version 2019-q3 or 2018-q2 (and earlier). See the issue with 2018-q4.

  • Only the MinGW and MinGW-w64 environments are supported (CPP-15034).

    Open a project from the .ioc file

    • If you already have a project that contains an .ioc file, call File | Open, select the .ioc file, and choose Open as Project. CLion will automatically generate the CMake project structure.

    Open an existing project in CLion If you already have an embedded project, just open it in CLion. When you then first open an .ioc file in the editor, CLion will generate the CMake project for you:

    Create a new project

    For new projects, you can either start from configuring your MCU and generating code in CubeMX and then open the .ioc file in CLion, or you can create a project from scratch as described below.

    1. In the New Project wizard (File | New Project), select STM32CubeMX as your project type. CLion will create the .ioc file for the default STM32F030F4 MCU:

      template new project

      The .ioc files are CubeMX original project files that contain static initialization settings. In CLion, this file is read-only and on the current step it is a place-holder for the configuration that will be set up in CubeMX on the next step.

    2. Now we need to switch to CubeMX to generate the project sources and fill the .ioc file. Click the Open with STM32CubeMX link to open CubeMX right from CLion.

    3. In CubeMX, the configuration is already set for the newly created project. Click STM32F030F4Px to invoke the MCU/Board Selector and change the default MCU:

      changing mcu in cubemx

      After the hardware configuration is ready, check that the Toolchain / IDE field is set to STM32CubeIDE and select Generate Under Root, then click Generate Code:

      generate code in cubemx

    4. Back in CLion, when the CMake project loading completes, we are prompted to select the board config file. The list of suggestions is taken from the set of board config files shipped with OpenOCD in the Openocd-Installation/share/openocd/scripts/board folder:

      board configs selection dialog

      You will also be able to select the proper board config, including a custom one, later in the settings of the OpenOCD Run/Debug configuration.

      Click Copy to Project & Use to add the selected board config file to your project tree. You will be able to open and adjust it in the editor.

    Edit source code

    • When you change the MCU configuration in CubeMX and click Generate Code, your CLion project is updated automatically:

      project update after changes in cubemx

      You can also update the project manually: to do this, select Update CMake project with STM32CubeMX from Tools | CMake on the main menu or from the context menu of the Project View.

    • While editing the files generated by CubeMX, always enclose your code in the /* USER CODE BEGIN ... */ and /* USER CODE END ... */ pseudo comments. This will protect your code from being overwritten by CubeMX during code regeneration. Also, consider extracting pieces of code into separate .c or .h files (see the next point).

    • If you add source or include files, place them in the Src and Inc directories.

    • CLion regenerates CMakeLists.txt from the template every time the project is updated. If you need to make changes in the CMake script (for example, to add external libraries or FPU support), do so in CMakeLists_template.txt and then call Update CMake project with STM32CubeMX.

    Build a project

    Choose one of the following options:

    Last modified: 06 July 2020