《嵌入式系统 – Zephyr开发笔记》 第4章 Zephyr应用开发

在开发应用之前,先添加一个工具,tree,从名字就可以看出,这个是用于查看工程的结构,非常方便。使用以下命令安装:

#sudo apt-get update
#sudo apt-get install tree

4.1工程简介

Zephyr是基于Cmake开发的,编译系统是一个以应用程序为中心的系统,它需要一个应用程序来初始化需要编译的内核源码树。应用程序的编译会驱使应用程序和内核的配置、编译过程的发生,并将它们最终编译成一个单一的二进制文件。

Zephyr 内核的基本目录包括内核源代码、配置选项以及内核的一些预定义编译选项。
位于应用程序目录中的文件包括应用程序的预定义(例如应用程序相关的配置选项)和应用程序的源代码,最简单形式的应用程序具有以下内容:

<home\>/app
├── CMakeLists.txt
├── prj.conf
└── src
    └── main.c
  • CMakeLists.txt:此文件告诉构建系统在哪里可以找到其他应用程序文件,并将应用程序目录与Zephyr的CMake构建系统链接。该链接提供了Zephyr的构建系统支持的功能,例如特定于电路板的内核配置文件,在真实或仿真硬件上运行和调试已编译二进制文件的功能,以及更多功能。

  • 内核配置文件:应用程序通常提供一个Kconfig配置文件(通常称为prj.conf),用于指定一个或多个内核配置后选项相关的值。这些应用程序设置与特定于主板的设置合并以生成内核配置。如果被忽略,则将会使用应用程序已存在的内核配置选项值;如果应用程序没有该选项值,则将会使用内核的默认配置值。

  • 应用程序源代码文件:应用程序通常会提供一个或多个由 C 或者汇编语言编写的应用程序相关的文件。这些文件通常位于一个叫做 src 的子目录中。
    定义应用程序后,可以使用CMake创建项目文件,以便从要托管这些文件的目录中构建该文件。这称为构建目录。应用程序构建工件始终在构建目录中生成; Zephyr不支持“树内”构建。
    以下各节介绍了如何创建、构建和运行Zephyr应用程序,然后提供了更详细的参考资料。

4.2源码结构

了解Zephyr源代码树有助于查找与特定Zephyr功能关联的代码。

BevniD.png

在雨源码根目录,有几个重要的文件:
CMakeLists.txt:The top-level file for the CMake build system, containing a lot of the logic required to build Zephyr.

Kconfig:The top-level Kconfig file, which refers to the file Kconfig.zephyr also found at the top-level directory.

west.yml:The West (Zephyr’s meta-tool) manifest, listing the external repositories managed by the west command-line tool.

arch:Architecture-specific kernel and system-on-chip (SoC) code. Each supported architecture (for example, x86 and ARM) has its own subdirectory, which contains additional subdirectories for the following areas:

 architecture-specific kernel source files
 architecture-specific kernel include files for private APIs

soc:SoC related code and configuration files.

Boards:Board related code and configuration files.

Doc:Zephyr technical documentation source files and tools used to generate the https://docs.zephyrproject.org web content.

Drivers:Device driver code.

dts:devicetree source files used to describe non-discoverable board-specific hardware details.

include:Include files for all public APIs, except those defined under lib.

kernel:Architecture-independent kernel code.

lib:Library code, including the minimal standard C library.

misc:Miscellaneous code that doesn’t belong to any of the other top-level directories.

samples:Sample applications that demonstrate the use of Zephyr features.

scripts:Various programs and other files used to build and test Zephyr applications.

cmake:Additional build scripts needed to build Zephyr.

subsys:Subsystems of Zephyr, including:

 USB device stack code.
 Networking code, including the Bluetooth stack and networking stacks.
 File system code.
 Bluetooth host and controller

tests:Test code and benchmarks for Zephyr features.

share:Additional architecture independent data. Currently containing Zephyr CMake package.

4.3添加用户应用程序

开发一个新的应用程序时,您需要为应用程序创建一个目录,为应用程序源代码创建一个子目录;这种方式更容易以内核所期望的结构来组织目录和文件。

请按照以下步骤创建一个新的应用程序目录。

1.在内核的安装目录树外面创建一个新的目录。通常,这是你的工作空间目录。在控制台终端,进入一个你希望存放应用程序的目录。创建应用程序目录,输入:

#mkdir app

【警告】不支持在路径上任何位置都有空格的目录中构建Zephyr或创建应用程序。

2.建议将所有应用程序源代码放在一个名为src的子目录中。 这样可以更轻松地区分工程文件和源文件。在 app 下面创建一个源码目录:

#cd app
#mkdir src

3.将应用程序源代码放在src子目录中。 在此示例中,我们假设创建了一个名为src / main.c的文件。

