JTAG 调试

JTAG 接口 & esp-prog 驱动

注意事项和补充内容


配置

选择 JTAG 适配器

单拿出来将是因为踩过坑,ST-Link的JTAG不能使用。 详细查看OpenOCD 支持的适配器列表

安装 OpenOCD

其实按照CMake构建工程完成后基本就OK了,已经存在于ESP32的工具链中。
不懂的话参考 CentOS7 / Ubuntu + ESP32 + CMake
记得运行./install.sh. ./export.sh
查看版本和路径

yjw@ubuntu:~$ openocd --version
Open On-Chip Debugger  v0.10.0-esp32-20200709 (2020-07-09-08:54)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html

yjw@ubuntu:~$ echo $OPENOCD_SCRIPTS
/home/yjw/.espressif/tools/openocd-esp32/v0.10.0-esp32-20200709/openocd-esp32/share/openocd/scripts

连接 JTAG 接口

配置 ESP-WROVER-KIT 上的 JTAG 接口
配置其它 JTAG 接口
连接失败,权限不够执行sudo chmod -R 777 /etc/ttyUSB0
其他的参考一下下面的 错误 LIBUSB_ERROR_ACCESS


调试

调试之前先讲一下 openOCD 与 xtensa-esp32-elf-gdb 的关系
OpenOCD:

  • 创建一个Server接收 gdb debugger 的连接请求
  • 通过JTAG适配器与ESP32 交互,控制设备运行状态
    xtensa-esp32-elf-gdb debugger:
  • 作为一个client,连接OpenOCD
  • 与用户交互,接收用户的 debug 命令(s\n等指令),发送到OpenOCD

运行 OpenOCD

参考:OpenOCD
根据目标芯片配置 OpenOCD

我这里使用的是 ESP-Prog 调试器,开发板为ESP32-DevKitC V4开发板
所以打开一个终端窗口运行如下指令:

openocd -f interface/ftdi/esp32_devkitj_v1.cfg -f target/esp32.cfg

运行 xtensa-esp32-elf-gdb

参考: 使用命令行调试
打开一个新的终端窗口,记得配置ESP32的环境./install.sh. ./export.sh
之后进入我们想要调试的项目目录

cd cd ~/esp/esp_idf_demo/blink/

配置配置参数和命令,这里采用新建一个gdbinit文件。

gedit gdbinit

文件内粘贴如下内容后保存

target remote :3333
set remote hardware-watchpoint-limit 2
mon reset halt
flushregs
thb app_main
c

接下来进行调试,执行如下命令:

xtensa-esp32-elf-gdb -x gdbinit build/blink.elf

关于GDB的内容参考:GDB


示例

blink.c中的内容

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#define BLINK_GPIO CONFIG_BLINK_GPIO
void app_main(void)
{
    gpio_pad_select_gpio(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
    while(1) {
        printf("Turning off the LED\n");
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        printf("Turning on the LED\n");
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

启动GDB后会停止,并以(gdb)停止,这时候我们输入gdb命令。

Temporary breakpoint 1, app_main () at ../main/blink.c:9
9           gpio_pad_select_gpio(BLINK_GPIO);
(gdb)  

首先可以程序运行到了blink.c文件的第9行,接下来我们将整个app_main

(gdb) l app_main 
3       #include "freertos/task.h"
4       #include "driver/gpio.h"
5       #include "sdkconfig.h"
6       #define BLINK_GPIO CONFIG_BLINK_GPIO
7       void app_main(void)
8       {
9           gpio_pad_select_gpio(BLINK_GPIO);
10          gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
11          while(1) {
12              printf("Turning off the LED\n");
(gdb) 
13              gpio_set_level(BLINK_GPIO, 0);
14              vTaskDelay(1000 / portTICK_PERIOD_MS);
15              printf("Turning on the LED\n");
16              gpio_set_level(BLINK_GPIO, 1);
17              vTaskDelay(1000 / portTICK_PERIOD_MS);
18          }
19      }
(gdb)

我们在12行和15行各添加一个断点

(gdb) b blink.c:12
Breakpoint 2 at 0x400d39d6: file ../main/blink.c, line 12.
(gdb) b blink.c:15
Breakpoint 3 at 0x400d39ea: file ../main/blink.c, line 15.
(gdb) info b
Num     Type           Disp Enb Address    What
2       breakpoint     keep y   0x400d39d6 in app_main at ../main/blink.c:12
3       breakpoint     keep y   0x400d39ea in app_main at ../main/blink.c:15
(gdb) 

错误

错误 LIBUSB_ERROR_ACCESS

错误提示:

Open On-Chip Debugger  v0.10.0-esp32-20200709 (2020-07-09-08:54)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 20000 kHz

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: no device found
Error: unable to open ftdi device with vid 0403, pid 6010, description '*', serial '*' at bus location '*'
Error: no device found
Error: unable to open ftdi device with vid 0403, pid 6014, description '*', serial '*' at bus location '*'

解决办法:
运行指令lsusb查看设备的UID和VID。

yjw@ubuntu:/etc/udev/rules.d$ lsusb
Bus 001 Device 016: ID 0403:6010 Future Technology Devices International, Ltd FT2232C/D/H Dual UART/FIFO IC

VID(idVendor):0403
PID(idProduct):6010
创建一个新的udev规则,并填入设备的 idVendor 和 idProduct

sudo gedit /etc/udev/rules.d/90-myusb.rules

内容如下:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", GROUP="plugdev", MODE="0666"

将登陆的账户添加到plugdev

sudo usermod -a -G plugdev yjw

产看用户所在组

yjw@ubuntu:~$ sudo usermod -a -G plugdev yjw
yjw@ubuntu:~$ groups yjw
yjw : yjw adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare

重新加载 udev 规则

sudo udevadm control --reload

错误:libpython2.7.so.1.0

xtensa-esp32-elf-gdb: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

工具链的版本问题:
在现在的工具链版本中,已经使用Python 3库来构建GDB。老版本是Python 2
有两种选择:

参考下面的文档重新构建一下工具链就可以了
从零开始设置 Linux 环境下的工具链
Linux 平台工具链的标准设置


错误:Error: couldn't bind tcl to socket on port 6666: Address already in use

端口被占用错误,查看端口pid,并杀死。

yjw@yjw-pc:~/esp/demo/blink$ lsof -i:6666
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
openocd 11547  yjw    3u  IPv4  72086      0t0  TCP localhost:6666 (LISTEN)
yjw@yjw-pc:~/esp/demo/blink$ kill 11547

或者直接

sudo pkill openocd 
最后修改:2021 年 07 月 08 日
男宾一位~ 欢迎下次再来!