Skip to content

michaelniedermayr/clion_stm32_openocd

Repository files navigation

Programming and Debugging of STM32 with Clion

A short guide on how to program and debug different STM32 boards with CLion on a Windows computer (I use Windows 10). For this purpose we will use a CLion plugin which is called OpenOCD + STM32CubeMX support and was written by Ilya Motornyy. We will create a CLion project based on an initial C-code that is generated by STM32CubeMX. And I will also show you how to transform this project into C++.

What do we need?

Here is a list of the software and tools we will use.

Required software

  • ST Link Utility (Utility: 4.2.0.0, USBDriver.dll 5.0.3.0, CLI.exe: 3.2.0.0)
  • STM32CubeMx (4.25.0)
  • GNU Arm Embedded Toolchain (gcc-arm-none-eabi 7-17-q4-major)
  • MinGW (5.0)
  • OpenOCD (0.10.0)
  • Clion (2017.3.4)
  • OpenOCD Plugin für Clion (1.0.beta3)

Required hardware

  • STM32F4 discovery board

  • Generic ST-Link V2 Programmer

Setup the software

Let’s start and setup the required software. This will take some time but is not too complicated.

Setup ST-Link Utility and STM32CubeMX

The ST-LINK Utility is a software provided by ST for programming their microcontrollers. STM32CubeMX is a tool that allows a very simple configuration of the STM32 microcontrollers and the generation of the corresponding initialization C code.

  • Download and install the ST-Link Utility (you have to register and sign in).
  • Download and install the STM32CubeMX (you have to register and sign in).

Setup GNU Arm Embedded Toolchain

This contains the C/C++ compiler for Arm Cortex-M processors (like the STM32s).

  • Download and install the GNU Arm Embedded Toolchain.
  • During installation you will be asked if you want to 'add path to environment variable'. Selected it!
  • Now you should be able to execute the toolchain from the command line. To test this, just open the command line window and enter ‘arm-none-eabi-gcc --version’.

Setup MinGW

The MinGW toolchain provides the GNU Compiler Collection (GCC) and the GNU Debugger (GDB) for windows.

  • Download and install MinGW.
  • Use the default installation path: 'C:\MinGW'

Setup OpenOCD

Open On-Chip Debugger (OpenOCD) provides debugging and in-system programming for embedded devices.

  • Download the newest win32.zip version of OpenOCD and unpack it.

Setup CLion

There are different licences available for CLion. For example free licences for students and open-source projects. For the first try you can use the 30-days trial license.

  • Download and install CLion.
  • During the installation process you can set up MinGW as the default toolchain. clion_toolchain1

Setup openOCD Plugin in Clion

Next we install the openOCD plugin. But let’s check first if the toolchain was installed properly.

  • Start Clion.
  • Create a new project.
  • Go to: File | Settings | Build, Execution, Deployment | Toolchains.
  • If you only see green check marks you are happy and can continue. clion_toolchain2

Now install the plugin.

  • Go to: File | Settings | Plugins.
  • Search for the plugin ‘OpenOCD + STM32CubeMX support’ and install it.
  • Restart CLion.
  • Go to: File | Settings | Build, Execution, Deployment | OpenOCD Support.
  • OpenOCD Home: Set the location of the OpenOCD project you have downloaded and unzipped earlier.
  • Board Config File: OpenOCD provides different config files for all kinds of boards. Since we use the STM32F4 discovery board for the first demo, you should select the corresponding config file (board/stm32f4discovery.cfg). clion_openocd

Run the first demo

Now we will test if everything works. For this we will write a simple LED blinking demo and install it on the discovery board.

Can we connect to the discovery board

Let’s see if all drivers are installed properly and we can connect to the board. We use the ST-Link Utility for this.

  • Connect the discovery board with your computer.
  • Start ST-Link Utility.
  • Connect to the target (Target | Connect).
  • Now you should see some information about the board. stutility

Generate the initial code with STM32CubeMX

We use STM32CubeMX to generate the initial code base. In the next step, we will create the CLion project on top of that.

  • Start STM32CubeMX.
  • Selected New Project.
  • Select the Board Selector-tab.
  • Search for stm32f4discovery and double-click on the corresponding board.
  • When you asked ‘Initialize all peripherals with their default Mode?’ select No. At the moment we only need the basic peripherals like the GPIO pins.
  • Now you should see that the pins PD12-PD15 of the MCU are already marked green and set to GPIO_Output. These pins are connected to the LEDs on the discovery board which we want to let blink.

stm32cubemx

  • Next click on: Project | Generate Code.
  • Select a project name and a project location.
  • Change the toolchain to SW4STM32.
  • Click on OK.
  • The code will be generated.

Create the CLion project

