CMake on STM32 | Episode 2: build with CLion
Tags: embedded, cpp, microcontrollers, cmake
Published: 2020-04-29 17:21:36

In this article, you will learn how to build a CMake project for STM32 and download the software to the MCU with [CLion, Jetbrains IDE for C & C++](https://www.jetbrains.com/clion/).

In the [episode 1](https://dev.to/younup/cmake-on-stm32-the-beginning-3766), we got a BSP along with the `CMakeLists.txt`:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gvzoc0rzt4qyf3igdms3.png)

Finally we built the software from the command line.

To be honest, in my every day life, I don't use the command line. I use instead CLion because it is much more confortable... Let's try it!

## Why CLion?

Coding with CLion is really a pleasure and IMHO it is the best IDE for C & C++ out there. If I want to remain objective, I will just say that there are two very good reasons to talk about CLion in this series about CMake and STM32:

1. Clion uses CMake as its default build system.
2. It has an integrated plugin with OpenOCD so we can download / run / debug our code on STM32 microcontrollers.

Great, right?

The license is very affordable for an enterprise but [200€ per year](https://www.jetbrains.com/clion/buy/#commercial?billing=yearly) are probably too much for a hobbyist. Anyway there is a solution: 
[CLion EAP version](https://www.jetbrains.com/resources/eap/) is a free version of CLion. In fact, EAP versions are beta versions of the next releases of Jetbrains IDEs. They are stable and you get some cool features ahead... for free.

## Open the project in CLion

Install CLion, open it, go to _File_, _Open_ and select the location of your project:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/itj80x49k5os42fv18qw.png)

As soon as you click _OK_, CLion will parse your `CMakeLists.txt` and will complain:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6scpdngtg5a6tfbstc3h.png)

As I mentioned in episode 1, we have to specify the toolchain file. Here CLion doesn't ask this to CMake so CMake uses the default native toolchain, mingw64, which is not able to compile with options that are specific to ARM GCC.

## Specify the toolchain file

To specify the toolchain file, go _File_ and then _Settings_. In _Build, Execution, Deployment_, you will find the _CMake_ entry. This is where you control the CMake configurations and their options. Simply add the option `-DCMAKE_TOOLCHAIN_FILE=arm-none-eabi-gcc.cmake` (the option for the build type is added automatically based on the selection of the _Build type_ drop-down list):

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/duhvyqfnz9aexlic2jvn.png)

When you click _OK_, CLion reloads the CMake project with success.

## Build the project

This is as simple as doing _Build_ and _Build project_!

Alternatively, you can click the green hammer on the top-right of the screen:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6t7pwjdz6gzm2u7u4g9e.png)

You can follow the build process in the _Messages_ view:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hqptd5mq1nu746a800md.png)

All good!

## Download & run the application

CLion has an [integrated plugin](https://www.jetbrains.com/help/clion/openocd-support.html) to download & run a software on a MCU using [OpenOCD](http://openocd.org/).

Let's create a [run/debug configuration](https://www.jetbrains.com/help/clion/creating-and-editing-run-debug-configurations.html) to program our board.

Select _Run_ and then _Edit configurations..._. You will see one existing configuration:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xi9f72dbz1jfp3y9blk8.png)

This configuration (created by default when the CMake project is loaded) is bad: it will try run the executable on Windows... You can delete it with the *-* button.

Click the *+* button to create a new _OpenOCD Download & Run_ configuration. Surprise: a board config file is requested by OpenOCD... How do we get such a file? We simply go to OpenOCD's repository on GitHub and [download the appropriate file](https://github.com/ntfreak/openocd/blob/master/tcl/board/st_nucleo_f4.cfg)! Here is how this `st_nucleo_f4.cfg` looks like:

```cfg
# This is for all ST NUCLEO with any STM32F4. Known boards at the moment:
# STM32F401RET6
# http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF260000
# STM32F411RET6
# http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF260320

source [find interface/stlink.cfg]

transport select hla_swd

source [find target/stm32f4x.cfg]

reset_config srst_only
```

Select it in your run configuration:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1rukydnkzvuhi6zwfc74.png)

Click _OK_ to close the menu.

We can now download and run our program. Be sure the NUCLEO-F413ZH board is connected to your computer and click the green triangle next to the hammer:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/abklgkdhb3cqohsdf5xq.png)

Hell yeah!

## Debugging

We saw in episode 1 that our application does nothing more than an infinite idle loop... Consequence: there is no sign from the outside that our software is running.

Let's debug it so that we can check what it is actually doing! Open `BSP/Src/main.c` and double-click in the margin to add a breakpoint:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/48kqeh1cj0pl24l6lmqo.png)

Click the green bug next to the green triangle to start the debugging session. The debugger will stop on the breakpoint:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lfalayl5dj8i9m6kljyy.png)

This a regular debugging session: you can step over lines, step into a function, see the stack frames, add or remove breakpoint, etc. And you will end in the infinite loop at line 102.

## Conclusion

CLion is a great IDE for C and C++ but it is especially great to work with CMake on STM32. In fact, it is great for any board / MCU that can be targeted by OpenOCD.... and [there are a lot of them](https://github.com/ntfreak/openocd/tree/master/tcl/board)! If you want go get confortable, give a try! I'm sure you will like it :)