硬件环境

Linux 主机 + JLink v8 + 树莓派

OpenOCD 简介


OpenOCD(Open On-Chip Debugger)开源片上调试器,是一款开源软件,旨在提供针对嵌入式设备的调试、系统编程、边界扫描功能。
OpenOCD 的功能是在仿真器的辅助下完成的,仿真器是必须的。
更多详细介绍请看这里。

安装 OpenOCD

下载源码,编译,安装
https://openocd.sourceforge.io/

硬件连接

主机通过 USB 连接 JLink,JLink 另一端接在树莓派上(JLink 可以理解为:USB + JTAG)。树莓派上留有 JTAG 引脚,但是没有标准的 JTAG 插槽,所以需要使用杜邦线将 JLink 和 树莓派连接起来,引脚对应如下:

由于杜邦线连线比较乱,所以我打了个板,这样线就不乱了

树莓派引脚设置为 JTAG 功能

这里是个坑。一般芯片引脚都有多个功能,树莓派的 JTAG 功能所对应的引脚,其第一功能也不是 JTAG。所以要先设置这些引脚的功能模式,使其处于 JTAG 模式。代码如下

#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>#define BCM2835_PERI_BASE 0x3f000000
#define GPIO_BASE (BCM2835_PERI_BASE + 0x200000)#define GPIO_ALT_FUNCTION_4 0b011
#define GPIO_ALT_FUNCTION_5 0b010volatile unsigned *m_pAltFunctionRegisters;void SetGPIOFunction(int GPIO, int functionCode)
{int registerIndex = GPIO / 10;int bit = (GPIO % 10) * 3;unsigned oldValue = m_pAltFunctionRegisters[registerIndex];unsigned mask = 0b111 << bit;printf("Changing function of GPIO%d from %x to %x\n", GPIO, (oldValue >> bit) & 0b111, functionCode);m_pAltFunctionRegisters[registerIndex] = (oldValue & ~mask) | ((functionCode << bit) & mask);
}int main()
{int fd;fd = open("/dev/mem", O_RDWR | O_SYNC);if (fd < 0)return -1;m_pAltFunctionRegisters = (volatile unsigned *)mmap(NULL, 0x18, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);if (m_pAltFunctionRegisters == MAP_FAILED)return -1;close(fd);if (m_pAltFunctionRegisters == MAP_FAILED){printf("Cannot open /dev/mem! Re-run me as root!\n");return 1;}SetGPIOFunction(22, GPIO_ALT_FUNCTION_4);SetGPIOFunction(4, GPIO_ALT_FUNCTION_5);SetGPIOFunction(27, GPIO_ALT_FUNCTION_4);SetGPIOFunction(25, GPIO_ALT_FUNCTION_4);SetGPIOFunction(23, GPIO_ALT_FUNCTION_4);SetGPIOFunction(24, GPIO_ALT_FUNCTION_4);if (m_pAltFunctionRegisters != MAP_FAILED)munmap((void *)m_pAltFunctionRegisters, 0x18);printf("Successfully enabled JTAG pins. You can start debugging now.\n");return 0;
}

交叉编译,放入树莓派中运行就行了。

启动 OpenOCD

$ sudo ../src/openocd -f interface/jlink.cfg -f board/myrpi3b+.cfg

如果未连接 JLink,会报如下错误

$ sudo ../src/openocd -f interface/jlink.cfg -f board/myrpi3b+.cfg
Open On-Chip Debugger 0.11.0+dev-00590-g2586fec-dirty (2022-03-12-02:34)
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'adapter speed' not 'adapter_khz'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: No J-Link device found

如果未连接树莓派,会报如下错误

