目录
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)https://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