STLink Mini刷v2固件
· 阅读需 5 分钟
1. 解除Read Protection
STLink V2的bootloader开启了Read Protection,导致起始的8KB Flash不能被修改,在刷写Flash前需要解除Read Protection。
可以使用openocd命令来解锁:
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg \
-c "init" -c "reset init" \
-c "stm32f1x unlock 0; reset init" \
-c "reset" -c "shutdown"
输出如下:
Open On-Chip Debugger 0.12.0
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
Info : clock speed 1000 kHz
Info : STLINK V2J43S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.102632
Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477
看上去是手上的板子使用的仿制的mcu,在openocd安装目录(如*/usr/local/Cellar/open-ocd/0.12.0_1/share/openocd/scripts/target*)下找到target/stm32f1x.cfg,复制一份并命名为apm32f1x.cfg,将apm32f1x.cfg里的0x1ba01477修改为0x2ba01477:
...
#jtag scan chain
if { [info exists CPUTAPID] } {
set _CPUTAPID $CPUTAPID
} else {
if { [using_jtag] } {
# See STM Document RM0008 Section 26.6.3
set _CPUTAPID 0x3ba00477
} {
# this is the SW-DP tap id not the jtag tap id
set _CPUTAPID 0x2ba01477 # <== replace 0x1ba01477 with 0x2ba01477!
}
}
...
再次尝试使用openocd解锁flash:
openocd -f interface/stlink.cfg -f target/apm32f1x.cfg -c "init" -c "reset init" -c "stm32f1x unlock 0; reset init" -c "reset" -c "shutdown"
Open On-Chip Debugger 0.12.0
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
Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable}
Info : clock speed 1000 kHz
Info : STLINK V2J43S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.083684
Info : [apm32f1x.cpu] Cortex-M3 r2p1 processor detected
Info : [apm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for apm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections
[apm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Info : device id = 0x20036410
Info : flash size = 128 KiB
[apm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
shutdown command invoked
可以看到解锁成功,并且这个仿制的mcu是128KB的,可以刷V2.1的固件。
2. 备份旧固件
openocd的flash命令可以备份固件:
flash read_bank bank_id filename [offset [length]]
确认bank_id:
openocd -f interface/stlink.cfg -f target/apm32f1x.cfg -c "init" -c "reset init" -c "flash banks"
输出如下:
Open On-Chip Debugger 0.12.0
...
[apm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002e0c msp: 0x20001ce0
#0 : apm32f1x.flash (stm32f1x) at 0x08000000, size 0x00000000, buswidth 0, chipwidth 0
...
openocd -f interface/stlink.cfg -f target/apm32f1x.cfg -c "init" -c "reset init" -c "flash read_bank 0 stlink-old.bin"
输出如下:
Open On-Chip Debugger 0.12.0
...
Info : device id = 0x20036410
Info : flash size = 128 KiB
wrote 131072 bytes to file stlink-old.bin from flash bank 0 at offset 0x00000000 in 2.005690s (63.818 KiB/s)
...