VsCode中搭建 Rt-Thread 开发环境(编译,调试,下载)
文章目录
一 . 下载rt-thread源码
git clone https://github.com/RT-Thread/rt-thread.git
二. 安装env脚本环境
在Powershell执行以下命令
wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_windows.ps1 -O install_windows.ps1
set-executionpolicy remotesigned
.\install_windows.ps1 --gitee
- Powershell要以管理员身份运行。
- 将其设置为 remotesigned 后,您可以作为普通用户运行 PowerShell。( After setting it to remotesigned, you can run PowerShell as a normal user.)
- 一定要关闭杀毒软件,否则安装过程可能会被杀毒软件强退
三 . 配置工具链
3.1 使用 SDK方式下载工具链arm-none-eabi-gcc
使用 ~/.env/env.psl 激活env环境,第一次需要加载.
3.1.1 下载工具链
输入 命令 sdk,会弹出如下界面,使用回车选中我们下载的工具链:arm-none-eabi-gcc toolchain
保存后退出,开始自动下载工具链。
如下:下载成功
Using default symbol values (no '.config')
Configuration saved to '.config'
[Use Gitee server - auto decision based on timezone]
Package was NOT found on mirror server. Using a non-mirrored address to download.
downloading arm-gnu-toolchain-13.2.rel1-mingw-w64-i686-arm-none-eabi.zip ...
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 364M/364M [29:48<00:00, 204kB/s]
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8563/8563 [01:21<00:00, 104.94it/s]
==============================> ARM_NONE_EABI_GCC v13.2.rel1 is downloaded successfully.
Operation completed successfully.
(.venv) PS C:\Users\RTT\Desktop\rt\rt-thread-master>
(.venv) PS C:\Users\RTT\Desktop\rt\rt-thread-master>
3.1.2 测试工具链
原理:scons在sdk中自动探测工具链并进行使用下载的工具链
进入rt-thread-master\bsp\stm32\stm32f407-rt-spark 目录 使用 scons 进行编译
(.venv) PS C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\stm32f407-rt-spark> scons
scons: Reading SConscript files ...
C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\stm32f407-rt-spark\rtconfig.py:80: SyntaxWarning: invalid escape sequence '\l'
LFLAGS = DEVICE + ' --scatter "board\linker_scripts\link.sct" --info sizes --info totals --info unused --info veneers --list rt-thread.map --strict'
set CC to C:\Users\RTT\.env\tools\scripts\packages\arm-none-eabi-gcc-v13.2.rel1\bin
Newlib version: 4.3.0
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build
CC build\applications\main.o
CC build\board\board.o
CC build\board\CubeMX_Config\Src\stm32f4xx_hal_msp.o
CC build\kernel\components\drivers\core\device.o
CC build\kernel\components\drivers\i2c\dev_i2c_bit_ops.o
CC build\kernel\components\drivers\i2c\dev_i2c_core.o
CC build\kernel\components\drivers\i2c\dev_i2c_dev.o
CC build\kernel\components\drivers\ipc\completion_comm.o
CC build\kernel\components\drivers\ipc\completion_up.o
CC build\kernel\components\drivers\ipc\condvar.o
CC build\kernel\components\drivers\ipc\dataqueue.o
CC build\kernel\components\drivers\ipc\pipe.o
CC build\kernel\components\drivers\ipc\ringblk_buf.o
CC build\kernel\components\drivers\ipc\ringbuffer.o
CC build\kernel\components\drivers\ipc\waitqueue.o
CC build\kernel\components\drivers\ipc\workqueue.o
CC build\kernel\components\drivers\misc\rt_drv_pwm.o
CC build\kernel\components\drivers\pin\dev_pin.o
CC C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\libraries\HAL_Drivers\drivers\drv_gpio.o
CC C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\libraries\HAL_Drivers\drivers\drv_usart.o
AS C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\libraries\STM32F4xx_HAL\CMSIS\Device\ST\STM32F4xx\Source\Templates\gcc\startup_stm32f407xx.o
LINK rt-thread.elf
Memory region Used Size Region Size %age Used
CODE: 69876 B 1 MB 6.66%
RAM1: 5372 B 128 KB 4.10%
RAM2: 0 GB 64 KB 0.00%
MCUlcdgrambysram: 0 GB 1 MB 0.00%
arm-none-eabi-objcopy -O ihex rt-thread.elf rtthread.hex
arm-none-eabi-size rt-thread.elf
text data bss dec hex filename
69876 1196 4172 75244 125ec rt-thread.elf
arm-none-eabi-objcopy -O binary rt-thread.elf rtthread.bin
arm-none-eabi-size rt-thread.elf
text data bss dec hex filename
69876 1196 4172 75244 125ec rt-thread.elf
scons: done building targets.
3.2 使用Keil MDK的编译器ARMCLANG
3.2.1 修改rtconfig.py
修改rt-thread/bsp/stm32/stm32f407-rt-spark/rtconfig.py
1.修改为CROSS_TOOL = ‘keil’ 如图所示
2.修改PLATFORM = ‘armclang’ ,修改 EXEC_PATH 为你keil安装的目录
3.2.2 修改building.py
修改rt-thread/tools/building.py
将 env[‘ASCOM’]= env[‘ASPPCOM’] 注释掉
四 . 测试环境
每次打开终端需要执行 ~/.env/env.ps1 以激活环境变量。
4.1 Powershell 中测试环境
4.1.1 在Powershell中编译工程
使用 scons 命令进行编译
4.1.2 在Powershell中生成Keil MDK的工程
使用 scons --target=mdk5 生成Keil MDK 工程
4.2 在vscode中测试环境
4.2.1 使用gcc-arm-none-eabi在vscode中编译工程
如何配置使用gcc-arm-none-eabi工具链在上一章已经介绍了,不懂的话可以看上一章。
使用 scons 命令进行编译
输出部分结果如下:
PS C:\Users\RTT\Desktop\rt\rt-thread-master\bsp\stm32\stm32f407-rt-spark> scons
scons: Reading SConscript files ...
set CC to C:\Users\RTT\.env\tools\scripts\packages\arm-none-eabi-gcc-v13.2.rel1\bin
Newlib version: 4.3.0
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build
CC build\applications\main.o
CC build\board\board.o
CC build\board\CubeMX_Config\Src\stm32f4xx_hal_msp.o
CC build\kernel\components\drivers\core\device.o
CC build\kernel\components\drivers\i2c\dev_i2c_bit_ops.o
CC build\kernel\components\drivers\i2c\dev_i2c_core.o
CC build\kernel\components\drivers\i2c\dev_i2c_dev.o
CC build\kernel\components\drivers\ipc\completion_comm.o
CC build\kernel\components\drivers\ipc\completion_up.o
CC build\kernel\components\drivers\ipc\condvar.o
CC build\kernel\components\drivers\ipc\dataqueue.o
CC build\kernel\components\drivers\ipc\pipe.o
CC build\kernel\components\drivers\ipc\ringblk_buf.o
CC build\kernel\components\drivers\ipc\ringbuffer.o
CC build\kernel\components\drivers\ipc\waitqueue.o
CC build\kernel\components\drivers\ipc\workqueue.o
CC build\applications\main.o
LINK rt-thread.elf
Memory region Used Size Region Size %age Used
CODE: 81524 B 1 MB 7.77%
RAM1: 5444 B 128 KB 4.15%
RAM2: 0 GB 64 KB 0.00%
MCUlcdgrambysram: 0 GB 1 MB 0.00%
arm-none-eabi-objcopy -O ihex rt-thread.elf rtthread.hex
arm-none-eabi-size rt-thread.elf
text data bss dec hex filename
81524 1972 3468 86964 153b4 rt-thread.elf
arm-none-eabi-objcopy -O binary rt-thread.elf rtthread.bin
arm-none-eabi-size rt-thread.elf
text data bss dec hex filename
81524 1972 3468 86964 153b4 rt-thread.elf
scons: done building targets.
4.2.2 使用ARMCLANG在vscode中编译工程
如何配置使用 ARMCLANG 在上一章已经介绍了,不懂的话可以看上一章。
使用 scons 进行编译 部分输出信息如下:
(.venv) PS C:\Users\RTT\Desktop\1\rt-thread\bsp\stm32\stm32f407-rt-spark> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build
CC build\applications\main.o
CC build\board\board.o
CC build\board\CubeMX_Config\Src\stm32f4xx_hal_msp.o
CC build\kernel\components\drivers\core\device.o
CC build\kernel\components\drivers\i2c\dev_i2c_bit_ops.o
CC build\kernel\components\drivers\i2c\dev_i2c_core.o
CC build\kernel\components\drivers\i2c\dev_i2c_dev.o
AS C:\Users\RTT\Desktop\1\rt-thread\bsp\stm32\libraries\STM32F4xx_HAL\CMSIS\Device\ST\STM32F4xx\Source\Templates\arm\startup_stm32f407xx.o
Warning: A1950W: The legacy armasm assembler is deprecated. Consider using the armclang integrated assembler instead.
0 Errors, 1 Warning
LINK rt-thread.axf
fromelf --bin rt-thread.axf --output rtthread.bin
fromelf -z rt-thread.axf
========================================================================
** Object/Image Component Sizes
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
50150 5652 9386 776 3536 416033 rt-thread.axf (uncompressed)
50150 5652 9386 264 3536 416033 rt-thread.axf (compressed)
0 0 4 0 0 0 (incl. padding)
50150 5652 9386 264 0 0 ROM Totals for rt-thread.axf
scons: done building targets.
4.2.3 在vscode中生成 Keil MDK 工程
使用 scons --target=mdk5 生成Keil MDK 工程
输出信息如下:
(.venv) PS C:\Users\RTT\Desktop\1\rt-thread\bsp\stm32\stm32f407-rt-spark> scons --target=mdk5
scons: Reading SConscript files ...
Keil5 project is generating...
Keil Version: MDK Plus 5.39/Arm Compiler for Embedded 6.21/armlink [5ec1fa00]
Keil-MDK project has generated successfully!
五. vscode中调试程序
5.1 安装Cortex-Debug
5.2 配置launch.json文件
{
// 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": "${workspaceFolder}",
"executable": "./rt-thread.elf", //调试文件
"request": "launch",
"type": "cortex-debug", //调试方式
"runToEntryPoint": "Reset_Handler",
"servertype": "jlink", //类型
"serverpath": "D:/jlink/JLink_V798e/JLinkGDBServerCL.exe", //jink 路径
"device": "STM32F407ZG", //设备类型
"gdbPath": "D:/gcc-arm-none/10 2021.10/bin/arm-none-eabi-gdb.exe", //gdb 路径
"armToolchainPath": "D:/gcc-arm-none/10 2021.10/bin" //工具链路径
}
]
}
5.3 调试开始
使用F5快捷键即可启动调试
六. vscode中下载程序
6.1 添加jlink到环境变量
将Jlink安装路径添加进环境变量(使vscode中能找到jlink路径)
6.2 安装pylink库
python调用jlink时需要用到
使用命令 : pip install pylink-square 安装pylink库
6.3 增加download_flash.py
在目录下新建download_flash.py文件,该文件是下载程序的脚本
download_flash.py脚本内容如下:
import os
import pylink
def download_program():
link = pylink.JLink()
try:
#连接JLlink设备
link.open()
#选择芯片和下载方式
link.set_tif(pylink.enums.JLinkInterfaces.SWD)
link.connect('STM32F407ZG')
#擦除程序
#link.erase()
#下载并烧入程序
link.flash_file("./rtthread.hex",0)
finally:
#关闭与JLink的连接
link.close()
download_program()
6.4 下载程序
执行指令下载程序
python download_flash.py
结果如下图: