嗨害嗨

分类 技术 下的文章

13/12
2024

yocto编译构建树莓派flutter系统

目前,嵌入式系统中的 UI 开发严重依赖 Qt 框架,该框架被认为是创建具有良好质量水平的强大系统的可靠工具。在处理硬件时,这一点变得尤其重要,因为硬件缺乏通常连接到互联网的手机或电脑等设备中的持续更新可访问性。然而,随着时间的推移,嵌入式系统与外界建立连接变得越来越普遍,无论是通过蜂窝网络、WiFi,还是 LoRaWAN 等不太传统的方式。通过允许无线 (OTA) 更新,开发人员可以更少担心严格的质量标准,而更多地专注于提供各种功能,这是由于保持市场竞争力日益重要而推动的。这就是 Flutter 这样的框架未来有可能成为新的行业标准的地方,其简单性和结构可以显着缩短上市时间。

本文将会介绍如何在Ubuntu 20.04系统上构建适用于树莓派zero 2w的yocto系统并且支持flutter图形框架。

安装依赖

sudo apt install gawk wget git diffstat unzip texinfo gcc build-essential \
chrpath socat cpio python3 python3-pip python3-pexpect xz-utils debianutils \
iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev \
python3-subunit mesa-common-dev zstd liblz4-tool file locales libacl1

Yocto系统构建

拉取poky源码

git clone git://git.yoctoproject.org/poky

接下来,我们必须checkout一个发布分支,在本例中必须是 kirkstone 分支。由于我们将在以后使用的某些层具有一定的兼容性,因此需要此特定版本。

cd poky
git checkout -t origin/kirkstone -b kirkstone

接着设置环境变量

source ../oe-init-build-env

配置成功后会有以下打印

You had no conf/bblayers.conf file. This configuration file has therefore been
created for you from poky/meta-poky/conf/bblayers.conf.sample
To add additional metadata layers into your configuration please add entries
to conf/bblayers.conf.

The Yocto Project has extensive documentation about OE including a reference
manual which can be found at:
    https://docs.yoctoproject.org
For more information about OpenEmbedded see the website:
    https://www.openembedded.org/

### Shell environment set up for builds. ###
You can now run 'bitbake <target>'
Common targets are:
    core-image-minimal
    core-image-full-cmdline
    core-image-sato
    core-image-weston
    meta-toolchain
    meta-ide-support
You can also run generated qemu images with a command like 'runqemu qemux86'
Other commonly useful commands are:
 - 'devtool' and 'recipetool' handle common recipe tasks
 - 'bitbake-layers' handles common layer tasks
 - 'oe-pkgdata-util' handles common target package tasks

项目结构会像这样子

├── bitbake
├── build
│   └── conf
│       ├── bblayers.conf
│       ├── local.conf
│       └── templateconf.cfg
├── contrib
├── documentation
├── meta
├── meta-poky
├── meta-selftest
├── meta-skeleton
├── meta-yocto-bsp
└── scripts

现在已经准备好构建yocto系统了,但默认构建目标是quemux86-64,我们需要改一下。

添加树莓派的元层

为了能够构建适合 Raspberry Pi 的映像,我们需要添加提供必要的机器配方的元层。我们首先必须克隆 meta-raspberrypi 存储库并checkout出与我们的poky版本相匹配的分支

git clone https://github.com/agherzan/meta-raspberrypi.git # 在poky目录下
cd meta-raspberrypi
# git branch -a : list of all branches
git checkout -t origin/kirkstone

现在这一层提供了以下机器(rpi板)的配方,它们可以在meta-raspberrypi文件夹中找到

├── conf
│   ├── layer.conf
│   └── machine
│       ├── include
│       ├── raspberrypi0-2w-64.conf
│       ├── raspberrypi0-2w.conf
│       ├── raspberrypi0.conf
│       ├── raspberrypi0-wifi.conf
│       ├── raspberrypi2.conf
│       ├── raspberrypi3-64.conf
│       ├── raspberrypi3.conf
│       ├── raspberrypi4-64.conf
│       ├── raspberrypi4.conf
│       ├── raspberrypi-cm3.conf
│       ├── raspberrypi-cm.conf
│       └── raspberrypi.conf

