Blog:
通过 Yocto Project 定制嵌入式 Yocto Linux 镜像

Thursday, June 23, 2022

简介

嵌入式设备采用Embedded Linux操作系统进行开发已经越来越成为主流,但是如何将开发完成的Linux uboot/kernel配置,以及应用程序整合到Embedded Linux镜像中以便在量产的时候更方便的更新到嵌入式设备中可能是很多嵌入式开发者面临的挑战之一,而本文就以在Embedded Linux中使用比较广泛的Yocto project 为例,基于Openembedded开发框架定制嵌入式 Linux镜像。

本文所演示的平台来自于Toradex Colibri iMX8x嵌入式平台,这个平台是基于近年发布的NXP iMX8x系列ARM处理器,核心为Cortex-A35。同时Toradex也提供了基于Yocto project/Poky 编译发布的嵌入式Yocto Linux Demo镜像,这样就无需完全从头开始配置自己的layer,只需要修改添加需要的部分即可。

准备

a). Colibri iMX8x ARM核心版配合Colibri Eva Board 载板,并连接7inch Display,用于测试编译好的Yocto Linux image。

b). 配置Yocto Project/Openembedded开发环境,请参考这里说明,本文测试使用目前最新的分支dunfell-5.x.y。

c). 参考这里说明下载Toradex Yocto Linux BSP 5.x.y版本Kernel和Uboot源代码,用于产生相应的patch文件。

c). 本文演示的定制Yocto Linux镜像主要添加修改以下内容,下面会按照这个内容逐一说明:

./ 修改Uboot默认环境变量,适配Splash开机启动画面,关于Splash启动画面的详细修改说明可以参考这里说明;修改后续要默认启动加载的定制化device tree命名 imx8qxp-colibri-customer-v1.dtb

./ 修改Linux Kernel,增加并使能定制化Splash开机启动画面图片;同时修改Linux device tree文件,使能Colibri iMX8x自带的两路FlexCAN驱动并关闭默认打开的载板SPI CAN驱动,并保存为定制化device tree文件imx8qxp-colibri-customer-v1.dtb;最后配置默认启动适配7inch Display的Device Tree Overlay文件 

./ 将演示Qt应用程序集成到Image,并配置为开机自动启动

创建定制Yocto Linux 相关配置Layer

a). 在配置下载好的OpenEmbedded环境Layer目录下,由于Toradex默认已经包含meta-toradex-demos layer用于Toradex Yocto Linux Demo Image编译,我们这里直接基于这个layer创建用于定制化的Layer – meta-customer-demos.

### create customer layer meat-customer-demos ###
$ cd cd .../oe-core/layer
$ cp -r meta-toradex-demos/ meta-customer-demos


b). 修改meta-customer-demos目录下的 conf/layer.conf文件,适配新的layer命名,同时include machine配置文件

diff --git a/conf/layer.conf b/conf/layer.conf
index 954e4f8..ab8afb6 100644
--- a/conf/layer.conf
+++ b/conf/layer.conf
@@ -1,12 +1,15 @@
+# Include extra machine setting
+include conf/machine/${MACHINE}-extra.conf
+
# We have a conf and classes directory, append to BBPATH
BBPATH .= ":${LAYERDIR}"

# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb ${LAYERDIR}/recipes-*/*/*.bbappend"

-BBFILE_COLLECTIONS += "toradex-demos"
-BBFILE_PATTERN_toradex-demos = "^${LAYERDIR}/"
-BBFILE_PRIORITY_toradex-demos = "24"
+BBFILE_COLLECTIONS += "customer-demos"
+BBFILE_PATTERN_customer-demos = "^${LAYERDIR}/"
+BBFILE_PRIORITY_customer-demos = "24"

# Let us add layer-specific bbappends which are only applied when that
# layer is included in our configuration
@@ -16,11 +19,11 @@ BBFILES += "${@' '.join('${LAYERDIR}/%s/recipes*/*/*.bbappend' % layer \
BBFILES += "${@' '.join('${LAYERDIR}/%s/recipes*/*/*.bb' % layer \
              for layer in BBFILE_COLLECTIONS.split())}"

