项目场景:
在使用 VS code 给 stm32g431 下载程序时出现的报错。
问题描述
原因分析:
打开 launch.json 文件。
```c
{
"version": "0.2.0",
"configurations": [
{
"name": "ARM Debug",
// "showDevDebugOutput": "raw",
"executable": "./build/${workspaceRootFolderName}.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"configFiles": [
"D:/app/xpack-openocd/xpack-openocd-0.12.0-1/openocd/scripts/interface/stlink.cfg",
"D:/app/xpack-openocd/xpack-openocd-0.12.0-1/openocd/scripts/target/stm32g4x.cfg"
],
"armToolchainPath": "D:/app/gcc-arm-none-eabi/gcc-arm-none-eabi-10.3-2021.10/bin",
"svdFile": "STM32G431.svd",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build",
}
]
}
```
找到 executable file 的配置,发现编译生成的 ELF 文件路径为工作区根目录。
假设你的工作区根目录的名称是 "test",那么 ELF 文件路径就会是 ./build/test.elf。
```c
"executable": "./build/${workspaceRootFolderName}.elf"
```
这里 launch.json 文件的配置是没有问题的。
在准备开始调试时我复制了一份程序文件副本,改名为 test,结果无法下载程序。
单独编译后打开程序文件中的 build 文件夹,发现其中的 ELF 文件依然是以 currentSensor 命名的,于是找到问题所在。
这里的 currentSensor 是原程序文件名。
解决方案:
打开 Makefile 文件,检查 TARGET 是否与工作区根目录名称一致。
```c
######################################
# target
######################################
TARGET = currentSensor
```
将这里的 TARGET = currentSensor 改成 TARGET = test 便可以成功编译下载程序了。
打开程序文件中的 build 文件夹查看编译生成的 ELF 文件。