当前位置: 首页 > 工具软件 > Timer > 使用案例 >

Linux内核定时器---init_timer()、add_timer()与mod_timer()使用

鲁宏爽
2023-12-01

一、内核定时器API函数

init_timer(timer):初始化timer。

setup_timer(timer, fn, data):与init_timer()类似,fn为定时器回调函数,data为回调函数的参数。

void add_timer(struct timer_list *timer)
用于向 Linux 内核注册定时器,使用 add_timer 函数向内核注册定时器以后,定时器就会开始运行。

int mod_timer(struct timer_list *timer, unsigned long expires)
用于修改定时值,如果定时器还没有激活的话, mod_timer 函数会激活定时器。
timer:要修改超时时间的定时器。
expires:修改后的超时时间。
返回值: 0,调用 mod_timer 函数前定时器未被激活; 1,调用 mod_timer 函数前定时器已被激活。

del_timer(struct timer_list * timer)
用于删除一个定时器,不管定时器有没有被激活,都可以使用此函数删除。

上面定义的timer为timer_list结构体,结构体定义如下:

struct timer_list {
	/*
	 * All fields that change during normal runtime grouped to the
	 * same cacheline
	 */
	struct hlist_node	entry;
	unsigned long		expires;
	void			(*function)(unsigned long);
	unsigned long		data;
	u32			flags;

#ifdef CONFIG_TIMER_STATS
	int			start_pid;
	void			*start_site;
	char			start_comm[16];
#endif
#ifdef CONFIG_LOCKDEP
	struct lockdep_map	lockdep_map;
#endif
};

/* by 面朝大海0902 */

二、内核定时器使用方法

struct timer_list timer; /* 定义定时器 */

/* 定时器回调函数 */
void function(unsigned long arg)
{
/*
* 定时器处理代码
*/

/* 如果需要定时器周期性运行的话就使用 mod_timer
* 函数重新设置超时值并且启动定时器。
*/
mod_timer(&dev->timertest, jiffies + msecs_to_jiffies(2000));
}

/* 初始化函数 */
void init(void)
{
init_timer(&timer); /* 初始化定时器 */

timer.function = function; /* 设置定时处理函数 */
timer.expires=jffies + msecs_to_jiffies(2000);/* 超时时间 2 秒 */
timer.data = (unsigned long)&dev; /* 将设备结构体作为参数 */

/*上面也可以用setup_timer()函数*/

add_timer(&timer); /* 启动定时器 */
}

/* 退出函数 */
void exit(void)
{
del_timer(&timer); /* 删除定时器 */
/* 或者使用 */
del_timer_sync(&timer);
}

此处参考《【正点原子】I.MX6U嵌入式Linux驱动开发指南》

三、驱动使用实例

/* by 面朝大海0902 */

#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

struct gpio_key{
	int gpio;
	struct gpio_desc *gpiod;
	int flag;
	int irq;
};

static struct gpio_key *ptr_gpio_key;

static int major =0;
static struct class *key_class;
static int g_key_value = 0;
struct fasync_struct *key_fasync;

/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;

#define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void)
{
	return (r == w);
}

static int is_key_buf_full(void)
{
	return (r == NEXT_POS(w));
}

static void put_key(int key)
{
	if (!is_key_buf_full())
	{
		g_keys[w] = key;
		w = NEXT_POS(w);
	}
}

static int get_key(void)
{
	int key = 0;
	if (!is_key_buf_empty())
	{
		key = g_keys[r];
		r = NEXT_POS(r);
	}
	return key;
}

static DECLARE_WAIT_QUEUE_HEAD(key_wait);
struct timer_list key_timer;

void timer_function(unsigned long arg)
{
	int value;
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	struct gpio_key *ptr_gpio_key_temp = arg;
	value = gpiod_get_value(ptr_gpio_key_temp->gpiod);
	g_key_value = (ptr_gpio_key_temp->gpio << 8) | value;
	printk(KERN_INFO "g_key_value is %d \r\n", g_key_value);
	put_key(g_key_value);
	wake_up_interruptible(&key_wait);
	kill_fasync(&key_fasync, SIGIO, POLLIN);
}

static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	wait_event_interruptible(key_wait, !is_key_buf_empty());
	g_key_value = get_key();
	copy_to_user(buf, &g_key_value, 4);
	return 4;
}

/* by 面朝大海0902 */
static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &key_wait, wait);//非阻塞函数
	
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static int key_drv_fasync(int fd, struct file *file, int on)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	if(fasync_helper(fd, file, on, &key_fasync) >= 0)
		return 0;
	else
		return -EIO;
}

