Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compile the driver in module way

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to compile the driver in the way of module". The editor shows you the operation process through the actual case. The operation method is simple and fast, and it is practical. I hope this article "how to compile the driver in the way of module" can help you solve the problem.

Compiling the driver in the way of module requires the following parts:

1 the kernel was compiled successfully

2 find the arm compiler of the kernel

3 compile simple driver

4 to compile a simple Makefile file, you need to point to the kernel source directory in the Makefile file (the kernel source directory that has been successfully compiled)

Along with the documents are "Makefile", c files, and ko files, which you can use for testing.

To compile the kernel dynamically, you first need to compile the kernel source code. Please refer to Chapter 5 of the user's manual for kernel compilation.

1. Kernel and compiler path

This section describes the kernel path and the compiler path. The kernel source code of both Qt and Ubuntu is in the android source code package, so you must first decompress the android source code into 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.

Go to the "kernel_imx" directory and view the script file in "build_android_kernel.sh", as shown in the following figure.

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 path to the compiler 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 practice, with

Modules way to compile the kernel driver, using this compiler, it is impossible to compile!

You should use the compiler ".. / prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-", as shown in the following figure.

The compiler path is the kernel source directory corresponding to.. / prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi- ", which is tested by the author. The author does not have much time to delve into the compiled script, but this compiler is OK. The compiler described in the previous red section will prompt for an error. Freescale's official response to this error is a simple reply, "you used the android compiler." there is no more explanation and no hint method, but the author has tested several kernel drivers, all of which can work with insmod and rmmod.

2. Makefile and test-driven source code and compilation

The author creates a new "imx_driver_modules" directory under the "/ home/imx6" directory and places the compiled drivers and Makefile files 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) masking $(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 indicates that the compiled source file is iTOP_IMX6_treedriver_hello.c, and if the source file name changes, it needs to be changed to the corresponding one.

The second line: the KDIR 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 need to modify it according to their own specific conditions.

The third line: PWD? = $(shell pwd) means that the path of the current directory is assigned to the PWD variable, that is, / home/imx6_tree_driver/iTOP_IMX6_treedriver_hello. The author will put the Makefile file and the driver source code into this directory to compile.

Line 5: where make-C $(KDIR) MIMO $(PWD) modules indicates that the files in the current directory are compiled into modules and the path to the kernel source code is established

Where ARCH=arm indicates that the target CPU category is set to arm, that is, the compiled dependent kernel and driver module target CPU is ARM

Where 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 name of the driver file is iTOP_IMX6_treedriver_hello.c, and 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 has only basic entry and exit functions. Print "Hello Worldenter!" when loading and uninstalling, respectively. And "Hello world exit!"

2.3 compilation

Copy the source code and Makefile files to the Ubuntu14 system.

Using the command "make", as shown in the following figure, you can see that the "iTOP_IMX6_treedriver_hello.ko" file is generated.

Using the command "make clean", you can delete intermediate files.

3. Common problems in module compilation

In the process of compiling the driver as a module, the novice may have 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 for errors such as missing libraries; if the path is not set correctly, the kernel will not be found.

two。 The source code and Makefile files are written under Windows and then copied to Ubuntu, resulting in transcoding errors due to different editors.

This kind of error is easy to solve. After Make is compiled, the system will prompt Makefile or a specific line of the driver file to have a problem. Use the vim editor to open and check, you can find some garbled, use the vim editor to correct it and then compile it.

4. Module loading and unloading

The author here uses the minimum linux system to test the loading and unloading of the module, which is described in Chapter 13 of the user manual. Before compiling the module, the kernel source code must be compiled and passed, the author here is in the minimum system to load the module, then 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 following figure, copy the driver module to the development board (the author uses the method of nfs shared directory. About nfs, you can refer to the documents related to nfs in group sharing. Device tree and non-device Ubuntu are common. You can also use a tf card or a USB disk.

Then use the command "insmod iTOP_IMX6_treedriver_hello.ko" to load the driver module, as shown in the following figure, print out "Hello World enter!", indicating that the module driver loaded successfully.

Then use the command "rmmod iTOP_IMX6_treedriver_hello" to uninstall the module, as shown in the following figure, 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 following figure, create a new directory with the command "mkdir / lib/modules/4.1.15" and uninstall the driver module with the command "rmmod iTOP_IMX6_treedriver_helloello" again.

The print message "Hello world exit!" was found and the module was uninstalled successfully.

As long as you re-burn the write system, these new directories only need to be established once.

This is the end of the introduction to "how to compile drivers in module". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report