chakokuのブログ(rev4)

テック・コミック・ごくまれにチャリ

ST LinkV2(互換品)を使ってOpenOCDと評価ボード(STM32F103C8T6)を接続してみる

やりたいこと
ST link V2を使ってOpenOCD + GDBデバッグ環境を作る。ターゲットは、STM32F103C8T6 (BluePillという愛称のボード)

ST-Link V2 / BluePill
結論
以下の手順でgdbによるデバッグが可能
1. stm32f1x.cfgのIDを修正する
2. 以下の引数でOpenOCDを起動

$ ./bin-x64/openocd  -f ./scripts/interface/stlink-v2.cfg -f ./scripts/target/stm32f1x.cfg

3. gdbを起動、3333ポートに接続

試行錯誤の記録

$ ./bin-x64/openocd.exe -f scripts/interface/stlink-v2-1.cfg  -f scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.10.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 u
se 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results mi
ght differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Error: open failed
in procedure 'init'
in procedure 'ocd_bouncer'

評価ボードをつないでいないときは以下のメッセージ

$ ./bin-x64/openocd -f ./scripts/interface/stlink-v2.cfg -f ./scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.10.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 u
se 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results mi
ght differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v29 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.174546
Error: init mode failed (unable to connect to the target)
in procedure 'init'
in procedure 'ocd_bouncer'

stlinkのconfigが v2-1かv2なのか。。v2.cfgを使うと、ID不一致で怒られる。こっちの方がまだまし。

$ ./bin-x64/openocd  -f ./scripts/interface/stlink-v2.cfg -f ./scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.10.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 u
se 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results mi
ght differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v29 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.162078
Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477
in procedure 'init'
in procedure 'ocd_bouncer'

以下のID不一致エラーについて

Warn : UNEXPECTED idcode: 0x2ba01477
Error: expected 1 of 1: 0x1ba01477

「config内のIDを書き換えろ」というコメントあり
Cannot upload to STM32 Bluepill board over STLink-V2 - Development Platforms - PlatformIO Community

上記コメントに従い、stm32f1x.cfgを修正

#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 0x1ba01477  # patched by advice
      set _CPUTAPID 0x2ba01477
   }
}

修正版configを使ってOpenOCDを起動、エラーなしでターゲットに接続できたようである。

$ ./bin-x64/openocd  -f ./scripts/interface/stlink-v2.cfg -f ./scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.10.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 u
se 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results mi
ght differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v29 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.162078
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints

3333,4444でListenしているようなので、gdbを動かしてみる

$ ./arm-none-eabi-gdb.exe

(gdb) target remote localhost:3333         # 3333ポートでOpenOCDに接続
Remote debugging using localhost:3333
0x08000194 in ?? ()

(gdb) monitor reset halt                           # OpenOCD経由でターゲット(BulePill)停止
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000144 msp: 0x20000400

(gdb) info register                                    # ターゲットのレジスタ確認
r0             0x253c0  152512
r1             0x422201b4       1109524916
r2             0x40011000       1073811456
r3             0x10032000       268640256
r4             0x80004cc        134218956
r5             0x80004cc        134218956
r6             0x0      0
r7             0x0      0
r8             0x0      0
r9             0x0      0
r10            0x0      0
r11            0x0      0
r12            0x2000   8192
sp             0x20000400       0x20000400
lr             0x8000493        134218899
pc             0x8000198        0x8000198
xPSR           0x21000000       553648128

(gdb) continue
Continuing.                         # ターゲット(BluePill)のLチカ再開

Program received signal SIGINT, Interrupt.
0x08000194 in ?? ()            # ターゲットのLチカ停止

メモリダンプ

(gdb) x/64bx 0x0

gdbからreset後、continueで実行させると、ボード(BluePill)のLチカが再開、gdbでCtrl-Cを打ち込むとボードのLチカが止まって、PCが0x8000194になる。再度continueするとLチカが再開する。これらの動作によりgdbによるデバッグ環境が構築できたと判断。


ビルド環境、デバッグ環境ができた(はず)なので、、あとはSTM32用のプログラムを組むだけ。。か。。

■メモ、ご参考URL
評価ボードの名称:
STM32F103C8T6 ARM STM32 Minimum システム 開発ボードモジュール(通称BluePill)
ねむいさんのぶろぐ | Project Examples and Misc Files
GitHub - ve3wwg/stm32f103c8t6: libopencm3 and FreeRTOS projects using the STM32F103C8T6 MCU
GitHub - texane/stlink: stm32 discovery line linux programmer
OpenOCD User’s Guide: GDB and OpenOCD
eclipse - STM32 GDB/OpenOCD Commands and Initialization for Flash and Ram Debugging - Stack Overflow
OpenOCD User’s Guide: Top