static struct file_operations key_drv =
{
	.owner = THIS_MODULE,
	.read  = key_drv_read,
	.poll  = key_drv_poll,
	.fasync = key_drv_fasync,
};

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	key_timer.data =(unsigned long) dev_id;
	mod_timer(&key_timer, jiffies + HZ/4);
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	return IRQ_HANDLED;
}
/* by 面朝大海0902 */
static int key_probe(struct platform_device *pdev)
{
	int count = 0;
	int i=0;
	enum of_gpio_flags flag;
	struct device_node *node = pdev->dev.of_node;
	
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);
	ptr_gpio_key = kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
	
	for(i=0;i<count;i++)
	{
		ptr_gpio_key[i].gpio = of_get_gpio_flags(node, i, &flag);
		if(ptr_gpio_key[i].gpio < 0)
		{
			printk(KERN_ERR "of_get_gpio_flags is err\r\n");
		}
		ptr_gpio_key[i].gpiod = gpio_to_desc(ptr_gpio_key[i].gpio);
		ptr_gpio_key[i].flag = flag & OF_GPIO_ACTIVE_LOW;
		ptr_gpio_key[i].irq = gpio_to_irq(ptr_gpio_key[i].gpio);
		request_irq(ptr_gpio_key[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpio_key", &ptr_gpio_key[i]);
	}
	
	major = register_chrdev(0, "my_keydrv", &key_drv);
	key_class = class_create(THIS_MODULE, "my_key_class");
	if(IS_ERR(key_class))
	{
		printk(KERN_ERR "class_create is err\r\n");
	}
	
	device_create(key_class, NULL, MKDEV(major, 0), NULL, "my_key%d", 0);
	init_timer(&key_timer);
	key_timer.function = timer_function;
	key_timer.expires = ~0;
	add_timer(&key_timer);

	return 0;	
}
/* by 面朝大海0902 */

static int key_remove(struct platform_device *pdev)
{
	int count = 0;
	int i = 0;
	struct device_node *node = pdev->dev.of_node;

	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);

	device_destroy(key_class, MKDEV(major, 0));
	class_destroy(key_class);
	unregister_chrdev(major, "my_keydrv");
	for(i=0;i<count;i++)
	{
		free_irq(ptr_gpio_key[i].irq, &ptr_gpio_key[i]);
	}

	kfree(ptr_gpio_key);
	del_timer(&key_timer);

	return 0;	
}


static const struct of_device_id my_key[] =
{
	{.compatible = "my,key_driver"},
	{},
};


static struct platform_driver key_driver =
{
	.probe  = key_probe,
	.remove = key_remove,
	.driver = 
	{
		.name = "key_gpio",
		.of_match_table = my_key,
	},
};


static int __init my_key_init(void)
{
	int result;
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	result = platform_driver_register(&key_driver);
	return result;
}
/* by 面朝大海0902 */

static void __exit my_key_exit(void)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	platform_driver_unregister(&key_driver);
}

module_init(my_key_init);
module_exit(my_key_exit);

MODULE_LICENSE("GPL");

上面的程序比较啰嗦,大家捡重点看,参照内核定时器的使用方法,重点关注定时器几个API函数如何使用就可以,程序主要是在按键按下后启动250ms的定时器以去抖后读取按键电平值。程序也是在上一节Linux异步通知—fasync_helper()、kill_fasync()函数介绍与使用,驱动的基础上进行修改的,有兴趣的可以参考下。

四、实验结果

运行读取key值的应用(应用程序源码参考Linux异步通知—signal()、fcntl()函数介绍与使用),2个按键分别按下打印如下:

[root@:/mnt]# ./key-fasync-test /dev/my_key0
[  348.849247] /home/book/code/test/key-timer.c key_drv_fasync line is 115
by mianchaodahai 0902
[  353.465002] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  353.497099] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  353.754661] /home/book/code/test/key-timer.c timer_function line is 85
[  353.761511] g_key_value is 33025
[  353.766473] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x8101
by mianchaodahai 0902
[  356.423750] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  356.674635] /home/book/code/test/key-timer.c timer_function line is 85
[  356.681481] g_key_value is 33024
[  356.685883] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x8100
by mianchaodahai 0902
[  356.696007] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  356.954636] /home/book/code/test/key-timer.c timer_function line is 85
[  356.961483] g_key_value is 33025
[  356.965885] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x8101
by mianchaodahai 0902
[  358.266013] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  358.524630] /home/book/code/test/key-timer.c timer_function line is 85
[  358.531524] g_key_value is 33024
[  358.536222] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x8100
by mianchaodahai 0902
[  359.783520] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  360.034661] /home/book/code/test/key-timer.c timer_function line is 85
[  360.041512] g_key_value is 33025
[  360.045921] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x8101
by mianchaodahai 0902
[  363.732414] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  363.984584] /home/book/code/test/key-timer.c timer_function line is 85
[  363.991372] g_key_value is 28160
[  363.995392] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x6e00
by mianchaodahai 0902
[  365.786148] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  366.044637] /home/book/code/test/key-timer.c timer_function line is 85
[  366.051484] g_key_value is 28161
[  366.055900] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x6e01
by mianchaodahai 0902
[  367.154126] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  367.404645] /home/book/code/test/key-timer.c timer_function line is 85
[  367.411495] g_key_value is 28160
[  367.415914] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x6e00
by mianchaodahai 0902
[  368.834009] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  368.840782] /home/book/code/test/key-timer.c gpio_key_isr line is 134
[  369.094551] /home/book/code/test/key-timer.c timer_function line is 85
[  369.101311] g_key_value is 28161
[  369.105202] /home/book/code/test/key-timer.c key_drv_read line is 97
get button : 0x6e01
by mianchaodahai 0902
^C[  370.907659] /home/book/code/test/key-timer.c key_drv_fasync line is 115

可以看到按键按下后先触发中断回调函数gpio_key_isr(),间隔250ms左右(我们设置的超时时间一致)定时器回调函数timer_function()被调用。
/* by 面朝大海0902 */

这个示例代码可以学习内核定时器的相关使用,大家有兴趣也可以看下应用层定时器的使用Linux应用层定时器timer使用—timerfd_create()、timerfd_settime()、timerfd_gettime()

 类似资料: