当前位置:网站首页>OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
2022-07-28 17:40:00 【标biao】
首先,关于OpenOCD的入门介绍,以及如何调试,看我这篇文章:
嵌入式IDE原理 OpenOCD介绍 以及stlink如何连接stm32板子_标biao的博客-CSDN博客
由于OpenOCD一旦连接上,会自动进入3种端口监听模式(其中就包括了gdbserver端口,其实OpenOCD本质上是一个gdbserver),所以进行调试(选择gdb端口即可,上面那个文章有讲),还是比较容易的。但是本文要讲的是如何直接下载程序,而不是调试。
已有的不错参考博客如下(这两博客讲得差不多):
Windows上使用 OpenOCD 给 STM32 下载程序_MCUlover666的技术博客_51CTO博客
【STM32开发环境】Linux下开发stm32(二) | 使用openocd下载程序_Mculover666的博客-CSDN博客_openocd
其中有两种把程序下载到板子上的方法:
(win10,stm32f103ZET6单片机,st-link v2硬件调试器,OpenOCD版本 OpenOCD-20211118-0.11.0)
1. 使用端口连接模式
先连接OpenOCD到硬件调试器,打开powershell命令行窗口
命令行:openocd -f <接口配置文件> -f <目标芯片配置文件>
例如我的stm32f103zet6板子:openocd -f scripts/interface/stlink.cfg -f scripts/target/stm32f1x.cfg

所以可以有三种端口连接协议方式:tcl 、 telnet 、gdb(调试功能使用,我这篇博客就是用的这个 嵌入式IDE原理 OpenOCD介绍 以及stlink如何连接stm32板子_标biao的博客-CSDN博客)。
假如选择 telnet端口连接,然后再打开另一个powershell命令行窗口,
输入命令行:telnet localhost 4444连接到openocd
- 输入命令:halt,目标芯片挂起,相当于关机
- 输入命令:flash write_image erase <要下载的文件>,将文件下载到目标芯片flash
- 输入命令:reset,目标芯片复位

同理,tcl端口连接也是类似的。
但是下载程序都感觉太麻烦,能不能不连接端口,一行命令行,直接下载完成呢?可以的,如下
2. 使用直接下载方式
一行命令行:
openocd -f scripts/interface/stlink.cfg -f scripts/target/stm32f1x.cfg -c init -c "reset halt; wait_halt; flash write_image erase build/项目15.bin 0x08000000" -c reset -c shutdown
解释:
- openocd -f scripts/interface/stlink.cfg -f scripts/target/stm32f1x.cfg 用于连接OpenOCD
- -c表示要执行的选项,-c init 用于初始化
- -c "reset halt; wait_halt; flash write_image erase build/项目15.bin 0x08000000" 表示执行命令,其中 reset halt; wait_hal 先停止单片机 (分号不能少),这句非常重要
- -c reset -c shutdown,表示复位单片机,且退出OpenOCD
注意:
reset halt; wait_hal ,这句必不可少,否则第一次下载能成功,但是第二次下载就会报错:timeout waiting for algorithm, a target reset is recommended,比如下面的博客就有这样的问题:
Windows上使用 OpenOCD 给 STM32 下载程序_MCUlover666的技术博客_51CTO博客 STM32开发环境】Linux下开发stm32(二) | 使用openocd下载程序_Mculover666的博客-CSDN博客_openocd
Thread: [OpenOCD-devel] J-TAG flash programming issues STM32F7 | OpenOCD - Open On-Chip Debugger
我猜测原因:
- 第一是我的OpenOCD版本跟他们不一样,我这个是最新版。
- 第二,因为连接上了OpenOCD,这个的gdbserver没有正常退出啥的
- 第三,直接写的命令 -c halt 没有生效,必须按照我那种写法。
网上也有人是这样写的命令行:(我就是从这儿得到的启发,大家也可以自己启发一下)
openocd -f interface/ftdi/jtagkey.cfg -f myboard_EK390.cfg -c "init; targets; reset halt; wait_halt; poll; flash write_image erase unlock Debug-EK390-0/FirmwareEK390_caffe.elf; flash erase_check 0; reset run; shutdown"
OpenOCD参考手册:
- 使用openocd的一些技巧:OpenOCD使用指南-oceanhehy-ChinaUnix博客
使用jtag时,最好使的cpu复位后不执行任何指令直接halt,相关指令为:reset halt,另外还有类似指令reset run、reset init,这些指令在openocd手册中都有说明。
Openocd的配置文件为tcl脚本,所以使用proc可以定义一个类似函数的接口。通过使用proc,可以省去不少麻烦。
在MinGW编译出的openocd可以在windows的cmd中直接运行,可是如果jtag接口没有和板子接口,会出现程序死掉的情况,而且退不掉。此时重新在板子带电情况下拔插一下openjtag,然后重新打开新的cmd运行openocd即可。不过,此时必须更改telnet_port,因为,刚才死掉的openocd占用着原先的端口。 - 官方文档 OpenOCD用户指南_子曰小玖的博客-CSDN博客_openocd
边栏推荐
- 亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发
- [notes] Apocalypse: list of practical experience and reflection of product managers
- SaltStack进阶
- sudo rosdep init 出现 ERROR: cannot download default
- [深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
- SaltStack之数据系统
- 测试开发备忘
- NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
- 使用百度EasyDL实现明厨亮灶厨师帽识别
- An intern's journey to cnosdb
猜你喜欢

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters

WPF implements MessageBox message prompt box with mask

Application of TSDB in civil aircraft industry

Application of time series database in cigarette factory

WPF 实现带蒙版的 MessageBox 消息提示框

Transformer for anomaly detection - instra "painting transformer for anomaly detection"

Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

When CNN meets transformer cmt:revolutionary neural networks meet vision transformers

R language text mining and natural language processing tutorial
随机推荐
Application of time series database in Hydropower Station
Application of TSDB in civil aircraft industry
测试开发备忘
How to use Qianqian listening sound effect plug-in (fierce Classic)
Rust Getting Started Guide (crite Management)
Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23
这种动态规划你见过吗——状态机动态规划之股票问题(下)
App自动化测试是怎么实现H5测试的
The mystery of ID number
Sudo rosdep init error: cannot download default
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
C语言循环语句强化练习题
ES6 conversion of new data type set and arr set map
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
调用整数或字符数组函数里的参数应该传入啥
SQL audit tool self introduction owls
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
Doxygen文档生成工具
App自动化测试是怎么实现H5测试的
Test Development Notes