$ sudo ../src/openocd -f interface/jlink.cfg -f board/myrpi3b+.cfg
Open On-Chip Debugger 0.11.0+dev-00590-g2586fec-dirty (2022-03-12-02:34)
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'adapter speed' not 'adapter_khz'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : J-Link ARM V8 compiled Aug 18 2009 09:06:07
Info : Hardware version: 8.00
Info : VTarget = 0.000 V
Error: JTAG I/O operation failed: 0x5.
Error: jaylink_jtag_io() failed: device: unspecified error
Info : clock speed 1000 kHz
Error: JTAG I/O operation failed: 0x5.
Error: jaylink_jtag_io() failed: device: unspecified error
Error: JTAG I/O operation failed: 0x5.
Error: jaylink_jtag_io() failed: device: unspecified error
embedded:startup.tcl:56: Error:
in procedure 'jtag_init' called at file "src/jtag/core.c", line 1678
in procedure 'init_reset' called at file "embedded:startup.tcl", line 39
at file "embedded:startup.tcl", line 56
embedded:startup.tcl:56: Error:
in procedure 'jtag_init' called at file "src/jtag/core.c", line 1678
in procedure 'init_reset' called at file "embedded:startup.tcl", line 39
at file "embedded:startup.tcl", line 56

如果树莓派未处于 JTAG 模式,会报如下错误

$ sudo ../src/openocd -f interface/jlink.cfg -f board/myrpi3b+.cfg
Open On-Chip Debugger 0.11.0+dev-00590-g2586fec-dirty (2022-03-12-02:34)
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'adapter speed' not 'adapter_khz'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : J-Link ARM V8 compiled Aug 18 2009 09:06:07
Info : Hardware version: 8.00
Info : VTarget = 3.267 V
Info : clock speed 1000 kHz
Error: JTAG scan chain interrogation failed: all zeroes
Error: Check JTAG interface, timings, target power, etc.
Error: Trying to use configured scan chain anyway...
Error: rpi3.tap: IR capture error; saw 0x00 not 0x01
Warn : Bypassing JTAG setup events due to errors
Error: Invalid ACK (0) in DAP response

下面是正常连接的 log

$ sudo ../src/openocd -f interface/jlink.cfg -f board/myrpi3b+.cfg
Open On-Chip Debugger 0.11.0+dev-00590-g2586fec-dirty (2022-03-12-02:34)
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'adapter speed' not 'adapter_khz'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Warn : DEPRECATED! use '-baseaddr' not '-ctibase'
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : J-Link ARM V8 compiled Aug 18 2009 09:06:07
Info : Hardware version: 8.00
Info : VTarget = 3.267 V
Info : clock speed 1000 kHz
Info : JTAG tap: rpi3.tap tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd), part: 0xba00, ver: 0x4)
Info : rpi3.a53.0: hardware has 6 breakpoints, 4 watchpoints
Info : rpi3.a53.1: hardware has 6 breakpoints, 4 watchpoints
Info : rpi3.a53.2: hardware has 6 breakpoints, 4 watchpoints
Info : rpi3.a53.3: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for rpi3.a53.0 on 3333
Info : Listening on port 3333 for gdb connections
Info : starting gdb server for rpi3.a53.1 on 3334
Info : Listening on port 3334 for gdb connections
Info : starting gdb server for rpi3.a53.2 on 3335
Info : Listening on port 3335 for gdb connections
Info : starting gdb server for rpi3.a53.3 on 3336
Info : Listening on port 3336 for gdb connections

接着就可以通过 telnet 或 gdb 连接 OpenOCD 进行调试了。telnet 和 gdb 相当于是客户端,OpenOCD 相当于是服务端

telnet 方式

$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> halt
> resume

gdb 方式

$ gdb
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) target remote 127.0.0.1:3333
Remote debugging using 127.0.0.1:3333
warning: while parsing target description (at line 4): Target description specified unknown architecture "arm"
warning: Could not load XML target description; ignoring
0x81031bbf in ?? ()
(gdb) c
Continuing.

gdb 连接后,一上来就会将板子 hold 住,我们输入 continue 指令,板子继续运行。
更多调试方法和命令后面有机会再介绍。

