Ubuntu下使用DAPLink(OpenOCD)

目录

1. 下载OpenOCD源代码

2. 编译代码

2.1 运行bootstrap

 2.2 安装关联库

2.3 运行./configure

2.4 运行make

2.5 运行sudo make install

3. 烧录程序

3.1 挂起MCU

3.2 写入镜像

3.3 校验镜像


通过OpenOCD实现,在Ubuntu18 64bit下验证。

1. 下载OpenOCD源代码

OpenOCD对应的官网:

Open On-Chip Debugger (openocd.org)icon-default.png?t=N7T8https://openocd.org/点击“Getting OpenOCD”,找到Source Code字段,红色字体链接都可以转到源代码的网站。

 以官方的网站为例(sourceforge):

左侧选择版本,然后点击Download即可

git clone https://git.code.sf.net/p/openocd/code openocd-code

2. 编译代码

终端进入下载的OpenOCD文件夹。

2.1 运行bootstrap

./bootstrap

 提示错误

configure.ac:32: error: Macro PKG_PROG_PKG_CONFIG is not available. It is usually defined in file pkg.m4 provided by package pkg-config.

安装pkg-config

sudo apt-get install pkg-config

新的错误:

./bootstrap: 41: git: not found

安装git

sudo apt-get install git

还是提示错误,改为git pull下来工程。

 2.2 安装关联库

运行./configure可以看到很多关联库需要安装。

configure: WARNING: libusb-1.x not found, trying legacy libusb-0.1 as a fallback; consider installing libusb-1.x instead

checking for capstone... no

checking for hidapi... no

checking for hidapi-hidraw... no

checking for hidapi-libusb... no

checking for libftdi1... no

checking for libftdi... no

checking for libgpiod... no

checking for libjaylink >= 0.2... no

而且配置完后提示的功能都不支持:

OpenOCD configuration summary

--------------------------------------------------

MPSSE mode of FTDI based devices        no

ST-Link Programmer                      no

TI ICDI JTAG Programmer                 no

Keil ULINK JTAG Programmer              no

Altera USB-Blaster II Compatible        no

Bitbang mode of FT232R based devices    no

Versaloon-Link JTAG Programmer          no

TI XDS110 Debug Probe                   no

CMSIS-DAP v2 Compliant Debugger         no

OSBDM (JTAG only) Programmer            no

eStick/opendous JTAG Programmer         no

Olimex ARM-JTAG-EW Programmer           no

Raisonance RLink JTAG Programmer        no

USBProg JTAG Programmer                 no

Espressif JTAG Programmer               no

CMSIS-DAP Compliant Debugger            no

Nu-Link Programmer                      no

Cypress KitProg Programmer              no

Altera USB-Blaster Compatible           no

ASIX Presto Adapter                     no

OpenJTAG Adapter                        no

Linux GPIO bitbang through libgpiod     no

SEGGER J-Link Programmer                no

Bus Pirate                              yes (auto)

Use Capstone disassembly framework      no

安装对应的库:

sudo apt-install libusb-1.0-0-dev
sudo apt-get install libcapstone-dev
sudo apt-get install libhidapi-dev
sudo apt-get install libgpiod-dev
sudo apt-get install libftdi-dev
sudo apt-get install libftdi1-dev
sudo apt-get install libjaylink-dev

2.3 运行./configure

这时候所有的配置都为yes了。

2.4 运行make

2.5 运行sudo make install

安装完成后运行openocd -v确认是否安装成功。

Open On-Chip Debugger 0.12.0+dev-00271-g2c57d11c7 (2023-07-18-10:25)

Licensed under GNU GPL v2

For bug reports, read

	http://openocd.org/doc/doxygen/bugs.html

3. 烧录程序

拷贝配置文件到hex文件所在的文件夹,配置文件相对路径如下:

openocd-code-v0.12.0/openocd-code/tcl/interface/cmsis-dap.cfg

在hex文件所在文件夹运行:

sudo openocd -f cmsis-dap.cfg

提示错误(这个错误是虚拟机的问题):

Error: unable to find a matching CMSIS-DAP device

先使用lsusb看一下USB设备,没有对应的USB设备。

安装VBOX的增强功能,然后在VBox中选择USB3.0(如果设备插入的是3.0的接口) 

再次运行openocd:

Open On-Chip Debugger 0.12.0+dev-00271-g2c57d11c7 (2023-07-18-10:25)

Licensed under GNU GPL v2

For bug reports, read

	http://openocd.org/doc/doxygen/bugs.html

Info : Listening on port 6666 for tcl connections

Info : Listening on port 4444 for telnet connections

Warn : An adapter speed is not selected in the init scripts. OpenOCD will try to run the adapter at very low speed (100 kHz).

Warn : To remove this warnings and achieve reasonable communication speed with the target, set "adapter speed" or "jtag_rclk" in the init scripts.

Info : CMSIS-DAP: SWD supported

Info : CMSIS-DAP: Atomic commands supported

Info : CMSIS-DAP: FW Version = 2.0.0

