Timer for Linux Device Driver Programming

Source: Internet
Author: User
Linux Device Driver Programming Timer
Author:
Source: Linux
Responsible editor: Fangzhou

 

Related Topics:
Linux device driver development

The Linux kernel defines a timer_list structure, which can be used in the driver:

Struct timer_list {
Struct list_head list;
Unsigned long expires; // timer expiration time
Unsigned long data; // The timer processing function is input as a parameter.
Void (* function) (unsigned long );
};

The following are the timer API functions:

Add Timer

Void add_timer (struct timer_list * timer );

Delete Timer

Int del_timer (struct timer_list * timer );

  
Modify the expire of the timer

Int mod_timer (struct timer_list * timer, unsigned long expires );

The general process of using a timer is:

(1) timer and function writing;

(2) assign values to expires, data, and function of timer;

(3) Call add_timer to add timer to the list;

(4) When the timer expires, the function is executed;

(5) Call del_timer, mod_timer to delete timer or modify the expires of timer where Timer control is involved in the program.

For details, refer to the timer section of the keyboard driver in drivers/Char/keyboard. C:

...
# Include <Linux/Timer. h>
...
Static struct timer_list key_autorepeat_timer =
{
Function: key_callback
};

Static void

Kbd_processkeycode (unsigned char keycode, char up_flag, int autorepeat)
{
Char raw_mode = (KBD-> kbdmode = vc_raw );
If (up_flag ){
Rep = 0;
If (! Test_and_clear_bit (keycode, key_down ))
Up_flag = kbd_unexpected_up (keycode );
} Else {
Rep = test_and_set_bit (keycode, key_down );
/* If the keyboard autorepeated for us, ignore it.
* We do our own autorepeat processing.
*/
If (REP &&! Autorepeat)
Return;
}
If (kbd_repeatkeycode = keycode |! Up_flag | raw_mode ){
Kbd_repeatkeycode =-1;
Del_timer (& key_autorepeat_timer );
}
...
/*
* Calculate the next time when we have to do some autorepeat
* Processing. Note that we do not do autorepeat Processing
* While in raw mode but we do autorepeat processing in
* Medium RAW mode.
*/
If (! Up_flag &&! Raw_mode ){
Kbd_repeatkeycode = keycode;
If (vc_kbd_mode (KBD, vc_repeat )){
If (REP)
Key_autorepeat_timer.expires = jiffies + kbd_repeatinterval;
Else
Key_autorepeat_timer.expires = jiffies + kbd_repeattimeout;
Add_timer (& key_autorepeat_timer );
}
}
...
}

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.