CLion 2019.1 Help

Embedded Development

For embedded development, CLion bundles the updated functionality of the OpenOCD + STM32CubeMX support plugin. This plugin targets the STMicroelectronics boards and provides on-chip debugging with OpenOCD.

STM32CubeMX support and OpenOCD support are independent parts of this plugin. So for MCUs other than STM32, you can still use the functionality of OpenOCD. In this case, you will need to create a CMake project for your sources in advance (take a look at the plugin's template for CMakeLists.txt), provide a custom linker script and a board config file. This way, you will be able to edit and build your code in CLion, and then flash and debug on-chip with the help of OpenOCD Run/Debug configurations.

Required tools

  1. STM32CubeMX - a graphical tool to configure STM32 microcontrollers and generate the corresponding C code.

    In CubeMX, you can configure both CPU and peripherals for your MCU, adjust the frequencies and pin modes. After that, CubeMX generates a stub project that CLion will convert into a CMake project. Also note that CubeMX automatically downloads the firmware packages for each particular MCU family.

  2. OpenOCD - the Open On-Chip Debugger.

    For your embedded project in CLion, OpenOCD will be used as a flash probe and remote gdbserver.

  3. GNU ARM toolchain - a set of cross-platform tools that enables you to launch the tools on desktop, while the build result will only run under the target ARM MCU environment. Make sure that GNU ARM Toolchain is presented in the system PATH by running arm-none-eabi-gcc from the command line.

    Mind that pretty printers are not available for the arm-none-eabi-gdb.exe debugger on Windows and macOS. You can use the arm-none-eabi-gdb-py.exe debugger instead - it includes pretty printers but requires python 2.7 (32 bit) to be installed.

In Settings/Preferences | Build, Execution, Deployment | Embedded Development, you can check whether the tools were detected automatically and set the paths manually if needed.

  • In addition to the tools required for all platforms, install the ST-LINK/V2 driver.

  • For GNU Arm Toolchain, make sure to use version 2018-q2 or earlier (see the issue with the 2018-q4 version).

  • Note that only the MinGW and MinGW-w64 environments are supported (CPP-15034).

Configure a new embedded project

  1. In the New Project wizard, 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

    Note that CubeMX resets the project name when the MCU changes. So we need to provide the name manually in Project Manager, and then accept the suggestion to overwrite the project.

    After the hardware configuration is ready, check that the Toolchain / IDE field is set to SW4STM32 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.

Edit the 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. Alternatively, consider extracting your 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.

Run/Debug with OpenOCD

Upon the project creation, CLion added an OCD project_name configuration (go to Run | Edit Configurations to view the details). In the settings, you can change the board config file, adjust GDB and Telnet ports, and tune Download/Reset options. To create more configurations of the same type, use the OpenOCD Download & Run template.

openocd run-debug configuration

If you Runartwork studio icons avd run this configuration, the compiled firmware is downloaded to the target board, and then the chip is reset. If you Debugicons actions startDebugger svg, the firmware is downloaded, the chip is reset, and then the remote debugger is attached to the MCU. You can now use all the CLion debugging features for your firmware running on-chip:

debug the embedded project

The above screenshot shows how to randomly light a LED on a STM32L073 Nucleo-64 board.

Last modified: 24 July 2019