-LAYERDEPENDS_toradex-demos = " \
+LAYERDEPENDS_customer-demos = " \
   core \
   yocto \
   openembedded-layer gnome-layer multimedia-layer networking-layer \
   freescale-layer \
   freescale-distro \
"
-LAYERSERIES_COMPAT_toradex-demos = "zeus dunfell"
+LAYERSERIES_COMPAT_customer-demos = "zeus dunfell

c). 在meta-customer-demos/conf 目录下创建 machine/colibri-imx8x-extra.conf文件,为设备增加新的定制化设备树文件

$ mkdir -p .../oe-core/layers/meta-customer-demos/conf/machine
$ vi .../oe-core/layers/meta-customer-demos/conf/machine/colibri-imx8x-extra.conf
KERNEL_DEVICETREE_append = " \
  freescale/imx8qxp-colibri-customer-v1.dtb \
"

OpenEmbedded下Uboot修改示例

a). 为了后续配置Splash开机画面正常显示,需要修改 U-boot 环境变量去除显示输出的 console 打印输出。

b). 首先进入下载的Uboot源代码对 include/configs/colibri_imx6.h 文件进行修改,产生相应的patch文件

0001-uboot-env-update.patch

diff --git a/include/configs/colibri-imx8x.h b/include/configs/colibri-imx8x.h
index 2c6a88de80..e2c1b28e31 100644
--- a/include/configs/colibri-imx8x.h
+++ b/include/configs/colibri-imx8x.h
@@ -94,7 +94,7 @@
      "console=ttyLP3,115200 earlycon=lpuart32,0x5a090000,115200\0" \
      "fdt_addr=0x83000000\0" \
      "fdt_high=\0" \
-       "fdt_board=eval-v3\0" \
+       "fdt_board=customer-v1\0" \
      "finduuid=part uuid mmc ${mmcdev}:2 uuid\0" \
      "image=Image\0" \
      "initrd_addr=0x83800000\0" \
@@ -109,6 +109,7 @@
              "setexpr blkcnt ${filesize} + 0x1ff && setexpr blkcnt " \
              "${blkcnt} / 0x200; mmc dev 0 1; mmc write ${loadaddr} 0x0 " \
              "${blkcnt}; fi\0" \
+       "setupargs=vt.global_cursor_default=0 consoleblank=0 console=${console}\0" \
      "vidargs=video=imxdpufb5:off video=imxdpufb6:off video=imxdpufb7:off\0"

/* Link Definitions */

c). 在 meta-customer-demos layer目录下创建 recipes-bsp/u-boot/files目录,并对Colibri iMX8x默认使用的 u-boot-toradex_2020.04.bb 文件进行append,集成上面生成的patch

### prepare patch file ###
$ mkdir -p recipes-bsp/u-boot/files
$ cd .../recipes-bsp/u-boot/
$ cp .../0001-uboot-env-update.patch files/
### crteate bbappend file ###
$ vi u-boot-toradex_2020.04.bbappend
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"

SRC_URI_append = " \
  file://0001-uboot-env-update.patch \

d). 另外这里也提示下,U-boot环境变量的修改也可以通过修改Yocto编译生成的Image压缩包文件里面的 ” u-boot-initial-env-sd ” 文件来更改,这样通过Toradex Easy Installer更新到模块后就是修改后的变量配置了,对于某些场景可能更方便一些,不一定需要上述在Yocto编译环境通过修改源码的方式进行修改。

OpenEmbedded下Linux kernel 和 device tree 修改示例

a). 参考上述 ”准备“ 章节提到的Splash启动画面文章生成Logo文件 “logo_custom_clut224.ppm ” 和menuconfig修改后的Kernel 配置文件 ”.config”,并重命名为 ”defconfig”

→ Device Drivers → Graphics support → Bootup logo
<*>   Custom 224-color Linux logo

b). 创建定制化Device Tree文件 ”imx8x-colibri-customer-v1.dtsi” 和 “imx8qxp-colibri-customer-v1.dts” ,用于关闭Colibri iMX8x默认使能的Colibri Eva Board的板载MCP2515 CAN控制器,同时使能Imx8x芯片自带的两路FlexCAN接口。

