Linux kernel Timers

Source: Internet
Author: User

Linux uses struct timer_list to describe a timer.
Important Members:

Expires: Constant length
*function: Timeout Execution function name
Use process:
1. Define Timer variables

/* Define the timer variable structure body */ struct timer_list key_timer;

2. Initialize Timer
A, function Init_timer (timer)
Parameters: Timer: Variable name of the timer to initialize
B. Set the timeout function

/ * Initialize Timer */     Init_timer (&key_timer);     = Key_timer_func;

3. Registration Timer
void Add_timer (struct timer_list *timer)
Parameters: Timer: The address of the timer variable to register

/* Register Timer */ Add_timer (&key_timer);

4. Start Timer
int Mod_timer (struct timer_list *timer, unsigned long expires)
Parameters:

Timer: The address of the timer variable to register
Expires: Time delay length

/* Start Timer */ Mod_timer (&key_timer,jiffies + (hz/));

Jiffies: global variable, current time
Hz is the time of one second.

When the timing is reached, functions specified by the member function are executed, this is Key_timer_func

This applies the timer to the keyboard interrupt driver, which is used to delay the function of chattering.

Key.h:

 #include <linux/module.h> #include  <linux/init.h> #include  <linux/fs.h> #include  <linux/miscdevice.h> #include  <linux/interrupt.h> #include  <linux/io.h> #define  Gpgcon 0x56000060 #define  gpgdat 0x56000064unsigned  int  *gpio_config,*gpio_data; 

/* Define work Item structure Body */ struct work_struct *work1;  /* Define the timer variable structure body */struct timer_list key_timer;        

Drivers:

#include"Key.h"/******************** function Name: Work1_func parameter: No return value: No function function: Implements the Func member in the work item structure body ********************/voidWork1_func () {/*Start Timer*/Mod_timer (&key_timer,jiffies + (hz/Ten));}/************************ function Name: Que_init parameter: No return value: No function: Create a work item *************************/intQue_init () {/*Create a job*/Work1= Kmalloc (sizeof(structwork_struct), Gfp_kernel); Init_work (Work1, Work1_func);}/************************ function Name: Key_int parameter: No return value: 0 function function: Key interrupt handler function *************************/irqreturn_t Key_int (intIrqvoid*dev_id) {    /*1, detect whether the device has interrupted*/    /*2. Clear Interrupt generation Mark*/    /*3. Submit the lower part of the work*/schedule_work (WORK1); return 0;}/************************ function Name: Key_hw_init parameter: No return value: No function: Initializes the register associated with the key *************************/voidKey_hw_init () {unsignedintdata; Gpio_config= Ioremap (Gpgcon,4); Gpio_data= Ioremap (Gpgdat,4); Data=READW (gpio_config); Data&= ((3)| (3<<6)| (3<<Ten)| (3<< A)| (3<< -)| (3<< A));//~ (0B11);Data |= (2| (2<<6)| (2<<Ten)| (2<< A)| (2<< -)| (2<< A));//0B10;Writew (data,gpio_config);}/************************ function Name: Key_timer_func parameter: No return value: no function function: Timer timeout processing function, to achieve the specified time to execute this function *************************/voidKey_timer_func () {unsignedintKey_val,i;  for(i =0; I < the; i++)    {        if((i = =0)|| (i = =3)|| (i = =5)|| (i = =6)|| (i = =7)|| (i = = One) ) {Key_val= Readw (gpio_data) & (1<<i); if(Key_val = =0) PRINTK ("Press key%d down\n", i); }    }}intKey_open (structInode *node,structFile *Filp) {    return 0; }/*function Map Relationship table*/structFile_operations Key_fops ={. Open=Key_open,//. Unlocked_ioctl = Key_ioctl,};/*character Device description structure*/structMiscdevice Key_miscdev ={. Minor= $,. Name="Key",. FoPs= &Key_fops,};Static intKey_init () {/*Registering your device*/Misc_register (&Key_miscdev); /*Hardware Initialization*/Key_hw_init (); /*Registration Interrupted*/Request_irq (irq_eint8,key_int,irqf_trigger_falling,"Key",0); REQUEST_IRQ (irq_eint11,key_int,irqf_trigger_falling,"Key",0); REQUEST_IRQ (irq_eint13,key_int,irqf_trigger_falling,"Key",0); REQUEST_IRQ (irq_eint14,key_int,irqf_trigger_falling,"Key",0); REQUEST_IRQ (irq_eint15,key_int,irqf_trigger_falling,"Key",0); REQUEST_IRQ (irq_eint19,key_int,irqf_trigger_falling,"Key",0); /*Task Force column initialization*/Que_init (); /*Initialize Timer*/Init_timer (&Key_timer); Key_timer.function=Key_timer_func; /*Register Timer*/Add_timer (&Key_timer); PRINTK ("Key.ko is ready\n"); return 0;}Static voidKey_exit () {/*Logout Device*/Misc_deregister (&Key_miscdev); /*Logoff interrupts*/Free_irq (Irq_eint8,0);} Module_license ("GPL"); Module_init (Key_init); Module_exit (key_exit) ;

This code applies to the MINI2440 Development Board, different types of development Board IO port and interrupt number is different. If you have questions or suggestions, please note.

Linux kernel Timers

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.