1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[メモ] Zephyr RTOSのHello WorldビルドをYoctoにて

Last updated at Posted at 2025-07-14

概要

  • Yoctoで、Zephyr のサンプル Hello Worldをqemu-cortex-m3の構成でビルド、QEMUで実行。
  • ビルド環境は とりあえず、Docker にて

環境

  • Host: Ubuntu 24.04 (x86_64)
    • Docker version 28.3.2, build 578ccf6
    • 作業フォルダ: /home/$USER/zephyr-yocto/

参考

手順

  1. 作業フォルダの準備

    # Workフォルダ作成
    mkdir -p ~/zephyr-yocto/docker
    cd $_
    
  2. Dockerfile作成

    # Dockerfile作成
    cat << 'EOF' > Dockerfile
    FROM ubuntu:24.04
    
    ARG DEBIAN_FRONTEND=noninteractive
    ARG http_proxy
    ARG ID_USER=1001
    ARG ID_GROUP=1000
    ARG ID_GROUP_NAME="build" 
    #ARG GROUP_NAME=$(grep :1000: /etc/group | cut -d ':' -f 1) 
    ARG GIT_USER_NAME=Build
    ARG GIT_USER_EMAIL=build@example.com
    
    RUN \
            apt-get update && \
            apt-get install -yq build-essential chrpath cpio debianutils \
                            diffstat file gawk gcc git iputils-ping libacl1 liblz4-tool \
                            locales python3 python3-git python3-jinja2 python3-pexpect python3-pip \
                            python3-subunit socat texinfo unzip wget xz-utils zstd \
                            ubuntu-server-minimal && \
                            rm -rf /var/lib/apt-lists/*
    
    RUN sed -i -e 's/^GID_MAX.*/GID_MAX 400000000/g' -e 's/^UID_MAX.*/UID_MAX 400000000/g' /etc/login.defs
    
    # Remove User `ubuntu` (Ubuntu 24.04 container)
    RUN userdel -f ubuntu && groupadd ${ID_GROUP_NAME} -g ${ID_GROUP}
    RUN useradd -ms /bin/bash -p build build -u ${ID_USER} -g ${ID_GROUP} && \
            usermod -aG sudo build && \
            echo "build:build" | chpasswd && \
            echo -e "build ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/build
    
    RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
        locale-gen
    
    ENV LANG=en_US.utf8
    
    USER build
    WORKDIR /home/build
    RUN git config --global user.email "${GIT_USER_EMAIL}" && git config --global user.name "${GIT_USER_NAME}"
    EOF
    
  3. Docker build

    docker build   --no-cache --build-arg ID_USER=$(id -u)   --build-arg ID_GROUP=$(id -g)   --build-arg GIT_USER_NAME="\"$(git config user.name)\""   --build-arg GIT_USER_EMAIL="$(git config user.email)"   -t yoctocontainer .
    
    
    # apt cacherとかいれてたら、http_proxyを設定すると良い
    # docker build   --no-cache   --build-arg http_proxy=http://10.0.0.10:3142   --build-arg ID_USER=$(id -u)   --build-arg ID_GROUP=$(id -g)   --build-arg GIT_USER_NAME="\"$(git config user.name)\""   --build-arg GIT_USER_EMAIL="$(git config user.email)"   -t yoctocontainer .
    
  4. docker run

    # 作業フォルダに移動して
    cd ~/zephyr-yocto
    # 
    docker run --user $(id -u):$(id -g) --device=/dev/kvm:/devb/kvm --device=/dev/net/tun:/dev/net/tun -e MACHINE=qemu-cortex-m3 -e  DISTRO="zephyr" -p 1234:1234 --cap-add NET_ADMIN --hostname buildserver -it --rm -v `pwd`:/home/build/workdir -e WORKDIR=/home/build/workdir -w /home/build/workdir yoctocontainer
    

    --- Docker yoctocontainerコンテナ内にて ---

  5. 環境設定(source poky/oe-init-build-env)

    1. 初回時実行

      export MACHINE=qemu-cortex-m3
      export DISTRO="zephyr"
      #branch=scarthgap #5.0.x
      branch=walnascar #5.2.x python 3.10+ g++Ver.10+
      
      mkdir ${DISTRO}-${branch}
      cd ${DISTRO}-${branch}
      
      git clone -b ${branch} --recursive --depth 1 https://git.yoctoproject.org/meta-zephyr/ && \
      git clone -b ${branch} --recursive --depth 1 https://git.yoctoproject.org/poky && \
      git clone -b ${branch} --recursive --depth 1 https://git.openembedded.org/meta-openembedded
      
      source poky/oe-init-build-env
      
      bitbake-layers add-layer ../meta-openembedded/meta-oe
      bitbake-layers add-layer ../meta-openembedded/meta-python
      bitbake-layers add-layer ../meta-zephyr/meta-zephyr-core
      bitbake-layers add-layer ../meta-zephyr/meta-zephyr-bsp
      
      mkdir -p ../{downloads,sstate-cache}
      ln -sf ../downloads .
      ln -sf ../sstate-cache/ .
      
    2. 2回目以降

          export MACHINE=qemu-cortex-m3
          export DISTRO="zephyr"
          #branch=scarthgap #5.0.x
          branch=walnascar #5.2.x python 3.10+ g++Ver.10+
      
          cd ${DISTRO}-${branch}
      
          source poky/oe-init-build-env
      
  6. ビルド

    # helloworld sample
    bitbake zephyr-helloworld
    
    # philosophers sample
    # bitbake zephyr-philosophers
    
  7. QEMUにて、実行

    • CTRL-a + x にて、QEMU終了
    runqemu qemuparams='-serial mon:stdio'
    