./ “imx8x-colibri-customer-v1.dtsi” 相对于默认的 “imx8x-colibri-eval-v3.dtsi” 文件改动如下

--- arch/arm64/boot/dts/freescale/imx8x-colibri-eval-v3.dtsi        2022-06-16 17:38:34.293724009 +0800
+++ arch/arm64/boot/dts/freescale/imx8x-colibri-customer-v1.dtsi     2022-06-20 17:49:30.735567256 +0800
@@ -88,7 +88,7 @@
                interrupt-parent = <&lsio_gpio3>;
                interrupts = <13 IRQ_TYPE_EDGE_FALLING>;
                spi-max-frequency = <10000000>;
-                 status = "okay";
+                status = "disabled";
      };
      /* To keep the CAN controller enabled by default,
       * disable conflicting spidev. This spidev device
@@ -182,3 +182,24 @@
&vpu_lpcg {
      status = "okay";
};
+
+&flexcan1 {
+        status = "okay";
+};
+
+&flexcan2 {
+        status = "okay";
+};
+
+&iomuxc {
+        //pinctrl-names = "default";
+        //pinctrl-0 = <&pinctrl_lpuart0>;
+        colibri-imx8qxp {
+        pinctrl_lpuart0: lpuart0grp {
+                        fsl,pins = <
+                                IMX8QXP_UART0_RX_ADMA_UART0_RX                  0x06000020      /* SODIMM  36 */
+                                IMX8QXP_UART0_TX_ADMA_UART0_TX                  0x06000020      /* SODIMM  38 */
+                        >;
+                };
+        };
+};

./ “imx8qxp-colibri-customer-v1.dts” 相对于默认的 “imx8qxp-colibri-eval-v3.dts” 文件改动如下:

--- arch/arm64/boot/dts/freescale/imx8qxp-colibri-eval-v3.dts     2022-06-16 17:38:34.289723980 +0800
+++ arch/arm64/boot/dts/freescale/imx8qxp-colibri-customer-v1.dts  2022-06-20 17:45:04.354142165 +0800
@@ -6,11 +6,11 @@
/dts-v1/;

#include "imx8qxp-colibri.dtsi"
-#include "imx8x-colibri-eval-v3.dtsi"
+#include "imx8x-colibri-customer-v1.dtsi"

/ {
-        model = "Toradex Colibri iMX8QXP on Colibri Evaluation Board V3";
-        compatible = "toradex,colibri-imx8x-eval-v3",
+       model = "Toradex Colibri iMX8QXP on Customer Carrier Board V1";
+       compatible = "toradex,colibri-imx8x-customer-v1",
                     "toradex,colibri-imx8x",
                     "fsl,imx8qxp";
};


./ 修改 Makefile 文件,增加新的定制化Device Tree文件

--- a/arch/arm64/boot/dts/freescale/Makefile
+++ b/arch/arm64/boot/dts/freescale/Makefile
@@ -174,6 +174,8 @@ dtb-$(CONFIG_ARCH_MXC) += imx8qxp-mek.dtb imx8qxp-mek-dsp.dtb imx8qxp-mek-ov5640
                        imx8dx-colibri-aster.dtb \
                        imx8qxp-colibri-eval-v3.dtb \
                        imx8dx-colibri-eval-v3.dtb \
+                         imx8qxp-colibri-customer-v1.dtb \
+                          imx8dx-colibri-customer-v1.dtb \
                        imx8qxp-colibri-iris.dtb \
                        imx8dx-colibri-iris.dtb \
                        imx8dx-colibri-iris-v2.dtb \


创建完成后,通过 ”git add” 命令添加两个新文件,然后通过 “git diff” 命令可以生成用于Yocto编译集成的patch文件,命名为 ”0001-imx8qxp-colibri-customer-v1.patch”

