|
文件: |
timer.rar |
大小: |
8KB |
下载: |
下载 | |
//test.c文件
/*
Linux内核中定义了一个timer_list结构,我们在驱动程序中可以利用之:
struct timer_list {
struct list_head list;
unsigned long expires; //定时器到期时间
unsigned long data; //作为参数被传入定时器处理函数
void (*function)(unsigned long);
};
下面是关于timer的API函数:
增加定时器
void add_timer(struct timer_list * timer);
删除定时器
int del_timer(struct timer_list * timer);
修改定时器的expire
int mod_timer(struct timer_list *timer, unsigned long expires);
使用定时器的一般流程为:
(1)timer、编写function;
(2)为timer的expires、data、function赋值;
(3)调用add_timer将timer加入列表;
(4)在定时器到期时,function被执行;
(5)在程序中涉及timer控制的地方适当地调用del_timer、mod_timer删除timer或修改timer的expires。
我们可以参考drivers\char\keyboard.c中键盘的驱动中关于timer的部分:
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int callback(unsigned long data)
{
printk(KERN_INFO "timer callback the argv is =%d\n",data);
return 0;
}
static struct timer_list test_timer =
{
function: callback,
};
unsigned int test_major = 0;
static int globalvar_count = 0;
static ssize_t read_test(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
int left;
printk(KERN_INFO "read_test\n");
printk(KERN_INFO "timer starting ...\n");
test_timer.expires = jiffies + 20;
test_timer.data=20000;
add_timer(&test_timer);
printk(KERN_INFO "timer end\n");
if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT )
return -EFAULT;
for(left = count ; left > 0 ; left--) {
__put_user('1',buf);
buf++;
}
return count;
}
static ssize_t write_test(struct file *file, const char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_INFO "write_test\n");
return count;
}
static int open_test(struct inode *inode,struct file *file )
{
printk("globalvar open\n");
if (globalvar_count){
return - EBUSY;
}
globalvar_count++;
printk(KERN_INFO "open_test\n");
MOD_INC_USE_COUNT;
return 0;
}
static int release_test(struct inode *inode,struct file *file )
{
printk(KERN_INFO "release_test\n");
MOD_DEC_USE_COUNT;
globalvar_count--;
return 0;
}
struct file_operations test_fops = {
read:read_test,
write:write_test,
open: open_test,
release:release_test
};
int init_module(void)
{
int result;
result = register_chrdev(0, "test", &test_fops);
if (result < 0) {
printk(KERN_INFO "test: can't get major number\n");
return result;
}
if (test_major == 0) test_major = result; /* dynamic */
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(test_major, "test");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("BECKHAM");
--------------------------------------
//Makefile文件
CC=gcc
MODCFLAGS:=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include
test.o:test.c
$(CC) $(MODCFLAGS) -c test.c
--------------------------------------
//测试文件
#include
#include
#include
#include
main()
{
int testdev;
int i;
int j;
char *buf=malloc(150);
testdev = open("/dev/test",O_RDWR);
if ( testdev == -1 )
{
perror(open);
printf("Cann't open file \n");
exit(0);
}
j=read(testdev,buf,10);
//for (i = 0; i < 10;i++)
//printf("%d\n",buf);
strcat(buf,"\0");
printf("%d===%s\n",j,buf);
close(testdev);
}
---------------------------------------
编译后加载模块
rmmod test
insmod test.o
rm /dev/test -f
mknod /dev/test c 254 0
gcc -o test t.c
./test
dmesg | tail
运行dmesg后可以看到经过定时后执行了callback函数。
阅读(1832) | 评论(1) | 转发(0) |