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

## やってみること
[Zephyr(wikipedia)](https://ja.wikipedia.org/wiki/Zephyr_(%E3%82%AA%E3%83%9A%E3%83%AC%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0)) のサンプルプログラム(Hello world)を[micro:bit](https://microbit.org/)用にビルドして
動作、[VS Code](https://code.visualstudio.com/)でデバッグ方法について書きます。

## Zephyr とは

公式サイトは[ここ](https://www.zephyrproject.org/)

> 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.

だそうです。サポートしているボードの情報は[ここ(※)](https://docs.zephyrproject.org/latest/boards/index.html)
リンク先を見ると [micro:bit](https://microbit.org/) をサポートしているところが気に入りました:grinning:

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

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

[Getting Started Guide](https://docs.zephyrproject.org/latest/getting_started/index.html) の記載のとおりにすれば
環境は出来上がります。

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

```
$ west build -b bbc_microbit samples/hello_world/
```

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

```
$ west flash
```

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

![gtkterm](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/429094/28a82d60-b16d-8a34-f9ee-9bfa34aed059.jpeg)

## デバッグ

デバッガの起動も簡単で以下のコマンドを実行で 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に以下のようにします。

```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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/429094/0ec1f0d3-9adf-ed08-eda5-a4ba20f8fdeb.jpeg)