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

Example Analysis of developing hello world with Linux embedded driver

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

Share

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

Linux embedded driver development hello world example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. The code is written with only one file, and the contents are as follows

The file name is: mini2440_hello_module.c

# include # include static int _ _ init mini2440_hello_module_init (void) {printk ("Hello, Mini2440 module is installed!\ n"); return 0;} static void _ exit mini2440_hello_module_cleanup (void) {printk ("Good-bye, Mini2440 module was removed!\ n");} module_init (mini2440_hello_module_init); module_exit (mini2440_hello_module_cleanup); MODULE_LICENSE ("GPL")

Notes:

Two header files are required, don't ask why

B, mini2440_hello_module_init () driver load function, is also necessary

C, mini2440_hello_module_cleanup () driver unloading function is also necessary.

D, module_init (mini2440_hello_module_init) you must have this if you want your driver to run

E, module_exit (mini2440_hello_module_cleanup) and module_init have the opposite meaning.

B, MODULE_LICENSE ("GPL"); used to declare the license of the module

2. Driver installation

There are two ways to install the driver, the first is module mode, and the second is to compile directly into the kernel

Here I'll talk about how to modify the corresponding configuration file and add the configuration menu options for the kernel. Here involves two files, one is the function of makefile and kconfig,makefile is to compile, specifically, to write a program, you must write its makefile file. The purpose of kconfig is to add written drivers to the graphics configuration menu. To put it simply, if you want to see your kernel options in the kernel configuration menu, you have to modify the kconfig file.

Let's talk about how to modify the makefile file and the kconfig file. Suppose we write a driver, and then put it in the drivers/char directory, and the file name is mini2440_hello_module.c. First of all, let's modify the makefile file in the same directory, how to modify it? Most of the time, as soon as you open it, it's so dense, but it doesn't matter, don't you see? Most of the formats are actually the same, which is simple. Let's follow suit. Let's add this sentence: obj-$ (CONFIG_MINI2440_HELLO) + = mini2440_hello_module.o.

I don't want to go into too much detail about what this statement means. The rightmost one is the file name of our driver file. As for the CONFIG on the right, it is necessary, and the MINI2440_HELLO is what we will use in the Kconfig file later. Then we modify the Kconfig file in the same directory, find the menu "Character devices" and start to add it here, as follows:

Config MINI2440_HELLO tristate "MINI2440 BOARD HELLO TEST" depends on ARCH_S3C2440 default m if ARCH_S3C2440 help MINI2440 Board First module test.

Here I also briefly explain the above sentences, the top config is a must, this is its format, let's put it this way. The following sentence is the text shown on the kernel configuration sheet, and the following sentence is its dependency, that is to say, only when ARCH_S3C2440 is configured can we configure this driver. Finally, there is its help message, so you can write it according to its format in the future. Got it!

The next step is to compile the kernel according to how to compile the kernel. Type: make menuconfig, select M for the module just now, then compile the kernel and burn the kernel image into the development board. Finally, use the command make SUBDIR=drivers/char/

Modules recompiles. Get mini2440_hello_module.ko this is the driver module we want, we add it to the development board, there are many ways to load, I usually use serial port to add to the development board. Type rz in the lib directory of the development board and download it.

Ok, if you have successfully downloaded the driver module to the development board, what you need to do at this time is to load the driver module. In order to be vivid, I will take a screenshot:

Note: in performing make menuconfig, graphical interface

Device Drivers-- >.... Character devices-- >

The next may not appear.

MINI2440 BOARD HELLO TEST

Option

This option appears if the following actions are required

The # make menuconfig command enters the kernel configuration interface, goes to the "System Type" option configuration menu, and makes the following choices under the S3C2440 Machines option: select only SMDK2440 and SMDK2440 withS3C2440 CPU module options.

Explanation of commands related to module operation in Linux operating system

Lsmod to see the installed modules, you can also view the contents of the / proc/modules file. In fact, the lsmod read command displays module information by looking at the contents of / proc/modules.

Modinfo display module information

Modprobe does not need to specify a path, it will look for modules under the default path. Modules also have dependency problems: for example, if you want to load msdos.ko, you need to load fat.ko. Modprobe View / lib/module/version/modules.dep learns about the dependency of the module

Rmmod uninstalls the module, but the kernel considers it unsafe, so you can add a command to force it.

Depmod checks the dependencies between modules in the system and stores the dependency information in / lib/modules/2.6.18-1.2798/modules.dep. Generally, after loading the module, you need to execute depmod-a under the command line first, and then check the module dependency and path information in modules.dep when the system loads the module. Only in this way can you load the required module successfully.

When insmod loads the module, you need to specify the full path and module name.

Note: the problems I have encountered

The module needs to be uploaded to:

/ lib/modules/2.6.32.2

The rmmod command will only work under the. [2.6.32.2] is the linux kernel version number.

As shown in the picture

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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