It is actually very simple to transplant the driver of Feiling embedded i.MX8MP development board
[Copy link]
Engineers may need to transplant drivers when developing the Feiling embedded OKMX8MP-C development board . To prevent users from affecting the development progress due to not understanding the process of transplanting drivers, today I will take writing a hello driver as an example to demonstrate the process of transplanting drivers. Friends who have needs can refer to this method to operate by themselves.
Enter the drivers directory of the source code and create a directory called hello:
forlinx@ubuntu:~$ cd /home/forlinx/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$mkdir hello
Enter the hello directory and create hello.c:forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd hello forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi hello.c
Write the following content in hello.c:
#include <linux/init.h> #include <linux/module.h> static int hello_init(void) { printk(KERN_ALERT "Hello world\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye world\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("Dual BSD/GPL");
Program meaning: insmod driver prints Hello world when mounted, rmmod driver prints Goodbye world when uninstalled
Create two files, Kconfig and Makefile, in this folder.
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig
Write the following content in the Kconfig file:
config HAVE_HELLO tristate "hello driver" help This hello driver is just to show how to develop driver process.
This driver can also be built as a module. If so, the module will be called . default y #endmenu
This means that if CONFIG_HAVE_HELLO is enabled, the hellodrivers menu will be displayed in the kernel trimming configuration file and compiled into the kernel by default:
y: compile into the kernel
m: compile to module .ko file
n: indicates not to compile and not to enable.
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig
Write the following content in the Makefile:
obj-$(CONFIG_HAVE_HELLO) += hello.o
Notice:
The name of the macro definition should be the same as that in Kconfig. Add the file name to be compiled at the end. Because the kernel will automatically add the prefix CONFIG, we also need to add CONFIG_ in front of the name here, indicating that when CONFIG_HAVE_HELLO is enabled, the file specified by the compilation rule is hello.c.
Give the three added files permissions:
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 hello.c forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Kconfig forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Makefile
Edit the top-level Kconfig and Makefile files of drivers.
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ cd .. forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Kconfig
Write the following content in the Kconfig file:
source "drivers/counter/Kconfig" source "drivers/mxc/Kconfig" source "drivers/hello/Kconfig" //在endmenu前添加hello文件夹的配置文件解析 endmenu
In this way, the configuration system will parse the Kconfig in the hello folder according to this configuration.
Edit the Makefile:
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Makefile
Write the following content in the Makefile:
obj-$(CONFIG_COUNTER) += counter/ obj-y += mxc/ obj-$(CONFIG_HAVE_HELLO) += hello/ //在Makefile最后加入这一句
The purpose of this sentence is to find the source file when CONFIG_HAVE_HELLO is enabled. Combined with the module Makefile under the hello file, a hierarchical Makefile is formed. Note that the / is not missing. Adding the name of the custom folder here means compiling this folder into the kernel.
Start compiling:
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd ../.. forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . /opt/fsl-imx-xwayland/5.4-zeus/environment-setup-aarch64-poky-linux forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . environment-setup-aarch64-poky-linux forlinx@ubuntu:~/work/OK8MP-linux-sdk$ cd OK8MP-linux-kernel forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel$ make modules scripts/kconfig/conf --syncconfig Kconfig drivers/hello/Kconfig:7:warning: ignoring unsupported character '' drivers/hello/Kconfig:7:warning: ignoring unsupported character '' drivers/hello/Kconfig:7:warning: ignoring unsupported character '' drivers/hello/Kconfig:7:warning: ignoring unsupported character '' * * Restart config... * * * Device Drivers * Trust the bootloader to initialize Linux's CRNG (RANDOM_TRUST_BOOTLOADER) [N/y/?] n Platform support for Chrome hardware (transitional) (MFD_CROS_EC) [Y/n/m/?] y Trusted Execution Environment support (TEE) [Y/n/m/?] y hello driver (HAVE_HELLO) [Y/n/m/?] (NEW) m //将hello驱动编译进内核就配置为m CALL scripts/checksyscalls.sh CALL scripts/atomic/check-atomics.sh CHK include/generated/compile.h GZIP kernel/config_data.gz CC kernel/configs.o […] LD vmlinux SORTEX vmlinux SYSMAP System.map Building modules, stage 2. MODPOST 536 modules CC [M] drivers/hello/hello.mod.o LD [M] drivers/hello/hello.ko
After the compilation is complete, you can see the compiled driver in the OK8MP-linux-kernel/drivers/hello directory:
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/$ ls drivers/hello hello.c hello.ko hello.mod hello.mod.c hello.mod.o hello.o Kconfig Makefile modules.order
Copy hello.ko to the development board using a USB flash drive or TF card for verification:
rootK8MP:~#cd/run/media/sda1///进入U盘的路径下 root@OK8MP:/run/media/sda1#insmodhello.ko//挂载hello.ko [138.679964]Helloworld //挂载驱动打印信息 root@OK8MP:/run/media/sda1#rmmodhello.ko//卸载hello.ko [142.022115]Goodbyeworld //卸载驱动打印信息 root@OK8MP:/run/media/sda1#
From the above test, we can see that the hello.ko driver can run normally.
The above is the process of writing and adding a driver by yourself, which I demonstrated to you. If you want to transplant a certain module, you can ask the module manufacturer for the ready-made driver .c file, and then configure Makefile and Kconfig according to the above steps.
|