ST Nucleo WB55RGを動かしてみた

# はじめに

2023年8月22日 IoTLT vol.102＠初台LabでいただいたMatter対応ボードが積み基板になっていました。

https://iotlt.connpass.com/event/292204/

https://x.com/Zakkiea/status/1693929512407580747/

![IMG_6031.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/72479/7d5a32f7-a14d-dff3-ed39-092478ed1cc6.jpeg)

Matterの動作確認のため、取り出して動かしてみました。

製品紹介はこちら。

https://www.st.com/ja/evaluation-tools/nucleo-wb55rg.html

# 開発環境の構築

macOSで開発環境を構築する前提で説明します。

Zephyrのスタートガイドに沿って環境を構築します。

https://docs.zephyrproject.org/latest/develop/getting_started/index.html

## 手順

Homebrewをインストールします。

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

brewコマンドで必要なソフトウェアをインストールします。

```bash
brew install cmake ninja gperf python3 ccache qemu dtc libmagic wget openocd
```

Pythonの仮想実行環境(virtualenv)を構築します。

```bash
python3 -m venv ~/zephyrproject/.venv
```

仮想実行環境を有効にします。ターミナルを起動する度に、毎回実行する必要があります。

```bash
source ~/zephyrproject/.venv/bin/activate
```

westコマンドをインストールします。

```bash
pip install west
```

Zephyrのソースコードをインストールします。

```bash
west init ~/zephyrproject
cd ~/zephyrproject
west update
```

ZephyrのアプリケーションをビルドするためのCMakeパッケージをexportします。

```bash
west zephyr-export
```

Zephyrの実行に必要なPythonモジュールをインストールします。

```bash
pip install -r ~/zephyrproject/zephyr/scripts/requirements.txt
```

Zephyr SDKをインストールします。
SDKのバージョンによってファイル名が異なります。(2024年5月18日時点の最新バージョンの例)
macOSはアーキテクチャの種類(Apple Silicon、または、intel)によってファイル名が異なります。
Apple Siliconの場合、aarch64
intelの場合、x86_64

```bash
(Apple Siliconの場合)
cd ~
curl -L -O https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/zephyr-sdk-0.16.5-1_macos-aarch64.tar.xz
curl -L https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/sha256.sum | shasum --check --ignore-missing

(intelの場合)
cd ~
curl -L -O https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/zephyr-sdk-0.16.5-1_macos-x86_64.tar.xz
curl -L https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.5-1/sha256.sum | shasum --check --ignore-missing
```

ダウンロードしたファイルを展開します。

```bash
(Apple Siliconの場合)
tar xvf zephyr-sdk-0.16.5-1_macos-aarch64.tar.xz

(intelの場合)
tar xvf zephyr-sdk-0.16.5-1_macos-x86_64.tar.xz
```

セットアップスクリプトを実行します。

```bash
cd zephyr-sdk-0.16.5-1
./setup.sh
```

# サンプルプログラムを実行する

zephyrのサンプルプログラムをビルド、インストールして実行します。

```bash
cd ~/zephyrproject/zephyr
```

## blinkyプログラム(LED2が点滅します)

```bash
west build -b nucleo_wb55rg samples/basic/blinky
```

(実行例)

