Zephyr Tutorial (1)

Space ROS Club Zephyr Tutorial (1)

目的: Zephyr RTOS の Official Document を見ながら Zephyr-based な開発の体験をする

Zephyr RTOS とは

Resources

もとになっているドキュメント

SC-OBC module A1 の Software Manual がベース

Install

Sample を試す

Hello World

west build -p -t run -b qemu_cortex_m3 zephyr/samples/hello_world

philosophers

west build -p -t run -b qemu_cortex_m3 zephyr/samples/philosophers

POSIX

west build -p -t run -b qemu_cortex_m3 zephyr/samples/posix/philosophers

C++

west build -p -t run -b qemu_cortex_m3 zephyr/samples/cpp/hello_world

Native Sim

west build -p -t run -b native_sim zephyr/samples/posix/philosophers

Rapsberry Pi Pico

west build -p -b rpi_pico zephyr/samples/hello_world

Rapsberry Pi 5

west build -p -b rpi_5 zephyr/samples/hello_world

Lチカ (Blinky)

west build -p -t run -b qemu_cortex_m3 zephyr/samples/hello_world
  • 失敗する
  • LED が無いから
    • LED が無いだけなのに、ひどいエラーがでる
    • Device Tree の問題なのだが、 Macrobatics やら黒魔術など色々言われている
    • Device Tree に関しては、次回か、次次回で
  • LED があるボードを探す

開発環境とか

基本の開発環境

  • Zephyr のビルドシステムは CMakeベース
  • でも、若干 CMake っぽくないところもある
  • CMake だけど、 GNU Make には対応していない (はず?)
  • Ninja を使います
  • Kconfig も使います

基本の West

  • West (Zephyr’s meta-tool) — Zephyr Project Documentation
  • これまで見てきた通り、 Zephyr の開発は West というツールを使う
  • プロジェクト固有の新しい道具や、やり方は、障壁になるので、ここで簡単に説明する
  • West は Zephyr の何でも屋
  • もともとは、 repo や git submodule のように複数のレポジトリ管理のために作られた
  • でも今では、 Build も Debug も SDK のインストールもこなす、 multi-purpose ツール
  • ROS の人には vcstool と colcon と ros2 と自作スクリプトが一つになった物だと思ってもらって良い

