「ZephyrRTOS を改めて導入してみる」
https://qiita.com/nanbuwks/items/dbbf09c2aaff02767043
の続きです。
上記で Zephyr の SDK の一式をインストールを終えました。これから ターゲットボードである ESP32 DevkitV1 にあわせて進めていきます。
環境
- ターゲット ESP32 DevkitV1
- 母艦 Ubuntu 24.04 LTS
ESP32 用の HAL を設定
まず最初にストレージの空き容量を確認。
$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/nvme0n1p2 233G 137G 85G 62% /
以下のようにして、Wi-Fi などを使えるようにします。k
$ west blobs fetch hal_espressif
そんなに大きくストレージを使うものではありませんでした。
$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/nvme0n1p2 233G 137G 85G 62% /
ボードの指定
このページから、ボード情報を確認しようとこちらを見ようとしたらリンク切れでした。
なのでこちらから検索します。
ページ下部にリストが表示されます。
それから DOIT ESP32-DevKit-V1 を選択しました。
https://docs.zephyrproject.org/latest/boards/others/doit_esp32_devkit_v1/doc/index.html
ボードIDは doit_esp32_devkit_v1 のようです。
ビルドとエラー
$ west build -p always -b doit_esp32_devkit_v1 samples/basic/blinky
とすると、
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/owner/zephyrproject/zephyr/samples/basic/blinky
-- CMake version: 3.28.3
-- Found Python3: /home/owner/zephyrproject/.venv/bin/python3 (found suitable version "3.12.3", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: /home/owner/.cache/zephyr
-- Zephyr version: 4.3.99 (/home/owner/zephyrproject/zephyr)
-- Found west (found suitable version "1.5.0", minimum required is "0.14.0")
CMake Error at /home/owner/zephyrproject/zephyr/cmake/modules/boards.cmake:285 (message):
Board qualifiers `/esp32` for board `doit_esp32_devkit_v1` not found.
Please specify a valid board target.
Valid board targets for doit_esp32_devkit_v1 are:
doit_esp32_devkit_v1/esp32/procpu
doit_esp32_devkit_v1/esp32/appcpu
Call Stack (most recent call first):
/home/owner/zephyrproject/zephyr/cmake/modules/zephyr_default.cmake:129 (include)
/home/owner/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
/home/owner/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
CMakeLists.txt:4 (find_package)
-- Configuring incomplete, errors occurred!
FATAL ERROR: command exited with status 1: /usr/bin/cmake -DWEST_PYTHON=/home/owner/zephyrproject/.venv/bin/python3 -B/home/owner/zephyrproject/zephyr/build -GNinja -DBOARD=doit_esp32_devkit_v1 -S/home/owner/zephyrproject/zephyr/samples/basic/blinky
(.venv) owner@owner-PC-VKT16GZG6:~/zephyrproject/zephyr$
うーん? どうも、
doit_esp32_devkit_v1 ではなく、doit_esp32_devkit_v1/esp32/procpu か doit_esp32_devkit_v1/esp32/appcpu と指定しないといけないようです。
では改めて。
(.venv) $ west build -p always -b doit_esp32_devkit_v1/esp32/appcpu samples/basic/blinky
とすると、
-- west build: making build dir /home/owner/zephyrproject/zephyr/build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/owner/zephyrproject/zephyr/samples/basic/blinky
.
.
.
/home/owner/zephyrproject/zephyr/samples/basic/blinky/src/main.c
In file included from /home/owner/zephyrproject/zephyr/include/zephyr/toolchain/gcc.h:98,
from /home/owner/zephyrproject/zephyr/include/zephyr/toolchain.h:52,
from /home/owner/zephyrproject/zephyr/include/zephyr/kernel_includes.h:23,
from /home/owner/zephyrproject/zephyr/include/zephyr/kernel.h:17,
from /home/owner/zephyrproject/zephyr/samples/basic/blinky/src/main.c:8:
/home/owner/zephyrproject/zephyr/include/zephyr/device.h:96:41: error: '__device_dts_ord_DT_N_ALIAS_led0_P_gpios_IDX_0_PH_ORD' undeclared here (not in a function)
96 | #define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
| ^~~~~~~~~
/home/owner/zephyrproject/zephyr/include/zephyr/toolchain/common.h:168:26: note: in definition of macro '_DO_CONCAT'
168 | #define _DO_CONCAT(x, y) x ## y
.
.
.
/home/owner/zephyrproject/zephyr/include/zephyr/devicetree.h:236:25: note: in expansion of macro 'DT_CAT'
236 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
| ^~~~~~
/home/owner/zephyrproject/zephyr/samples/basic/blinky/src/main.c:15:19: note: in expansion of macro 'DT_ALIAS'
15 | #define LED0_NODE DT_ALIAS(led0)
| ^~~~~~~~
/home/owner/zephyrproject/zephyr/samples/basic/blinky/src/main.c:21:57: note: in expansion of macro 'LED0_NODE'
21 | static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
| ^~~~~~~~~
[45/205] Building C object zephyr/CMakeFiles/zephyr.dir/home/owner/zephyrproject/modules/hal/espressif/components/hal/mmu_hal.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/owner/zephyrproject/zephyr/build
このエラーは DevKit V1 にはオンボードのLEDが搭載されていないので、LED0がそもそも定義されていないというエラーっぽいです。
それでは、 blinky ではなく、 Hello_world で試してみます。
(.venv) $ west build -p always -b doit_esp32_devkit_v1/esp32/appcpu samples/hello_world
-- west build: making build dir /home/owner/zephyrproject/zephyr/build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/owner/zephyrproject/zephyr/samples/hello_world
-- CMake version: 3.28.3
.
.
.
[6/203] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.3.99 (/home/owner/zephyrproject/zephyr), build: v4.3.0-1368-gd02cdc734e6e
[203/203] Linking C executable zephyr/zephyr.elf
Memory region Used Size Region Size %age Used
mcuboot_hdr: 32 B 32 B 100.00%
metadata: 32 B 32 B 100.00%
FLASH: 19568 B 4194240 B 0.47%
iram0_1_seg: 16552 B 64 KB 25.26%
dram0_1_seg: 9104 B 64 KB 13.89%
IDT_LIST: 0 GB 8 KB 0.00%
Generating files from /home/owner/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: doit_esp32_devkit_v1
今度はうまくいったようです。さて、elf ファイルを書き込むにはどうしたらいいかな?
$ west flash
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner esp32
-- runners.esp32: reset after flashing requested
-- runners.esp32: Flashing esp32 chip on None (921600bps)
esptool v5.1.0
Found 32 serial ports...
Serial port /dev/ttyS9:
/dev/ttyS9 failed to connect: Could not open /dev/ttyS9, the port is busy or doesn't exist.
(Could not configure port: (5, 'Input/output error'))
Serial port /dev/ttyS8:
/dev/ttyS8 failed to connect: Could not open /dev/ttyS8, the port is busy or doesn't exist.
(Could not configure port: (5, 'Input/output error'))
.
.
.
Serial port /dev/ttyS0:
/dev/ttyS0 failed to connect: Could not open /dev/ttyS0, the port is busy or doesn't exist.
(Could not configure port: (5, 'Input/output error'))
A fatal error occurred: Could not connect to an Espressif device on any of the 32 available serial ports.
FATAL ERROR: command exited with status 2: esptool --baud 921600 --before default-reset --after hard-reset write-flash -u --flash-mode dio --flash-freq 40m --flash-size 4MB 0x2c0000 /home/owner/zephyrproject/zephyr/build/zephyr/zephyr.signed.bin
となりました。
ポート指定してもう一度。
$ west flash --esp-device /dev/ttyUSB0
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner esp32
-- runners.esp32: reset after flashing requested
-- runners.esp32: Flashing esp32 chip on /dev/ttyUSB0 (921600bps)
esptool v5.1.0
Serial port /dev/ttyUSB0:
A fatal error occurred: Could not open /dev/ttyUSB0, the port is busy or doesn't exist.
([Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0')
Hint: Check if the port is correct and ESP connected
FATAL ERROR: command exited with status 2: esptool --port /dev/ttyUSB0 --baud 921600 --before default-reset --after hard-reset write-flash -u --flash-mode dio --flash-freq 40m --flash-size 4MB 0x2c0000 /home/owner/zephyrproject/zephyr/build/zephyr/zephyr.signed.bin
(.venv) owner@owner-PC-VKT16GZG6:~/zephyrpro
おや? 調べてみたら、充電用のUSBケーブルをつないでました。ケーブルを交換して
$ west flash --esp-device /dev/ttyUSB0
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner esp32
-- runners.esp32: reset after flashing requested
-- runners.esp32: Flashing esp32 chip on /dev/ttyUSB0 (921600bps)
esptool v5.1.0
Connected to ESP32 on /dev/ttyUSB0:
Chip type: ESP32-D0WD-V3 (revision v3.1)
Features: Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency: 40MHz
MAC: 38:18:2b:98:e6:a8
Stub flasher running.
Changing baud rate to 921600...
Changed.
Configuring flash size...
Flash will be erased from 0x002c0000 to 0x002c4fff...
Wrote 32768 bytes at 0x002c0000 in 0.5 seconds (539.6 kbit/s).
Hash of data verified.
Hard resetting via RTS pin...
となって書き込めたようです。
さて、 hello メッセージはシリアル出力されるぽいです。 zephyr ではシリアルポートのモニターは qemu を使うことが多そうですが、特にqemu にこだわらずに普通にシリアルコンソールを使えばいいだけっぽいので、使い慣れた picocom を使うことにします。
$ sudo apt install picocom
として picocom をインストール。
接続すると・・・
(.venv) $ picocom /dev/ttyUSB0 -b 115200
picocom v3.1
port is : /dev/ttyUSB0
flowcontrol : none
baudrate is : 115200
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
hangup is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,
logfile is : none
initstring : none
exit_after is : not set
exit is : no
Type [C-a] [C-h] to see available commands
Terminal ready
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
ets Jul 29 2019 12:21:46
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
ets Jul 29 2019 12:21:46
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
invalid header: 0xffffffff
ets Jul 29 2019 12:21:46
.
.
.
うーん?
procpu で試す
今までのファームは doit_esp32_devkit_v1/esp32/appcpu でビルドしたのですが、
doit_esp32_devkit_v1/esp32/procpu で試してみます。
(.venv) $ west build -p always -b doit_esp32_devkit_v1/esp32/procpu samples/hello_world
としてビルド。
'/home/owner/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: /home/owner/zephyr-sdk-0.17.4/xtensa-espressif_esp32_zephyr-elf/xtensa-espressif_esp32_zephyr-elf/bin/ld.bfd (found version "2.38")
-- 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: /home/owner/zephyr-sdk-0.17.4/xtensa-espressif_esp32_zephyr-elf/bin/xtensa-espressif_esp32_zephyr-elf-gcc
-- Espressif HAL path: /home/owner/zephyrproject/modules/hal/espressif
-- Image partition address: 0x1000
-- Using ccache: /usr/bin/ccache
-- Found gen_kobject_list: /home/owner/zephyrproject/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done (5.9s)
-- Generating done (0.1s)
-- Build files have been written to: /home/owner/zephyrproject/zephyr/build
-- west build: building application
[2/218] Preparing syscall dependency handling
[3/218] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.3.99 (/home/owner/zephyrproject/zephyr), build: v4.3.0-1368-gd02cdc734e6e
[218/218] Linking C executable zephyr/zephyr.elf
Memory region Used Size Region Size %age Used
FLASH: 141 KB 4194048 B 3.44%
iram0_0_seg: 32512 B 224 KB 14.17%
dram0_0_seg: 11944 B 192 KB 6.08%
dram1_0_seg: 5 KB 96 KB 5.21%
irom0_0_seg: 13 KB 11456 KB 0.11%
drom0_0_seg: 64 KB 4 MB 1.56%
rtc_iram_seg: 0 GB 8 KB 0.00%
rtc_slow_seg: 0 GB 8 KB 0.00%
rtc_data_seg: 0 GB 8 KB 0.00%
IDT_LIST: 0 GB 8 KB 0.00%
Generating files from /home/owner/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: doit_esp32_devkit_v1
esptool v5.1.0
Creating ESP32 image...
Image has only RAM segments visible. ROM segments are hidden and SHA256 digest is not appended.
Merged 11 ELF sections.
Successfully created ESP32 image.
Memory resion とか違って表示されてますね・・・ 書き込んで
(.venv) $ west flash --esp-device /dev/ttyUSB0
-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner esp32
-- runners.esp32: reset after flashing requested
-- runners.esp32: Flashing esp32 chip on /dev/ttyUSB0 (921600bps)
esptool v5.1.0
Connected to ESP32 on /dev/ttyUSB0:
Chip type: ESP32-D0WD-V3 (revision v3.1)
Features: Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency: 40MHz
MAC: 38:18:2b:98:e6:a8
Stub flasher running.
Changing baud rate to 921600...
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00020fff...
Wrote 131072 bytes at 0x00001000 in 2.1 seconds (506.3 kbit/s).
Hash of data verified.
Hard resetting via RTS pin...
usbシリアルを見てみます。
(.venv) $ picocom /dev/ttyUSB0 -b 115200
picocom v3.1
port is : /dev/ttyUSB0
flowcontrol : none
baudrate is : 115200
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
hangup is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,
logfile is : none
initstring : none
exit_after is : not set
exit is : no
Type [C-a] [C-h] to see available commands
Terminal ready
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3ffb0000,len:6916
load:0x40080000,len:32272
entry 0x40083920
I (30) soc_init: ESP Simple boot
I (30) soc_init: compile time Dec 3 2025 00:17:38
W (30) soc_init: Unicore bootloader
I (30) soc_init: chip revision: v3.1
I (33) flash_init: SPI Speed : 40MHz
I (37) flash_init: SPI Mode : DIO
I (40) flash_init: SPI Flash Size : 4MB
I (44) boot: DRAM : lma=00001020h vma=3ffb0000h size=01b04h ( 6916)
I (50) boot: IRAM : lma=00002b2ch vma=40080000h size=07e10h ( 32272)
I (56) boot: IROM : lma=00010000h vma=400d0000h size=03400h ( 13312)
I (62) boot: DROM : lma=00020000h vma=3f400000h size=00c24h ( 3108)
I (80) boot: libc heap size 180 kB.
I (80) spi_flash: detected chip: generic
I (80) spi_flash: flash io: dio
*** Booting Zephyr OS build v4.3.0-1368-gd02cdc734e6e ***
Hello World! doit_esp32_devkit_v1/esp32/procpu
うまくいったようです。