```bash
% west build -b nucleo_wb55rg samples/basic/blinky 

-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /Users/kitazaki/zephyrproject/zephyr/samples/basic/blinky
-- CMake version: 3.29.2
-- Found Python3: /Users/kitazaki/zephyrproject/.venv/bin/python3.9 (found suitable exact version "3.9.12") found components: Interpreter
-- Cache files will be written to: /Users/kitazaki/Library/Caches/zephyr
-- Zephyr version: 3.3.99 (/Users/kitazaki/zephyrproject/zephyr)
-- Found west (found suitable version "1.2.0", minimum required is "0.7.1")
-- Board: nucleo_wb55rg
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.16.5 (/Users/kitazaki/zephyr-sdk-0.16.5-1)
-- Found toolchain: zephyr 0.16.5 (/Users/kitazaki/zephyr-sdk-0.16.5-1)
-- Found Dtc: /opt/homebrew/bin/dtc (found suitable version "1.7.0", minimum required is "1.4.6")
-- Found BOARD.dts: /Users/kitazaki/zephyrproject/zephyr/boards/arm/nucleo_wb55rg/nucleo_wb55rg.dts
-- Generated zephyr.dts: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/dts.cmake
Parsing /Users/kitazaki/zephyrproject/zephyr/Kconfig
Loaded configuration '/Users/kitazaki/zephyrproject/zephyr/boards/arm/nucleo_wb55rg/nucleo_wb55rg_defconfig'
Merged configuration '/Users/kitazaki/zephyrproject/zephyr/samples/basic/blinky/prj.conf'
Configuration saved to '/Users/kitazaki/zephyrproject/zephyr/build/zephyr/.config'
Kconfig header saved to '/Users/kitazaki/zephyrproject/zephyr/build/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /Users/kitazaki/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Configuring done (2.4s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/kitazaki/zephyrproject/zephyr/build
-- west build: building application
[1/156] Preparing syscall dependency handling

[3/156] Generating include/generated/version.h
-- Zephyr version: 3.3.99 (/Users/kitazaki/zephyrproject/zephyr), build: zephyr-v3.3.0-1032-g332f326ab18b
[146/156] Linking C executable zephyr/zephyr_pre0.elf

[150/156] Linking C executable zephyr/zephyr_pre1.elf

[156/156] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       15482 B       808 KB      1.87%
             RAM:        4352 B       192 KB      2.21%
           SRAM1:          0 GB        10 KB      0.00%
           SRAM2:          0 GB        20 KB      0.00%
        IDT_LIST:          0 GB         2 KB      0.00%
```

macOSとボードをUSB Micro-Bケーブルで接続します。
ボード側はST-LINKのコネクタ(CN15)に接続します。

![IMG_6033.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/72479/5b16816b-6a71-51b5-c247-1ce42bd08567.jpeg)

![IMG_6032.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/72479/5d61d02c-47f3-a3ba-d1aa-49209b8fd12f.jpeg)

ボードへ書き込みます。

```bash
west flash
```

(実行例)

```bash
 % west flash
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner openocd
-- runners.openocd: Flashing file: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.hex
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 500 kHz
Info : STLINK V2J39M27 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.252880
Info : [stm32wbx.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32wbx.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32wbx.cpu on 3333
Info : Listening on port 3333 for gdb connections
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* stm32wbx.cpu       hla_target little stm32wbx.cpu       running

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
[stm32wbx.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080010f0 msp: 0x200010c0
Info : device idcode = 0x20016495 (STM32WB5x - Rev 2.1 : 0x2001)
Info : RDP level 0 (0xAA)
Info : flash size = 1024 KiB
Info : flash mode : single-bank
Info : Padding image section 0 at 0x08003c7a with 6 bytes (bank write end alignment)
Warn : Adding extra erase range, 0x08003c80 .. 0x08003fff
auto erase enabled
wrote 15488 bytes from file /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.hex in 0.430744s (35.114 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
[stm32wbx.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001128 msp: 0x200010c0
verified 15482 bytes in 0.469174s (32.225 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
shutdown command invoked
```

![IMG_6034.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/72479/c03c8ac4-411b-7cd2-b859-f4a1b2bd8199.jpeg)


## Hello Worldプログラム(シリアルコンソールにHello Worldの文字が表示されます)

