LoginSignup
1
1

More than 3 years have passed since last update.

Zephyr を使ってみる(microbit でhello worldのビルド、VS Codeでデバッグまで)

Posted at

やってみること

Zephyr(wikipedia) のサンプルプログラム(Hello world)をmicro:bit用にビルドして
動作、VS Codeでデバッグ方法について書きます。

Zephyr とは

公式サイトはここ

The Zephyr™ Project is a scalable real-time operating system (RTOS) supporting multiple hardware architectures, optimized for resource constrained devices, and built with safety and security in mind.

だそうです。サポートしているボードの情報はここ(※)
リンク先を見ると micro:bit をサポートしているところが気に入りました:grinning:

※ 現時点 (2019/12/08現在) 最新のバージョンは "v: 2.1.0-rc3" のようです。

環境構築からHello worldのビルドまで

Getting Started Guide の記載のとおりにすれば
環境は出来上がります。

Hello worldをmicro:bit 用にビルドするのは以下のように "-b"オプションに "bbc_microbit" をつける。

$ west build -b bbc_microbit samples/hello_world/

micro:bitへの書き込みは以下

$ west flash

gtktermなどをつないでいると、以下のように Hello worldが表示されます。

gtkterm

デバッグ

デバッガの起動も簡単で以下のコマンドを実行で GDBが起動します。

$ west debug
-- west debug: rebuilding
ninja: no work to do.
-- west debug: using runner pyocd
-- runners.pyocd: pyOCD GDB server running on port 3333
GNU gdb (crosstool-NG 1.24.0-rc2-dirty) 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 "--host=x86_64-build_pc-linux-gnu --target=arm-zephyr-eabi".
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 /home/USERNAME/zephyrproject/zephyr/build/zephyr/zephyr.elf...done.
0000894:INFO:board:Target type is nrf51
0001154:INFO:dap:DP IDR = 0x0bb11477 (v1 MINDP rev0)
0001223:INFO:ap:AP#0 IDR = 0x04770021 (AHB-AP var2 rev0)
0001350:INFO:rom_table:AP#0 ROM table #0 @ 0xf0000000 (designer=244 part=001)
0001417:INFO:rom_table:[0]<e00ff000:ROM class=1 designer=43b part=471>
0001417:INFO:rom_table:  AP#0 ROM table #1 @ 0xe00ff000 (designer=43b part=471)
0001463:INFO:rom_table:  [0]<e000e000:SCS-M0+ class=14 designer=43b part=008>
0001473:INFO:rom_table:  [1]<e0001000:DWT-M0+ class=14 designer=43b part=00a>
0001485:INFO:rom_table:  [2]<e0002000:BPU class=14 designer=43b part=00b>
0001498:INFO:rom_table:[1]<f0002000:??? class=9 designer=43b part=9a3 devtype=13 archid=0000 devid=0:0:0>
0001508:INFO:cortex_m:CPU core #0 is Cortex-M0 r0p0
0001520:INFO:dwt:2 hardware watchpoints
0001534:INFO:fpb:4 hardware breakpoints, 0 literal comparators
0001553:INFO:server:Semihost server started on port 4444
0001555:INFO:gdbserver:GDB server started on port 3333
Remote debugging using :3333
0002388:INFO:gdbserver:One client connected!
arch_cpu_idle () at /home/USERNAME/zephyrproject/zephyr/arch/arm/core/cpu_idle.S:78
78      bx lr
0002650:INFO:gdbserver:Attempting to load Argon
0002651:INFO:gdbserver:Attempting to load FreeRTOS
0002652:INFO:gdbserver:Attempting to load Zephyr
0002654:INFO:gdbserver:Attempting to load RTX5
Loading section text, size 0xa8 lma 0x0
Loading section _TEXT_SECTION_NAME_2, size 0x3046 lma 0xa8
Loading section .ARM.exidx, size 0x8 lma 0x30f0
Loading section sw_isr_table, size 0xd0 lma 0x30f8
Loading section devconfig, size 0x78 lma 0x31c8
Loading section rodata, size 0x13c lma 0x3240
Loading section datas, size 0x30 lma 0x337c
Loading section initlevel, size 0x78 lma 0x33ac
Loading section _k_mutex_area, size 0x14 lma 0x3424
[====================] 100%
0004551:INFO:loader:Erased 0 bytes (0 sectors), programmed 0 bytes (0 pages), skipped 14336 bytes (14 pages) at 7.92 kB/s
Start address 0xc0c, load size 13366
Transfer rate: 7 KB/sec, 891 bytes/write.
(gdb) 

VS Codeでデバッグ

west debug で GDBは起動しますが、VS Codeを使用したい:exclamation:

VS Codeの準備

launch.jsonに以下のようにします。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 起動",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/USERNAME/zephyrproject/zephyr/build/zephyr/zephyr.elf",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/home/USERNAME/zephyr-sdk-0.10.3/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb",
            "miDebuggerArgs": "/home/USERNAME/zephyrproject/zephyr/build/zephyr/zephyr.elf -ex 'target remote :3333' -ex 'monitor halt' -ex 'monitor reset' -ex load",
        }
    ]
}

pyocd-gdbserverの起動

pyocd-gdbserverを下記オプションで起動します。

$ pyocd-gdbserver -p 3333 -T 4444 -t nrf51

VS Codeでデバッグ開始

デバッグを開始すると、main関数で停止します。

img002.jpg

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1