OpenOCD 通过 JTAG 调试树莓派相关推荐

  1. 使用JLink、OpenOCD通过JTAG调试树莓派4

    通过JTAG调试树莓派4 硬件环境 使能树莓派JTAG管脚 树莓派与JLink连线 安装JLink驱动 启动openocd 启动gdb 硬件环境 树莓派4一个,本例程使用Raspberry Pi OS ...

  2. Espressif IDF for VSCode 爬坑之路二:ESP32 的 JTAG 调试(OpenOCD GDB)

    今天我们来探索如何在 Espressif IDF 插件里进行 JTAG 调试.如果还未成功安装与入门 Espressif IDF for VSCode,可以先参考 Espressif IDF for ...

  3. OpenOCD安装与使用(JTAG调试)

    本文介绍openocd开源软件的安装以及搭配JTAG对Xilinx u500VC707devkit的调试 PC OS: Ubuntu20.04 LTS Target ARCH: riscv64 JTA ...

  4. 使用JLINK仿真器调试树莓派4

    学习armv8,怎么能没有开发板呢?有的小伙伴说,我可以用QEMU啊?笨叔要很负责任的说,QEMU不能100%地模拟armv8的硬件行为,有不少地方QEMU模拟不出来的,所以还是需要一个真实的硬件板子 ...

  5. jtag调试ls1012a linux-5.3内核

    1.jtag连接 OK1012A-C jtag引脚如下: 如果jlink的VCC对外输出供电,那么需要关闭,VCC对外供电导致jtag连接不上.使用引脚匹配的转接板连接开发板的jtag插座.使用交叉串 ...

  6. JTAG篇(4)——OpenOCD与JTAG TAP通信

    OpenOCD与JTAG TAP通信 OpenOCD 简介 运行OpenOCD JATG TAP Controller Result OpenOCD 简介 OpenOCD (Open On-Chip_ ...

  7. 使用 ESP-Prog _ Jlink 进行 JTAG 调试时的常见错误及解决办法

    此篇博客用来记录使用 ESP-PROG / Jlink 来对 ESP32-Lyrat 进行 JTAG 调试时遇到的一些问题以及解决办法.如果对进行 JTAG 相关操作有疑惑,请参考以下资料: 使用 E ...

  8. 【开发工具】【JTAG】JTAG调试实例【三】

    JTAG因为文章内容比较多,拆分成了四个部分,读者可以根据需求,点击查看其它的JTAG信息: JTAG基础 JTAG调试原理 JTAG调试实例 模拟系统崩溃,使用JTAG调试找到崩溃点 JTAG调试实 ...

  9. 迅为I.MX6ULL终结者开发板支持JTAG调试

    因为有很多小伙伴是从单片机转过来的,对JTAG非常熟悉.想用JTAG来调试裸机代码,而且一直用卡拷贝经常会出现虚拟机连接不上TF卡的情况. I.MX6ULL本身是支持JTAG的,但是由于关于这个资料真 ...

最新文章

  1. Linux很实用的命令查找软件安装目录
  2. BRIEF描述子生成算法
  3. 硅谷大厂也看「名校学历」?Reddit小哥灵魂拷问引起热议
  4. 突发!贝壳董事长左晖因疾病意外去世
  5. 数据库 事务的特性ACID
  6. 尽可能保留原有数据,建立UEFI与BIOS双启PE优盘
  7. 皮一皮:碰上一个说倒装句的直男怎么办...
  8. python与js通用的数据结构_五种编程语言解释数据结构与算法——顺序表3(JavaScript与Python语言实现)...
  9. Android Canvas 绘图
  10. 架构师必备技能指南:SaaS(软件即服务)架构设计
  11. CTFshow 命令执行 web38
  12. SQL SERVER 通用分页存储过程
  13. CAS Server(二):基于SpringBoot搭建客户端
  14. Python第一天学习---基础语法
  15. python之路_自定义属性、json及其他js补充
  16. python 反射实体,动态应用
  17. Android--Fragment基本介绍
  18. 4_2 刽子手游戏(UVa489)自顶向下逐步求精法
  19. php获取cookie值的方法,怎么获取cookie的值
  20. 企业级数据模型主题域模型划分(NCR FS-LDM)

热门文章

  1. mysql安装了如何启动_Mysql安装、启动与使用
  2. Redis操作List
  3. Java使用PBE算法进行对称加解密最简入门和示例
  4. 决策树中结点的特征选择方法
  5. java解析tfrecord_TensorFlow高效读取数据的方法——TFRecord的学习
  6. 什么是回调地狱?怎么解决回调地狱
  7. (二)使用Ajax简单实现前后端交互
  8. Burp Suite-第二章 Burp Suite代理和浏览器设置
  9. Git本地连接远程仓库
  10. SSM毕设项目网上书店1xy76(java+VUE+Mybatis+Maven+Mysql)