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 );
}
}
...
}