我们可以使用 bitbake-layers add-layer 命令添加该层:

cd poky/build
bitbake-layers add-layer ../meta-raspberrypi

这将在 build/conf 文件夹中的 bblayers.conf 文件中添加该层的绝对路径。

由于我使用的是 raspberry-pi Zero 2,因此我需要将 build/conf/local.conf 中的MACHINE设置为raspberrypi0-2w-64

MACHINE ??= "raspberrypi0-2w-64"

现在可以通过以下命令运行yocto构建了

bitbake core-image-base

编译完成后,build/tmp/deploy/images/<machine-name>/ 文件夹中就会出现可加载到 SD 卡的映像文件。在这种情况下,Raspberry Pi 元层会创建一个 *.wic.bz2 镜像文件,可使用 bmaptool 工具将其写入 SD 卡。

可以用fdisk工具检查SD卡的分区,需要注意的是SD卡只能能够有一个分区

fdisk -l

然后将 *.wiz.bmap*.wiz.bz2 文件写入图像:

sudo bmaptool copy --bmap core-image-base-raspberrypi0-2w-64.rootfs.wic.bmap \
core-image-base-raspberrypi0-2w-64.rootfs.wic.bz2 /dev/sdb

我这里是sdb分区

启动树莓派后,终端会提示你输入登录名,默认为 root,没有密码。

添加meta-flutter元层

要执行 Flutter 应用程序,必须在系统上安装 Flutter 引擎。这是通过meta-flutter元层促进的。由于我使用的是 Raspberry Pi,因此我将依赖 flutter-pi 命令实用程序。该工具可以执行 Flutter 应用程序,而无需像 Wayland 这样的显示服务器。

首先,我们想要在我们的poky根目录中clone元层存储库以及它的 meta-clangmeta-oe 依赖项

git clone https://github.com/meta-flutter/meta-flutter.git
git clone https://github.com/kraj/meta-clang
git clone git://git.openembedded.org/meta-openembedded

checkout出kirkstone分支

cd meta-flutter
#git branch -a : list of available branches
git checkout -t origin/kirkstone
cd meta-clang
git checkout -t origin/kirkstone
cd meta-openembedded
git checkout -t origin/kirkstone

我们需要将这些层添加到 local.conf 文件中:

cd poky/build
bitbake-layers add-layer ../meta-openembedded/meta-oe
bitbake-layers add-layer ../meta-flutter
bitbake-layers add-layer ../meta-flutter/meta-flutter-apps
bitbake-layers add-layer ../meta-clang

现在我们可以通过修改 local.conf 文件来将包添加到我们的发行版中:

IMAGE_INSTALL:append = "\
              flutter-pi \
              "
DISTRO_FEATURES:append = " systemd opengl"

为了能够运行示例应用程序并编译 flutter 引擎,我们需要启用 opengl 和 systemd 功能。

编译

bitbake core-image-base

烧录固件

sudo bmaptool copy --bmap core-image-base-raspberrypi0-2w-64.rootfs.wic.bmap \
core-image-base-raspberrypi0-2w-64.rootfs.wic.bz2 /dev/sdb

系统启动后,从欢迎消息中,我们可以以 root 身份登录并运行 flutter-pi。构建的示例画廊包可以在 /usr/share/flutter/gallery/3.13.9/release/ 下找到

Poky (Yocto Project Reference Distro) 4.0.15 raspberrypi0-2w-64 /dev/tty1

raspberrypo0-2w-64 login: root
root@raspberrypi0-2w-64:~# flutter-pi --release /usr/share/flutter/gallery/3.13.9/release/

至此一个flutter基础应用已经构建完成并在树莓派上运行起来。

总结

当前的过程非常简单,尽管我只接触了基础知识。真正的挑战等待着集成定制硬件,这是我的目标是在本次旅程的第三部分中解决的任务。 Flutter 的使用速度令人印象深刻,并且有可能对嵌入式世界产生重大影响。虽然它可能不如 Qt 那么强大,但它擅长快速创建有吸引力的界面

原文:https://medium.com/@kevin.ceresa/exploring-yocto-with-flutter-part-1-f6c5e1caabf4

+ MORE