Error: CMSIS-DAP: JTAG not supported

拷贝board的配置文件:

openocd-code/tcl/board/stm32f103c8_blue_pill.cfg

并将文件名改为stm32f103c8.cfg,打开编辑,可以看到关联另外一个cfg文件,将这个文件也拷贝过来,将这个文件的路径改为当前路径:

source [find stm32f1x.cfg]

运行

sudo openocd -f cmsis-dap.cfg -f stm32f103c8.cfg

结果如下:

Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.

Info : Listening on port 6666 for tcl connections

Info : Listening on port 4444 for telnet connections

Info : CMSIS-DAP: SWD supported

Info : CMSIS-DAP: Atomic commands supported

Info : CMSIS-DAP: FW Version = 2.0.0

Info : CMSIS-DAP: Interface Initialised (SWD)

Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 1 nRESET = 1

Info : CMSIS-DAP: Interface ready

Info : clock speed 1000 kHz

Info : SWD DPIDR 0x1ba01477

Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected

Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints

Info : starting gdb server for stm32f1x.cpu on 3333

Info : Listening on port 3333 for gdb connections

此时已经处于gdb链接状态,需要另外开一个终端运行

telnet localhost 4444

OpenOCD会监听本机的4444端口,通过telnet登录上去,之后就可以控制OpenOCD干些什么了。

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

Open On-Chip Debugger

> 

后续的操作即在这个终端中输入对应的命令。 

3.1 挂起MCU

输入halt挂起MCU,相当于关机,不执行halt,无法把程序写入MCU。

> halt

[stm32f1x.cpu] halted due to debug-request, current mode: Thread 

xPSR: 0x81000000 pc: 0x0800046c msp: 0x20004ff8

3.2 写入镜像

> flash write_image erase stm32f10x.hex 

Adding extra erase range, 0x08004c6c .. 0x08004fff

auto erase enabled

wrote 19564 bytes from file stm32f10x.hex in 2.569553s (7.435 KiB/s)

3.3 校验镜像

> flash verify_image stm32f10x.hex

verified 19564 bytes from file stm32f10x.hex in 0.124474s (153.490 KiB/s)

除了命令的方式,也可以通过运行脚本的方式。

新建一个program.sh的脚本文件,编辑加入:

#!/bin/bash

echo "OpenOCD-program stm32f103"

openocd -f cmsis-dap.cfg \

	-f stm32f103c8.cfg \

	-c "program stm32f10x.hex verify reset exit"

文件属性那里改为“允许执行文件”,在终端中运行:

sudo ./program.sh

结果如下:

Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.

Info : CMSIS-DAP: SWD supported

Info : CMSIS-DAP: Atomic commands supported

Info : CMSIS-DAP: FW Version = 2.0.0

Info : CMSIS-DAP: Interface Initialised (SWD)

Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 1 nTRST = 1 nRESET = 1

Info : CMSIS-DAP: Interface ready

Info : clock speed 1000 kHz

Info : SWD DPIDR 0x1ba01477

Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected

Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints

Info : starting gdb server for stm32f1x.cpu on 3333

Info : Listening on port 3333 for gdb connections

[stm32f1x.cpu] halted due to debug-request, current mode: Thread 

xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc

** Programming Started **

Info : device id = 0x20036410

Info : ignoring flash probed value, using configured bank size

Info : flash size = 128 KiB

Warn : Adding extra erase range, 0x08004c6c .. 0x08004fff

** Programming Finished **

** Verify Started **

** Verified OK **

** Resetting Target **

shutdown command invoked

  • 30
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用DAPLink给STM32烧录固件的过程如下: 首先,将DAPLink与您的STM32单片机连接起来。通常,DAPLink具有标准的10针JTAG接口或20针SWD接口,并与单片机上的相应引脚进行连接。 接下来,在您的计算机上下载并安装DAPLink驱动程序。这些驱动程序可从DAPLink的官方网站或相关厂商的网站上获得。 然后,使用USB线将DAPLink连接到计算机的USB接口上。确保电源供应充足,并且连接正确无误。 接下来,在计算机上打开命令行界面(例如Windows的CMD或Mac的终端)。 然后,使用命令行导航到保存固件文件的目录。确保您已准备好需要烧录的固件文件,并且文件是可执行的。 接下来,使用特定的命令行工具来执行烧录操作。具体的命令行工具取决于您使用的开发环境和DAPLink驱动程序。例如,对于使用OpenOCD的环境,您可以使用类似下面的命令: "openocd -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg -c "program your_firmware.bin reset exit"。 命令执行后,DAPLink会自动将固件文件烧录到STM32单片机中。烧录完成后,程序将会自动重启。 最后,您可以使用开发环境的调试工具(如STM32CubeIDE、Keil MDK等)来验证固件是否成功烧录,并进行相关的调试。 总结来说,使用DAPLink给STM32烧录固件需要连接硬件、安装驱动程序、配置命令行,并执行命令进行烧录。这是一种常见的烧录方法,可以帮助您将固件文件加载到STM32单片机中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值