硬件软件系统方案应用范例智能车大赛技术支持学习园地关于我们恩智浦官网

在云实验室开发板上运行第一个驱动程序

1.      在实验本节代码前,准备环境一个ubuntu物理机或者虚拟机,下载并使能交叉编译工具链,然后编译好内核源码,参考《编译内核镜像并在云实验室开发板运行》

2.      开始第一个内核模块编写和编译运行

在你要写代码的目录下用touch新建两个文件,一个源码文件,一个Makefile。如下

~/linux_driver/01_hello$ touch helloworld_drvmod.c
~/linux_driver/01_hello$ touch Makefile
~/linux_driver/01_hello$ vim helloworld_drvmod.c 

将下面的代码复制到helloworld_drvmod.c, 如下:

/*
* Copyright 2024 NXP
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("MIT/GPL");
MODULE_AUTHOR("Cloud Lab");
 
static int __init helloworld_drvmod_init(void)
{
        printk(KERN_INFO "Helloworld driver init\n");
        return 0;
}
 
static void __exit helloworld_drvmod_exit(void)
{
        printk(KERN_INFO "Helloworld driver exit\n");
}
 
module_init(helloworld_drvmod_init);
module_exit(helloworld_drvmod_exit);
~

        同样的方法编写Makefile:

PWD    := $(shell pwd)
KERDIR := /home/test/kernel_build/linux-imx 
obj-m += helloworld_drvmod.o
 
all:
        make -C $(KERDIR) M=$(PWD) modules
clean:
        make -C $(KERDIR) M=$(PWD) clean
 

3.      编译:

~/linux_driver/01_hello$ export CROSS_COMPILE=~/toolchain/arm-gnu-toolchain-13.3.rel1-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
~/linux_driver/01_hello$ export ARCH=arm64
~/linux_driver/01_hello$ make
 

编译完成后当前目录会有如下文件

~/linux_driver/01_hello$ ls
helloworld_drvmod.c   helloworld_drvmod.mod    helloworld_drvmod.mod.o  Makefile       Module.symvers
helloworld_drvmod.ko  helloworld_drvmod.mod.c  helloworld_drvmod.o      modules.order  text.c

4.      替换云实验室开发板镜像并重新启动

根据《编译内核镜像并在云实验室开发板运行》替换dtb和image,这里仍然需要这一步,因为内核模块在加载时需要和内核编译版本相匹配

上传并替换云实验室板子的dtb和Image,依次点击用户网页端下面1,2,3的位置,在点击位置2时,选择TFTP,点击3后选择文件~/kernel_build/linux-imx/arch/arm64/boot/dts/freescale/imx8mp-evk.dtb和~/kernel_build/linux-imx/arch/arm64/boot/Image