The kernel driver can not only compile the driver into the kernel, but also dynamically compile the kernel driver. This document describes how to
compile the kernel driver in a module way.
To compile the driver in a module way, the following parts are required:
1 The kernel has been successfully compiled;
2 Find the kernel's arm compiler;
3 Compile a simple driver;
4 Compile a simple Makefile file, and the Makefile file needs to point to the kernel source code directory (the successfully compiled kernel source code directory);
and the document is accompanied by "Makefile", c file and ko file, which you can use for testing.
To dynamically compile the kernel, you first need to compile the kernel source code. For kernel compilation, please refer to Chapter 5 of the user manual.
1. Kernel and compiler path
This section introduces the kernel path and compiler path. Both Qt and Ubuntu kernel source codes are in the android source code package, so you must first unzip the android source code to Ubuntu14.04.
As shown in the figure below, the author's android source code is in the "/home/iMX6Q/iTOP-iMX6_android6.0.1" directory, and the kernel source code is in the "kernel_imx" directory.
Enter the "kernel_imx" directory and view the script file in "build_android_kernel.sh", as shown in the figure below.
As shown in the figure above, we can get some information. When compiling the kernel module later, we need to set the compilation target platform to arm, "export ARCH=arm";
the compiler path is "$(pwd)/../prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-". In theory, this compiler should be used, but in fact,
when compiling the kernel driver in modules mode, using this compiler, it cannot be compiled! !
You should use the compiler "../prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-", as shown in the figure below.
AFE4490 DCM03 HWSPI.rar
(109.49 KB, downloads: )
The compiler path is ../prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-", which corresponds to the kernel source directory. This is what the author has tested. The author does not have much time to study the compilation script in depth, but this compiler is OK. The compiler introduced in the red part above will prompt an error. For this error, Freescale's official response is simply "You used the android compiler", without providing more explanations or prompting methods. However, the author tested several kernel drivers, all of which can be insmod and rmmoded normally.
2. Makefile and test driver source code and compilation
The author creates a new "imx_driver_modules" directory in the "/home/imx6" directory and puts the driver and Makefile files to be compiled in this directory.
2.1 Makefile
Makefile script file:
obj-m += iTOP_IMX6_treedriver_hello.o
KDIR =/home/iMX6Q/iTOP-iMX6_android6.0.1/kernel_imx
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules modules ARCH=arm
CROSS_COMPILE=$(KDIR)/../prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
clean:
rm -rf modules.order *.o workqueue.o Module.symvers *.mod.c *.ko
In the script:
The first line
bj-m += iTOP_IMX6_treedriver_hello.o means the compiled source file is iTOP_IMX6_treedriver_hello.c. If the source file name changes, it needs to be modified to the corresponding one.
The second line: KDIR The parameter points to the corresponding kernel source directory. The author's kernel source code is in the /home/iMX6Q/iTOP-iMX6_android6.0.1/kernel_imxx directory, and users should modify it according to their specific situation.
The third line: PWD ?= $(shell pwd) means assigning the path of the current directory to the PWD variable, that is, /home/imx6_tree_driver/iTOP_IMX6_treedriver_hello. The author will put the Makefile file and driver source code in this directory for compilation.
The fifth line: make -C $(KDIR) M=$(PWD) modules, which means compiling the files in the current directory into modules and setting the path of the kernel source code;
ARCH=arm means setting the target CPU category to arm, that is, the target CPU of the compiled dependent kernel and driver modules is ARM;
CROSS_COMPILE=$(KDIR)/../prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-, the path here points to the path of the kernel compiler.
2.2 Simple driver source code
The driver file name is: iTOP_IMX6_treedriver_hello.c, the source code is as follows:
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("iTOPEET_dz");
static int hello_init(void)
{
printk(KERN_EMERG "Hello World enter!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "Hello world exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
The driver source code only has basic entry and exit functions. When loading and unloading, it prints "Hello Worldenter!" and "Hello world exit!" respectively.
2.3 Compilation
Copy the source code and Makefile files to the Ubuntu14 system.
Use the command "make", as shown in the figure below, you can see that the "iTOP_IMX6_treedriver_hello.ko" file is generated.
Use the command "make clean" to delete the intermediate files.
3. Common problems in module compilation
In the process of compiling the driver in module mode, novices may encounter the following problems.
1. The kernel source code is not compiled or the kernel source code path is not set correctly.
If the kernel source code is not compiled, the module will prompt errors such as missing libraries; if the path is not set correctly, it will prompt that the kernel cannot be found.
2. The source code and Makefile files were written in Windows and then copied to Ubuntu. Due to different editors, the transcoding error occurred.
This error is relatively easy to solve. After Make is compiled, the system will prompt that there is a problem with a specific line in the Makefile or driver file. Use the vim editor to open and check, and you can find some garbled characters. Use the vim editor to correct them and then compile.
4. Module loading and unloading
The author uses the minimum Linux system to test the loading and unloading of modules. The minimum system is introduced in Chapter 13 of the user manual. Before compiling the module, the kernel source code must be compiled. The author is loading the module in the minimum system, so the kernel source code must also be compiled into the qt kernel (the minimum system uses the qt kernel), otherwise it cannot be loaded.
As shown in the figure below, copy the driver module to the development board (the author uses the nfs shared directory method. For nfs, you can refer to the nfs-related documents in the group sharing. The device tree and non-device Ubuntu are universal. You can also use a tf card or USB disk).
Then use the command "insmod iTOP_IMX6_treedriver_hello.ko" to load the driver module. As shown in the figure below, "Hello World enter!" is printed, indicating that the module driver is loaded successfully.
Then use the command "rmmod iTOP_IMX6_treedriver_hello" to uninstall the module. As shown in the figure below, it is found that there is no directory 4.1.15. Here we create a new "/lib/modules/4.1.15".
As shown in the figure below, use the command "mkdir /lib/modules/4.1.15" to create a new directory, and use the command "rmmod iTOP_IMX6_treedriver_helloello" again to uninstall the driver module.
The print message "Hello world exit!" is found, indicating that the module is successfully uninstalled.
As long as the system is re-burned, these new directories only need to be created once.