STM32 Development With Visual Code
Brief
This is a tutorial about how to setup Visual code On Windows, to make VS work with STM32 development. If you are using linux, the tutorial is not for you. If you already got st-link utils work, the tutorial is not for you either.
TL;DR;
Install *MSYS2
At first, we need MSYS2 to get a linux like environment. WSL doesn't work well.
Here the MSYS2 official site. We can download MSYS2 and install it.
After the installation, open a msys2 terminal and install necessary packages:
$pacman -S mingw-w64-x86_64-make
The default version of arm-none-eabi-gcc is 8.4.0, we will cover it up with newest one, which is 10.2.1
Install ARM Official Tool Chain
The official ARM gcc tool(10.2.1) can be found here.
Download the tool and install or extract it. The version of MSYS2 builtin tool chain is 8.4.0, which is much lower. To apply the newer tool chain, run command:
$cd your-arm-tool-chain-dir
$ls
arm-none-eabi bin lib share
$cp -r * /mingw64/
$arm-none-eabi-gcc --version
arm-none-eabi-gcc.exe (GNU Arm Embedded Toolchain 10-2020-q4-major) 10.2.1 20201103 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Setup STM32 Official Utilities
-
Install STM32CubeIDE The ST official IDE includes stlink gdb server and stm32 programmer, which are needed both. That's why we have to install STM32CubeIDE. Why shouldn't use the official IDE directly? Because we love Visual Code which is more beautiful. The download link. Download the Windows edition and install it. Find the directory where Cube IDE has been installed and open it to locate two plugins as necessary tools.
-
Find
STM32 gdb ServerThe gdb server is located at :
plugins\com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.win32_1.5.0.202011040924\toolsThe directory may look different on the version if the plugin has been updated.
-
Find
STM32 ProgrammerThe programmer can be located at
plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_1.5.0.202011040924\toolsThe directory may look different on the version if the plugin has been updated.
-
Setup
Environment VariablesThe environment variables will be used to configure tools.
- On the desktop, right click Windows icon.
- click System item.
- click Advanced System Setting.
- click Environment Variables... button.
- Under User variables for Your name list, click New button to add variables:
Variable Name Value (For example only) MSYS_HOME E:\msys64 STM32_GDB_PATH E:\STM\STM32CubeIDE_1.5.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.win32_1.5.0.202011040924\tools\bin\ST-LINK_gdbserver.exe STM32_PROG_PATH E:\STM\STM32CubeIDE_1.5.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_1.5.0.202011040924\tools\bin\ STM32_TOOL_CHAIN_PATH e:\Downloads\gcc-arm-none-eabi-10-2020-q4-major\win32\bin
Create Makefile Project With STM32CubeMX
For future projects, we should create it with STM32CubeMx, the official page is here. Download and install STM32CubeMX. Create a new MX project and generate code as Makefile project.
Open the project folder with Visual Studio Code. We will done all left steps in VS.
Setup mingw64 as VS default terminal
Open global settings.json, add the following lines:
"terminal.integrated.shell.windows": "${env:MSYS2_HOME}\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": [
"-lic",
"cd $OLDPWD; exec bash",
"--login",
"-i"
],
"terminal.integrated.env.windows": {
"CHECK_INVOKING": "1",
"MSYSTEM": "MINGW64",
"MSVSCODE": "1"
},
Install & Setup Cortex-Debug for VS
Search and install Cortex-Debug plugins.
In the Cortex-Debug setting panel, click gear icon to open settings.json, add or edit it and make it look like the following lines:
"cortex-debug.armToolchainPath": "${env:STM32_TOOL_CHAIN_DIR}",
"cortex-debug.stlinkPath": "${env:STM32_GDB_PATH}",
"cortex-debug.stm32cubeprogrammer": "${env:STM32_PROG_PATH}",
Setup Task.json for Project
Click Terminal menu of VS IDE, choose Run Build Task. VS will notify you that No build task to run found, Create build task. Click it to create task.json under project directory's .vscode directory. Edit the file to make it like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "process",
"command": "${env:MSYS2_HOME}\\usr\\bin\\bash.exe",
"linux": {},
"options": {
"cwd": "${workspaceFolder}",
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "new",
"showReuseMessage": true,
"clear": false
},
"args": [
"--login",
"-lic",
"-c",
"cd $OLDPWD && make -j${env:NUMBER_OF_PROCESSORS} all"
],
"isBackground": false,
"group":{
"kind": "build",
"isDefault": true
},
},
{
"label": "Clean",
"type": "process",
"command": "make",
"args": [
"clean",
],
}
]
}
Now we have Build and Clean tasks to make & clean.
Setup Launch.json for Project
Click Run button on VS sidebar, on the RUN dropdown list, choose Add Configuration to add new one.
Select type to ST-LINK.
Edit launch.json like this:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceRoot}",
"executable": "./build/my_awesome_board.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "stlink",
//--------------------------------------
// The name of task to run before debug
//--------------------------------------
"preLaunchTask": "Build",
"device": "STM32F103ZET6", // Which MCU you are using?
"interface": "swd",
"runToEntryPoint": "main",
"serverArgs": [
"-v",
// "--frequency 24000", 24000Khz if your ST-Link can run on it.
"-e", // Persistent
"-l 1", // log verbosity
"-s", // Verify flash
"-m 0", // AppID to debug
"-k", // Init under reset,
],
"showDevDebugOutput": true,
"gdbTarget": ":50000", // The port
}
]
}
Run & Debug
Now we can run and debug our stm32 project, if everything goes right....maybe.
Other
Install clang-format for better code formatting. A useful online site for clang format configuration is here.
The instruction to install clang-format(which is included in clang package) is:
$pacman -S mingw-w64-x86_64-clang
The add the following lines to global settings.json:
"C_Cpp.default.compilerPath": "/mingw64/bin/clang++",
"clang-format.executable": "/mingw64/bin/clang-format",
"clang-format.fallbackStyle": "Google", // My favorite one.
"C_Cpp.default.cppStandard": "gnu++17",
"C_Cpp.default.cStandard": "gnu17",