c). 在 meta-customer-demos layer目录下创建 recipes-kernel/linux目录,并对 linux-toradex_5.4-2.3.x.bb 文件进行append,同时创建files目录,存入defconfig、0001-imx8qxp-colibri-customer-v1.patch和 logo_custom_clut224.ppm三个文件以适配Splash图片和Device Tree的修改

### prepare patch file ###
$ mkdir -p recipes-kernel/linux/files
$ cd .../recipes-kernel/linux/
$ cp .../defconfig files/
$ cp .../logo_custom_clut224.ppm files/
$ cp .../0001-imx8qxp-colibri-customer-v1.patch files/
### crteate bbappend file ###
$ vi linux-toradex_5.4-2.3.x.bbappend
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"

SRC_URI_append = " \
  file://0001-imx8qxp-colibri-customer-v1.patch \
  file://defconfig \
  file://logo_custom_clut224.ppm \
"

do_configure_prepend() {
  # logo support, if you supply logo_custom_clut224.ppm in SRC_URI, then it's going to be used
  if [ -e ${WORKDIR}/logo_custom_clut224.ppm ]; then
      install -m 0644 ${WORKDIR}/logo_custom_clut224.ppm ${S}/drivers/video/logo/logo_custom_clut224.ppm
  fi
}

d). 这里提示下在OpenEmbedded环境下,也可以通过下面命令在编译系统中直接修改Kernel 配置,不过由于不如上述patch方式维护方便,只应临时测试使用,不建议实际部署编译时候用这样的方式修改。

$ bitbake -c menuconfig virtual/kernel


OpenEmbedded下Linux rootfs 文件系统中配置文件修改示例

a). 在 meta-customer-demos/recipes-kernel/linux目录下创建device-tree-overlays_git.bbappend文件,以完成Splash开机画面文章中章节7中对于显示分辨率的适配

$ cd meta-customer-demos/recipes-kernel/linux/
$ vi device-tree-overlays_git.bbappend
TEZI_EXTERNAL_KERNEL_DEVICETREE_BOOT = " \
      colibri-imx8x_parallel-rgb_overlay.dtbo \
      display-lt161010_overlay.dtbo \
"

b). 在 meta-customer-demos layer目录下创建 recipes-graphics/wayland 目录,并对 weston-init.bb 文件进行append,同时创建files目录,存入如下Patch文件以完成Splash开机画面文章中章节9.a中对Weston桌面显示的修改配置

### create bbappend file ###
$ cd meta-customer-demos
$ mkdir -p recipes-graphics/wayland/
$ cd recipes-graphics/wayland/
$ vi weston-init.bbappend
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"

SRC_URI_append = " \
  file://0001-weston-env.patch  \
"
### create patch file ###
$ mkdir files
$ cd files
$ vi 0001-weston-env.patch
--- a/weston.ini        2022-04-29 13:28:14.445478543 +0800
+++ b/weston.ini        2022-06-21 16:47:00.794980329 +0800
@@ -4,8 +4,10 @@
#use-g2d=1
#xwayland=true

-#[shell]
+[shell]
#size=1920x1080
+panel-position=none
+background-color=0x00FFFFFF

#[output]
#name=HDMI-A-1

7). OpenEmbedded下部署Qt应用示例

a). 本文所使用的Qt 应用demo说明请参考下面文章:

http://blog.sina.com.cn/s/blog_d733e5170102wyay.html 

b). Yocto Linux Multimedia Demo Image在Colibri iMX8x模块上面默认启动的是Qt Cinema示例应用

./ Qt Cinema 示例应用bb文件,包括编译应用源码以及安装启动脚本

.../oe-core/layers/meta-qt5/recipes-qt/examples/cinematicexperience_1.0.bb


./ wayland app launch bb文件,通过设定wayland-app-launch systemd服务启动项目,来达到开机自动启动的目的

.../oe-core/layers/meta-toradex-demos/recipes-graphics/wayland-app-launch/wayland-qtdemo-launch-cinematicexperience_1.0.bb


c). 基于上面示例,我们同样方法配置Qt HelloworldUI 示例demo的编译、安装和自动启动

