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 driver in the way of module

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

Share

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

Today, the editor will share with you how to compile the driver in the way of module. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

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 successfully compiled kernel

Nuclear source directory)

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. Both Qt and Ubuntu kernel sources are in the android source.

In the 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.

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

Show.

As shown in the figure above, we can get some information that when compiling the kernel module later, we need to set the compilation target level.

Taiwan is arm, "export ARCH=arm"

The path to the compiler is "$(pwd) /.. / prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-.

In theory, you should use this compiler, but in practice you should use this compiler with

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

The compiler ".. / prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-" should be used.

Only then, as shown in the following figure.

The compiler path is the prebuilts/gcc/linux-x86/arm/arm-eabi- corresponding to the kernel source directory.

4.8 dictionary arm script-", which is tested by the author, who doesn't have much time to delve into the compiled script, but this

A compiler is OK. The compiler introduced in the previous red section will prompt for an error. For this error, Freescale officially

The simple reply is "you used android's compiler", without further explanation or prompt.

However, the author has tested several kernel drivers, all of which are capable of normal 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 the driver to be compiled

Move and Makefile files are placed in this directory.

2.1 Makefile

Makefile script file:

In the script:

The first line: obj-m + = iTOP_IMX6_treedriver_hello.o means that the compiled source file is

ITOP_IMX6_treedriver_hello.c, 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

Under the / home/iMX6Q/iTOP-iMX6_android6.0.1/kernel_imxx directory, users should act according to their own specific conditions

To modify it.

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

The source code is compiled under this directory.

Line 5: where make-C $(KDIR) Manners $(PWD) modules indicates that the files in the current directory are compiled

For the module, and worked out the path of the kernel source code

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

Mark CPU as ARM

Where CROSS_COMPILE=$ (KDIR) /.. / prebuilts/gcc/linux-x86/arm/arm-eabi-

4.8 bind arm arm, 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:

The driver source code has only basic entry and exit functions. Print "Hello World" when loading and uninstalling respectively

Enter! "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 there is a "iTOP_IMX6_treedriver_hello.ko" file

Generate.

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 set incorrectly, it will

Prompt that the kernel cannot be found.

two。 The source code and Makefile files are written under Windows and then copied to Ubuntu due to different editors

Causes a transcoding error.

This kind of error is easy to solve. After Make is compiled, the system will prompt Makefile or driver file to specify a specific one.

Okay, there's a problem. Use the vim editor to open and check, you can find some garbled code, use the vim editor to correct it

Just compile it again.

4 module loading and unloading

The author here uses the minimum linux system to test the loading and unloading of the module. The minimum system is available in Chapter 13 of the user manual.

Introduction. Before compiling the module, the kernel source code must be compiled and passed, the author here is in the minimum system is to load the module, then

The nuclear 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

Families can refer to nfs-related documents in group sharing. Device trees and non-device Ubuntu are common. You can also use a tf card or

Or U disk).

Then use the command "insmod iTOP_IMX6_treedriver_hello.ko" to load the driver module, as shown below

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.

There is no directory 4.1.15, here we create a new "/ lib/modules/4.1.15".

As shown in the following figure, use the command "mkdir / lib/modules/4.1.15" to create a new directory and use the command again

"rmmod iTOP_IMX6_treedriver_helloello" uninstall the driver module.

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.

These are all the contents of this article entitled "how to compile drivers in module". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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