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-flash
和st-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 出現下面的錯誤
st-info: error while loading shared libraries: libstlink.so.1: cannot open shared object file: No such file or directory
運行一下sudo ldconfig
可以解決問題
通過 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: 56FF6B064966485627161123
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: 56FF6B064966485627161123
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 了.