Embedded Linux Driver Development Add a temperature sensor module to your Linux system

Source: Internet
Author: User

Busy a few days, finally can let Ds18b20 in their own Development Board Linux system ran! Although Ds18b20 is not a new thing, but think of a confidant can add modules to the Linux system or a little chicken cold!

Although the hardware resources are very rich and a lot of surplus, you can use software resources to replace hardware resources, such as video codec software can replace the hardware to work. But there are many modules need real-time data collection This is software can never replace, and with the further development of the Internet, intelligent is also an inevitable trend, so a large number of sensors and control chips will be applied to the production of life, so the individual feel that driving development is a good direction. At the same time, as learners, in the process of embedded Linux development, can touch the software and hardware, how they work together with a more profound understanding. These things in the computer world should be regarded as the internal strength, the cultivation of these, in the practice of those superior martial arts may be faster.

Although successful, but through this development I do have a little worry, because this time deliberately do not refer to other predecessors wrote the information, I want to rely on the data manual to write this drive, but the problem came, suddenly himself silly, do not know how to start, from there? How could that be? Contact code for such a long time, suddenly want to own "naked write", incredibly no idea, or have a general idea, the details of the place a little vague, in short, not a pen. Then think about the internet is really a double-edged sword Ah! I usually rely on this deep Baidu skills to almost solve the study encountered 80% problems, so the resource dependence on the network is imperceptible in the growing, especially some straight male predecessors to put the code, before I like such predecessors, now think a little worried! or self-control is not enough Ah! Therefore, in view of this, I will not post the full code in the blog, only describe the implementation process and ideas.

Another point is that plagiarism is too serious, although the same problem may be the page of search results, but the version is really too little, one or two versions are very common, want to find a different point of view is really too difficult, so, this blog after the arbitrary according to their own ideas, even if it is wrong, Also to this blog park and CSDN bring a different color.

Well, it's too much nonsense, so let's get down to it.

About Ds18b20 ———————— the "language" between microelectronics

This secondary want to add to the Linux module is ESU ds18b20, about DS18B20 and embedded under the drive online data are many, the basic do not say. Let's talk about how DS18B20 communicates with our core chip. Ds18b20 and CPU are semiconductor chips, if you want to collect temperature through Ds18b20 to the CPU, they must exist between the effective communication and dialogue between people speak by language, they communicate also have language, Their language is the time to control and control the line that connects them, so they must have a rule to pull the high and low levels. We humans have grammar, too! There is a real grammar between them, and that is the sequence diagram in datasheet, which is the syntax of their dialogue, to hold the DS18B20, the core is in the timing diagram.

Take Ds18b20 's write timing as an example to describe this dialog process: when our microcontroller (master chip) want to DS18B20 perform tasks must tell him the task, this task is issued by the command, the microcontroller first pull low and ds18b20 connected to the IO port level and then continue 15us, It is like to say to Ds18b20: "The child, has the task to come, comes to take the task!" Ds18b20 heard, and then see if it is linked with the MCU "line" is now high or low, is high level of 1, The low level is 0. So it repeatedly received 8 times is a complete command, ds18b20 a look at this command data is 0xbe, I know that the single-chip to read the temperature data, hurriedly ready.

Ds18b20 The story of the first to talk about here! After all, these things need to be read and tried several times over and over again.

However, there is a lot of convenience in writing drivers to Ds18b20 under Linux, Ds18b20 is a time sensitive device, and the Udelay/ndelay in Linux provides us with a microsecond and nanosecond-level precision delay, which is handy.

In addition the system provides me with, s3c2410_gpio_setpin,s3c2410_gpio_getpin, etc. IO port operation function, can let us conveniently set the high and low level of the IO port. What is particularly noteworthy is the S3c_2410_getpin function, which when the IO port is set to input, which is the output, its return value is 0 or greater than 1, do not mistakenly think that the return value is 1. Especially when writing a byte to read, you need to pay special attention.

About embedded Linux driver ———— links and specifications

The core of the Linux drive is also the device itself, it is recommended to write a module of the device, first familiar with the device on the bare board of the driver, first of all the details of the driver of the device is clear, and then in Linux as long as the attention to specification and provide interface.

The previous article mainly introduces the application-to-kernel interface, that is, the relationship mapping between system functions and file_operations. This article details the link between the device and the file_operations, thus forming a complete Linux down-drive chain. That is, the device ——— >file_operations ———— > System function interface.

Ds18b20 is a character device, so here's a detailed description of how to create a character device and how this character device makes a relationship with File_operations.

Before starting our work, clap your forehead and wake up, what are we going to do?

First of all, to make sure that Linux is a disciplined organization, we want to add a new module under Linux, Linux certainly has this mechanism, so in the driver writing, it is important to keep in mind that a little rule, since we added Ds18b20 is a character device, So you want Linux to be so large a system with only three device types, does it provide a standard sample for a device of character type? Must have AH! This goods is Cdev, you can see that it is the abbreviation of the English character device, it is a structure, so must take a good look at it.

1 structCdev {2         structKobject kobj;3         structModule *owner;4         Const structFile_operations *ops;5         structlist_head list;6dev_t Dev;7Unsignedintcount;8};

Do not see, a look startled, originally this character device inside the member function although not many, but very useful ah! This first and four we will not say, it is automatically populated with the system. As a little white we make sure the rest of it is awesome! First saw file_operatition This member function, the heart is very excited, because File_operactions is a System application function interface Ah, it is the driving function of the transport hub AH! It appears in the structure of the character device, with the bottom of the mind to come to the conclusion that as long as we initialize the File_operations this member function, our character device and the file_operations set up a correspondence relationship. That way, when we use a character device, we know which file_operatitions to call.

