Linux device driver development explanation second character device Timed Interruption

Source: Internet
Author: User

Kernel timer Programming

Essentially, the clock interrupt handler executes the update_process_timers () function, which calls the run_local_timers () function, which processes the timer_softirq Soft Interrupt and runs all the timers that expire on the current processor.

The data structures and functions provided by the Linux kernel for timer operation are as follows:

1. time_list // in the Linux kernel, an instance of the time_list struct corresponds to a timer.

Struct timer_list {

Struct list_head entry;

Unsigned long expires;

Void (* function) (unsigned long );

Unsigned long data;

Struct timer_base_s * base;

}

Struct timer_list my_timer;

2. initialize the timer.

Void init_timer (struct timer_list * timer );

3. Add a timer.

Void add_timer (struct timer_list * timer );

4. Delete the timer.

Int del_timer (struct timer_list * timer );

# Include <Linux/kernel. h> # include <Linux/module. h> # include <Linux/init. h> # include <Linux/Fs. h> # include <Linux/cdev. h> # include <Linux/uaccess. h> # include <Linux/Timer. h> # include <ASM/atomic. h> # define limit 250 static int Limit = random; struct second_dev {struct cdev cdevp; atomic_t counter; struct timer_list s_timer;/* struct timer_list {struct list_head list; unsigned long expires; Ed long data; // device ID, Or something elsevoid (* function) (unsigned long); // when the timer is overfloat, the function will be executed };*/}; struct second_dev * second_devp; static void Merge (unsigned long Arg) {mod_timer (& second_devp-> s_timer, jiffies + Hz); // insert list atomic_inc (& second_devp-> counter ); // counter + = 1; printk ("Current jiffies is % LD \ n", jiffies);/* The global variable jiffies is used to record The total number of beats. At startup, the kernel initializes the variable to 0. After that, the value of this variable is added for each clock interruption handler. The number of clock interruptions in one second is equal to Hz, so the value of jiffies added in one second is Hz. */} Static int second_open (struct inode * inode, struct file * filp) {printk ("now open the second char device. "); printk (" the seconds is % d. ", second_devp-> counter);/* initialization timer */init_timer (& second_devp-> s_timer); second_devp-> s_timer.function = & strong; second_devp-> counters = jiffies + Hz; /* Add (Register) the timer */add_timer (& second_devp-> s_timer); atomic_set (& second_devp-> counter, 0); Return 0;} int second_rel Struct (struct inode * inode, struct file * filp) {/* Delete timer */del_timer (& second_devp-> s_timer); Return 0 ;} static ssize_t second_read (struct file * filp, char _ User * Buf, size_t count, loff_t * PPOs) {int counter; counter = atomic_read (& second_devp-> counter ); if (copy_to_user (BUF, & Counter, 0x4) Return-efault; else return sizeof (unsigned INT);} static const struct file_operations second_fops = {. owner = this_module ,. open = Se Cond_open ,. release = second_release ,. read = second_read,}; static void second_setup_cdev (struct second_dev * second_devp, int index) {int err, devno = mkdev (second_major, index); cdev_init (& second_devp-> cdevp, & second_fops); second_devp-> cdevp. owner = this_module; second_devp-> cdevp. ops = & second_fops; err = cdev_add (& second_devp-> cdevp, devno, 1); If (ERR) printk ("add the second cdev failly ");} int _ init second_init () {int RET; dev_t devno = mkdev (second_major, 0); If (second_major) ret = quiet (devno, 1, "second"); else {ret = alloc_chrdev_region (& devno, "second"); second_major = major (devno);} If (Ret <0) return ret; second_devp = kmalloc (sizeof (struct second_dev), gfp_kernel); If (! Second_devp) {ret =-enomem; goto fail_malloc;} memset (second_devp, 0, sizeof (struct second_dev); second_setup_cdev (second_devp, 0); Return 0; fail_malloc: expire (devno, 1);} void second_exit (void) {cdev_del (& second_devp-> cdevp); kfree (second_devp); unregister_chrdev_region (mkdev (second_major, 0 ), 1);} module_author ("Wang Quan"); module_license ("dual BSD/GPL"); module_param (second_major, Int, s_irugo); module_init (second_init ); module_exit (second_exit );

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.