在ubuntu下用makefile或者cmake编译好stm32程序后,需要烧录到stm32板子上,不管只直接用jlink的驱动程序烧录还是用openocd烧录,命令都比较多,怎么方便烧录呢?
用集成开发环境VSCode配置好脚本可以实现一键调试和烧录。
1.ubuntu下VSCode + CMake 编译调试
使用命令行cmake编译就不说了,VSCode集成开发环境下如何用cmake编译
下面这个博客写的很清晰,强烈推荐
https://blog.csdn.net/huangyifei_1111/article/details/108850986
2.ubuntu 使用命令行一键烧录程序
make编译好后生成可执行程序后记下可执行程序的名字
先切换到jlink安装目录下,我的安装在/opt/SEGGER/JLink,然后使用下面命令一键烧录
cd /opt/SEGGER/JLink
JLinkExe -device STM32F103RC -if SWD -speed 4000 -CommanderScript flash.jlink
或者
JLinkExe -device STM32F103RC -si SWD -speed 4000 -CommanderScript flash.jlink
其中指定-si 或者 -if 都是指定烧录方式,使用SWD
发生命令的最后需要一个flash.jlink脚本
在/opt/SEGGER/JLink目录下新建flash.jlink脚本内容如下
r
loadfile /home/qian/LED.hex
r
exit
注意,脚本中loadfile /home/qian/LED.hex是指定需要烧录的程序的绝对路径,脚本中的r代表reset。
如果想在其他地方用脚本一键烧录,就将JLinkExe设为全局变量,在、
.bashrc中加入
export PATH=$PATH:/opt/SEGGER/JLink/
flash.jlink放到其他地方执行都ok
3.ubuntu vscode 一键编译+烧录
首先编译就不说了,如果是makefile工程直接make一下就编译好了。一键编译加烧录就需要修改vscode的配置了。
分为三步:
(1)在.vscode 目录下新建一个tasks.json文件并且设置tasks.json配置文件
(2)在make文件夹目录下新建一个flask.jlink文件
(3)鼠标选择工具栏的Terminal->run task 选择下拉框中选择build
3.1 新建tasks.json
其中tasks.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": "2.20.0",
"tasks": [
{
"label": "make",
"type": "shell",
"command": "make -j4",
"presentation": {
"showReuseMessage": false
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build",
"type": "shell",
"command": "JLinkExe -device STM32F103RC -if SWD -speed 4000 -CommanderScript flash.jlink",
"presentation": {
"showReuseMessage":false
},
"dependsOn": "make",//执行jlink之前先执行make任务
"problemMatcher":[]
}
]
}
以上JLinkExe已经设置为全局路径了。
3.2 新建一个flask.jlink
内容如下
r
loadfile ./build/ledmakefiletest.hex
r
exit
注意这里使用的是相对路径
3.3 鼠标选择工具栏的Terminal->run task 选择下拉框中选择build
如图
4.vscode一键调试
2.1在线调试
参考文献Ubuntu下使用VSCode编译调试Betaflight飞控
中有很详细的说明
1.使用OpenOCD实现烧录调试一体总结起来有如下步骤:
2.修改vscode的Debug配置文件
3.修改openocd的jlink配置
4.修改芯片配置文件
特别需要注意的是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": "${workspaceRoot}",
"executable": "./build/ledmakefiletest.elf", //固件路径,需要根据实际情况修改
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"configFiles": [
"interface/jlink.cfg", //Jlink配置文件
"target/stm32f1x.cfg" //目标芯片配置文件,需要根据自己飞控的实际芯片修改,可以到/usr/share/openocd/scripts/target目录下查看存在的配置文件
]
}
]
}
配置好后按F5就进入调试了。
完整的工程链接下载
https://download.csdn.net/download/ktigerhero3/16663286