Looking at dev_t, this is the device number character type of the character device, that is, our device must have a device number, just as we students want to have a school number, then why we do not use the name directly, but to get a device number it! Personally feel that the first number more conditioning, more easily detect whether the device number is re-used, the second Linux system in a master device number can correspond to multiple devices of the same type, in view of this, the device must also need a device number, or that sentence we say this useless, Linux drive development The biggest feature you have to adapt to the rules, People have this member function, we must fill in the requirements.

After a fierce analysis, we understand that birds, in fact, about the establishment of this new equipment and to get the recognition of the kernel, our core task is to CDEV this structure to initialize the completion. This way, with the device node name we can know the device number, with the device number we know the system function call when we should provide the file_operactions function of that device,

AO, the search was dead! Just fill in the blanks below!

We need to prepare for the blanks: the first is to get dev_t dev; device number

Step1: To create an acceptable driver for a Linux system, we must request a device number from Linux for our device.

Creating a device in the previous version of the 2.6 kernel is very easy, only need a function, you can request/allocate device memory/registration device, but in the future kernel mechanism, this convenient thing is abolished, because previously we set a device number, and then apply, This practice is prone to duplicate device number collisions when Linux is heavily applied. Therefore, in the new kernel mechanism, we recommend that you dynamically apply for the device number, so that the system will automatically find an unused device number. The more classic processing in Linux comes from this paragraph in the LDD book:

1 dev_t Dev;2dev=MKDEV (ds18b20_major,ds18b20_minor);3         if(ds18b20_major)4         {5Ret=register_chrdev_region (Dev,ds18b20_devs,"Ds18b20");6         }7         Else8         {9Ret=alloc_chrdev_region (&dev,ds18b20_minor,ds18b20_devs,"Ds18b20");TenDs18b20_major=MAJOR (dev); One}

dev_t is the type used to hold the device number, Mkdev is the primary and secondary device number converted to dev_t type.

Then is the detection of the main device number is 0, 0 is marked to be dynamic application device number. It is generally recommended to use the dynamic application method.

You can use major (dev) to obtain the main device number for the application after the application is complete.

Step2:file_operactions function of the writing, this is also a heavy priority, it is written because the previous has been accepted, there is not much to mention.

1 Static struct file_operations ds18b20_fops={2         . owner=this_module,3         . open=Ds18b20_open,4         . read=ds18b20_read,5 };

Of course, the Open,read function in this structure is not listed in this entrap posterity.

With the two steps above, we'll begin to fill in the blanks!

This time the blank is divided into two steps, need two functions, the first is Cdev_init, the second is cdev_add;

With these two functions we fill in the blanks: 1 call Cdev_init first, and fill in our file_operactions.

1 void cdev_init (structconststruct file_operations *fops)  2 3 Parameters: 4 5 Cdev: Previously I defined the CDEV structure; 6 7 FoPs: The file operation structure of the corresponding device.  8  9 return value: (the function may fail to see the return value is required)ten successful return 0, the demonstration returned the corresponding error code

2 The function that is called by the device number of the device that is filled in is Cdev_add

  1  int  cdev_add (struct  cdev *cdev, dev_t Dev, unsigned count)    3   parameter:   4   5  cdev: Specifies the CDEV structure to be added;    7    8   9  count: Add count devices starting from the device number dev.  10   return value:  12  13  successfully returns 0, failure returns the corresponding error code. 

This function associates our device number with the device so that our character device structure is populated and we add the character device to the kernel.

In this way our character device-the device number--file_operactions to establish a connection, and file_operactions is the system function of the hub, so in writing the application function of the system, We can know the file_operactions of the device by contacting the node of the device, because they become a ring, and the other section in the ring will be found through one of the sections.

This seems to be less device node Ah! The next thing to do is to create a device node, when creating a device node, we generally adopt the method of automatically creating the device node, Linux provides us with a struct class structure, this struct corresponds to a class, we use Class_ The CREATE function creates a class that is stored in the SYSFS and then calls Device_create to create a device node.

1 Static struct class*Ds18b20_class;2 Static structClass_device *Ds18b20_class_dev;
/
..........................................................
/
3Ds18b20_class=class_create (This_module,"Ds18b20_sys_class");4 if(Is_err (ds18b20_class))5 returnPtr_err (ds18b20_class);6Ds18b20_class_dev=device_create (Ds18b20_class,null,mkdev (Ds18b20_major,0), NULL,"Ds18b20");7 if(Unlikely (Is_err (Ds18b20_class_dev)))8 returnPtr_err (Ds18b20_class_dev);

In the process of doing this drive, encountered a lot of good articles, share to everyone

The first is a bit of jammy predecessors:

http://wenku.baidu.com/link?url=r_w8dizlv8cxd1s-6cml3n3kztrm9q3xegp9o6a3jjs86i_0- Mnsob8hyjvgks7c18y7upiphhwb7868g-8nm8cki4_bedkv31pf-o4z6fy

Jammy's code uses a lot of preprocessing, which makes code in code very readable and well worth learning, but there's a small error in the program. And Jammy was written in 09, many of the device's registrations are not registered in the new kernel.

The second place is a small white predecessor:

Http://blog.chinaunix.net/uid-25014876-id-59416.html

Not much to say, small white predecessors of the article Gray often serious, gray often comprehensive, well-organized, see a lot of my stuff is where he.

The third is sg131971 's predecessor:

Http://www.linuxidc.com/Linux/2012-02/54677.htm

This predecessor is straight male, directly on the code, its article character registration adopts the new mechanism, can be used for reference.

Embedded Linux Driver Development Add a temperature sensor module to your Linux system

Related Article

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.