
As we all know, Keil is a paid software. Although it can be used with a workaround, many companies are still a bit hesitant, and many readers may have received ** letters.
Previously, when developing microcontroller projects, Keil was one of the best choices. However, since the emergence of VSCode, this situation has changed.
Because VSCode is free and user-friendly, it can install various plugins, and many engineers have gradually shifted from Keil to VSCode. Moreover, VSCode can achieve some functionalities that Keil cannot.
Next, I will share some common and useful features for setting up the STM32 development environment in VSCode.
1. Required Software and Tools
-
Download the latest version of VS Code: Install the plugins, which have good code completion and debugging capabilities.
VS Code download link:https://code.visualstudio.com/
-
Download LLVM: Used for code completion, it can actually be understood as Clang. Because the automatic completion feature of the “C/C++” plugin in VS Code is not very practical. Many library functions in STM32 cannot be completed. Remember to add the path to the environment variable after installation.
LLVM download link:http://releases.llvm.org/download.html
-
Download and install Git for Windows: Provides Git support and MINGW64 command terminal.
Git download link:https://gitforwindows.org/
-
Download and install arm-none-eabi-gcc: Choose <span>gcc-arm-none-eabi-5_4-2016q3-20160926-win32.exe</span>
(md5) This is a 2016 version, which I am currently using. It seems that there are other newer versions available for download, GNU Arm Embedded Toolchain. After normal installation, remember to set the bin folder of the software installation directory into the environment variable PATH.
arm-none-eabi-gcc download link:https://launchpad.net/gcc-arm-embedded/+download
-
Download OpenOCD for Windows: An open-source on-chip debugger (Open On-Chip Debugger). Compiling it yourself on Windows may have issues, so we choose a precompiled version. The downloaded files are not installation packages; place the program folder into your software installation directory and add the software’s bin folder path to the user environment variable PATH.
OpenOCD download link:http://gnutoolchains.com/arm-eabi/openocd/
-
STM32CubeMX: Used to generate projects with makefile. This way, we don’t need to write MakeFile ourselves. Just use its template and modify it to our directory.
STM32CubeMX link:http://www.stm32cube.com/
2. Setting Up the Compilation Environment
1. Create a project using STM32Cube.
<span>help -> updater settings ->Connection Parameters</span>
Set the network properly before downloading the firmware package.2. Configure VS Code
<span>.ioc</span>
file is the project file of STM32Cube, Inc and Src are the user-modifiable source codes, and Driver contains the STM32 and ARM CMSIS libraries, which should not be modified.(2) Install VS Code plugins, you need these:
(3) Configure the built-in terminal of VS Code
"terminal.integrated.shell.windows": "D:\Program Files\Git\bin\bash.exe",
"terminal.external.windowsExec": "D:\Program Files\Git\bin\bash.exe"
(4) Configure smart completion and smart sensing plugins.
<span>c_cpp_properties.json</span>
configuration file in the .vscode folder of the current directory to tell VS Code about the macros we defined and the paths of the files.{
"configurations": [
{
"name": "Win32",
"browse": {
"path": [
"${workspaceFolder}/",
"${workspaceFolder}/Drivers/CMSIS",
"${workspaceFolder}/Drivers/FWlib/inc",
"${workspaceFolder}/Drivers/CMSIS/startup",
"${workspaceFolder}/User/inc",
"${workspaceFolder}/User",
"${workspaceFolder}/ThirdParty/crclib/include"
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"${workspaceFolder}/",
"${workspaceFolder}/",
"${workspaceFolder}/Drivers/CMSIS",
"${workspaceFolder}/Drivers/FWlib/inc",
"${workspaceFolder}/Drivers/CMSIS/startup",
"${workspaceFolder}/User/inc",
"${workspaceFolder}/User",
"${workspaceFolder}/ThirdParty/crclib/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"__CC_ARM",
"USE_STDPERIPH_DRIVER",
"STM32F10X_HD"
],
"compilerPath": "C:\Program Files\LLVM\bin\clang-format.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
<span>variable “uint32_t” is not a type name</span>
is not a type.<span>__CC_ARM</span>
. If this macro definition is missing, the<span>uint32_t</span>
type will throw an error. Additionally, if a structure uses members defined with<span>uint32_t</span>
, it will also not complete.(5) Compile the program
The compilation process under gcc is shown in the figure below:
-
<span>.c</span>
file is compiled into<span>.o</span>
file using<span>arm-none-eabi-gcc</span>
-
<span>.s</span>
file is compiled into<span>.o</span>
file using<span>arm-none-eabi-as</span>
-
<span>.o</span>
file and<span>.a</span>
file are linked into<span>.elf</span>
file using<span>arm-none-eabi-ld</span>
-
<span>.elf</span>
file is converted into hex file<span>/dis</span>
file using<span>arm-none-eabi-objcopy</span>
and<span>arm-none-eabi-objdump</span>
-
<span>arm-none-eabi-gdb</span>
is used for debugging with<span>.elf</span>
file

In the Terminal, enter the make command to start building the program

After compilation, all generated files will be placed in the Build folder.
3. Setting Up the Debugging Environment
1. Configure openocd.
-
What emulator to use? (J-link, ST-link…) -
What interface to use? (JTAG, SWD…) -
What is the target chip? (STM32F4x, tm4c123g…)
openocd –f interface/stlink.cfg –f target/stm32f4.cfg


2. Connect gdb to openocd
target remote localhost:3333
3. Configure openocd tasks and build tasks
(1) Create a build.py file in the current directory.

(2) In the VSCode interface, click on “Tasks”, select “Configure Task”
4. Configure the debugging function of VS Code
{
"version": "0.2.0",
"configurations": [
{
"name": "ARM Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Build/STM32F103RC_Template.elf",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\Program Files (x86)\GNU Tools ARM Embedded\5.4 2016q3\bin\arm-none-eabi-gdb.exe",
"targetArchitecture": "arm",
"setupCommands": [
{
"description": "Select debug file (.elf) to gdb",
"text": "file E:/VScode/STM32_VSCode/stm32f103_temp/Build/STM32F103RC_Template.elf",
"ignoreFailures": false
},
{
"description": "Connect to GDB Server",
"text": "target remote localhost:3333",
"ignoreFailures": false
},
{
"description": "Reset MCU",
"text": "monitor reset",
"ignoreFailures": false
},
{
"description": "Halt",
"text": "monitor halt",
"ignoreFailures": false
},
{
"description":"Download code to MCU",
"text": "load" ,
"ignoreFailures": false
}
],
"preLaunchTask": "build",
}
]
}
Source: https://blog.csdn.net/qq_33559992/article/details/97548915
———— END ————
● Column “Embedded Tools“
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials in Embedded Column
Follow the public account reply “Join Group” to join the technical exchange group as per the rules, reply “1024” to see more content.