```bash
 west build -b nucleo_wb55rg samples/hello_world 
 ```

 (実行例)


 ```bash
% west build -b nucleo_wb55rg samples/hello_world 
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /Users/kitazaki/zephyrproject/zephyr/samples/hello_world
-- CMake version: 3.29.2
-- Found Python3: /Users/kitazaki/zephyrproject/.venv/bin/python3.9 (found suitable exact version "3.9.12") found components: Interpreter
-- Cache files will be written to: /Users/kitazaki/Library/Caches/zephyr
-- Zephyr version: 3.3.99 (/Users/kitazaki/zephyrproject/zephyr)
-- Found west (found suitable version "1.2.0", minimum required is "0.7.1")
-- Board: nucleo_wb55rg
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.16.5 (/Users/kitazaki/zephyr-sdk-0.16.5-1)
-- Found toolchain: zephyr 0.16.5 (/Users/kitazaki/zephyr-sdk-0.16.5-1)
-- Found Dtc: /opt/homebrew/bin/dtc (found suitable version "1.7.0", minimum required is "1.4.6")
-- Found BOARD.dts: /Users/kitazaki/zephyrproject/zephyr/boards/arm/nucleo_wb55rg/nucleo_wb55rg.dts
-- Generated zephyr.dts: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/dts.cmake
Parsing /Users/kitazaki/zephyrproject/zephyr/Kconfig
Loaded configuration '/Users/kitazaki/zephyrproject/zephyr/boards/arm/nucleo_wb55rg/nucleo_wb55rg_defconfig'
Merged configuration '/Users/kitazaki/zephyrproject/zephyr/samples/hello_world/prj.conf'
Configuration saved to '/Users/kitazaki/zephyrproject/zephyr/build/zephyr/.config'
Kconfig header saved to '/Users/kitazaki/zephyrproject/zephyr/build/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /Users/kitazaki/zephyr-sdk-0.16.5-1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Configuring done (2.4s)
-- Generating done (0.1s)
-- Build files have been written to: /Users/kitazaki/zephyrproject/zephyr/build
-- west build: building application
[1/156] Preparing syscall dependency handling

[3/156] Generating include/generated/version.h
-- Zephyr version: 3.3.99 (/Users/kitazaki/zephyrproject/zephyr), build: zephyr-v3.3.0-1032-g332f326ab18b
[146/156] Linking C executable zephyr/zephyr_pre0.elf

[150/156] Linking C executable zephyr/zephyr_pre1.elf

[156/156] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       14942 B       808 KB      1.81%
             RAM:        4352 B       192 KB      2.21%
           SRAM1:          0 GB        10 KB      0.00%
           SRAM2:          0 GB        20 KB      0.00%
        IDT_LIST:          0 GB         2 KB      0.00%
```

ボードへ書き込みます。

```bash
% west flash
```

(実行例)

```bash
% west flash
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner openocd
-- runners.openocd: Flashing file: /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.hex
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : clock speed 500 kHz
Info : STLINK V2J39M27 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.254450
Info : [stm32wbx.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32wbx.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32wbx.cpu on 3333
Info : Listening on port 3333 for gdb connections
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* stm32wbx.cpu       hla_target little stm32wbx.cpu       running

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
[stm32wbx.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08001128 msp: 0x200010c0
Info : device idcode = 0x20016495 (STM32WB5x - Rev 2.1 : 0x2001)
Info : RDP level 0 (0xAA)
Info : flash size = 1024 KiB
Info : flash mode : single-bank
Info : Padding image section 0 at 0x08003a5e with 2 bytes (bank write end alignment)
Warn : Adding extra erase range, 0x08003a60 .. 0x08003fff
auto erase enabled
wrote 14944 bytes from file /Users/kitazaki/zephyrproject/zephyr/build/zephyr/zephyr.hex in 0.422190s (34.567 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
[stm32wbx.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080010f0 msp: 0x200010c0
verified 14942 bytes in 0.454205s (32.126 KiB/s)

Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
shutdown command invoked
```

別のターミナルを開き、ボードのシリアルコンソールに接続します。

(ボードが/dev/cu.usbmodem1103として認識されている例)

```bash
screen /dev/cu.usbmodem1103 115200
```

ボードのリセットボタンを押すと、ターミナルにメッセージが表示されます。

```bash
*** Booting Zephyr OS build zephyr-v3.3.0-1032-g332f326ab18b ***
Hello World! nucleo_wb55rg
```

## エラーが表示される場合

ビルドを実行するとエラーが表示される場合があります。

```bash
ERROR: Build directory "/Users/kitazaki/zephyrproject/zephyr/build" is for application "/Users/kitazaki/zephyrproject/zephyr/samples/hello_world", but source directory "/Users/kitazaki/zephyrproject/zephyr/samples/basic/blinky" was specified; please clean it, use --pristine, or use --build-dir to set another build directory
FATAL ERROR: refusing to proceed without --force due to above error
```

別のプログラムがbuildディレクトリを使用しているためです。
一度、buildディレクトリを削除してからビルドを再度実行します。

```bash
rm -rf build
```

または、"-p always"オプションをつけてビルドを実行します。

```bash
west build -p always -b nucleo_wb55rg samples/hello_world

west build -p always -b nucleo_wb55rg samples/basic/blinky
```