前提条件已经了解Yocto编译
一.Yocto部署驱动到开发板
1.初始化构建环境,会自动跳转到构建目录
DISTRO=openstlinux-weston MACHINE=<Machine name> source layers/meta-custom-leyers/scripts/envsetup.sh
2.创建内核模块例子
PC $> mkdir kernel_module_example
PC $> cd kernel_module_example
3.为内核模块编写驱动源码例子:kernel_module_example.c
1 // SPDX-identifier: GPL-2.0
2 /*
3 * Copyright (C) STMicroelectronics SA 2018
4 *
5 * Authors: Jean-Christophe Trotin <jean-christophe.trotin@st.com>
6 *
7 */
8
9 #include <linux/module.h> /* for all kernel modules */
10 #include <linux/kernel.h> /* for KERN_INFO */
11 #include <linux/init.h> /* for __init and __exit macros */
12
13 static int __init kernel_module_example_init(void)
14 {
15 printk(KERN_INFO "Kernel module example: hello world from STMicroelectro
16 return 0;
17 }
18
19 static void __exit kernel_module_example_exit(void)
20 {
21 printk(KERN_INFO "Kernel module example: goodbye from STMicroelectroni
22 }
23
24 module_init(kernel_module_example_init);
25 module_exit(kernel_module_example_exit);
26
27 MODULE_DESCRIPTION("STMicroelectronics simple external out-of-tree Linux ker
28 MODULE_AUTHOR("Jean-Christophe Trotin <jean-christophe.trotin@st.com>");
29 MODULE_LICENSE("GPL v2");
4.为内核模块编写Makefile
# Makefile for simple external out-of-tree Linux kernel module example
# Object file(s) to be built
obj-m := kernel_module_example.o
# Path to the directory that contains the Linux kernel source code
# and the configuration file (.config)
#KERNEL_DIR ?= <Linux kernel path>
# Path to the directory that contains the source file(s) to compile
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
5.这里要写内核源码目录,在Yocto根本不知道在哪里,可以用以下命令进行查找,在<build dir>:
PC $ bitbake -s | grep linux
. .........
...........
linux-<MACHINE NAME> :4.19+AUTOINC+76f632af0a-r0
......
6.输入如下命令,就会找到对应得内核路径,然后代替Makefile得<linux kernel path>:
bitbake -e linux-<MACHINE NAME> | grep ^S=
7.添加新得recipe到workspace
PC $> cd <build dir>
PC $> devtool add mymodule kernel_module_example/
8.将recipe应用于内核模块构建
PC $> devtool edit-recipe mymodule
然后修改ricipe如下:
1 # Recipe created by recipetool
2 # This is the basis of a recipe and may need further editing in order to be fully functional.
3 # (Feel free to remove these comments when editing.)
4
5 # Unable to find any files that looked like license statements. Check the accompanying
6 # documentation and source headers and set LICENSE and LIC_FILES_CHKSUM accordingly.
7 #
8 # NOTE: LICENSE is being set to "CLOSED" to allow you to at least start building - if
9 # this is not accurate with respect to the licensing of the software being built (it
10 # will not be in most cases) you must specify the correct value before using this
11 # recipe for anything other than initial testing/development!
12 LICENSE = "CLOSED"
13 LIC_FILES_CHKSUM = ""
14
15 # No information for SRC_URI yet (only an external source tree was specified)
16 SRC_URI = ""
17
18
19 # NOTE: this is a Makefile-only piece of software, so we cannot generate much of the
20 # recipe automatically - you will need to examine the Makefile yourself and ensure
21 # that the appropriate arguments are passed in.
22 DEPENDS = "virtual/kernel"
23 inherit module
24
25 EXTRA_OEMAKE = "ARCH=arm"
26 EXTRA_OEMAKE += "KERNEL_DIR=${STAGING_KERNEL_BUILDDIR}"
29 S = "${WORKDIR}"
30
31 do_configure () {
32 # Specify any needed configure commands here
33 :
34 }
35
36 do_compile () {
37 # You will almost certainly need to add additional arguments here
38 oe_runmake
39 }
40
41 do_install () {
42 # NOTE: unable to determine what to put here - there is a Makefile but no
43 # target named "install", so you will need to define this yourself
44 install -d ${D}/lib/modules/${KERNEL_VERSION}</font>
45
46 install -m 0755 ${B}/kernel_module_example.ko ${D}/lib/modules/${KERNEL_VERSION}/</font>
47
48 }
49
9.进入构建目录,并生成内核模块例子:
PC $> cd <build dir>
PC $> devtool build mymodule
注意:这一步可能会出现驱动编译错误 /bin/sh: scripts/mod/modpost,提示信息:
/bin/sh: scripts/mod/modpost: No such file or directory
出现这样的错误,说明scripts下没有生成相应的文件,两种解决办法:a.cd到kernel所在目录,执行: make scripts
b.先用Yocto把镜像编译好,因为就像我们平时写驱动的时候,linux内核要写编译完才能编译驱动
10.把内核模块部署到板子上
PC $> devtool deploy-target -Ss mymodule root@<board ip address>
11.更新可加载内核模块的依赖项描述,并将磁盘上的数据与内存同步
Board $> /sbin/depmod -a
Board $> sync
12.将内核模块示例插入Linux内核
Board $> modprobe kernel_module_example
[18167.821725] Kernel module example: hello world from STMicroelectronics
二.整合驱动到Yocto用户层(用于测试)
1.在客户层新建recipe
PC $> mkdir ../layers/meta-my-custo-layer/recipes-custom/mymodule
PC $> cp workspace/recipes/mymodule/mymodule.bb ../layers/meta-my-custo-layer/recipes-custom/mymodule
2.拷贝源码和Makefile到用户层
PC $> mkdir ../layers/meta-my-custo-layer/recipes-custom/mymodule/mymodule
PC $> cp kernel_module_example/Makefile ../layers/meta-my-custom-layer/recipes-custom/mymodule/mymodule
PC $> cp kernel_module_example/kernel_module_example.c ../layers/meta-my-custom-layer/recipes-custom/mymodule/mymodule
PC $> devtool reset mymodule
3.还必须完成新菜谱的一些字段,至少包括LICENSE、LIC_FILES_CHKSUM和SRC_URI,配方更新现在必须直接在..lsyers/meta-my-custo-layer/recipes-custom/mymodule/mymodule.bb中完成
对于Linux 内核模块:
1 LICENSE = "GPLv2"
2 LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.GPLv2;md5=751419260aa954499f7abaabaa882bbe"
4 SRC_URI = "file://Makefile \
5 file://kernel_module_example.c \
6 "
对于Linux应用程序:
1 LICENSE = "MIT"
2 LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"
3 SRC_URI = "file://hello_world_example.c \
4 "
4.检查新配方更新没有破坏编译,用bitbake命令编译它:
PC $> bitbake mymodule
5.快速添加recipe到镜像中(image),进入到镜像recipe中
PC $> cd ../leyers/meta-st-openstlinux/recipes-st/images/
6.打开 st-image-weston.bb 并添加这一行 : CORE_IMAGE_EXTRA_INSTALL += " mymodule ",然后编译:
PC $> bitbake st-image-weston
7.在烧写镜像和启动该板之后,将内核模块示例插入/移除
Board $> modprobe kernel_module_example
[18167.821725] Kernel module example: hello world from STMicroelectronics
Board $> rmmod kernel_module_example
[18180.086722] Kernel module example: goodbye from STMicroelectronics
三.构造定制系统
1.一旦这个快速检查完成,请在st-image-weston删除插件:
- 定制客户需求系统
- 必须在编译的自定义镜像中添加这个新配方(mymodule.bb)
PC $> cd ..layers/meta-my-custom-layer/recipes-samples/images/
2.打开 my-custom-image.bb 并添加这一行 : IMAGE_INSTALL += "mymodule"
PC $> bitbake <my custome image>