前回に引き続き、RaspberryPi Picoの開発環境のセットアップを行っていきたいと思います。

まず、OpenOCD用(SWD経由)のpicoアダプターを作成します。
ソフトは、picoprobe.uf2ですでにセットアップの中でコンパイル済みです。
例題サンプルblinkと同じような手順で、OpenOCD用picoに書き込みします。

picoprobe.uf2のフォルダーに移動します。

$ cd ${PICO_SDK_PATH}/../picoprobe
$ cd build

pico本体の「BOOTSEL」ボタンを押しながら、USBをRaspberryPiのUSB端子に接続します。
接続されたストレージデバイスを確認します。(sdb1でした)

$ dmesg | tail
略
[ 1425.543882] sd 1:0:0:0: [sdb] Attached SCSI removable disk

マウントポイント/mnt/picoは前回作成済みなので、/mnt/picoへマウントします。

$ sudo mount /dev/sdb1 /mnt/pico

内容を確認します

$ ls /mnt/pico
INDEX.HTM  INFO_UF2.TXT

picoprobe.uf2を書き込みします。

$ sudo cp picoprobe.uf2 /mnt/pico
$ sudo sync

LEDが点灯します。

マウントを解除します。

$ sudo umount /mnt/pico

ソフトを書き込みしたので、ターゲットのpicoと接続します。

OpenOCD用pico   ターゲット用pico
GND(38pin)  --->   GED(38pin)
VSYS(39pin)  --->   VSYS(39pin)(5V)
GP4(6pin)Tx  --->   Rx(GP1-2pin)
GP5(7pin)Rx  --->   Tx(GP0-1pin)
GP2(4pin)   --->   SWCLK
GP3(5pin)   --->   SWDIO

6pinのコネクタで接続しました。

参考(picoのケース)
OpenOCD用(SWD経由)pico本体はむき出しなので、お菓子のケース(MINTIAの青い半透明ケース、上下は圧入になっているので外せます。中央のリブは切断)をお菓子の取り出し口側の短て方向にニッパーで切れ込みを入れてUSBケーブルをつないだ状態にし反対側は6pinケーブルの引き出し口を切り込みし、ケースに収めました。

新規のユーザーでpicoprobeへの接続をすると、接続に失敗するので、デバイスルールを追加します。
下記ホルダーに移動します。

$ cd /etc/udev/rules.d/

新規にルールテキストを作成します。ここでは 98-picoprobe.rulesとしています。

$ sudo vim 98-picoprobe.rules
# Raspberry Pi Pico probe
ATTRS{idVendor}=="2e8a", ATTRS{idProduct}=="0004", MODE="0666"

OpenOCDを起動します。

新しい端末を開きます。
openocdを起動します。下記の例はターゲット用picoを接続していない状態です。

$ cd ${PICO_SDK_PATH}/../openocd
$ src/openocd -f interface/picoprobe.cfg -f target/rp2040.cfg -s tcl
Open On-Chip Debugger 0.10.0+dev-geb22ace-dirty (2021-02-28-13:17)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'swd'
Warn : Transport "swd" was already selected
adapter speed: 5000 kHz

Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 5000 kHz
Info : DAP init failed

ターゲット用picoを接続して、再度openocdを起動してみます。
gdbの接続待ちにになりました。端末はこのまま立ち上げた状態にします。

$ src/openocd -f interface/picoprobe.cfg -f target/rp2040.cfg -s tcl
Open On-Chip Debugger 0.10.0+dev-geb22ace-dirty (2021-02-28-13:17)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'swd'
Warn : Transport "swd" was already selected
adapter speed: 5000 kHz

Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x10000001
Info : rp2040.core0: hardware has 4 breakpoints, 2 watchpoints
Info : rp2040.core1: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections

端末を立ち上げて、gdbを立ち上げてみます。
ターゲット用ピコには前回の書き込みで例題のblinkを実行しているので、再度上書きしてみます。まず例題blinkのフォルダーに移動します。

$ cd ${PICO_SDK_PATH}/../pico-examples
$ cd build/blink

gdbを立ち上げます。その際 blink.elfを指定します。
立ち上がるとgdbのコマンド入力待ちになります。

$ gdb-multiarch blink.elf
GNU gdb (Raspbian 8.2.1-2) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from blink.elf...done.
(gdb) 

openocdに接続します。
target remote localhost:3333 を入力します。

(gdb) target remote localhost:3333
Remote debugging using localhost:3333
0x10000d76 in hardware_alarm_irq_handler ()
    at /home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_timer/timer.c:128
128	}
(gdb)

blinkを書き込みます。
loadと入力します。

(gdb) load
Loading section .boot2, size 0x100 lma 0x10000000
Loading section .text, size 0x4768 lma 0x10000100
Loading section .rodata, size 0xcfc lma 0x10004868
Loading section .binary_info, size 0x1c lma 0x10005564
Loading section .data, size 0x9dc lma 0x10005580
Start address 0x100001e8, load size 24412
Transfer rate: 13 KB/sec, 4068 bytes/write.
(gdb) 

リセットして実行します。

(gdb) monitor reset init
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
(gdb) continue
Continuing.

LEDが点滅します。

実行を強制停止します。
Ctrlキーを押しながらcキーを押します。
点滅が停止します。

^Ctarget halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00

Thread 1 received signal SIGINT, Interrupt.
time_reached (t=...)
    at /home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:109
109	    uint32_t hi_target = target >> 32u;
(gdb)

リセットします。

(gdb) monitor reset init
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ee msp: 0x20041f00
(gdb)

main()にブレイクポイント設定

(gdb) b main
Breakpoint 1 at 0x1000035c: file /home/hogehoge/pico/pico-examples/blink/blink.c, line 9.
(gdb)

実行して、main()で停止

(gdb) continue
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00

Thread 1 hit Breakpoint 1, main ()
    at /home/hogehoge/pico/pico-examples/blink/blink.c:9
9	int main() {
(gdb)

再度実行して、LED点滅

(gdb) continue
Continuing.

実行を強制停止します。
Ctrlキーを押しながらcキーを押します。
点滅が停止します。

^Ctarget halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00

Thread 1 received signal SIGINT, Interrupt.
time_reached (t=...)
    at /home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:109
109	    uint32_t hi_target = target >> 32u;
(gdb)

リモートを切ります。

(gdb) detach
Detaching from program: /home/rika/pico/pico-examples/build/blink/blink.elf, Remote target
Ending remote debugging.
[Inferior 1 (Remote target) detached]
(gdb) 

gdbを終了します。

(gdb) quit


自動でプロジェクトの雛形を作成してくれるpico-project-generatorをインストールします。
picoホルダーに移動します。

$ cd ${PICO_SDK_PATH}/../

gitで取得します。
git clone https://github.com/raspberrypi/pico-project-generator.git

$  git clone https://github.com/raspberrypi/pico-project-generator.git
Cloning into 'pico-project-generator'...
remote: Enumerating objects: 86, done.
remote: Counting objects: 100% (86/86), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 86 (delta 49), reused 47 (delta 25), pack-reused 0
Unpacking objects: 100% (86/86), done.

これで開発環境のセットアップは終了しました。


おすすめの記事