OpenOCD烧录STM32失败的问题

Linux下使用 OpenOCD 烧录 STM32, 出现了 Error: init mode failed (unable to connect to the target) 错误.

如果在代码中, 不小心将 PA13,PA14 的 SWD 功能关闭, 例如使用了下面的代码

rcc_periph_clock_enable(RCC_GPIOA);         // Need GPIOA clock
gpio_primary_remap(
AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF, // Optional
AFIO_MAPR_TIM2_REMAP_NO_REMAP); // This is default: TIM2.CH2=GPIOA1

或者在 STM32CubeMX 中忘记勾选 PA13/PA14 的串口调试功能, 都会导致后续烧录和连接失败, 出现

Uploading .pio/build/bluepill_f103c8/firmware.elf
xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:15)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 1 hla_swd
none separate Error: init mode failed (unable to connect to the target)
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked *** [upload] Error 1

STLINK 工具 st-flash 和 st-info

首先检查一下系统中是否有st-flashst-info命令, 没有的话需要安装

安装步骤

git clone https://github.com/stlink-org/stlink.git
cd stlink/
make
# 如果报 libusb 错误, 就安装一下 libusb-1.0-0-dev, 再make
sudo apt install libusb-1.0-0-dev
make clean
make
cd build/Release/
sudo make install

通过 st-info 检查 会提示无法进入 SWD 模式

$ st-info --probe
Failed to parse flash type or unrecognized flash type
Failed to enter SWD mode
Found 1 stlink programmers
version: V2J37S7
serial: 56FF6B064966485627461667
flash: 0 (pagesize: 0)
sram: 0
chipid: 0x000 detected chip_id parametres # Device Type: unknown
# Reference Manual: RM0000
#
chip_id 0x0
flash_type 0
flash_size_reg 0x0
flash_pagesize 0x0
sram_size 0x0
bootrom_base 0x0
bootrom_size 0x0
option_base 0x0
option_size 0x0
flags 0 dev-type: unknown

使用 st-flash 和 st-info 解决 STM32 烧录失败

在网上搜到的大部分方案, 例如这个stackoverflow的解答, 都是要到Windows下, 通过 ST-link utility 去重置擦除, 这里介绍一下 Linux 下的处理方法

st-info 使用 --connect-under-reset 参数

此时需要使用--connect-under-reset参数, 按住开发板的 RESET 键之后, 执行下面的命令, 就能正确检测到芯片信息

$ st-info --probe  --connect-under-reset
Failed to parse flash type or unrecognized flash type detected chip_id parametres # Device Type: STM32F1xx_MD
# Reference Manual: RM0008
#
chip_id 0x410
flash_type 1
flash_size_reg 0x1ffff7e0
flash_pagesize 0x400
sram_size 0x5000
bootrom_base 0x1ffff000
bootrom_size 0x800
option_base 0x1ffff800
option_size 0x10
flags 2 Found 1 stlink programmers
version: V2J37S7
serial: 56FF6B064966485627461667
flash: 0 (pagesize: 1024)
sram: 20480
chipid: 0x410 detected chip_id parametres # Device Type: STM32F1xx_MD
# Reference Manual: RM0008
#
chip_id 0x410
flash_type 1
flash_size_reg 0x1ffff7e0
flash_pagesize 0x400
sram_size 0x5000
bootrom_base 0x1ffff000
bootrom_size 0x800
option_base 0x1ffff800
option_size 0x10
flags 2 dev-type: STM32F1xx_MD

如果是这种情况, 说明是PA13/PA14的功能复用问题, 可以通过固件擦除解决问题

st-flash 擦除固件

st-flash 同样地要加上--connect-under-reset参赛, 在按住 RESET 键后执行下面的命令

$ st-flash --connect-under-reset erase
st-flash 1.7.0-184-g468b1d2
Failed to parse flash type or unrecognized flash type
2022-02-14T22:51:20 ERROR common.c: Soft reset failed: timeout detected chip_id parametres # Device Type: STM32F1xx_MD
# Reference Manual: RM0008
#
chip_id 0x410
flash_type 1
flash_size_reg 0x1ffff7e0
flash_pagesize 0x400
sram_size 0x5000
bootrom_base 0x1ffff000
bootrom_size 0x800
option_base 0x1ffff800
option_size 0x10
flags 2 2022-02-14T22:51:20 INFO common.c: STM32F1xx_MD: 20 KiB SRAM, 0 KiB flash in at least 1 KiB pages.
Mass erasing

在出现Mass erasing后, 不能立即断电, 需要等待一小段时间, 之后用st-info --probe检查是否成功, 如果还显示Failed to enter SWD mode, 就再重复一遍上面的操作.

如果st-info --probe能直接检测到芯片, 就说明SWD功能已经恢复, 可以继续在 Linux 下用 OpenOCD 愉快地烧录 STM32 了.

最新文章

  1. MySQL查询和删除重复数据
  2. sql对于between和时间
  3. Intellij编译时报“java: System Java Compiler was not found in classpath” 解决办法
  4. Android——按钮的事件监听
  5. HDU2054JAVA
  6. asp.net 下载文件(图片、word、excel等)
  7. js1中call和apply的用法
  8. Spring读书笔记——bean解析
  9. MySQL触发器在PHP项目中用来做信息备份、恢复和清空的方法介绍
  10. 关于MySQL中的8个 character_set 变量
  11. flagr a/b 测试特性开关&&微服务动态配置工具
  12. sonarqube安装的坑
  13. Java 范例 - 字节处理
  14. 使用netty编写IM通信界面
  15. Kubernetes集群部署之二CA证书制作
  16. java-普通类文件@Autowired自动注入为null
  17. Hbase数据读写流程
  18. qt调用js,js调用qt
  19. kafka 部署
  20. Oracle相关

热门文章

  1. captcha_生成图片验证码并返回给前端展示
  2. spring boot 使用 AOP 的正确姿势 --- 心得
  3. 第10组 Alpha冲刺 (5/6)
  4. Linux上天之路(六)之Linux文件管理
  5. python极简教程04:进程和线程
  6. MCU软件最佳实践——使用printf打印数据
  7. .NET 云原生架构师训练营(KestrelServer源码分析)--学习笔记
  8. Servlet Listener(监听器)
  9. golang中使用switch语句根据年月计算天数
  10. java 坐标练习