Contributed by sujingliang, organized by Ai-Thinker
[Ai-WB2 Review] Three Ways to Set Up the VS Code Environment
The official Ai-WB2 Eclipse environment setup tutorial: <https://blog.csdn.net/Boantong_/article/details/128480919>
Because disk space is limited, I didn't want to install Eclipse IDE for Embedded C/C++ Developers.
So I only downloaded Ai-Thinker-WB2.zip from the cloud drive of the SDK package.
To save space, after extracting Ai-Thinker-WB2.zip, I kept only MSYS under toolchain\riscv.
MSYS had been installed before, but git and make were not installed under it. This time I installed them:
pacman -S git pacman -S make
Run MSYS2 MSYS (the one with MSYS). Because in toolchain.mk under Ai-Thinker-WB2\make_scripts_riscv:
- CONFIG_TOOLPREFIX ?=
(shell uname |cut -d '_' -f1)/bin/riscv64-unknown-elf-
Here, $(shell uname |cut -d '_' -f1) needs to resolve to the MSYS in the MSYS2 MSYS tool name in order to access the cross-compilation toolchain under Ai-Thinker-WB2\toolchain\riscv\MSYS.
Of course, you can also comment out this line and enable the next one:
- CONFIG_TOOLPREFIX ?= riscv64-unknown-elf-
This way, the cross-compilation tools set in the system PATH are used, and the MSYS version no longer matters.
After entering MSYS2, run code to launch VS Code. 
Open the get-started\helloworld folder.
Then you can configure the build, download, and clean tasks.
1. Terminal -> Run Task Create tasks.json under .vscode with the following content:
Click to expand full code
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Build BL602",
"command": "c:\\msys64\\usr\\bin\\make.exe",
"args": [
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1",
"-j8",
"-C",
"${workspaceFolder}",
"all" // 明确指定构建目标
],
"options": {
"cwd": "${workspaceFolder}" // 修正为项目目录
},
"problemMatcher": [
"$gcc" // 保留用于编译错误匹配
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译 BL602 项目(8线程)"
},
{
"type": "shell",
"label": "Clean BL602",
"command": "c:\\msys64\\usr\\bin\\make.exe",
"args": [
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1",
"-C",
"${workspaceFolder}",
"clean"
],
"options": {
"cwd": "${workspaceFolder}" // 修正为项目目录
},
"problemMatcher": [], // clean 不需要错误匹配
"group": "build",
"detail": "清理 BL602 项目构建文件"
},
{
"type": "shell",
"label": "Flash BL602 (COM10)",
"command": "c:\\msys64\\usr\\bin\\make.exe",
"args": [
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1",
"-C",
"${workspaceFolder}",
"flash",
"p=COM10",
"b=921600"
],
"options": {
"cwd": "${workspaceFolder}" // 关键修正:工作目录设为项目根目录
},
"problemMatcher": [], // 烧录任务通常无需错误匹配
"group": {
"kind": "build",
"isDefault": true
},
"detail": "烧录 BL602 到 COM10,波特率 921600"
}
]
}Run terminal tasks: ... -> Terminal -> Run Task...

After running, a prompt appears: 
Select each of the three items to build, download, or clean.
2. Enabling Debugging (launch.json) Create launch.json under .vscode with the following content:
Click to expand full code
{
"configurations": [
{
"name": "make compile",
"type": "cppvsdbg",
"request": "launch",
//make文件的绝对地址,这样可以防止和别的环境冲突
"program": "c:\\msys64\\usr\\bin\\make.exe",
//原理就是通过参数-C进入指定的目录
// 烧录就很简单了只需要加上flash和p=端口号就可以了
"args": [
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1",
"-j8",
"-C",
"${workspaceFolder}"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
//使用vscode内部的终端输出,不用弹出来终端窗口
"console": "integratedTerminal"
},
{
"name": "make download",
"type": "cppvsdbg",
"request": "launch",
"program": "c:\\msys64\\usr\\bin\\make.exe",
"args": [
"CONFIG_CHIP_NAME=BL602",
"-C",
"${workspaceFolder}",
"flash",
//端口号需要改成自己电脑的端口
"p=COM10",
"b=921600"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal",
},
{
"name": "make clean",
"type": "cppvsdbg",
"request": "launch",
"program": "c:\\msys64\\usr\\bin\\make.exe",
"args": [
"CONFIG_CHIP_NAME=BL602",
"-C",
"${workspaceFolder}",
"clean"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal"
},
],
"version": "2.0.0"
}Press F5 to enable debugging. 
At this point, the VS Code status bar shows an extra item: 
Click it to select build, download, or clean. 
3. Using the Makefile Tools Extension
First install the Makefile Tools extension. 
Add the following to settings.json under .vscode:
Click to expand full code
{
"makefile.configureOnOpen": false,
"makefile.configureAfterCommand": false,
"makefile.parseBuildLog": false,
"makefile.buildBeforeLaunch": true,
"makefile.phonyTargets": ["all"],
"makefile.makePath": "C:\\msys64\\usr\\bin\\make.exe",
"makefile.configurations": [
{
"name": "BL602 Build",
"makeArgs": [
"-j${env:NUMBER_OF_PROCESSORS}",
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1"
],
"overrideMakefilePath": "${workspaceFolder}/Makefile",
"environmentVariables": {
"PATH": "C:\\msys64\\usr\\bin;${env:PATH}"
}
},
{
"name": "BL602 Flash",
"makeArgs": [
"CONFIG_CHIP_NAME=BL602",
"CONFIG_LINK_ROM=1",
"flash",
"SERIAL_PORT=COM10",
"SERIAL_BAUDRATE=921600",
],
"overrideMakefilePath": "${workspaceFolder}/Makefile",
"environmentVariables": {
"PATH": "C:\\msys64\\usr\\bin;${env:PATH}"
}
}
]
}
Click as shown in the figure to select build or download. 
Press this button to run the selection.
Result of running the build: 
Result of running the download: 
4. Going Back to the Native Makefile Create a Makefile under Ai-Thinker-WB2 with the following content:
Click to expand full code
# 默认编译所有子目录
#SUBDIRS := $(shell find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/Makefile \; -print)
SUBDIRS := applications/bluetooth/ble_slave
COMX :=COM10
BAUDRATE :=921600
DIRS ?= $(SUBDIRS) # 默认全部编译,但可通过 make DIRS="dir1 dir2" 指定
.PHONY: all clean flash $(DIRS)
all: $(DIRS)
$(DIRS):
$(MAKE) -C $@ -j8
clean:
$(MAKE) -C $(DIRS) clean
#$(foreach dir,$(DIRS),$(MAKE) -C $(dir) clean;)
flash:
$(MAKE) -C $(DIRS) flash p=$(COMX) b=$(BAUDRATE)
#$(foreach dir,$(DIRS),$(MAKE) -C $(dir) flash p=$(COMX) b=$(BAUDRATE);)In the VS Code terminal, type: make // compile make flash // download make clean // clean
Modify SUBDIRS to build different examples. I feel the native approach is easier to understand and simple to use.
Finally
- Although the methods differ, they all essentially find a way to issue the make command. Of course, if you're patient, you can also type and run it manually in MSYS2.
- Flashing via command line is actually a bit simpler than using a GUI tool.

