当前位置:网站首页>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
边栏推荐
- 用LEX(FLEX)生成PL语言的词法分析器
- Time waits for no man. The rise of TSDB is at the right time
- Application of time series database in monitoring operation and maintenance platform
- C string to short[] method
- ES6 new - arrow function
- 业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)
- navicate修改数据库名的方式
- R language text mining and natural language processing tutorial
- Sword finger offer II 109. unlock the password lock
- Pandownload revival tutorial
猜你喜欢

ES6's new data container map

力扣 1331. 数组序号转换

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

JS modify table font and table border style

使用SaltStack自动化部署Zabbix

Image processing web application development tutorial

Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network

Method of win7 system anti ARP attack

Nips18(AD) - 利用几何增广的无监督异常检测《Deep Anomaly Detection Using Geometric Transformations》

Asp net MVC web development tutorial
随机推荐
Pandownload revival tutorial
App自动化测试是怎么实现H5测试的
Scrapy Spider源码分析
Web 3.0 development learning path
Smart contract security - overflow vulnerability
【笔记】《结网:互联网产品经理改变世界》
Learn from Li Mu in depth -softmax return
Get to know nodejs for the first time (with cases)
一家芯片公司倒在了B轮
业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)
Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
SaltStack常用的模块
Rust Getting Started Guide (crite Management)
用LEX(FLEX)生成PL语言的词法分析器
Gmoea code operation 2 -- establishment and operation of operation environment
New this prototype precompiled exercise
文章翻译软件-批量免费翻译软件支持各大翻译接口
Sword finger offer II 109. unlock the password lock
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!