在dragonboard410c上编写一个秒表定时器

xiaozhu12371237 2017-04-12 11:50:11


(1)驱动程序文件timer_demo.c的具体实现代码如下:
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/timer.h> /*包括timer.h头文件*/
#include <asm/atomic.h>

#define SECOND_MAJOR 240 /*预设的second的主设备号*/
static int second_major = SECOND_MAJOR;

/*second设备结构体*/
struct second_dev
{
struct cdev cdev; /*cdev结构体*/
atomic_t counter;/* 一共经历了多少秒?(定义为原子量)*/
struct timer_list s_timer; /*设备要使用的定时器*/ 1
};

struct second_dev *second_devp; /*设备结构体指针*/

/*定时器处理函数*/
static void second_timer_handle(unsigned long arg)
{
mod_timer(&second_devp->s_timer,jiffies + HZ);//定义定时器到期时间为1秒后 5
atomic_inc(&second_devp->counter);
printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);
}

/*文件打开函数*/
int second_open(struct inode *inode, struct file *filp)
{
/*初始化定时器*/ 2
init_timer(&second_devp->s_timer);
//进一步初始化定时器 3
second_devp->s_timer.function = &second_timer_handle;
second_devp->s_timer.expires = jiffies + HZ;
//激活定时器 4
add_timer(&second_devp->s_timer); /*添加(注册)定时器*/
atomic_set(&second_devp->counter,0); //计数清0(原子操作之设置原子量counter为0)
return 0;
}
/*文件释放函数*/
int second_release(struct inode *inode, struct file *filp)
{
del_timer(&second_devp->s_timer);//删除定时器 6
return 0;
}

/*globalfifo读函数*/
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);//读取原子量counter的整数值
if(put_user(counter, (int*)buf))//将counter写入用户空间
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,
};

/*初始化并注册cdev*/
static void second_setup_cdev(struct second_dev *dev, int index)
{
int err, devno = MKDEV(second_major, index);//组合设备号
cdev_init(&dev->cdev, &second_fops);//初始化设备结构体
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &second_fops;
err = cdev_add(&dev->cdev, devno, 1);//为设备结构体关联设备号
if (err)
printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}

/*设备驱动模块加载函数*/
int 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);
}
/*模块卸载函数*/
void second_exit(void)
{
cdev_del(&second_devp->cdev); /*注销cdev*/
kfree(second_devp); /*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(second_major, 0), 1); /*释放设备号*/
}

MODULE_AUTHOR("Sola");
MODULE_LICENSE("Dual BSD/GPL");
module_param(second_major, int, S_IRUGO);
module_init(second_init);
module_exit(second_exit);


(2)驱动程序文件timer_demo_test.c的具体实现代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
main()
{
int fd;
int counter = 0;
int old_counter = 0;

/*打开/dev/second设备文件*/
fd = open("/dev/second", O_RDONLY);
if (fd != - 1)
{
while (1)
{
read(fd,&counter, sizeof(unsigned int));//读目前经历的秒数

if(counter!=old_counter)

{
printf("seconds after open /dev/second :%d\n",counter);
old_counter = counter;

}
}
}
else
{
printf("Device open failure\n");
}
}
...全文
1706 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ANDYMFC_No1 2017-04-13
  • 打赏
  • 举报
回复
這個沒必要採用驅動來搞吧,獲取系統時間錯就可以了,各種語言都有提供相應的時間獲取接口
肖大叔 2017-04-13
  • 打赏
  • 举报
回复
同意二楼,秒表实际已经进入应用的范畴,我们完全可以在应用层就实现这个功能,而且代码的实用性以及通用性更高。

2,851

社区成员

发帖
与我相关
我的任务
社区描述
本论坛以AI、WoS 、XR、IoT、Auto、生成式AI等核心板块组成,为开发者提供便捷及高效的学习和交流平台。 高通开发者专区主页:https://qualcomm.csdn.net/
人工智能物联网机器学习 技术论坛(原bbs) 北京·东城区
社区管理员
  • csdnsqst0050
  • chipseeker
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