1.linux内核定时器基本结构和函数
1)struct timer_list 一个struct timer_list对应了一个定时器。
#include
以下列出常用的接口:
struct timer_list
{
/*....*/
unsigned long expires;//定时器服务函数开始执行时间
void (*function)(unsigned long);//定义一个指向定时器服务函数的指针function,服务函数有一个 unsigned long的参数,并且返回void
unsigned long data;//定时时间到时,data参数会传入服务函数
}
void init_timer(struct timer_list* timer)//初始化一个定时器
-----------使用定时器的步骤--------------
struct timer_list my_timer_list;//定义一个定时器,可以把它放在你的设备结构中
init_timer(&my_timer_list);//初始化一个定时器
my_timer_list.expire=jiffies+HZ;//定时器1s后运行服务程序
my_timer_list.function=timer_function;//定时器服务函数
add_timer(&my_timer_list);//添加定时器
void timer_function(unsigned long)//写定时器服务函数
del_timer(&my_timer_list);//当定时器不再需要时删除定时器
del_timer_sync(&my_timer_list);//基本和del_timer一样,比较适合在多核处理器使用,一般推荐使用del_timer_sync
-------------------------------------------------
2.以下是一个定时1s的驱动程序,直接上代码
-
#include <linux/miscdevice.h>
-
#include <linux/delay.h>
-
#include <asm/irq.h>
-
//#include <mach/regs-gpio.h>
-
//#include <mach/hardware.h>
-
#include <linux/kernel.h>
-
#include <linux/module.h>
-
#include <linux/init.h>
-
#include <linux/mm.h>
-
#include <linux/fs.h>
-
#include <linux/types.h>
-
#include <linux/delay.h>
-
#include <linux/moduleparam.h>
-
#include <linux/slab.h>
-
#include <linux/errno.h>
-
#include <linux/ioctl.h>
-
#include <linux/cdev.h>
-
#include <linux/string.h>
-
#include <linux/list.h>
-
#include <linux/pci.h>
-
#include <asm/uaccess.h>
-
#include <asm/atomic.h>
-
#include <asm/unistd.h>
-
#include <asm/io.h>
-
#include <asm/system.h>
-
#include <asm/uaccess.h>
-
#define TIMER_MAJOR 300
-
#define TIMER_MINOR 0
-
dev_t timer_dev_t;//设备号
-
dev_t timer_dev_major=TIMER_MAJOR;
-
dev_t timer_dev_minor=TIMER_MINOR;
-
struct TIMER_DEV
-
{
-
struct cdev cdev;
-
atomic_t count;
-
struct timer_list timer_list;
-
-
};
-
struct TIMER_DEV* timer_dev;
-
//---------timer interrupt function----------------
-
static void timer_function(unsigned long data)
-
{
-
mod_timer(&(timer_dev->timer_list),jiffies+HZ);//重新设置时间
-
printk("current jiffies is %ld,count=%d\n",jiffies,timer_dev->count);
-
//(timer_dev->count)++;
-
atomic_inc(&(timer_dev->count));
-
}
-
//--------timer release function--------------------
-
static int timer_release(struct inode* inode, struct file* filp)
-
{
-
del_timer_sync(&(timer_dev->timer_list));
-
return 0;
-
}
-
-
//----------------file open function-----------------
-
static int timer_open(struct inode* inode,struct file* filp)
-
{
-
init_timer(&(timer_dev->timer_list));//初始化定时器
-
timer_dev->timer_list.function=timer_function;//设置定时器处理函数
-
timer_dev->timer_list.expires=jiffies+HZ;//处理函数1s后运行
-
add_timer(&timer_dev->timer_list);//添加定时器
-
atomic_set(&(timer_dev->count),0);
-
return 0;
-
}
-
//--------------------------------------
-
-
//----------------timer_read function---------------
-
static int timer_read(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)
-
{
-
-
unsigned int counter=atomic_read(&(timer_dev->count));
-
if(copy_to_user(buf,(unsigned int*)&counter,sizeof(unsigned int)))
-
{
-
printk("copy to user error\n");
-
goto out;
-
}
-
return (sizeof(unsigned int));
-
out:
-
return (-EFAULT);
-
-
}
-
-
struct file_operations timer_ops={
-
.owner=THIS_MODULE,
-
.open=timer_open,
-
.read=timer_read,
-
.release=timer_release,
-
};
-
static int __init timer_init(void)
-
{
-
int ret;
-
-
if(TIMER_MAJOR)//主设备号大于0,静态申请设备号
-
{
-
timer_dev_t=MKDEV(TIMER_MAJOR,TIMER_MINOR);
-
ret=register_chrdev_region(TIMER_MAJOR,1,"timer_dev");//first,count,name
-
}
-
else
-
{
-
ret=alloc_chrdev_region(&timer_dev_t,0,1,"time_dev");
-
timer_dev_major=MAJOR(timer_dev_t);
-
}
-
if(ret<0)
-
{
-
printk("can't get major %d\n",timer_dev_major);
-
return ret;
-
}
-
//-----------------------------------------------------------
-
timer_dev=kmalloc(sizeof(struct TIMER_DEV),GFP_KERNEL);
-
memset(timer_dev,0,sizeof(struct TIMER_DEV));
-
cdev_init(&(timer_dev->cdev),&timer_ops);
-
cdev_add(&(timer_dev->cdev),timer_dev_t,1);
-
printk("init timer_dev success\n");
-
return 0;
-
-
-
}
-
static void __exit timer_exit(void)
-
{
-
kfree(timer_dev);
-
cdev_del(&(timer_dev->cdev));
-
unregister_chrdev_region(MKDEV(TIMER_MAJOR,0),1);
-
}
-
module_init(timer_init);
-
module_exit(timer_exit);
阅读(2265) | 评论(0) | 转发(0) |