実行例

  • bitbake zephyr-helloworld

    build@buildserver:~/workdir/zephyr-walnascar/build$ time bitbake zephyr-helloworld
    Loading cache: 100% |###########################################| Time: 0:00:02
    Loaded 4569 entries from dependency cache.
    NOTE: Resolving any missing task queue dependencies
    
    Build Configuration:
    BB_VERSION           = "2.12.0"
    BUILD_SYS            = "x86_64-linux"
    NATIVELSBSTRING      = "universal"
    TARGET_SYS           = "arm-yocto-eabi"
    MACHINE              = "qemu-cortex-m3"
    DISTRO               = "zephyr"
    DISTRO_VERSION       = "1.0"
    TUNE_FEATURES        = "armv7m cortexm3"
    TARGET_FPU           = "soft"
    meta
    meta-poky
    meta-yocto-bsp       = "walnascar:41038342a471b4a8884548568ad147a1704253a3"
    meta-oe
    meta-python          = "walnascar:c009244a045923a9dfc32d7f2996cb61629870f6"
    meta-zephyr-core
    meta-zephyr-bsp      = "walnascar:3617fcdfd0f232dcaff4a153e667c26445b2077c"
    
    ...
    ...
    real    49m26.382s
    user    0m38.956s
    sys     0m3.261s
    build@buildserver:~/workdir/zephyr-walnascar/build$ cat /proc/cpuinfo  | grep 'model name'| sort -u
    model name      : 11th Gen Intel(R) Core(TM) i5-1155G7 @ 2.50GHz
    build@buildserver:~/workdir/zephyr-walnascar/build$
    
  • runqemu qemuparams='-serial mon:stdio

    • ctrl-a + xにて、QEMU: Terminated
    build@buildserver:~/workdir/zephyr-walnascar/build$ runqemu qemuparams='-serial mon:stdio'
    runqemu - INFO - Running MACHINE=qemu-cortex-m3 bitbake -e  ...
    runqemu - INFO - Continuing with the following parameters:
    KERNEL: [/home/build/workdir/zephyr-walnascar/build/tmp/deploy/images/qemu-cortex-m3/zephyr-helloworld.elf]
    MACHINE: [qemu-cortex-m3]
    FSTYPE: [elf]
    ROOTFS: [/home/build/workdir/zephyr-walnascar/build/tmp/deploy/images/qemu-cortex-m3/zephyr-helloworld-image-qemu-cortex-m3.elf]
    CONFFILE: [/home/build/workdir/zephyr-walnascar/build/tmp/deploy/images/qemu-cortex-m3/zephyr-helloworld-image-qemu-cortex-m3-20250714212511.qemuboot.conf]
    
    runqemu - INFO - Running /home/build/workdir/zephyr-walnascar/build/tmp/work/x86_64-linux/qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/qemu-system-arm -nic none   -icount shift=6,align=off,sleep=off -rtc clock=vm  -machine lm3s6965evb -cpu cortex-m3  -m 256  -serial mon:stdio -serial null -serial mon:vc -display sdl,show-cursor=on -nographic -vga none -kernel /home/build/workdir/zephyr-walnascar/build/tmp/deploy/images/qemu-cortex-m3/zephyr-helloworld.elf -append '  mem=256M swiotlb=0 '
    
    runqemu - INFO - Host uptime: 530480.25
    
    *** Booting Zephyr OS build v4.1.0 ***
    Hello World! qemu_cortex_m3/ti_lm3s6965
    QEMU: Terminated
    runqemu - INFO - Cleaning up
    runqemu - INFO - Host uptime: 530482.22
    
    build@buildserver:~/workdir/zephyr-walnascar/build$
    

TODO?

  • ユーザプログラムの追加方法
  • デバッグ (gdb)
    • gdb-multiarch
      gdb-multiarch ./tmp/deploy/images/qemu-cortex-m3/zephyr-helloworld-image-qemu-cortex-m3.elf`
      # on GDB
      set substitute-path /home/build/workdir/zephyr-walnascar/build /home/mt08/zephyr-yocto/zephyr-walnascar/build
      target remote :1234
      b main
      c
      
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?