While C may be the preferred language of many embedded programmers, C++ does have a place, especially if you want to use certain libraries, like TensorFlow Lite for Microcontrollers. In this tutorial, I will show you how to get started making a program (blinky) for an STM32 microcontroller using C++ on STM32CubeIDE.
I will use an ST Nucleo-L432KC for this demonstration, as it’s small, fits on a breadboard, and still has a powerful ARM Cortex-M4 core. You are welcome to use any STM32 part, but I generally recommend starting out with one of the Nucleo boards.
Plug your STM32 part into your computer.
Start STM32CubeIDE[link] and select File > New > STM32 Project. Select your target microcontroller or board (I’ll select my Nucleo-L432KC).
Click Next and then give your project a name. I like to prefix the board I’m using to the project name, so nucleo-l432-cpp-blinky is what I called mine. In Targeted Language, select C++.
Click Finish and click Yes when asked, “Initialize all peripherals with their default Mode?” Click Yes to open the new perspective.
Leave everything as default on the CubeMX device configuration GUI.
Code should be automatically generated. However, CubeMX generates a main.c file, which is not what we want for a C++ project. Right click on <project name> > Core > Src > main.c and select Rename. Change the name of the file to main.cpp. The compiler is sensitive to file suffixes, so .c files will compile as C code and .cpp or .cc files will compile as C++.
Open the main.cpp file associated with the project. Scroll down to the while (1) loop and add the following code just inside the while (1) loop:
HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin); HAL_Delay(500);
In context of the rest of the source code, this should look something like:
Note that the GPIO port and pin names might be different for your board. If you’re using a Nucleo-64, the labels will be LD2_GPIO_Port and LD2_Pin.
In case you are wondering: yes, this is still C inside of a C++ file. Most of the STM32 HAL libraries are written in C, but they’re safe to call in C++.
Save and click Project > Build Project. You should notice that any .cpp or .cc files get automatically built with g++.
Click Run > Debug and click OK to accept the default launch configuration properties. Accept any prompts you might see to open the debug perspective. Click Run > Resume, and you should see the LED begin to blink.
That’s it! You should be ready to build your STM32 project in C++ with all of the object-oriented programming you so desire.
Please note that you can generally call C functions from within a C++ program and vice versa. You will need to add extern “C” to your functions, which is detailed in this article.
If you are already working within a C project and you wish to convert it to C++, right-click on the project name in the project explorer pane and click Convert to C++. You will then need to rename any source files (to .cpp or .cc) you wish to be compiled by g++.
If you right-click on any C++ project, you’ll find that you can revert it back to a C project using a similar method.
I hope this helps! I know that I’m looking forward to using C++ libraries in my STM32 project.