This repo is a basic example configuring CMake and intellisense on vscode. The code itself is blinking LED and tested with STM32F446RE-nucleo
- CMake-Tools
- Cortex-Debug
Please modify according to the path you installed these tools
- ObKo/stm32-cmake(git repo):
/usr/local/src/stm32-cmake
- openocd(apt):
/usr/local/share/openocd
- gcc-arm-none-eabi toolchain(download):
/usr/local/gcc-arm-none-eabi-9-2020-q2-update
- stlink-tools
- STM32CubeMX package:
/opt/STM32Cube/Repository/STM32Cube_FW_F4_V1.26.1
- Install openocd, stm32-cmake, this repo
sudo apt install openocd
git clone https://github.com/stlink-org/stlink.git
cd stlink && mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=release
sudo make install && cd ../../
git clone https://github.com/ObKo/stm32-cmake.git
sudo mv stm32-cmake /usr/local/src/
git clone https://github.com/lentychang/vscodeProj-stm32-cpp.git
sudo mv vscodeProj-stm32-cpp /usr/local/src/
cp /usr/local/src/vscodeProj-stm32-cpp/gen_stm32_cppproj.sh ~/.local/bin/gen_stm32_cppproj
-
Install gcc-arm-none-eabi
- Download desired version
- Extract to
/usr/local/
-
Install STM32 Libraries from STM32CubeMX
- Home(Main Page)/ Install or remove embedded software packages/ STM32Cub MCU Package / STM32F4 / Install
- Where can we find the installed package? Dropdown Menu / Help / Update Settings / Repository Folder
-
Modify the configuration files for VS Code
All json files are in/usr/local/src/vscodeProj-stm32-cpp/.vscode
-
settings.json
Please update the shown values below. Example below is only partly shown.
Here we configure CMakeTools and cortex-debug extension.(optional) Custom linker script
Add"STM32_LINKER_SCRIPT":"${workspaceFolder}/STM32F446RETX_FLASH.ld"
to"cmake.configureSettings"
But it will produce warning, which is not yet solved.{ "cmake.generator": "Unix Makefiles", "cmake.configureSettings": { "CMAKE_MODULE_PATH":"/usr/local/src/stm32-cmake/cmake", "STM32_TOOLCHAIN_PATH":"/usr/local/gcc-arm-none-eabi-9-2020-q2-update", "STM32_TARGET_TRIPLET":"arm-none-eabi", "STM32_CHIP": "STM32F446RE", "STM32Cube_DIR":"/opt/STM32Cube/Repository/STM32Cube_FW_F4_V1.26.1", "STM32_CUBE_F4_PATH":"/opt/STM32Cube/Repository/STM32Cube_FW_F4_V1.26.1" }, "cortex-debug.armToolchainPath": "/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin", "cortex-debug.openocdPath": "/usr/local/bin/openocd" }
-
cmake-kits.json
Configuration of vscode extension cmake-tools where is toolchain file from stm32-cmake. Since we use toolchain file here, please do not specifiedcompliers
here, which leads to confliction.[{ "name": "C/C++ CMake-Kit for arm-none-eabi 9.3.1", "toolchainFile":"/usr/local/src/stm32-cmake/cmake/gcc_stm32.cmake", "compilers": { "CC":"/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc-9.3.1", "C": "/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc-9.3.1", "CXX":"/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-g++" } }]
-
c_cpp_properties.json
Configuration for intellisense. Please updateincludePath
,defines
,c/cppStandards
,compilerPath
ps. for cStandard, you should always use gnu standard instead of c standard, since STM32Cube HAL is written with gnu11, not c11 standard.{ "configurations": [ { "name": "Linux-arm-cross-platform", "includePath": [ "${workspaceFolder}/**", "/opt/STM32Cube/Repository/STM32Cube_FW_F4_V1.26.1/**" ], "defines": ["STM32F446xx","USE_HAL_DRIVER"], "compilerPath": "/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-g++", "cStandard": "gnu11", "cppStandard": "gnu++14", "compileCommands": "${workspaceFolder}/build/compile_commands.json", "intelliSenseMode": "gcc-arm", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 }
-
launch.json
for debugger
Please updatearmToolchainPath
,configFiles
,searchDir
,svdFile
Example below is only partly shown{ "armToolchainPath": "/usr/local/gcc-arm-none-eabi-9-2020-q2-update/bin", "configFiles": ["/usr/local/share/openocd/scripts/board/st_nucleo_f4.cfg"], "searchDir":["/usr/local/share/openocd/scripts/board/"], "svdFile": "${workspaceFolder}/etc/STM32F446.svd" }
- Add corresponded targets from HAL and CMSIS that need to be linked.
If you want to use printf to print message on host's terminal, or scanf from host's keyboard.
- enabling semihosting in openocd has been done in the launch.json (line25:
monitor arm semihosting enable
). You can remove it if you don't need semihosting. - uncomment
target_link_options
in CMakeLists.txt - comment
target_sources
withsyscall.c
in CMakeLists.txt - Include header
stdio.h
in the source file and addinitialize_monitor_handle();
before while loop - Now printf should work. The printed out message is shown in OUTPUT panel
ps. If you enable semihosting here, be aware that, the device will hang due to monitor_handle and will not start at all, unless you use debugger to run. So, if you want to test you program without debugger, remember to remove initialize_monitor_handle();
and printf
as well as revert the CMakeLists.txt
For CMake version before 3.14 use the following setting in CMakeLists.txt
set_property(TARGET ${CMAKE_PROJECT_NAME} APPEND_STRING PROPERTY LINK_FLAGS " -specs=rdimon.specs -lc -lrdimon")
-
Use STM32CubeMX to generate STM32CubeIDE proejct
If you select other type of projects, you have to modify gen_stm32_cppproj.sh to remove corresponding project files. -
Now you can execute
gen_stm32_cppproj
from CLI at the root directory of your STM32CubeMX_generated_project. It renames directories and removes unnecessary files.$ gen_stm32_cppproj
ps. If your
gen_stm32_cppproj
is not found, please check/home/<username>/.local/bin
is in environment variablePATH
-
Relauch vscode, open Command Pallette, choose
CMake: Select a kit
, then it should automatically configure. -
After flashing the code and restart your target, LED should blink after press user_button.
If you don't want to use this repository, here are some steps to use stm32-cmake directly
- Install required tools and VS Code extensions
- Configure and generated code from STM32CubeMX.
- Copy all files in
<STM32CubeMX_generated_project>/Src
to./src
exceptsystem_stm32f4xx.c
- Copy all header in
<STM32CubeMX_generated_project>/Inc
to./include
- Copy linker script to
./
. (XXX_FLASH.ld) - Finish the configuration below.
- Relauch vscode, open Command Pallette, choose
CMake: Select a kit
, then it should automatically configure. - After flashing the code and restart your target, LED should blink after press user_button.
- C/C++ intellisense (ms-vscode.cpptools) v0.30.0-insider Bug #5906 It will fail autocomplete, you can use disable extension autoupdate and use version 0.29.0. It might be bug from CMakeTool.
- Using STM32Programmer to flash