How to implement a stopwatch timer on the Dragonboard 410c

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
/* includes TImer.h header file */
#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");
}
}

Film Cutter Machine

According to the mobile phone model needed, it can be cut down in real time, which greatly reduces the risk of inventory backlog. Suitable for mobile phone shops, accessories shops, repair shops, digital shops, tool shops, personal entrepreneurship, DIY enthusiasts, etc.

Fully automatic integrated film cutting machine, formidable and powerful performance beyond actual needs, breaks through the traditional model, stays away from inventory pressure, and saves costs! The "drainage black technology" is better and more powerful than the laser film cutting machine and other film cutting machines.

A compact and lightweight cloud data integrated film cutting machine, which automatically cuts various soft films such as PET and TPU flexible films, covering 8000+ mobile phone models, and is constantly updated and iterative. It supports any models in the world, including lens/mobile phone front film and rear film, tablet computers, cameras, game consoles, smart watches, etc. One film can be used for all digital categories.
The machine supports multiple languages, built-in WIFI, Bluetooth, one-key automatic film cutting, can learn to operate in 2 minutes, no inventory, low investment, fast profit! A marketing tool designed for terminal stores and entrepreneurs.

Integrated Film Cutting Machine,Tablet Film Cut Machine,Film Cutting Machine,Unlimited Hydrogel Film Cutting Machine

Shenzhen Customized Technology Co., Ltd. , https://www.protectionfilm6.com

This entry was posted in on