Article directory
I. Introduction
What should I do when there is a bug in the code? There are certain limitations to locating the error by using the printf statement alone. This article will introduce how to build a JTAG debugging environment and use the built-in JTAG of ESP-C3 to perform single-step debugging, setting breakpoints, viewing stacks and threads and other debugging. .
1.2 Preparations
Hardware preparation:
- Anxinke ESP-C3 series modules and development boards
- USB cable, USB adapter board
Software preparation:
Set up the ESP-IDF development environment, and see the previous blog post for the construction tutorial https://aithinker.blog.csdn.net/article/details/121565113
Wiring preparation:
The ESP32-C3 chip has a built-in full-speed USB serial port/JTAG controller. The wiring is as follows:
Connect the USB JTAG port to the virtual machine after connecting to the
computer. If the USB JTAG device cannot be found on the computer, you need to install the driver:
Windows download and run esp-idf tool installer
Select the driver
After the installation is successful, restart the computer, after installing the driver, you can find the USB JTAG device! We connect it to the virtual machine.
2. Install and run OpenOCD
2.1 Introduction to OpenOCD
OpenOCD is an open source debugging software that runs on PC, it can control many JTAG hardware; we can understand it as a kind of GDB service program. It was originally a project (2005) initiated by fellow student Dominic Rath while still in college. OpenOCD is designed to provide debugging, in-system programming and boundary scan capabilities for embedded devices. The functions of OpenOCD are accomplished with the aid of an emulator, which is a small hardware unit capable of providing electrical signals for debug targets. An emulator is necessary because the debug host (the one running OpenOCD) usually does not have the ability to parse this electrical signal directly.
2.2 Install OpenOCD
When ESP-IDF v4.0 and above runs the install.sh installation script, openocd has been installed by default
检查安装版本
openocd --version
The terminal will output the following information (the actual version number may be updated):
Open On-Chip Debugger v0.10.0-esp32-20210902 (2021-09-02-09:38)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
检查 OPENOCD_SCRIPTS 环境变量的值,以确认 OpenOCD 配置文件的路径
Go into the ESP-IDF directory and run ../export.sh and echo $OPENOCD_SCRIPTS
cd esp-idf
. ./export.sh
echo $OPENOCD_SCRIPTS
echo $OPENOCD_SCRIPTS prints my OpenOCD path
~/.espressif/tools/openocd-esp32/v0.10.0-esp32-20210902/openocd-esp32/share/openocd/scripts
openocd needs to use usb, you need to change ~/.espressif/tools/openocd-esp32/v0.10.0-esp32-20210902/openocd-esp32/share/openocd/contrib/contrib/60-openocd.rules (modify according to your actual path) ) to /etc/udev/rules.d, so that openocd has permission to use usb to debug the device
cd ~/.espressif/tools/openocd-esp32/v0.10.0-esp32-20210902/openocd-esp32/share/openocd/contrib/contrib
sudo cp 60-openocd.rules /etc/udev/rules.d
Enter the project directory to be debugged, take examples/get-started/blink as an example
cd $IDF_PATH/examples/get-started/blink
禁用内存保护
Until 2021-11-26, you need to disable memory protection to run GDB normally.
Run idf.py menuconfig, disable memory protection
把日志输出到USB(可选)
in Component config → ESP System Settings → Memory protection, and set it in Component config → ESP System Settings → Channel for console output
编译烧录固件
idf.py build flash monitor
运行openocd
openocd -f board/esp32c3-builtin.cfg
The operation is successful as shown in the following figure
3. Install and run GBD
3.1 Introduction to GBD
GDB (GNU Project Debugger) is the debugging software in the GNU tool chain. GDB is a very widely used debugging tool that can be used to debug programs written in various languages such as C, C++, Ada, etc. It provides the following functions:
- Download or start the program
- Stop the program by setting various specific conditions
- View the running status of the processor, including the value of the general register, the value of the memory address, etc.
- View the state of the program, including the values of variables, the state of functions, etc.
- Change the operating state of the processor, including the value of general-purpose registers, the value of memory addresses, etc.
- Change the state of the program, including the values of variables, the state of functions, etc.
GDB can be used to debug running programs in the Linux system of the host PC, and can also be used to debug embedded hardware. In the environment of embedded hardware, due to limited resources, the general embedded target hardware cannot directly build GDB. Debugging environment (such as display screen and Linux system, etc.), at this time, remote debugging can be performed by GDB + GdbServer. Generally speaking, GdbServer runs on the target hardware, while GDB runs on the host PC.
3.2 Running GBD
Install python2, libpython2.7
sudo apt install python2
sudo apt install libpython2.7
Open a new terminal session and go to the project directory to be debugged, for example:
cd esp-idf
. ./export.sh
cd examples/get-started/blink
When starting the debugger, you usually need to provide several configuration parameters and commands. To avoid entering these commands line by line on the command line each time, you can create a new configuration file and name it gdbinit:
target remote :3333
set remote hardware-watchpoint-limit 2
mon reset halt
flushregs
thb app_main
c
Save this file in the current directory.
For more details on the inside of the gdbinit file, see the chapter Meaning of the debugger's startup command
Start GDB. Make sure openocd -f board/esp32c3-builtin.cfg is running before booting
idf.py gdbtui
Press the Enter key to enter the program
. JTAG debugging has run up here. I opened a total of three terminals, one runs idf.py monitor to view the serial port log, one runs openocd -f board/esp32c3-builtin.cfg, one runs idf.py gdbtui, and the three terminals cooperate with debugging.
3.3 GDB Common Debugging Commands
step
andnext
command (can be abbreviated ass
andn
) to step through the code, the difference between the two is that executing the "step" command will enter the called subroutine, while executing the "next" command will directly treat the subroutine as a single source code OK, you can end it in a single step
-
break M
: Set a breakpoint on line M, shorthandb
-
continue
: continue to run, shorthandc
-
quit
: quit GDB, shorthandq
-
set
: set the value of the variable -
print
: print value and address, shorthandp
-
finish
: End the current function and return to the function call point -
frame
: switch the stack frame of the function, shorthandf
-
backtrace
: View the stack frame and hierarchical relationship of the function call, shorthandbt
-
display
: Track and view specific variable values -
delete breakpoints num
: delete the numth breakpoint, shorthandd
-
info
: View the value of the local variable inside the function, shorthandi
-
frame
: switch the stack frame of the function, shorthandf
Here only describes how to quickly build a JTAG debugging environment and the simplest example. For those who need to learn more, please move to the official documentation . It is recommended to read it several times 几天
.
contact us
Official website: https://www.ai-thinker.com
Development of DOCS: https://docs.ai-thinker.com
Official forum: http://bbs.ai-thinker.com
Technical support: [email protected]