west build

  • Build するためのコマンド

  • 基本形

    west build -b board <board> path/to/project
    
  • -p --prinstine

    • ディレクトリを prinstine (原始化?ようは空っぽに) してからビルドする

    • なぜ、 --clean-before-build にしてくれなかったのかw

    • ボードを変更する時とか、ビルドディレクトリを更地にしないと正しくビルドできない

    • alternative

      rm -rf build
      west build ...
      
  • -t --target

    • ビルドターゲットの指定

    • -t usage でターゲットリストでる

      Cleaning targets:
        clean     - Remove most generated files but keep configuration and backup files
        pristine  - Remove all files in the build directory
      
      Kconfig targets:
        menuconfig - Update .config using a console-based interface
        guiconfig  - Update .config using a graphical interface
      
      Other generic targets:
        all          - Build a zephyr application
        run          - Build a zephyr application and run it if the board supports emulation
        flash        - Run "west flash"
        debug        - Run "west debug"
        debugserver  - Run "west debugserver" (or start GDB server on port 1234 for QEMU targets)
        attach       - Run "west attach"
        pahole       - Report struct padding (requires the pahole program)
        puncover     - Local web server to view RAM, ROM and stack usage (requires puncover pip module)
        ram_report   - Build and create RAM usage report
        rom_report   - Build and create ROM usage report
        footprint    - Create JSON RAM/ROM usage report in the build directory
        initlevels   - Display the initialization sequence
        boards       - Display supported boards
        shields      - Display supported shields
        usage        - Display this text
        llext-edk    - Build the Linkable Loadable Extension (LLEXT) Extension Development Kit (EDK)
        help         - Display all build system targets
      
      Build flags:
      
        ninja -v [targets] verbose build
        cmake -DW=n   Enable extra gcc checks, n=1,2,3 where
         1: warnings which may be relevant and do not occur too often
         2: warnings which occur quite often but may still be relevant
         3: more obscure warnings, can most likely be ignored
         Multiple levels can be combined with W=12 or W=123
      
      • -t run

        • QEMU なんかで実行したい時に使う
      • -t flash

        • west flash の代わり
      • -t ram_report

        • なにに RAMが使われているか、ちょっと小綺麗にだしてくれる
        Path                                                                                             Size       %  Address    Section    
        ==========================================================================================================================================
        Root                                                                                             4102 100.00%  - 
        ├── (hidden)                                                                                        2   0.05%  - 
        ├── (no paths)                                                                                   3700  90.20%  - 
        │   ├── _kernel                                                                                   288   7.02%  0x200001d4 bss
        │   ├── _thread_dummy                                                                             112   2.73%  0x20000120 bss
        │   ├── stderr                                                                                      4   0.10%  0x00002098 tbss
        │   ├── z_idle_threads                                                                            112   2.73%  0x20000040 bss
        │   ├── z_interrupt_stacks                                                                       2048  49.93%  0x20000308 noinit
        │   ├── z_main_stack                                                                             1024  24.96%  0x20000c08 noinit
        │   └── z_main_thread                                                                             112   2.73%  0x200000b0 bss
        ├── arch                                                                                            4   0.10%  - 
        │   └── arm                                                                                         4   0.10%  - 
        │       └── core                                                                                    4   0.10%  - 
        │           └── tls.c                                                                               4   0.10%  - 
        │               └── z_arm_tls_ptr                                                                   4   0.10%  0x200001b0 bss
        ├── drivers                                                                                        42   1.02%  - 
        │   ├── serial                                                                                     26   0.63%  - 
        │   │   └── uart_stellaris.c                                                                       26   0.63%  - 
        │   │       ├── __devstate_dts_ord_18                                                               2   0.05%  0x20000038 device_states
        │   │       ├── __devstate_dts_ord_19                                                               2   0.05%  0x20000036 device_states
        │   │       ├── __devstate_dts_ord_22                                                               2   0.05%  0x20000034 device_states
        │   │       ├── uart_stellaris_dev_cfg_1                                                            8   0.20%  0x2000001c datas
        │   │       ├── uart_stellaris_dev_data_0                                                           4   0.10%  0x20000024 datas
        │   │       ├── uart_stellaris_dev_data_1                                                           4   0.10%  0x20000018 datas
        │   │       └── uart_stellaris_dev_data_2                                                           4   0.10%  0x20000014 datas
        │   └── timer                                                                                      16   0.39%  - 
        │       └── cortex_m_systick.c                                                                     16   0.39%  - 
        │           ├── announced_cycles                                                                    4   0.10%  0x200001c8 bss
        │           ├── cycle_count                                                                         4   0.10%  0x200001cc bss
        │           ├── last_load                                                                           4   0.10%  0x200001d0 bss
        │           └── overflow_cyc                                                                        4   0.10%  0x200001c4 bss
        ├── kernel                                                                                        314   7.65%  - 
        │   ├── init.c                                                                                    257   6.27%  - 
        │   │   ├── z_idle_stacks                                                                         256   6.24%  0x20000b08 noinit
        │   │   └── z_sys_post_kernel                                                                       1   0.02%  0x20000300 bss
        │   ├── timeout.c                                                                                  20   0.49%  - 
        │   │   ├── announce_remaining                                                                      4   0.10%  0x200002fc bss
        │   │   ├── curr_tick                                                                               8   0.20%  0x200001a8 bss
        │   │   └── timeout_list                                                                            8   0.20%  0x2000002c datas
        │   └── timeslicing.c                                                                              37   0.90%  - 
        │       ├── pending_current                                                                         4   0.10%  0x200002f4 bss
        │       ├── slice_expired                                                                           1   0.02%  0x20000301 bss
        │       ├── slice_max_prio                                                                          4   0.10%  0x200002f8 bss
        │       ├── slice_ticks                                                                             4   0.10%  0x20000028 datas
        │       └── slice_timeouts                                                                         24   0.59%  0x20000190 bss
        └── lib                                                                                            40   0.98%  - 
            ├── libc                                                                                       32   0.78%  - 
            │   ├── common                                                                                 12   0.29%  - 
            │   │   └── source                                                                             12   0.29%  - 
            │   │       └── stdlib                                                                         12   0.29%  - 
            │   │           └── malloc.c                                                                   12   0.29%  - 
            │   │               └── z_malloc_heap                                                          12   0.29%  0x200001b8 bss
            │   └── picolibc                                                                               20   0.49%  - 
            │       └── stdio.c                                                                            20   0.49%  - 
            │           ├── __stdout                                                                       16   0.39%  0x20000004 datas
            │           └── _stdout_hook                                                                    4   0.10%  0x200001b4 bss
            ├── os                                                                                          4   0.10%  - 
            │   └── printk.c                                                                                4   0.10%  - 
            │       └── _char_out                                                                           4   0.10%  0x20000000 datas
            └── utils                                                                                       4   0.10%  - 
                └── last_section_id.c                                                                       4   0.10%  - 
                    └── last_id                                                                             4   0.10%  0x00002584 .last_section
        ==========================================================================================================================================
                                                                                                         4102
        
      • -t rom_report

        • どこにROMが使われてくれるか小綺麗に表示してくれる
        • ram_report の ROM板なので、表示は割愛
      • -t menuconfig