./ 在 meta-customer-demos layer目录下创建 qt5-layer/recipes-qt/qtdemo目录,并创建如下 helloworldUI_0.1.bb 文件用于从Qt 应用的git地址下载应用并编译部署

$ mkdir -p .../oe-core/layers/meta-customer-demos/qt5-layer/recipes-qt/qtdemo
$ cd .../oe-core/layers/meta-customer-demos/qt5-layer/recipes-qt/qtdemo/
$ vi helloworldUI_0.1.bb
SUMMARY = "Qt5 demo application deployment"
DESCRIPTION = "Qt5 GPIO widget demo applicaiton"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
PR = "r0"

SRC_URI = "git://github.com/simonqin09/QtGPIOProject.git;protocol=git;branch=master"
SRCREV = "efac8dfeb878f104dfcfcf54188b25d024f2f16b"

S = "${WORKDIR}/git/helloworldUI"

DEPENDS = "qtbase"
RDEPENDS_${PN} = "qtbase-plugins"

require recipes-qt/qt5/qt5.inc

do_install() {
 
install -d ${D}${datadir}/${P}

  install -m 0755 ${B}helloworldUI ${D}${datadir}/${P}
}

FILES_${PN} += "${datadir}"


./ 创建对应的wayland app launch bb文件 

$ vi .../oe-core/layers/meta-toradex-demos/recipes-graphics/wayland-app-launch/wayland-qtdemo-launch-helloworldUI_1.0.bb
# set the following variable to your one and only application which should
# be launched right after weston started

INITIAL_APP_PKGS ?= "helloworldUI qtwayland"
INITIAL_PATH ?= ""
APPLICATION_ENVIRONMENT ?= '\"QT_QPA_PLATFORM=wayland-egl\"'
WAYLAND_APPLICATION ?= "/usr/share/helloworldUI-1.0/helloworldUI --fullscreen"

require wayland-app-launch.inc

OpenEmbedded下设置自定义image镜像编译

a). 在 meta-customer-demos/recipes-images/images/ 目录下创建定制化Yocto Linux Image BB文件,基于Toradex默认的Multimeida Demo Image。如果需要修改Image里面包含的组件也可以直接在这个文件里面修改。

### create bbfile ###
$ cd .../oe-core/layers/meta-customer-demos/recipes-images/images
$ cp tdx-reference-multimedia-image.bb customer-multimedia-image.bb
#### modify bbfile ###
--- recipes-images/images/tdx-reference-multimedia-image.bb     2022-06-16 16:48:23.435865928 +0800
+++ recipes-images/images/customer-multimedia-image.bb  2022-06-22 17:31:12.938956311 +0800
@@ -1,12 +1,12 @@
require tdx-reference-minimal-image.bb

-SUMMARY = "Toradex Embedded Linux Reference Multimedia Image"
+SUMMARY = "Customer Embedded Linux Reference Multimedia Image"
DESCRIPTION = "Image for BSP verification with QT and multimedia features"

inherit populate_sdk_qt5

#Prefix to the resulting deployable tarball name
-export IMAGE_BASENAME = "Reference-Multimedia-Image"
+export IMAGE_BASENAME = "Customer-Multimedia-Image"

SYSTEMD_DEFAULT_TARGET = "graphical.target"

@@ -16,7 +16,7 @@
                                                      '', d), d)} \
"

-APP_LAUNCH_WAYLAND ?= "wayland-qtdemo-launch-cinematicexperience"
+APP_LAUNCH_WAYLAND ?= "wayland-qtdemo-launch-helloworldui"
APP_LAUNCH_WAYLAND_colibri-imx6ull ?= "wayland-qtdemo-launch-analogclock"
APP_LAUNCH_WAYLAND_colibri-imx6ull-emmc ?= "wayland-qtdemo-launch-analogclock"
APP_LAUNCH_WAYLAND_colibri-imx7 ?= "wayland-qtdemo-launch-analogclock"

b). 至此,meta-customer-demos layer 的所有修改都已经完成,下面我们进入 OpenEmbedded编译环境下 build/conf 目录, 对下面两个文件进行修改:

./ bblayers.conf – 用定制化的meta-customer-demos layer替换默认的meta-toradex-demos layer

