歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> S3C6410實時時鐘RTC 秒字符設備

S3C6410實時時鐘RTC 秒字符設備

日期:2017/3/1 10:10:47   编辑:Linux編程

/*《Linux 設備驅動開發詳解》 http://www.linuxidc.com/Linux/2011-07/38211.htm

驅動程序

*/

  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/init.h>
  8. #include <linux/cdev.h>
  9. #include <asm/io.h>
  10. #include <asm/system.h>
  11. #include <asm/uaccess.h>
  12. #define SECOND_MAJOR 248 /*預設的second的主設備號*/
  13. static int second_major = SECOND_MAJOR;
  14. /*second設備結構體*/
  15. struct second_dev {
  16. struct cdev cdev; /*cdev結構體*/
  17. atomic_t counter;/* 一共經歷了多少秒?*/
  18. struct timer_list s_timer; /*設備要使用的定時器*/
  19. };
  20. struct second_dev *second_devp; /*設備結構體指針*/
  21. /*定時器處理函數*/
  22. static void second_timer_handle(unsigned long arg)
  23. {
  24. mod_timer(&second_devp->s_timer,jiffies + HZ);
  25. atomic_inc(&second_devp->counter);
  26. printk(KERN_NOTICE "current jiffies is %ld\n", jiffies);
  27. }
  28. /*文件打開函數*/
  29. int second_open(struct inode *inode, struct file *filp)
  30. {
  31. /*初始化定時器*/
  32. init_timer(&second_devp->s_timer);
  33. second_devp->s_timer.function = &second_timer_handle;
  34. second_devp->s_timer.expires = jiffies + HZ;
  35. add_timer(&second_devp->s_timer); /*添加(注冊)定時器*/
  36. atomic_set(&second_devp->counter,0); //計數清0
  37. return 0;
  38. }
  39. /*文件釋放函數*/
  40. int second_release(struct inode *inode, struct file *filp)
  41. {
  42. del_timer(&second_devp->s_timer);
  43. return 0;
  44. }
  45. /*讀函數*/
  46. static ssize_t second_read(struct file *filp, char __user *buf, size_t count,
  47. loff_t *ppos)
  48. {
  49. int counter;
  50. counter = atomic_read(&second_devp->counter);
  51. if(put_user(counter, (int*)buf))
  52. return - EFAULT;
  53. else
  54. return sizeof(unsigned int);
  55. }
  56. /*文件操作結構體*/
  57. static const struct file_operations second_fops = {
  58. .owner = THIS_MODULE,
  59. .open = second_open,
  60. .release = second_release,
  61. .read = second_read,
  62. };
  63. /*初始化並注冊cdev*/
  64. static void second_setup_cdev(struct second_dev *dev, int index)
  65. {
  66. int err, devno = MKDEV(second_major, index);
  67. cdev_init(&dev->cdev, &second_fops);
  68. dev->cdev.owner = THIS_MODULE;
  69. err = cdev_add(&dev->cdev, devno, 1);
  70. if (err)
  71. printk(KERN_NOTICE "Error %d adding LED%d", err, index);
  72. }
  73. /*設備驅動模塊加載函數*/
  74. int second_init(void)
  75. {
  76. int ret;
  77. dev_t devno = MKDEV(second_major, 0);
  78. /* 申請設備號*/
  79. if (second_major)
  80. ret = register_chrdev_region(devno, 1, "second");
  81. else { /* 動態申請設備號 */
  82. ret = alloc_chrdev_region(&devno, 0, 1, "second");
  83. second_major = MAJOR(devno);
  84. }
  85. if (ret < 0)
  86. return ret;
  87. /* 動態申請設備結構體的內存*/
  88. second_devp = kmalloc(sizeof(struct second_dev), GFP_KERNEL);
  89. if (!second_devp) { /*申請失敗*/
  90. ret = - ENOMEM;
  91. goto fail_malloc;
  92. }
  93. memset(second_devp, 0, sizeof(struct second_dev));
  94. second_setup_cdev(second_devp, 0);
  95. return 0;
  96. fail_malloc:
  97. unregister_chrdev_region(devno, 1);
  98. return ret;
  99. }
  100. /*模塊卸載函數*/
  101. void second_exit(void)
  102. {
  103. cdev_del(&second_devp->cdev); /*注銷cdev*/
  104. kfree(second_devp); /*釋放設備結構體內存*/
  105. unregister_chrdev_region(MKDEV(second_major, 0), 1); /*釋放設備號*/
  106. }
  107. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  108. MODULE_LICENSE("Dual BSD/GPL");
  109. module_param(second_major, int, S_IRUGO);
  110. module_init(second_init);
  111. module_exit(second_exit);

/*應用程序*/

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <signal.h>
  6. #include <sys/stat.h>
  7. main()
  8. {
  9. int fd;
  10. int counter = 0;
  11. int old_counter = 0;
  12. /*打開/dev/second設備文件*/
  13. fd = open("/dev/second", O_RDONLY);
  14. if (fd != - 1) {
  15. while (1) {
  16. read(fd,&counter, sizeof(unsigned int));/* 讀目前經歷的秒數 */
  17. if(counter!=old_counter) {
  18. printf("seconds after open /dev/second :%d\n",counter);
  19. old_counter = counter;
  20. }
  21. }
  22. } else {
  23. printf("Device open failure\n");
  24. }
  25. }

Copyright © Linux教程網 All Rights Reserved