开发环境配置指南

vscode + scons + arm-none-eabi-gcc + cortex-debug + jlink

1. 准备

1.1 安装 vscode 软件

https://code.visualstudio.com/download

install vscode extentions: C/C++ & C/C++ Extension Pack
install vscode extentions: Cortex-Debug
install vscode extentions: RTOS-Views, Memory-View, Peripheral-Viewer

1.2 安装 python 环境

https://www.python.org/downloads/

install python scripts: cmd > pip install scons==3.1.2
install python scripts: cmd > pip install pywin32
使用文本编辑器打开 scons 文件(该文件在你 python 安装路径下的 Scripts 目录中)
import os
import sys
sys.dont_write_bytecode = True
# 在 `import sys` 的下一行添加 `sys.dont_write_bytecode = True` 语句(避免生成 __pycache__ 目录)

2. 编译

2.1 安装 arm-none-eabi 交叉编译工具链

gcc-arm-none-eabi-10.3-2021.10-win32.zip

将压缩文件解压至任意位置

2.2 新建 build 任务

若任务存在则该步骤可跳过
task.jsonlink
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "scons -j16",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "scons -c",
},
],
}

2.3 测试 build 任务

使用 vscode 打开测试工程
点击 Terminal->RunTask->build(首次执行时会报错并自动生成 rtconfig.ini 文件)
打开 rtconfig.ini 填入 arm-none-eabi 安装路径(i.e. d:\arm-none-eabi-10.3-2021.10\bin)
点击 Terminal->RunTask->clean
点击 Terminal->RunTask->build

3. 烧录

JLink 7.22b

安装 JLink 后要记得修改 JLinkDevices.xml 并拷贝相应的 device 文件

3.2 编写 write 脚本

若脚本存在则该步骤可跳过
download.bat (build/gcc/bin/download.bat)
@echo off

set config=download.ini

if not exist %config% (
echo error: %config% does not exist. but it should have been generated automatically, you need to fill in some informations. ^(greetings from liujitong^)
echo replace this line with your jlink.exe path. ^(i.e. D:\JLink\JLink.exe^) ^(do not commit this file to svn^) > %config%
goto end
)

for /f "delims=" %%i in (%config%) do (
set path="%%i"
)

%path% -Device %1 -If SWD -Speed 12000 -AutoConnect 1 -NoGui 1 -CommandFile %~dp0\download.txt

:end
download.txt (build/gcc/bin/download.txt)
r
h
erase 0x00000000 0x00080000
loadfile .\rtthread.bin 0x00000000
r
g
q

3.3 新建 write 任务

若任务存在则该步骤可跳过
task.jsonlink
{
"version": "2.0.0",
"tasks": [
{
"label": "write",
"type": "shell",
"command": "cd ./build/gcc/bin/; ./download.bat STM32F103",
},
{
"label": "batch",
"type": "shell",
"command": "scons -j16; cd ./build/gcc/bin/; ./download.bat STM32F103",
},
],
}

3.4 测试 write 任务

使用 vscode 打开测试工程
点击 Terminal->RunTask->build
点击 Terminal->RunTask->write(首次执行时会报错并自动生成 build/gcc/bin/download.ini 文件)
打开 download.ini 填入 JLink.exe 所在路径(i.e. d:\JLink_V722b\JLink.exe)(注意 JLink.exe 不能省略)
点击 Terminal->RunTask->write
点击 Terminal->RunTask->batch (batch == build + write)

4. 调试

4.1 安装 vscode 插件

Cortex-Debug
RTOS-Views
Memory-View
Peripheral-Viewer

4.2 配置 vscode 插件

Cortex-Debug
"cortex-debug.ArmToolchainPath"  : "i.e. d:\\arm-none-eabi-10.3-2021.10\\bin",
"cortex-debug.JLinkGDBServerPath": "i.e. d:\\JLink_V722b\\JLinkGDBServerCL.exe",

4.3 新建 launch 配置

若配置存在则该步骤可跳过
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug@JLinkGDBServerCL.exe/stm32f103",
"type": "cortex-debug",
"servertype": "jlink",
"serverArgs": [],
"interface": "swd",
"request": "attach",
"cwd": "${workspaceRoot}",
"executable": "./build/gcc/bin/rtthread.elf",

"device": "STM32F103",
"svdFile": "./underlayer/cpu/st/arm/stm32f1xx_libraries/device/stm32f103.svd",
"liveWatch": {
"enabled": true,
"samplesPerSecond": 4,
},
},
],
}

4.3 测试 launch 配置

使用 vscode 打开测试工程
点击 RunAndDebug 图标(左侧边栏)
选择 launch 配置
点击 StartDebugging 按钮启动调试
左侧 VARIABLES 窗口查看本地变量数据
左侧 WATCH 窗口查看变量数据
左侧 XPERIPHERALS 窗口查看寄存器数据
打开 MEMORY 窗口查看内存数据
打开 DEBUG CONSOLE 窗口键入 x/nfu addr 查看内存数据
https://sourceware.org/gdb/current/onlinedocs/gdb.html/Memory.html

5. 跳转

5.1 安装 mingw 环境

MingW-W64: x86_64-x.x.x-release-win32-seh-ucrt-xxx.7z

将压缩文件解压至任意位置

5.2 配置 C/C++ 插件

C/C++
"C_Cpp.default.compilerPath": "i.e. d:\\mingw-w64-builds\\x86_64-win32-seh-ucrt\\mingw64\\bin\\gcc.exe",

5.3 配置 c_cpp_properties.json 文件

c_cpp_properties.json
{
"configurations": [
{
"name": "main",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/application",
"${workspaceFolder}/board",
"${workspaceFolder}/packages",
"${workspaceFolder}/rt-thread",
"${workspaceFolder}/underlayer",
"${workspaceFolder}"
],
"defines": [
"_DEBUG",
"__CC_ARM"
],
"cStandard": "c99",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

5.4 测试 c_cpp_properties.json 文件

重启 vscode 打开测试工程后不弹出警告即可

6. 交互(shell)

6.1 安装 shell 工具

6.2 新建 shell 会话

serial
- 会话
- 串口:COM3
- 数据:text
- 串口
- 协议
- 波特率:115200
- 数据位:8
- 校验位:N
- 停止位:1
- 流控制:N

6.3 测试 shell 交互

serial COM3
msh >
msh >
msh >