west flash

  • ボードにイメージを書き込む
  • OpenOCD や JLinkなど、ボードに合わせてよしなにやってくれる

west debug

  • Debugger を起動
  • VS Code や Emacs からつなぎたい場合は、 west debugserver を使う

west update

  • West module のアップデート

west list

west init

west boards

west twister

ボードの名前

RPi

❯ west boards | grep rpi 
rpi_pico
rpi_4b
rpi_pico2
rpi_5
❯ west boards -f "{name}/{qualifiers}" | grep rpi_
rpi_pico/rp2040,rp2040/w
rpi_4b/bcm2711
rpi_pico2/rp2350a/m33,rp2350a/m33/w
rpi_5/bcm2712

QEMU

❯ west boards | grep qemu
qemu_riscv32_xip
qemu_cortex_a9
qemu_rx
qemu_kvm_arm64
qemu_leon3
qemu_x86
qemu_x86_lakemont
qemu_x86_64
qemu_x86_tiny
qemu_xtensa
qemu_arc
qemu_riscv32e
qemu_cortex_r5
qemu_malta
qemu_riscv64
qemu_cortex_a53
qemu_cortex_m0
qemu_cortex_m3
qemu_riscv32
❯ west boards -f "{name}/{qualifiers}" | grep qemu_
qemu_riscv32_xip/fe310_g002
qemu_cortex_a9/xc7z007s
qemu_rx/r5f562n8
qemu_kvm_arm64/qemu_virt_arm64
qemu_leon3/leon3
qemu_x86/atom,atom/nokpti,atom/nommu,atom/nopae,atom/virt,atom/xip
qemu_x86_lakemont/lakemont
qemu_x86_64/atom,atom/nokpti
qemu_x86_tiny/atom
qemu_xtensa/dc233c,dc233c/mmu,sample_controller32,sample_controller32/mpu
qemu_arc/qemu_arc_em,qemu_arc_hs,qemu_arc_hs/xip,qemu_arc_hs5x,qemu_arc_hs6x
qemu_riscv32e/qemu_virt_riscv32e
qemu_cortex_r5/zynqmp_rpu
qemu_malta/qemu_malta,qemu_malta/be
qemu_riscv64/qemu_virt_riscv64,qemu_virt_riscv64/smp
qemu_cortex_a53/qemu_cortex_a53,qemu_cortex_a53/smp,qemu_cortex_a53/xip
qemu_cortex_m0/nrf51822
qemu_cortex_m3/ti_lm3s6965
qemu_riscv32/qemu_virt_riscv32,qemu_virt_riscv32/smp