やっすいデバッガでnRF52のデバッグしてみる (Jeff Probe編)

この記事は Akerun Advent Calendar 2021 - Qiita の18日目の記事です。

こんにちは。 いとう です。ファームウェアやってます。

普段フォトシンスのお仕事では Seggerの J-Link を使っているのですが、個人の趣味プロジェクトで使用しようとすると少々お高いのでFlirc社のBlackMagic ProbeクローンのJeff ProbeをAmazonで¥1800だったの妻に内緒でブラックフライデーセールのどさくさに紛れて購入してみました。

https://flirc.tv/more/flirc-jeff-probe-bmp-jtag-black-magic-probe

https://www.amazon.co.jp/dp/B07ZK4LFTQ

可愛い小箱で届きます。内容物は Jeff Probe本体と お馴染みのSWDデバッグ用の10ピンリボンケーブル、UART接続に使えるプローブです。USB mini-B ケーブルは付属しません。

f:id:photosynth-inc:20211220112336j:plain
JeffProbe

ものすごくコンパクトです。基板剥き出しなので取扱注意。

セットアップ

ハードウェア

今回は Nordic semiconductor のnRF52832搭載の nRF52 dkボードを使用しました。

f:id:photosynth-inc:20211220112410j:plain
dkboard

書き込むファームウェア

とりあえずのテストなので Zephyr のサンプルに添付されている Lチカ "blinky" を使ってみました。Zephyr環境で dkボード用にビルドします

west build -b nrf52dk_nrf52832  zephyr/samples/basic/blinky

デバッガインストール

デバッガにはgdbを使用していますが arm用をARM社が用意してくれていますのでこちらをダウンロード。ホストはmacOSを使用しました。

https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads

接続

おもむろにJeff ProbeをmacにUSBケーブルで接続してみましょう。うまく接続できていると /dev/にttyデバイスとしてマウントされます。

バイス名はシリアル番号によって変わるようで私の環境では下記のようなデバイスとして見えていました。 同時にusbmodemが二つありますが数値が若い方を指定するとデバッガとして使用できています。

$ ls /dev/ | grep tty.usbmodem
tty.usbmodem34C775A71
tty.usbmodem34C775A73

接続確認

デバッガを立ち上げて

arm-none-eabi-gdb

繋がっているか確認してみましょう。先程のttyデバイスを指定します

(gdb) target extended-remote /dev/tty.usbmodem34C775A71

下記のコマンドでJeff Probe経由で電源供給ができます。Lチカへのファームウェア書き換えだけであればdkボードに別途電源供給不要でした。

(gdb) monitor tpwr enable

接続してます。

(gdb) monitor swdp_scan
Target voltage: 3.2V
Available Targets:
No. Att Driver
 1      ARM Cortex-M
(gdb) attach 1
Attaching to Remote target
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0x0000176a in ?? ()

J-Linkでは書き換えるROMがイレースされてなければ自動的にイレースしてくれていたんですがgdbはそこまではしてくれないようで毎回 monitor erase_mass してからリセットしないと次のファームウェアは書き込めませんでした。

(gdb) monitor erase_mass
erase..
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program:  
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()

loadコマンドでファイルを読み込んでみましょう。

(gdb) load build/blinky/zephyr/zephyr.hex 
Loading section .sec1, size 0x43c8 lma 0x0
Start address 0x1704, load size 17352
Transfer rate: 19 KB/sec, 964 bytes/write.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program:  
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.

無事にLチカできています。

あとは普通に使えると思うのですが慣れていないとcliは辛いので今回はvscode上からデバッガとして使えるかを確認してみます。

vscodeでのデバッグ

Cortex-Debug

こちらのcoretex-debugを使用するとvscodeをからJ-Linkや他のデバッガを用いてIDEのようにデバッグができます。

https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug

Cortex-Debug 設定

Jeff ProbeはBlackMagic Probeから派生している製品ですのでservertypebmpを使用するとCoretex-Debugから使用できます。あとは毎回erase_massを実行するようにoverrideLaunchCommandsを使用してデバッガ起動時の処理をオーバーライドしています。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "JeffProbeDebug",
      "cwd": "${workspaceRoot}",
      "executable": "build/button/zephyr/zephyr.elf",
      "request": "launch",
      "type": "cortex-debug",
      "servertype": "bmp",
      "device": "nrf52",
      "BMPGDBSerialPort": "/dev/tty.usbmodem34C775A71",
      "targetId": 1,
      "powerOverBMP": "disable",
      "showDevDebugOutput": true,
      "interface": "swd",
      "armToolchainPath": "/usr/local/bin/",
      "breakAfterReset": false,
      "overrideLaunchCommands": [
        "monitor erase_mass",
        "SoftwareReset",
        "load",
        "SoftwareReset"
      ],
      "postLaunchCommands": [
        "set substitute-path /workspaces/ak /Users/yuito/repo/ak"
      ]
    }
  ]
}

postLaunchCommands はビルドをDockerコンテナの中で実行しているのでパスを差し替えるために行っています。

なぜかファームウェアをダウンロードするたびにProbeの抜き差しをしないといけませんでしたが、これでステップ実行とかブレイクポイントとかも動く立派なデバッグ環境が整えられました。

まとめ

他にも中華系偽J-Link、CMSIS-DAPなどありますがお値段と気軽にAmazonで買えるのでJeffProbe、個人のプロジェクトにはいいのではないでしょうか?

(番外)Jeff Probeのアップデート

手元に届いた時にはv1.6.2だったのですがgithub上で最新バージョンのビルド済みファームウェアバイナリが配布されていたのでアップデートしました。

https://github.com/flirc/blackmagic/releases/tag/v1.6.3

brew install dfu-util
dfu-util --device 1d50:6017 -s 0x00002000:leave -D ./blackmagic.bin

GDB経由でバージョンを確認してみます。

(gdb) monitor version
Black Magic Probe (Firmware v1.6.3) (Hardware Version 0)
Copyright (C) 2015  Black Sphere Technologies Ltd.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

:+1:


株式会社フォトシンスでは、一緒にプロダクトを成長させる様々なレイヤのエンジニアを募集しています。 hrmos.co

Akerun Proの購入はこちらから akerun.com