STMicroelectronics recently published an extension for turning VS Code into a STM32 IDE which combines CMake, ST MCU Finder, STM32CubeMX and STM32CubeCLT and a dozen of extensions.
STM32 Tools
ST-MCU-FINDER explores the full range of STM32 and STM8 microcontrollers, processors, dev boards, and examples.
STM32CubeMX simplifies the configuration of STM32 microcontrollers and generates the corresponding initialization C code. Starting from v6.11.0, STM32CubeMX can generate VSCode-compatible CMake projects, eliminating the need for .cproject/.project conversion in CubeIDE.
STM32CubeCLT is a package containing toolchain and STM32 device related data required for project creation, build, and debug functionality.
Install this extension, then configure paths for CubeMX, MCUFinder, and CubeCLT.
Generate new Project
Click on Launch STM32CubeMX to start a new project:
Choose to Start with an MCU or a Board:
Select a target in ST MCU Finder then click on Start Project:
If a board is selected, prefer to choose to Initialize all peripherals with their default mode which is already configured for the board.
Go through STM32CubeMX configuration:
Finally press on GENERATE CODE button.
Create a new git repo with .gitignore file:
Add an init commit with initialized files:
Build Project
Open CMake Extension to see Configurations and Tasks:
Open Debugger to start a debugging session:
Note that ST’s GDB is built with old libncurses5 which is not available in recent Linux release.
To install libncurses5, download the packages and install them manually:
Application
It is better to write application on-top of generated code in a separated layer, so that the STM32CubeMX will not actidently deleted application when it re-generates source files.
Create a new folder App, then create files as below structure:
The header file:
The source file:
Then the CMake file:
Then call the app_main in the main function:
Finally, add the App’s CMake file to the main CMake file:
FreeRTOS Kernel
Here are stesp to add FreeRTOS into an example project.
It’s quite straight forward as FreeRTOS already provides CMake files.
Copy FreeRTOS-LTS/FreeRTOS/FreeRTOS-Kernel to App/ThirdParty/FreeRTOS-Kernel.
Configuration
The guide for integrating FreeRTOS Kernel is written in its root CMakeLists.txt:
So in the App’s CMake file, set the definitions FREERTOS_PORT and FREERTOS_HEAP, create library freertos_config, then call to FreeRTOS-Kernel’s CMake file:
The template header file for FreeRTOS Configuration is located in FreeRTOS-Kernel/examples/template_configuration/FreeRTOSConfig.h.
Either copy the template and change the settings, or create a new one.
Here are must-have configs for creating a config file from zero.
Build and fill missing configs into the file.
App Task
App main function now should create an RTOS Task and start the RTOS Kernel:
When build the project again, an error happens: undefined reference to `vTaskDelay’.
At this step, project is able to be built, but app will crash on a Hard Fault Exception.
However, FreeRTOS also defines those exceptions handlers:
FreeRTOS exception handlers must be called:
either directly from Interrupt Vector Table (IVT):
When the configCHECK_HANDLER_INSTALLATION == 1, FreeRTOS will validate the Direct Installed handlers, if handlers are not in IVT, system will fail on an assertion:
or indirectly called in coresspinding generated handlers:
Working with STM32CubeMX generated project, it is recommended to reserve SysTick for FreeRTOS and use another Timer for HAL timming functions.
Then tell STM32CubeMX to not generate below interrupt handlers:
SVC_Handler
PendSV_Handler
SysTick_Handler
Then map the default handler names to FreeRTOS handlers: