Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从终端使用GDB(Gnu调试器)和OpenOCD进行微控制器调试?_Gdb_Gnu_Jtag_Openocd - Fatal编程技术网

如何从终端使用GDB(Gnu调试器)和OpenOCD进行微控制器调试?

如何从终端使用GDB(Gnu调试器)和OpenOCD进行微控制器调试?,gdb,gnu,jtag,openocd,Gdb,Gnu,Jtag,Openocd,对ARM微控制器编程的标准(低成本)方法是使用Eclipse,并插入一个复杂的工具链。Eclipse肯定有它的优点,但我希望感觉自己独立于这个IDE。我想了解在构建(编译-链接-闪存)软件以及运行调试会话时,幕后会发生什么。要获得如此深入的理解,从命令行运行整个过程将是非常棒的 注意:我使用的是64位Windows 10。但这里解释的大部分内容也适用于Linux系统。请打开所有具有管理员权限的命令终端。这可以帮你省去很多麻烦 1。构建软件 第一个“任务”完成了。现在,我可以通过命令行将我的软件编

对ARM微控制器编程的标准(低成本)方法是使用Eclipse,并插入一个复杂的工具链。Eclipse肯定有它的优点,但我希望感觉自己独立于这个IDE。我想了解在构建(编译-链接-闪存)软件以及运行调试会话时,幕后会发生什么。要获得如此深入的理解,从命令行运行整个过程将是非常棒的

注意:我使用的是64位Windows 10。但这里解释的大部分内容也适用于Linux系统。请打开所有具有管理员权限的命令终端。这可以帮你省去很多麻烦

1。构建软件

第一个“任务”完成了。现在,我可以通过命令行将我的软件编译并链接成二进制
.bin
.elf
图像。成功的关键是找出Eclipse将其特定项目的make文件放在何处。一旦你知道它们在哪里,你所要做的就是打开一个命令终端,然后键入
gnumake
命令

你不再需要Eclipse了!尤其是如果你能阅读(并理解)makefile,并在项目进展时根据需要调整它

请注意,在安装SW4STM32(STM32的系统工作台)之后,我在以下文件夹中找到了GNU工具(编译器、链接器、make utility、GDB等):

接下来,我在硬盘上创建了一个新文件夹,并将所有这些GNU工具复制到其中:

C:\Apps\AC6GCC
           |-> arm-none-eabi
           |-> bin
           '-> lib
我将这些条目添加到“环境路径变量”:

胡雷,现在我把所有的GNU工具都安装好并在我的系统上运行了!我将以下
build.bat
文件与
makefile
放在同一个文件夹中:

@echo off
echo.
echo."--------------------------------"
echo."-           BUILD              -"
echo."--------------------------------"
echo.

make -j8 -f makefile all

echo.
运行这个bat文件应该可以完成这项工作!如果一切顺利,编译的结果是一个
.bin
和一个
.elf
二进制文件

2。刷新和调试固件

接下来的自然步骤是将固件闪存到芯片并启动调试会话。在Eclipse中,只要“点击一个按钮”——至少在Eclipse为您的微控制器正确配置的情况下。但幕后发生了什么? 我已经阅读了OpenOCD开发人员DominicRath的硕士论文(部分)。你可以在这里找到它:。这是我学到的:

  • 当您单击“调试”图标时,Eclipse将启动OpenOCD软件。Eclipse还向OpenOCD提供了一些配置文件,这样OpenOCD就知道如何连接到微控制器“如何连接”不是一件小事。OpenOCD需要找到合适的USB驱动程序来连接到JTAG适配器(例如STLink)。JTAG适配器及其USB驱动程序通常由芯片制造商提供(例如,STMicroelectronics)。Eclipse还将描述微控制器规范的配置文件交给OpenOCD。一旦OpenOCD知道了所有这些事情,它就可以与目标设备建立可靠的JTAG连接

  • OpenOCD启动两台服务器。第一个是TCP端口4444上的Telnet服务器。它提供对OpenOCD CLI(命令行界面)的访问。Telnet客户端可以连接并向OpenOCD发送命令。这些命令可以是简单的“停止”、“运行”、“设置断点”

  • 这样的命令足以调试微控制器,但许多人已经熟悉Gnu调试器(GDB)。这就是OpenOCD在TCP端口3333上启动GDB服务器的原因。GDB客户端可以连接到该端口,并开始调试微控制器

  • Gnu调试器是一种命令行软件。许多人喜欢视觉界面。这正是Eclipse所做的。Eclipse启动一个连接到OpenOCD的GDB客户机——但这对用户来说都是隐藏的。Eclipse提供了一个图形界面,可以在后台与GDB客户端交互

我做了一个数字来解释所有这些事情:

>启动OpenOCD

我设法从命令行启动了OpenOCD。我会解释怎么做

  • 首先确保STLink-V2 JTAG程序员已正确安装。您可以使用STMicroelectronics的“STLink实用工具”测试安装。它有一个漂亮的GUI,您只需单击connect按钮。
  • 接下来从该网站下载OpenOCD软件可执行文件:。安装它,并将其放入硬盘上的文件夹中,如“C:\Apps\”
  • 打开命令终端,启动OpenOCD。您需要给OpenOCD一些配置文件,以便它知道在哪里可以找到微控制器。通常,您需要提供一个描述JTAG程序员的配置文件,以及一个定义微控制器的配置文件。使用命令行中的
    -f
    参数将这些文件传递给OpenOCD。您还需要通过将
    -s
    参数传递给OpenOCD,从而授予OpenOCD对
    scripts
    文件夹的访问权。以下是我如何使用命令行在计算机上启动OpenOCD:

    > "C:\Apps\OpenOCD-0.9.0-Win32\bin\openocd" -f "C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\interface\stlink-v2.cfg" -f "C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\target\stm32f7x.cfg" -s "C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts"
    
  • 如果正确启动OpenOCD(使用正确的参数),它将启动并显示以下消息:

    Open On-Chip Debugger 0.9.0 (2015-08-15-12:41)
    Licensed under GNU GPL v2
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
    Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
    adapter speed: 2000 kHz
    adapter_nsrst_delay: 100
    srst_only separate srst_nogate srst_open_drain connect_deassert_srst
    Info : Unable to match requested speed 2000 kHz, using 1800 kHz
    Info : Unable to match requested speed 2000 kHz, using 1800 kHz
    Info : clock speed 1800 kHz
    Info : STLINK v2 JTAG v24 API v2 SWIM v4 VID 0x0483 PID 0x3748
    Info : using stlink api v2
    Info : Target voltage: 3.231496
    Info : stm32f7x.cpu: hardware has 8 breakpoints, 4 watchpoints
    Info : accepting 'gdb' connection on tcp/3333
    Info : flash size probed value 1024
    
        GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20151217-cvs
        Copyright (C) 2015 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 "--host=i686-w64-mingw32 --target=arm-none-eabi".
        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)..
    
    如果工作正常,您将在终端上收到以下消息:

    Open On-Chip Debugger
    > ..
    
    你已经准备好向OpenOCD发送命令了!但我现在将切换到GDB会话,因为这是与OpenOCD交互的最方便的方式

    >启动GDB客户端会话以与OpenOCD交互

    打开另一个终端窗口,然后键入以下命令:

    > "C:\Apps\AC6GCC\bin\arm-none-eabi-gdb.exe"
    
    此命令仅启动
    arm none-eabi-gdb.exe
    gdb客户端。如果一切顺利,GDB将启动以下消息:

    Open On-Chip Debugger 0.9.0 (2015-08-15-12:41)
    Licensed under GNU GPL v2
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
    Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
    adapter speed: 2000 kHz
    adapter_nsrst_delay: 100
    srst_only separate srst_nogate srst_open_drain connect_deassert_srst
    Info : Unable to match requested speed 2000 kHz, using 1800 kHz
    Info : Unable to match requested speed 2000 kHz, using 1800 kHz
    Info : clock speed 1800 kHz
    Info : STLINK v2 JTAG v24 API v2 SWIM v4 VID 0x0483 PID 0x3748
    Info : using stlink api v2
    Info : Target voltage: 3.231496
    Info : stm32f7x.cpu: hardware has 8 breakpoints, 4 watchpoints
    Info : accepting 'gdb' connection on tcp/3333
    Info : flash size probed value 1024
    
        GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20151217-cvs
        Copyright (C) 2015 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 "--host=i686-w64-mingw32 --target=arm-none-eabi".
        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)..
    
    现在您已连接到OpenOCD!很高兴知道:如果您想使用本机OpenOCD命令(就像在Telne中一样)
        GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20151217-cvs
        Copyright (C) 2015 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 "--host=i686-w64-mingw32 --target=arm-none-eabi".
        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)..
    
        (gdb) target remote localhost:3333
    
        (gdb) monitor reset halt
           target state: halted
           target halted due to debug-request, current mode: Thread
           xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
        (gdb) monitor halt
    
        (gdb) monitor flash erase_address 0x08000000 0x00100000
           erased address 0x08000000 (length 1048576) in 8.899024s (115.069 KiB/s)
        (gdb) monitor reset halt
           target state: halted
           target halted due to debug-request, current mode: Thread
           xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
        (gdb) monitor halt
    
        (gdb) monitor flash protect 0 0 7 off
    
        (gdb) monitor flash info 0
           #0 : stm32f7x at 0x08000000, size 0x00100000, buswidth 0, chipwidth 0
                #  0: 0x00000000 (0x8000 32kB) not protected
                #  1: 0x00008000 (0x8000 32kB) not protected
                #  2: 0x00010000 (0x8000 32kB) not protected
                #  3: 0x00018000 (0x8000 32kB) not protected
                #  4: 0x00020000 (0x20000 128kB) not protected
                #  5: 0x00040000 (0x40000 256kB) not protected
                #  6: 0x00080000 (0x40000 256kB) not protected
                #  7: 0x000c0000 (0x40000 256kB) not protected
    
        (gdb) monitor halt
    
        (gdb) file C:\\..\\myProgram.elf
           A program is being debugged already.
           Are you sure you want to change the file? (y or n) y
           Reading symbols from C:\..\myProgram.elf ...done.
    
        (gdb) load
           Loading section .isr_vector, size 0x1c8 lma 0x8000000
           Loading section .text, size 0x39e0 lma 0x80001c8
           Loading section .rodata, size 0x34 lma 0x8003ba8
           Loading section .init_array, size 0x4 lma 0x8003bdc
           Loading section .fini_array, size 0x4 lma 0x8003be0
           Loading section .data, size 0x38 lma 0x8003be4
           Error finishing flash operation
    
        Error: error waiting for target flash write algorithm
        Error: error writing to flash at address 0x08000000 at offset 0x00000000
    
                #################################################
                #        MAKEFILE FOR BUILDING THE BINARY       #
                #        AND EVEN FLASHING THE CHIP!            #
                # Author: Othane                                #
                #################################################
    
        # setup compiler and flags for stm32f373 build 
        SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST))) 
    
    
        CROSS_COMPILE ?= arm-none-eabi- 
        export CC = $(CROSS_COMPILE)gcc 
        export AS = $(CROSS_COMPILE)gcc -x assembler-with-cpp 
        export AR = $(CROSS_COMPILE)ar 
        export LD = $(CROSS_COMPILE)ld 
        export OD   = $(CROSS_COMPILE)objdump 
        export BIN  = $(CROSS_COMPILE)objcopy -O ihex 
        export SIZE = $(CROSS_COMPILE)size 
        export GDB = $(CROSS_COMPILE)gdb 
    
    
        MCU = cortex-m4 
        FPU = -mfloat-abi=hard -mfpu=fpv4-sp-d16 -D__FPU_USED=1 -D__FPU_PRESENT=1 -DARM_MATH_CM4 
        DEFS = -DUSE_STDPERIPH_DRIVER -DSTM32F37X -DRUN_FROM_FLASH=1 -DHSE_VALUE=8000000 
        OPT ?= -O0  
        MCFLAGS = -mthumb -mcpu=$(MCU) $(FPU) 
    
    
        export ASFLAGS  = $(MCFLAGS) $(OPT) -g -gdwarf-2 $(ADEFS) 
        CPFLAGS += $(MCFLAGS) $(OPT) -gdwarf-2 -Wall -Wno-attributes -fverbose-asm  
        CPFLAGS += -ffunction-sections -fdata-sections $(DEFS) 
        export CPFLAGS 
        export CFLAGS += $(CPFLAGS) 
    
    
        export LDFLAGS  = $(MCFLAGS) -nostartfiles -Wl,--cref,--gc-sections,--no-warn-mismatch $(LIBDIR) 
    
    
        HINCDIR += ./STM32F37x_DSP_StdPeriph_Lib_V1.0.0/Libraries/CMSIS/Include/ \ 
            ./STM32F37x_DSP_StdPeriph_Lib_V1.0.0/Libraries/CMSIS/Device/ST/STM32F37x/Include/ \ 
            ./STM32F37x_DSP_StdPeriph_Lib_V1.0.0/Libraries/STM32F37x_StdPeriph_Driver/inc/ \ 
            ./ 
        export INCDIR = $(patsubst %,$(SELF_DIR)%,$(HINCDIR)) 
    
    
    
    
        # openocd variables and targets 
        OPENOCD_PATH ?= /usr/local/share/openocd/ 
        export OPENOCD_BIN = openocd 
        export OPENOCD_INTERFACE = $(OPENOCD_PATH)/scripts/interface/stlink-v2.cfg 
        export OPENOCD_TARGET = $(OPENOCD_PATH)/scripts/target/stm32f3x_stlink.cfg 
    
    
        OPENOCD_FLASH_CMDS = '' 
        OPENOCD_FLASH_CMDS += -c 'reset halt' 
        OPENOCD_FLASH_CMDS += -c 'sleep 10'  
        OPENOCD_FLASH_CMDS += -c 'stm32f1x unlock 0' 
        OPENOCD_FLASH_CMDS += -c 'flash write_image erase $(PRJ_FULL) 0 ihex' 
        OPENOCD_FLASH_CMDS += -c shutdown 
        export OPENOCD_FLASH_CMDS 
    
    
        OPENOCD_ERASE_CMDS = '' 
        OPENOCD_ERASE_CMDS += -c 'reset halt' 
        OPENOCD_ERASE_CMDS += -c 'sleep 10'  
        OPENOCD_ERASE_CMDS += -c 'sleep 10'  
        OPENOCD_ERASE_CMDS += -c 'stm32f1x mass_erase 0' 
        OPENOCD_ERASE_CMDS += -c shutdown 
        export OPENOCD_ERASE_CMDS 
    
    
        OPENOCD_RUN_CMDS = '' 
        OPENOCD_RUN_CMDS += -c 'reset halt' 
        OPENOCD_RUN_CMDS += -c 'sleep 10' 
        OPENOCD_RUN_CMDS += -c 'reset run' 
        OPENOCD_RUN_CMDS += -c 'sleep 10'  
        OPENOCD_RUN_CMDS += -c shutdown 
        export OPENOCD_RUN_CMDS 
    
    
        OPENOCD_DEBUG_CMDS = '' 
        OPENOCD_DEBUG_CMDS += -c 'halt' 
        OPENOCD_DEBUG_CMDS += -c 'sleep 10' 
    
    
        .flash: 
            $(OPENOCD_BIN) -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init $(OPENOCD_FLASH_CMDS) 
    
    
        .erase: 
            $(OPENOCD_BIN) -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init $(OPENOCD_ERASE_CMDS) 
    
    
        .run: 
            $(OPENOCD_BIN) -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init $(OPENOCD_RUN_CMDS) 
    
    
        .debug: 
            $(OPENOCD_BIN) -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init $(OPENOCD_DEBUG_CMDS) 
    
        (gdb) load
           Loading section .isr_vector, size 0x1c8 lma 0x8000000
           Loading section .text, size 0x39e0 lma 0x80001c8
           Loading section .rodata, size 0x34 lma 0x8003ba8
           Loading section .init_array, size 0x4 lma 0x8003bdc
           Loading section .fini_array, size 0x4 lma 0x8003be0
           Loading section .data, size 0x38 lma 0x8003be4
           Start address 0x8003450, load size 15388
           Transfer rate: 21 KB/sec, 2564 bytes/write.
        (gdb)