Helloword experience of embedded Linux driver development

Source: Internet
Author: User

Since the choice of the Internet of things this major, the word smart XX affects every learning this professional child.

Everyone came to school in an enthusiastic way, and all the imagination was different from what they had imagined. Imagine a variety of intelligent fantasy scenes into the real high number/circuit/mode electricity and so on! Do not know when the world becomes so impetuous, when everyone's efforts to see the results of a period of time will often not be very interested in the mold electricity we all did not understand, so we all self-comfort TMD study this thing exactly what to do? I was the same, but to the later contact with a single-chip microcomputer, contact the application circuit design to know those courses that useless ah! The original is still too young,too simple Ah!

This semester is also going to pass, next year will begin to intern! Realize your IoT dream now! Yes, it's the smart furniture system that compares ESU.

Considering the single-chip microcomputer limitation is relatively large, then to the classmate borrowed a friendly arm of the board, I first contact arm, single-chip computer self-study, the text and funny place everyone smile on the line, if you can give correct, it is better. The first is to achieve a relatively simple function, that is, through the Web Remote control light off. To achieve this function, we need to write the LED driver on the embedded Linux platform, it is difficult to learn to drive our direct write led driver, and it is not easy to succeed, so we first use the famous HelloWorld to familiarize ourselves with the environment.

Now, more and more to deal with the computer, there are two guidelines: 1 ideas to be clear 2 the implementation process takes one step at a time. Because computers are machines, 0 and 1 of the world, the process of a little attention will be wrong. So in the process of writing procedures must be step by step, 1.1-point test, 1.1-point promotion, it seems very slow, but very effective and easy to succeed. Well, not for me to write Hellow defense. In fact, when I write HelloWorld is also prepared, it is on the PC side has been successfully tested.

In addition, the drivers we write are ultimately loaded into the kernel, and we have two ways to add them to the kernel. The first is directly into the kernel, the second is dynamically programmed into the kernel. Adding the kernel dynamically is that we think of our driver as a module and then load the module into the kernel. Directly compiled into the kernel is the module and the kernel compiled together, if there is any problem with this driver, we also need to debug the entire kernel, so I started to learn when I used a modular dynamic loading kernel.

Here are some notes on the development process:

1 Where should I put the. c files that write drivers for the target machine on the PC?

This should be placed in the directory of the target machine kernel. For example, I am a friendly arm of the board, because HelloWorld is a character device, so HELLOWORLD.C is built in the/opt/friendlyarm/mini2440/linux-2.6.32.2/drivers/char directory. Why build it here, because then we can use the makefile in char to compile the module, that is to say we do not have to write makefile alone, a little hug thigh bright.

2 What is the simplest HelloWorld module?

1#include <linux/kernel.h>2#include <linux/module.h>3 Static int__init Mini2440_hello_module_init (void)4 {5Printk"Hello, World!\n");6 return 0;7 }8 Static void__exit Mini2440_hello_module_cleanup (void)9 {TenPrintk"good-bye!\n"); One } A Module_init (mini2440_hello_module_init); - Module_exit (mini2440_hello_module_cleanup); -Module_license ("GPL");

The first and second lines are not said, because we use the kernel's related functions and some of the module's things, so we have to declare.

The last line is the kernel 2.6 above the version recommended that you put the module lincense.

When we load and unload modules dynamically, we need both the init_module and Exit_module functions to load, and not in the code above. Where is the reason?

The original is module_init and module_exit in mischief. Take Module_init as an example, this function has two functions, one is to verify that the passed parameter is the correct module format, and the other is to change the parameter name to Init_module. This allows the module to be successfully loaded.

Although the module above is an empty shelf, it also allows us to have a sensory understanding of the modules.

3 What do I need to prepare before compiling this module?

The first step is to add the module to the kernel menu so that our newly added modules can be configured when we start the kernel menu.

1 config Hello_module 2         " Hello module " 3         depends on mach_mini24404         default if mach_mini2440 5         help6           Hello module

This module is written against the surrounding modules, of course, the user manual is also the detailed steps.

A simple look at this code, TriState is the module in the kernel menu display name.

Depends on is the platform to rely on, the following is said if relying on this platform by default is dynamically loaded into the kernel.

After adding the above code, go back to the Linux kernel directory and make menuconfig to bring up the kernel compilation menu. The newly added Hello module can be found in the place of the character device.

The second step, after adding the code, you also need to compile the file makefile and the source code, so that the implementation of makefile to find the source code to compile. Because we're makefile in the kernel, we don't have to re-write makefile, just add the relationship to the makefile file of the character device.

1 obj-$ (config_hello_module)      + = hello.o

This makes it possible to find the source file for the Hello module!

4 compiling modules and inspection modules

In the kernel of version 2.6, we only need to execute make modules in the kernel directory to compile the module.

After compiling the module must do the most important inspection work, you can use the Modinfo command to view the generated. ko file information. The most important thing is to check the vermagic of the. ko file: The kernel information shown is consistent with the kernel information of your target board, which is often important, or even if you ported to the target board, it will not load successfully.

5 Load Test

All right, now we're going to test the results, the chickens are frozen! In the current directory of the module, use Insmod to load our Hello.ko module.

What actually God horse also wood has, heart suddenly cool half ...

Fortunately not God horse big problem, the above article already said! PRINTK is a kernel-level function, see required output:DMESG | tail

Alternatively, you can use the LSMOD directive to verify that the module is loaded successfully.

Finally deliberately verified the life cycle of the module, exit the terminal re-entry, the View module is still in. The module does not see the bird after restarting.

At this point, all the work is done by the birds. The first one that was developed on the embedded device was completed. In the whole process, as long as a step by step to solve the problem can be solved, the machine is very serious, as long as we follow the machine-like thinking seriously to solve the problem in one step at a time, found that you can hold the machine, MD, will later become a and machine-like people ....

Found out why the big guys say that the embedded entry ash is often difficult, in fact, it is often complex, we can see that there are so many things basically did not relate to IQ-related things, all steps are stylized, when they did not go through a process when found this thing is too difficult, when the completion of a process will feel, NIMA, What the hell ...?

It's about one or two days to solve the problem. Find the more tangled in the time of the problem, when the results come out that moment will be more exciting, the more easy the problem, the solution after not a little excited bright!

It's too far!   Immediately into the live ammunition drive-led Drive! Come on! MD, who am I, such a handsome male kinky!

Finally feel Linux inside makefile and Kconfig really powerful, Linux kernel is a big project, through the kconfig layer to build a menu, through the Makefile file to batch to compile, is too strong. In this way, I think that Linux, although doing something is particularly troublesome, but more can let us know how the system works, Linux and SCM really worthy of learning the operating system and computer principles of the Magic weapon Ah!

Of course, there will be a lot of mistakes in the process, not listed here. Here are some of the blogs that may help some of our netizens in this process:

If the module you added does not appear in your kernel menu, please refer to the

http://blog.csdn.net/hadise/article/details/6222538

For a detailed analysis of Inti_module/exit_module, please refer to

Http://www.embedu.org/Column/Column517.htm

Suggested that the first contact of the friends can now be on the PC system to achieve, so the transition will be a little better, the following is the implementation of the HelloWorld driver on the PC link:

Http://www.cnblogs.com/heat-man/articles/4174899.html

Helloword experience of embedded Linux driver development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.