--- a/build/conf/bblayers.conf  2022-06-22 18:00:46.163360544 +0800
+++ b/build/conf/bblayers.conf  2022-06-16 16:51:46.159966787 +0800
@@ -23,7 +23,7 @@
 ${TOPDIR}/../layers/meta-openembedded/meta-multimedia \
 ${TOPDIR}/../layers/meta-openembedded/meta-python \
 ${TOPDIR}/../layers/meta-freescale-distro \
-  ${TOPDIR}/../layers/meta-toradex-demos \
+  ${TOPDIR}/../layers/meta-customer-demos \
 ${TOPDIR}/../layers/meta-qt5 \
 \
 \

./ local.conf – 将默认的machine 修改为 colibri-imx8x, 同样也include设备额外的conf文件,最后增加了License声明,如果需要一些额外的包和修改也可以在这个文件下修改

--- a/build/conf/local.conf     2022-06-22 18:04:33.959385766 +0800
+++ b/build/conf/local.conf     2022-06-22 18:05:55.091394476 +0800
@@ -27,7 +27,7 @@
#MACHINE ?= "colibri-imx6ull-emmc"
#MACHINE ?= "colibri-imx7"
#MACHINE ?= "colibri-imx7-emmc"
-#MACHINE ?= "colibri-imx8x"
+MACHINE ?= "colibri-imx8x"
#
#MACHINE ?= "verdin-imx8mm"
#MACHINE ?= "verdin-imx8mp"
@@ -277,3 +277,9 @@
include conf/machine/include/${MACHINE}.inc

# DO NOT SET THE MACHINE AFTER THE ABOVE INCLUDE
+
+# Include extra machine setting
+include conf/machine/${MACHINE}-extra.conf
+
+# Freescale EULA
+ACCEPT_FSL_EULA = "1"


f). 在 build目录下执行下面命令进行编译,编译成功后, 可以在 deploy/images/colibri-imx8x 目录下找到编译好的 image 压缩包。

$ bitbake customer-mutimedia-image

在Colibri imx6模块上面更新image并测试

a). 将上述生成好的image压缩文件 Colibri-iMX8X_Customer-Multimedia-Image-Tezi_5.6.0-devel-20220623030838+build.0.tar 按照这里的说明更新到 Colibri iMX8x 模块上面去。

b). image 更新好后,我们可以逐项去测试上面每一个配置

./ Uboot

如下可见默认环境变量的修改已经生效

Colibri iMX8X # printenv

fdt_board=customer-v1
...
setupargs=vt.global_cursor_default=0 consoleblank=0 console=${console}

./ Kernel Config 修改

如下可见kernel增加的驱动也已经编译进去了,同时设备启动后Splash开机画面也正常显示了

root@colibri-imx8x-06748681:~# zcat /proc/config.gz |grep CONFIG_LOGO_CUSTOM
CONFIG_LOGO_CUSTOM_CLUT224=y


./ Device Tree 修改

如下可见,两路FlexCAN都以及正常使能

root@colibri-imx8x-06748681:~# ip link show       
...
3: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT group default qlen 10
  link/can 
4: can1: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT group default qlen 10
  link/can 
...

./ Rootfs修改

如下可见,weston.ini文件修改已经生效

root@colibri-imx8x-06748681:~# vi /etc/xdg/weston/weston.ini
...
[shell]
#size=1920x1080
panel-position=none
background-color=0x00FFFFFF
...


./ Qt 程序自启动

如下图片

总结

本文只是简单演示了利用 Yocto/OpenEmbedded 可以非常方便的生成定制化 Embedded Linux,另外配合git,还可以很好的实现不同版本的管理。但是 OpenEmbedded 架构本身也是比较复杂的,要想灵活使用还需要对其做一些深入的学习才能得心应手,更多文档可以参考如下OpenEmbedded官方网站。

https://www.openembedded.org/wiki/Main_Page

Author: 秦海,技术销售工程师,韬睿(上海)
Share this on:

Leave a comment

Please login to leave a comment!
Have a Question?