Use Yocto 5.0 Scarthgap and the demo_char.c from Chapter 02. Build the external module against the target kernel, install it in the image, and verify /dev/demo-char on the target.
1. Add the sources to an existing layer
Use a custom layer already listed in BBLAYERS and copy over the source from Chapter 02.
meta-lab/
recipes-kernel/
demo-char/
demo-char_1.0.bb
files/
demo_char.c
Makefile
2. Create the Kbuild Makefile
Add a modules_install target. Yocto supplies KERNEL_SRC for the target kernel.
obj-m := demo_char.o
KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build
.PHONY: all modules_install
all:
$(MAKE) -C "$(KERNEL_SRC)" M="$(CURDIR)" modules
modules_install:
$(MAKE) -C "$(KERNEL_SRC)" M="$(CURDIR)" modules_install
Use a literal tab before each $(MAKE) command.
3. Write the recipe
This example assumes that the lab source is under GPL-2.0-only. For other source code, set the license fields to reflect its actual license.
SUMMARY = "Character device lab kernel module"
LICENSE = "GPL-2.0-only"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/GPL-2.0-only;md5=801f80980d171dd6425610833a22dbe6"
inherit module
SRC_URI = "file://demo_char.c file://Makefile"
S = "${WORKDIR}"
Enter the Yocto build environment, build the recipe, and check the package name.
bitbake demo-char
oe-pkgdata-util list-pkgs | grep 'kernel-module-demo-char'
4. Install the module in the image
Add the package to your image recipe. For a local test, you can add the setting to conf/local.conf.
IMAGE_INSTALL:append = " kernel-module-demo-char"
Rebuild your image. This example uses core-image-minimal.
bitbake core-image-minimal
For optional boot-time loading, add this to the module recipe.
KERNEL_MODULE_AUTOLOAD += "demo_char"
demo-char is the recipe name, kernel-module-demo-char is the image package name, and demo_char is the name passed to modprobe.
5. Verify on the target
Boot the new image and run as root.
uname -r
modprobe demo_char
ls -l /dev/demo-char
cat /dev/demo-char
printf 'Hello from the target\n' > /dev/demo-char
cat /dev/demo-char
modprobe -r demo_char
Confirm that the write can be read back and the node disappears after unloading. If you enabled automatic loading, reboot and check lsmod and ls -l /dev/demo-char before repeating the read/write test.
Reuse inherit module for the USB or SSD1306 experiments. For USB, keep the device match table and check USB host support. For the SSD1306, integrate the Device Tree node or overlay into the BSP and check that the I²C device binds to its driver.