$ cd /home/pi/pico/pico-examples/hoge
$ nano ./CMakeLists.txt
add_executable(hoge
blink.c
)
# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(hoge pico_stdlib)
# create map/bin/hex file etc.
pico_add_extra_outputs(hoge)
# add url via pico_set_program_url
example_auto_set_url(hoge)
$ cd /home/pi/pico/pico-examples
$ nano ./CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
# Initialize the SDK
pico_sdk_init()
include(example_auto_set_url.cmake)
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
-Wno-maybe-uninitialized
)
# Add My Programm Dir
add_subdirectory(hoge)
CPUには独自開発の「RP2040」が採用されており、開発メインマシンはRaspberry Pi 4が推奨されています。
Pi4に接続して開発する手法が多々紹介されているのですが、公式ドキュメントを読むと、Pi4以外の他OSのPC(WindowsやLinuxやMAC)からでもデバッグ開発できるようにできる仕組みが記載されています。
すると、pico-examplesディレクトリにある全てのフォルダに対してコンパイルがかけられ、これまた全てのコンパイル結果がbuildディレクトリ下に作られます。
特定のディレクトリだけコンパイルする方法はまた次の機会に。
※$ make -j4 の「-j4」は、Pi4使ってるなら必須。
→ 「Invoking make with -j4 will run four make jobs in parallel to speed it up. A Raspberry Pi 4 has 4 cores so -j4 is a
reasonable number.」
「-j4を指定してmakeを呼び出すと、4つのmakeジョブが並行して実行され、速度が向上します。 Raspberry Pi 4には4つのコアがあるため、-j4は妥当な数。」
PicoProbe作成
無事にコンパイルができる事実を確認できましたので、手っ取り早くデバッガを仕立てます。
デバッガと書いていますが、なんのことはありません。
その正体はRaspberry Pi Picoです。
このように書くと混乱しそうですが、開発対象とは別にもう一枚Raspberry Pi Picoの基板を用意し、それをデバッガとして仕立てると言うことです。
Picoの開発を行うためにPicoを使うというのも面白いです。
Raspberry Pi PicoのBOOTボタンを押したまま、USBケーブルでパソコンと接続 USBメモリを接続したかのような画面がパソコン上に表示されるはずです。
そこに、./pico/picoprobe/build/にあるpicoprobe.uf2をドラッグ&ドロップします。
$ cd ~/pico
$ cd ./openocd
$ src/openocd -f interface/picoprobe.cfg -f target/rp2040.cfg -s tcl
すると、配線間違いなどなければ、下記のように表示されるはず。
$ 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
$ cd ~/pico/pico-examples/build/blink
$ 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)
(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)
実行!
(gdb) continue
Continuing.
Target側のPicoに実装されているLEDがチカチカ点滅したら成功です。
ステップ実行とかしてみる
ここまでで実行できたので、Breakポイントはったりステップ実行したりしてみます。
まずは、動いている状態からCtrl+cで強制終了。
(gdb) continue
Continuing.
^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/pi/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:116
116 uint32_t hi_target = (uint32_t)(target >> 32u);
(gdb)
んで、もう一度リセットして仕切り直し。
(gdb) moniter reset init
Undefined command: "moniter". Try "help".
(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()にBreakポイントを貼ります
(gdb) b main
Breakpoint 1 at 0x1000035c: file /home/pi/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/pi/pico/pico-examples/blink/blink.c:9
9 int main() {
(gdb)
nextコマンドでステップ実行してみる。
Thread 1 hit Breakpoint 1, main ()
at /home/pi/pico/pico-examples/blink/blink.c:9
9 int main() {
(gdb) next
14 gpio_init(LED_PIN);
(gdb) next
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
15 gpio_set_dir(LED_PIN, GPIO_OUT);
(gdb) next
17 gpio_put(LED_PIN, 1);
(gdb) next
18 sleep_ms(250);
(gdb) next
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000178 msp: 0x20041f00
19 gpio_put(LED_PIN, 0);
(gdb)