4.在应用目录中创建一个名为CMakeLists.txt的文件,其内容如下:

#Find Zephyr. This also loads Zephyr's build system.
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr)
project(my_zephyr_app)

# Add your source file to the "app" target. This must come after
# find_package(Zephyr) which defines the target.
target_sources(app PRIVATE src/main.c)

find_package(Zephyr)设置编译所需的最低CMake版本,用于构建Zephyr系统,该系统会创建一个名为app的CMake目标(请参阅Zephyr CMake软件包)。

【注意】Zephyr软件包还调用cmake_minimum_required()。 这两个版本中的最新版本将由CMake强制执行。

5.设置Kconfig配置选项。 请参阅Kconfig配置。

6.配置应用程序所需的任何设备树文件。 请参阅设置设备树文件。

【注意】include($ENV {ZEPHYR_BASE} /cmake/app/boilerplate.cmake NO_POLICY_SCOPE)仍支持,以便与旧应用程序向后兼容。 在构建应用程序之前,直接将示例中的boilerplate.cmake包含在内仍需要运行源zephyr-env.sh或执行zephyr-env.cmd。

创建之后,该应用程序的目录结构是:

Bev5O1.png

4.4应用程序的预定义

你可以使用许多变量来控制Zephyr构建系统。 本节描述了每个Zephyr开发人员都应该了解的最重要的知识。

【注意】
变量BOARD,CONF_FILE和DTC_OVERLAY_FILE可以通过3种方式(按优先顺序)提供给Zephyr系统:

1.As a parameter to the west build or cmake invocation via the -D command-line switch. If you have multiple overlay files, you should use quotations, "file1.overlay;file2.overlay"
2.As Environment Variables.
3.As a set(<VARIABLE> <VALUE>) statement in your CMakeLists.txt

下列预定义变量用于配置工程:

 ZEPHYR_BASE: 构建系统使用的Zephyr基本变量。 find_package(Zephyr)将自动将此设置为缓存的CMake变量。 但是,也可以将ZEPHYR_BASE设置为环境变量,以强制CMake使用特定的Zephyr安装。
 BOARD: 选择应用程序将要运行到的开发板的默认配置。
 CONF_FILE: 配置文件的文件名,它包含用于覆盖默认配置值的 kconfig 配置值。
 DTC_OVERLAY_FILE: One or more devicetree overlay files to use. Multiple files can be separated with semicolons. See Set devicetree overlays for examples and Introduction to devicetree for information about devicetree and Zephyr.
 ZEPHYR_MODULES: A CMake list containing absolute paths of additional directories with source code, Kconfig, etc. that should be used in the application build.

4.5编译

Zephyr 的编译系统能够将应用程序的所有组件编译成一个单一的既可以运行在仿真硬件,又可以运行在真实硬件之上的应用程序镜像。

与任何其他基于CMake的系统一样,构建过程分为两个阶段。首先,在指定生成器的同时,使用cmake命令行工具生成生成文件(也称为生成系统)。该生成器确定构建系统将在第二阶段中使用的本机构建工具。第二阶段运行本机构建工具,以实际构建源文件并生成映像。要了解有关这些概念的更多信息,请参阅CMake官方文档中的CMake简介。

尽管Zephyr中的默认构建工具为west,而Zephyr的meta工具可在后台调用cmake和底层构建工具(忍者或make),但您也可以根据需要选择直接调用cmake。在Linux和macOS上,您可以在make和ninja生成器(即构建工具)之间进行选择,而在Windows上则需要使用ninja,因为该平台不支持make。为简单起见,我们将在本指南中始终使用ninja,如果您选择使用west build来构建应用程序,则应知道它默认为ninja。

举例来说,让我们为nucleo_f746zg构建app示例:

为了简单起见,我们将以samples/hello_world/例子作为用户的应用程序。首先创建~/zephyrproject/zephyr/app目录,再将hello_world的内容拷贝到 app下面。将src/main.c修改如下:

Bevv6A.png

 使用west:

#west build -p auto -b nucleo_f746zg app

 使用CMake和ninja:

##Use cmake to configure a Ninja-based buildsystem:
#cmake -B build -GNinja -DBOARD=nucleo_f746zg app

##Now run ninja on the generated build system:
#ninja -C build

 使用CMake和make:

在Linux和MAC中,也可使用cmake和make来编译。

##Use cmake to configure a Make-based buildsystem:
#cmake -B build -DBOARD=nucleo_f746zg app

##Now run ninja on the generated build system:
#make -C build

【问题解决】
如果在创建工程出现以下问题,那么删除编译路径下的所有文件,再次运行即可。
BexV6s.png

Related posts

Leave a Comment