Now we import the generated code in CLion, update the CMake files with the plugin and download the code to the discovery board.

  • Start CLion.

  • Import the generated code: File | Import Project or from the welcome screen: Import Project from Sources.

  • If CLion asks you to ‘Import CMake Project’, just click OK.

  • Next step, click: Tools | Update CMake project with STM32CubeMX project. The plugin will perform its magic and rewrites the CMakeLists.txt file.

  • If everything works, you should see this dialog. Now you should be able to program and debug the STM32 with CLion. clion_openocd_cmake

  • To add the blinking code, open the src/main.c file and go to the main while loop (line 102-109). clion_main

  • Replace the while-loop with the following code. This let’s the four leds blink on the discovery board.

while (1) {
   int waitingTime = 500; // waiting time in ms

   HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
   HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
   HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
   HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_SET);
   HAL_Delay(waitingTime);
   HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
   HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
   HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
   HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
   HAL_Delay(waitingTime);
   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */
}
  • Click on the green play icon or press Shift+F10 to download the code to the STM32F4 discovery board. Enjoy the colorful blinking!
  • Last step: Test debugging. Set a breakpoint in the while loop and run the debugger (click on the green bug or press Shift+F9). Now you can step through the code running on the discovery board. clion_debugger

Change the code to C++

The code generated by STM32CubeMX is written in C. Unfortunately, C++ is not supported at the moment. If you want to use C++ nevertheless, you just have to make some adjustments.

  • Open the Windows Explorer and go to the location of your project.
  • Go to ‘your project\Drivers\STM32F4xx_HAL_Driver\Src’
  • Rename the extensions of all files from .c to .cpp.
  • Also rename the extension of the files in ‘your project\Src’.
  • Start CLion.
  • Open CMakeLists.txt.
  • Search for the line PROJECT(projectname C ASM) and change it to PROJECT(projectname CXX ASM). This tells CMake to use the C++ compiler instead of the C compiler.
  • Rebuild CMake by clicking on Tools | CMake | Reload CMake Project.
  • Now you can use the full power of object oriented programming for your project.
  • Keep in mind that STM32CubeMX will overwrite these changes if you rerun the code generation. The OpenOCD plugin will also restore the changes in CMakeLists.txt if you run it again by clicking on Tools | Update CMake project with STM32CubeMX project.

Using an external ST-Link programmer

Of course we can also use CLion to program and debug custom STM32 boards. For this purpose I use an generic ST-Link V2 programmer which you can buy online for just a couple of Euros. If you have no custom board available but want to test the programmer nevertheless, you can use the programmer in combination with the discovery board.

Update the generic ST-Link V2 programmer

First, let’s see if the programmer is working and update to date.

  • Plugin the ST-Link V2 (don’t forget to unplug the discovery board).
  • Start the ST-Link Utility.
  • Go to: ST-LINK | Firmware update.
  • A new dialog opens. Click on ‘Device connect’.
  • Next click ‘Yes >>>>’. Now the update should start.

Program a custom board with the ST-Link V2 programmer

I found an old flight control board for drone in my basement that runs on an STM32F4. This should be sufficient for a first test. In order to use it, I generated the initial code for the type of microcontroller on this board in STM32CubeMX. Then I imported the code to CLion. Furthermore, I had to create a custom board config file (see below). Finally, I connected the board to the programmer (VDD, GND, SWCLK, SWDIO and REST). That’s it!

To create a custom board config file:

  • Open windows explorer and go to location of the OpenOCD projects you have downloaded earlier.
  • Go to openOCD\scripts\board and create a new file called 'stm32f4_stlink_programmer.cfg'.
  • Open the file with a text editor and add the following lines.
source [find interface/stlink.cfg]
source [find target/stm32f4x.cfg]
  • Open CLion.
  • Go to: File | Settings | Build, Execution, Deployment | OpenOCD Support.
  • Set the Board Config File to 'stm32f4_stlink_programmer.cfg'.

Program the discovery board with the ST-Link V2 programmer

In order to program the discovery board with an external programmer, you have to make some modifications.

  • There are two jumpers which connect the internal programmer of the discovery board with the actual microcontroller. Just remove them.

  • Unsolder the solder bridge SB11 on the backside of the discovery board which connects the rest line between the internal programmer and the microcontroller.

Connect the the external programmer with the discovery board.

external programmer discovery board
3.3V VDD
GND GND
RST NRST
SWCLK PA14
SWDIO PA13

Don’t forget to select the custom board config file (see above). If you get an error message when you program the discovery board for the first time, just press the black reset button on the board. This worked for me.

Use the discovery board as an external programmer

You can also use a discovery board to program other boards. In this case you have to remove the jumpers and the solder bridge as well. Now you can connect the target board to the SWD connector of the discovery board. For more information see the official manual.

Releases

No releases published

Packages

No packages published

Languages