For embedded projects that target STM32 boards, CLion integrates with STM32CubeMX.
You can open an .ioc file generated by CubeMX as project, and CLion will automatically create a CMake project structure. Or you can start a new project from scratch - the wizard will help you configure it in CubeMX.
STM32CubeMX support in CLion is a part of the bundled OpenOCD + STM32CubeMX plugin. For this reason, project settings and certain steps of the project creation procedure are coupled with OpenOCD. However, STM32CubeMX projects are not limited to be used with OpenOCD only: you can also set up a more generic Embedded GDB Server run/debug configuration. In the latter case, ignore the OpenOCD-related settings and steps of your project set up.
CubeMX is a graphical tool that helps 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 which CLion then converts into a CMake project.
GNU ARM toolchain is a cross-platform set of tools which 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-gccfrom the command line - your system should recognize this command.
In OpenOCD Location field empty., you can check whether the location of CubeMX was detected correctly, and set the path manually if needed. If you don't plan to use OpenOCD, leave the
Open a project from the .ioc file
If you already have a project that contains an .ioc file, call , select the .ioc file, and choose Open as Project. CLion will automatically generate a CMake project structure.
Create a new project
For new projects, you have two options: a) configure your MCU and generate the code in CubeMX, then open the .ioc file in CLion, or b ) create a project from scratch as described below.
Go to STM32CubeMX as your project type. CLion will create the .ioc file for the default STM32F030F4 MCU:and select
.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.
Now you need to switch to CubeMX to generate the project sources and fill the .ioc file. Click the Open with STM32CubeMX link to open CubeMX from CLion.
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:
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:
Back in CLion, when the CMake project loading completes, you will be prompted to select a board config file. The list of suggestions consists of board config files shipped with OpenOCD in Openocd-Installation/share/openocd/scripts/board:
Click Copy to Project & Use, and the selected board config file will be added 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 gets updated automatically:
You can also update the project manually: right-click the .ioc file in Project View and select Update CMake project with STM32CubeMX.
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 header 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
To build you project, use one of the following options:
Run or debug an OpenOCD or Embedded GDB Server configuration. Both of these configurations include Build as a Before launch step:
Use Build actions to build the project manually.