linux设备驱动开发详解p207内核定时器---秒字符设备
代码附件: timer_me.rar 将rar修改为tar.bz2
=====================================================================
-
#include <linux/module.h>
-
#include <linux/init.h>
-
#include <linux/kernel.h>
-
#include <linux/fs.h>//container_of
-
#include <linux/types.h>//atomic_t
-
#include <linux/cdev.h>//cdev
-
#include <asm/uaccess.h>//put_user
-
#include <linux/slab.h>//kmalloc
-
#include <linux/moduleparam.h>//moudule_param
-
-
static int second_major =240;
-
module_param(second_major,int,00644);//
-
-
struct second_dev{
-
struct cdev cdev;
-
atomic_t counter;
-
struct timer_list s_timer;
-
};
-
struct second_dev *second_devp;
-
-
static void second_timer_handler(unsigned long arg)
-
{
-
mod_timer(&second_devp->s_timer,jiffies+HZ);
-
atomic_inc(&second_devp->counter);//puls 1
-
printk(KERN_NOTICE"currretn jiffies is %ld\n",jiffies);
-
}
-
-
int second_open(struct inode *inode,struct file *filp)
-
{
-
struct second_dev *dev;
-
dev = container_of(inode->i_cdev,struct second_dev,cdev);
-
filp->private_data = dev;
-
-
init_timer(&second_devp->s_timer);
-
second_devp->s_timer.function = &second_timer_handler;
-
second_devp->s_timer.expires = jiffies + HZ;
-
add_timer(&second_devp->s_timer);
-
atomic_set(&second_devp->counter,0);
-
return 0;
-
}
-
-
int second_release(struct inode *inode,struct file *filp)
-
{
-
del_timer(&second_devp->s_timer);
-
return 0;
-
}
-
-
static ssize_t second_read(struct file *filp, char __user *buf,size_t count,off_t *ppos)
-
{
-
int counter;
-
struct second_dev *dev = filp->private_data;
-
-
// counter = atomic_read(&second_devp->counter);
-
counter = atomic_read(&dev->counter);
-
if(put_user(counter,(int *)buf))
-
return -EFAULT;
-
else
-
return sizeof(unsigned int);
-
}
-
-
static const struct file_operations second_fops={
-
.owner = THIS_MODULE,
-
.open = second_open,
-
.release = second_release,
-
.read = second_read,
-
};
-
-
static void second_setup_cdev(struct second_dev *dev ,int index)
-
{
-
int err;
-
int devno = MKDEV(second_major,index);
-
cdev_init(&dev->cdev,&second_fops);
-
dev->cdev.owner = THIS_MODULE;
-
err = cdev_add(&dev->cdev,devno,1);
-
if(err)
-
printk(KERN_INFO"error %d adding LED %d\n",err,index);
-
}
-
-
static int __init second_init(void)
-
{
-
int ret;
-
dev_t devno = MKDEV(second_major,0);
-
-
if(second_major)
-
{
-
ret = register_chrdev_region(devno,1,"second");
-
}
-
else
-
{
-
ret = alloc_chrdev_region(&devno,0,1,"second");
-
second_major = MAJOR(devno);
-
}
-
-
if(ret < 0)
-
return ret;
-
-
second_devp = kmalloc(sizeof(struct second_dev),GFP_KERNEL);
-
if(!second_devp)
-
{
-
ret = -ENOMEM;
-
goto fail_malloc;
-
}
-
-
memset(second_devp,0,sizeof(struct second_dev));
-
second_setup_cdev(second_devp,0);
-
return 0;
-
fail_malloc:
-
unregister_chrdev_region(devno,1);
-
return ret;
-
}
-
-
static void __exit second_exit(void)
-
{
-
cdev_del(&second_devp->cdev);
-
kfree(second_devp);
-
unregister_chrdev_region(MKDEV(second_major,0),1);
-
}
-
-
MODULE_LICENSE("GPL");
-
module_init(second_init);
-
module_exit(second_exit);
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <fcntl.h>//O_RDONLY
-
-
int main(int argc, char **argv)
-
{
-
int fd;
-
int counter = 0;
-
int old_counter = 0;
-
-
fd = open("/dev/second",O_RDONLY);
-
if(fd != -1)
-
{
-
while(1)
-
{
-
read(fd,&counter,sizeof(unsigned int));
-
if(counter != old_counter)
-
{
-
printf("secons after open /dev/second:%d\n",counter);
-
old_counter = counter;
-
}
-
}
-
}
-
else
-
{
-
printf("device open faile\n");
-
exit(EXIT_FAILURE);
-
}
-
}
使用方法:
-
ywx@ywx:~/Desktop/module/baohua/timer/timer_me$ ls
-
app modules.order timer.c timer.mod.c timer.o
-
Makefile Module.symvers timer.ko timer.mod.o
-
ywx@ywx:~/Desktop/module/baohua/timer/timer_me$ sudo insmod ./timer.ko
-
ywx@ywx:~/Desktop/module/baohua/timer/timer_me$ cat /proc/devices | grep second
-
240 second
-
ywx@ywx:~/Desktop/module/baohua/timer/timer_me$ sudo mknod /dev/second c 240 0
-
root@ywx:/home/ywx/desktop/module/baohua/timer/timer_me/app# ls
-
app app.c app.c.old app.o Makefile
-
root@ywx:/home/ywx/desktop/module/baohua/timer/timer_me/app# ./app
-
secons after open /dev/second:1
-
secons after open /dev/second:2
-
secons after open /dev/second:3
-
^C
-
root@ywx:/home/ywx/desktop/module/baohua/timer/timer_me/app#
-
root@ywx:/home/ywx/desktop/module/baohua/timer/timer_me/app# dmesg | tail -20
-
[ 6977.244239] currretn jiffies is 1669311
-
[ 6978.245753] currretn jiffies is 1669561
-
[ 6979.244045] currretn jiffies is 1669811
-
[ 6980.244742] currretn jiffies is 1670061
-
[ 6981.244750] currretn jiffies is 1670311
-
[ 7448.756231] currretn jiffies is 1787189
-
[ 7449.756231] currretn jiffies is 1787439
阅读(1979) | 评论(0) | 转发(0) |