Xunwei Embedded Linux Driver Development Notes (8) - Kernel Timer

kernel timer

two. Basic knowledge of Linux kernel timer
The Linux kernel uses the timer_list structure to represent the kernel timer. The timer_list is defined in the file include/linux/timer.h and is defined as follows:

struct timer_list {
    
     
			struct list_head entry;
			unsigned long expires; /* 定时器超时时间,不是时长,单位是节拍数 */
			struct tvec_base *base;
			void (*function)(unsigned long); /* 定时处理函数 */
			unsigned long data; /* 要传递给 function 函数的参数 */ 
			int slack; 
};

In this struct, there are a few parameters to focus on. One is the expiration time of expires, the unit is the number of beats. It is equal to the current clock beat count of the timing (stored in the global variable jiffies of the system) + the number of clock beats corresponding to the timing duration.
So how do you convert time to beat count?
Example: Timing 1 second from now: There is a macro HZ in the kernel, which represents the number of clock beats corresponding to one second, then you can use this macro to convert the time into beats. So, the timing of 1 second is: expires = jiffies + 1*HZ. The value of HZ can be set, that is to say, the number of clock beats corresponding to one second can be set, and the Linux kernel will use CONFIG_HZ to set the system clock.
Open the file include/asm generic/param.h with the following contents:

# undef HZ 
# define HZ CONFIG_HZ 
# define USER_HZ 100宏 

HZ is CONFIG_HZ, so HZ=100, which means that the number of beats per second is 100. When compiling the Linux kernel, you can set the system beat rate through the graphical interface, and open the configuration interface according to the following path. insert image description hereFrom the figure above, you can find that the optional system beat rate is 100Hz, 200Hz, 250Hz, 300Hz, 500Hz and 1000Hz. Default is 100Hz.
The second parameter to be concerned about is the function timeout handler, which is not a hardware interrupt service routine. Prototype: void function(unsigned long data);
The third parameter is the parameter data passed to the timeout processing function, which can convert the address of a variable into unsigned long.

3. Linux kernel timer related operation functions

1. Time conversion function
<1>ms is converted to clock beat function

 unsigned long msecs_to_jiffies(const unsigned int m) 

<2>us is converted to a clock beat function

 unsigned long usecs_to_jiffies(const unsigned int u) 

Example:
<1> Timing 10ms
calculation: jiffies +msecs_to_jiffies(10)

<2> Timing 10us
calculation: jiffies +usecs_to_jiffies(10)

<2> Macro DEFINE_TIMER
prototype: #define DEFINE_TIMER(_name, _function, _expires, _data)
Function: statically define structure variables and initialize initialization function, expires, data members.
Parameters: _name variable name;
_function timeout processing function; _data
parameter passed to the timeout processing function _expires time to point, generally need to be re-initialized before starting the timing.

<3>Add_timer function
Prototype: void add_timer(struct timer_list *timer)
Function: The add_timer function is used to register the timer with the Linux kernel. After using the add_timer function to register the timer with the kernel, the timer will start running
Parameters:
timer : To register 's timer.

<4>del_timer function
Prototype: int del_timer(struct timer_list * timer)
Function: The del_timer function is used to delete a timer, whether the timer is activated or not, you can use this function to delete it. On a multiprocessor system, the timer may be running on other processors, so before calling the del_timer function to delete the timer, wait for the other processor's timer handler function to exit
the timer: timer to be deleted.
Return value: 0, the timer has not been activated; 1, the timer has been activated.

<5>mod_timer function
Function prototype: int mod_timer(struct timer_list *timer, unsigned long expires)
Function: mod_timer function is used to modify the timing value, if the timer has not been activated, the mod_timer function will activate the timer!
Parameters:
timer : The timer to modify the timeout period (timed value).
expires : Modified timeout period.
Return value: 0, the timer is not activated before calling mod_timer function; 1, the timer has been activated before calling mod_timer function.

practice course

Every 1s, print a sentence.

Timer initialization:

#include <linux/timer.h>

DEFINE_TIMER(test_timer,timer_function,0,0);

static int hello_init(void)
{
    
    
    printk("hello world\n");

    test_timer.expires = jiffies + 1*HZ;
		//jiffies相当于手机的当前时刻!!!
    add_timer(&test_timer);//启动定时器

    return 0;
}

A timeout handler to continue loading after a timeout:

static void timer_function(unsigned long data)
{
    
    
    printk("This is timer function\n");
		//重新开启定时器
    mod_timer(&test_timer,jiffies + 1*HZ);
}

Button debounce

#include <linux/timer.h>

DEFINE_TIMER(test_timer,timer_function,0,0);

static void timer_function(unsigned long data)
{
    
    
    //超时时间处理函数
}

Interrupt handler:

irq_handler_t test_key(int irq, void *args)//中断处理函数
{
    
    
    test_timer.expires = jiffies + msecs_to_jiffies(20) ;
    //jiffies相当于手机的当前时刻!!!
    add_timer(&test_timer);//启动定时器

    return IRQ_HANDLED;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324117830&siteId=291194637