初识zephyr-rtos

本文介绍了Zephyr实时操作系统(RTOS),讲解了在Ubuntu 22.04环境下,针对nucleo_f401re开发板的源码下载、工具和环境依赖安装、编译及运行Demo的步骤。同时,文章探讨了Demo的简单性和如何在x86平台上运行,并预告了后续关于Zephyr程序运行机制的深入分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平台介绍:(以下仅为我自己的配置,实际支持多种平台,windows的话虽然可以支持,但还是建议在wsl上进行相关开发学习)

  1. 系统:ubuntu 22.04
  2. 开发板:nucleo_f401re
  3. $ python3 --version
    Python 3.10.12
  4. cmake --version
    cmake version 3.22.1
  5. dtc --version
    Version: DTC 1.6.1

目录

一、源码下载

        两个非常重要的网站

二、工具及环境依赖安装

三、编译一个demo程序

四、运行一个demo程序

五、demo程序分析


一、源码下载

        两个非常重要的网站

  1.  github源码
  2.  Zephyr Project Documentation — Zephyr Project Documentation

二、工具及环境依赖安装

按照官方文档的指引,安装对应的工具和依赖。Getting Started Guide — Zephyr Project Documentation

这里仅罗列重点步骤:

sudo apt update
sudo apt upgrade

wget https://apt.kitware.com/kitware-archive.sh
sudo bash kitware-archive.sh


sudo apt install --no-install-recommends git cmake ninja-build gperf \
  ccache dfu-util device-tree-compiler wget \
  python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \
  make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1

安装python环境

这里可以使用虚拟环境,也可以使用系统环境,取决于个人需求。

sudo apt install python3-venv
python3 -m venv ~/zephyrproject/.venv
source ~/zephyrproject/.venv/bin/activate

# 如果不适用虚拟环境,以上3个步骤省略

pip install west
west init ~/zephyrproject

# 这个过程时间会有点长
cd ~/zephyrproject && west update 

# 可提前更换镜像源,若安装失败请重复安装
pip install -r ~/zephyrproject/zephyr/scripts/requirements.txt

安装完成后,所得目录大概如下:

$ ls
bootloader  modules  tools  zephyr

安装SDK(重要)

cd ~
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.3/zephyr-sdk-0.16.3_linux-x86_64.tar.xz
wget -O - https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.3/sha256.sum | shasum --check --ignore-missing


tar xvf zephyr-sdk-0.16.3_linux-x86_64.tar.xz

cd zephyr-sdk-0.16.3
./setup.sh

#安装udev规则,实际可以不需要,取决于使用什么方式对目标板进行烧录
sudo cp ~/zephyr-sdk-0.16.3/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d
sudo udevadm control --reload

三、编译一个demo程序

通过编译一个简单的程序来验证环境工具是否已经安装到位。

west build -b native_posix zephyr/samples/hello_world/

输出大概如下:

-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/weymin/learning/new_zephyr/zephyr/samples/hello_world
-- CMake version: 3.22.1
-- Found Python3: /usr/bin/python3 (found suitable version "3.10.12", minimum required is "3.8") found components: Interpreter 
-- Cache files will be written to: /home/weymin/.cache/zephyr
-- Zephyr version: 3.5.0-rc3 (/home/weymin/learning/new_zephyr/zephyr)
-- Found west (found suitable version "1.1.0", minimum required is "0.14.0")
-- Board: qemu_x86
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.16.3 (/home/weymin/zephyr-sdk-0.16.3)
-- Found toolchain: zephyr 0.16.3 (/home/weymin/zephyr-sdk-0.16.3)
-- Found Dtc: /usr/bin/dtc (found suitable version "1.6.1", minimum required is "1.4.6") 
-- Found BOARD.dts: /home/weymin/learning/new_zephyr/zephyr/boards/x86/qemu_x86/qemu_x86.dts
-- Generated zephyr.dts: /home/weymin/learning/new_zephyr/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /home/weymin/learning/new_zephyr/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /home/weymin/learning/new_zephyr/build/zephyr/dts.cmake
Parsing /home/weymin/learning/new_zephyr/zephyr/Kconfig
Loaded configuration '/home/weymin/learning/new_zephyr/zephyr/boards/x86/qemu_x86/qemu_x86_defconfig'
Merged configuration '/home/weymin/learning/new_zephyr/zephyr/samples/hello_world/prj.conf'
Configuration saved to '/home/weymin/learning/new_zephyr/build/zephyr/.config'
Kconfig header saved to '/home/weymin/learning/new_zephyr/build/zephyr/include/generated/autoconf.h'
-- Found GnuLd: /home/weymin/zephyr-sdk-0.16.3/x86_64-zephyr-elf/bin/../lib/gcc/x86_64-zephyr-elf/12.2.0/../../../../x86_64-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/weymin/zephyr-sdk-0.16.3/x86_64-zephyr-elf/bin/x86_64-zephyr-elf-gcc
CMake Warning at /home/weymin/learning/new_zephyr/zephyr/subsys/random/CMakeLists.txt:12 (message):
  

      Warning: CONFIG_TIMER_RANDOM_GENERATOR is not a truly random generator.
      This capability is not secure and it is provided for testing purposes only.
      Use it carefully.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/weymin/learning/new_zephyr/build
-- west build: building application
[1/108] Preparing syscall dependency handling

[2/108] Generating include/generated/version.h
-- Zephyr version: 3.5.0-rc3 (/home/weymin/learning/new_zephyr/zephyr), build: v3.5.0-rc3-9-gb272760f062b
[108/108] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
             RAM:      106528 B        31 MB      0.33%
        IDT_LIST:          0 GB         2 KB      0.00%

四、运行一个demo程序

zephyr在west工具上有集成烧录功能,除此之外,开发者也可以自行选择烧录方法,如st-flash, CubeProgrammer,JFlash等。

除此之外,zephyr还提供了x86平台的支持,可以在x86平台上直接运行。

$ west build -t run
-- west build: running target run
[0/1] cd /home/weymin/learning/new_zephyr/build && /home/weymin/learning/new_zephyr/build/zephyr/zephyr.exe
*** Booting Zephyr OS build v3.5.0-rc3-9-gb272760f062b ***
Hello World! native_posix
^C
Stopped at 4.390s
ninja: build stopped: interrupted by user.

五、demo程序分析

在这个hello world sample中,只有一份源程序文件,且内容十分简单。

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>

int main(void)
{
	printf("Hello World! %s\n", CONFIG_BOARD);
	return 0;
}

那么它是如何运行起来的,使用zephyr不需要配置时钟了吗,不需要配置中断了吗?带着这些疑问,在后续的文章中将展开介绍。

六、调试程序

$ gdb build/zephyr/zephyr.exe

输出如下:

GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90
Copyright (C) 2022 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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 build/zephyr/zephyr.exe...
(gdb)
(gdb) r
Starting program: /home/weymin/learning/new_zephyr/build/zephyr/zephyr.exe 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0xf7bffac0 (LWP 892813)]
[New Thread 0xf73feac0 (LWP 892814)]
[New Thread 0xf69ffac0 (LWP 892815)]
*** Booting Zephyr OS build v3.5.0-rc3-9-gb272760f062b ***
Hello World! native_posix
[Thread 0xf73feac0 (LWP 892814) exited]
[Thread 0xf7bffac0 (LWP 892813) exited]

全速运行的结果。可以看到,demo程序已经跑起来了,并且得到了正确的输出。

gdb更多功能请自行查阅学习!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值