In this article, a system timer is started by a channel program. This timer uses the timer processing function at intervals of 1S. Each time the function is called, the counter will increment 1. Call the function read() in the device file dev/TImer_demo to read the value of the timer.
(1) The specific implementation code of the driver file TImer_demo.c is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SECOND_MAJOR 240 /*Default second major number*/
Static int second_major = SECOND_MAJOR;
/*second device structure*/
Struct second_dev
{
Struct cdev cdev; /*cdev structure*/
Atomic_t counter;/* How many seconds have you experienced? (defined as atomic weight)*/
Struct timer_list s_timer; /* timer to be used by the device*/ 1
};
Struct second_dev *second_devp; /* device structure pointer */
/*Timer handler*/
Static void second_timer_handle(unsigned long arg)
{
Mod_timer(&second_devp->s_timer,jiffies + HZ);//Define the timer expiration time to 1 second 5
Atomic_inc(&second_devp->counter);
Printk(KERN_NOTICE "current jiffies is %ld", jiffies);
}
/* file open function */
Int second_open(struct inode *inode, struct file *filp)
{
/*Initialize timer*/ 2
Init_timer(&second_devp->s_timer);
/ / Further initialize the timer 3
Second_devp->s_timer.function = &second_timer_handle;
Second_devp->s_timer.expires = jiffies + HZ;
/ / Activate timer 4
Add_timer(&second_devp->s_timer); /*Add (register) timer*/
Atomic_set(&second_devp->counter,0); //Count clear 0 (the atomic weight of the atomic operation is 0)
Return 0;
}
/* file release function */
Int second_release(struct inode *inode, struct file *filp)
{
Del_timer(&second_devp->s_timer);//Delete timer 6
Return 0;
}
/*globalfifo read function*/
Static ssize_t second_read(struct file *filp, char __user *buf, size_t count,
Loff_t *ppos)
{
Int counter;
Counter = atomic_read(&second_devp->counter);//Read the integer value of the atomic number counter if(put_user(counter, (int*)buf))//write the counter to the user space return - EFAULT;
Else
Return sizeof(unsigned int);
}
/* file operation structure */
Static const struct file_operations second_fops =
{
.owner = THIS_MODULE,
.open = second_open,
.release = second_release,
.read = second_read,
};
/* Initialize and register cdev*/
Static void second_setup_cdev(struct second_dev *dev, int index)
{
Int err, devno = MKDEV(second_major, index);//combined device number cdev_init(&dev->cdev, &second_fops);//initialize device structure dev->cdev.owner = THIS_MODULE;
Dev->cdev.ops = &second_fops;
Err = cdev_add(&dev->cdev, devno, 1); // is the device structure associated device number if (err)
Printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}
/ * device driver module load function * /
Int second_init(void)
{
Int ret;
Dev_t devno = MKDEV(second_major, 0);
/* Apply for equipment number*/
If (second_major)
Ret = register_chrdev_region(devno, 1, "second");
Else /* Dynamically apply for device number*/
{
Ret = alloc_chrdev_region(&devno, 0, 1, "second");
Second_major = MAJOR(devno);
}
If (ret < 0)
Return ret;
/* Dynamically request the memory of the device structure*/
Second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);
If (!second_devp) /*Application failed*/
{
Ret = - ENOMEM;
Goto fail_malloc;
}
/ / Clear the device structure memset (second_devp, 0, sizeof (struct second_dev));
/ / Reload the device second_setup_cdev (second_devp, 0);
Return 0;
Fail_malloc: unregister_chrdev_region(devno, 1);
}
/* module unload function */
Void second_exit(void)
{
Cdev_del(&second_devp->cdev); /*logout cdev*/
Kfree(second_devp); /*release device structure memory*/
Unregister_chrdev_region(MKDEV(second_major, 0), 1); /*release device number*/
}
MODULE_AUTHOR("Sola");
MODULE_LICENSE("Dual BSD/GPL");
Module_param(second_major, int, S_IRUGO);
Module_init(second_init);
Module_exit(second_exit);
(2) The specific implementation code of the driver file timer_demo_test.c is as follows:
#include
#include
#include
#include
#include
#include
Main()
{
Int fd;
Int counter = 0;
Int old_counter = 0;
/*Open the /dev/second device file*/
Fd = open("/dev/second", O_RDONLY);
If (fd != - 1)
{
While (1)
{
Read(fd,&counter, sizeof(unsigned int));//Read the number of seconds currently experienced
If(counter!=old_counter)
{
Printf("seconds after open /dev/second :%d",counter);
Old_counter = counter;
}
}
}
Else
{
Printf("Device open failure");
}
}
M12 Circular Connectors,M12 P Nylon Adhesive Waterproof Connector,M12 Industrial Waterproof Connector,M12 8-Core Male Head Waterproof Connector
Shenzhen HuaTao Electronic Co., LTD , https://www.htconnector.com