主题 : 各位帮忙看一下这个有点问题的驱动程序 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 17673
精华: 0
发帖: 29
金钱: 145 两
威望: 29 点
综合积分: 58 分
注册时间: 2010-03-31
最后登录: 2011-02-24
楼主  发表于: 2010-05-21 10:41

 各位帮忙看一下这个有点问题的驱动程序

复制代码
  1. /**********************************************************
  2. *
  3. *
  4. *说明:本程序为一个基于Linux操作系的简单LED驱动模块,
  5. *          本模块主要应用内核定时器进行定时LED,由于
  6. *         比较简单,所以在此不再进行说明
  7. *
  8. **环境:
  9. *        1)操作系统:Linux
  10. *        2)内核版本:kernel-2.6.13
  11. *        3)CPU     :S3C2440
  12. *        4)开发板  :Mini2440
  13. *
  14. *硬件连接:
  15. *        1):LED1 -- GPB5
  16. *        2):LED2 -- GPB6
  17. *        3):LED3 -- GPB7
  18. *        4):LED4 -- GPB8
  19. *
  20. *编译环境:arm-linux-gcc-3.4.1
  21. *
  22. *作者:soon
  23. *部门:长沙理工大学 计通学院  计算机07级04班
  24. *最后修改时间:2010.5.19
  25. *
  26. *
  27. **********************************************************/
  28. #ifndef _LED_C_
  29. #define _LED_C_
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/fs.h>
  33. #include <linux/errno.h>
  34. #include <linux/types.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/cdev.h>
  37. #include <linux/version.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/ctype.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/ioctl.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/time.h>
  44. #include <linux/timer.h>
  45. #include <asm/io.h>
  46. #include <asm/irq.h>
  47. #include <asm/signal.h>
  48. #include <asm/hardware.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/arch/regs-gpio.h>
  51. #include <asm/arch/regs-timer.h>
  52. #include <asm/mach/time.h>
  53. #include <asm/hardware/clock.h>
  54. #ifndef LED_MAJOR
  55. #define LED_MAJOR 240
  56. #endif
  57. #ifndef LED_MINOR
  58. #define LED_MINOR 0
  59. #endif
  60. MODULE_AUTHOR("soon");
  61. MODULE_LICENSE("Dual BSD/GPL");
  62. static unsigned int led_inc=0;
  63. static unsigned int led_count = 0;
  64. struct LED_dev
  65. {
  66.     struct cdev cdev;    
  67. };
  68. /*定义LED相应的CPU引脚*/
  69. static unsigned long led_table [] = {
  70.     S3C2410_GPB5,
  71.     S3C2410_GPB6,
  72.     S3C2410_GPB7,
  73.     S3C2410_GPB8,
  74. };
  75. /*相应引脚的工作方式*/
  76. static unsigned int led_cfg_table [] = {
  77.     S3C2410_GPB5_OUTP,
  78.     S3C2410_GPB6_OUTP,
  79.     S3C2410_GPB7_OUTP,
  80.     S3C2410_GPB8_OUTP,
  81. };
  82. static struct LED_dev *LED_devices;
  83. static struct timer_list *LED_TIMER;
  84. static void kerneltimer_timerover(unsigned long arg);
  85. static void
  86. kerneltimer_registertimer(void)
  87. {
  88.   init_timer(LED_TIMER);
  89.     LED_TIMER->expires = get_jiffies_64() + (5 * HZ/100);
  90.     LED_TIMER->function = kerneltimer_timerover;
  91.     add_timer(LED_TIMER);
  92. }
  93. static void
  94. kerneltimer_timerover(unsigned long arg)
  95. {
  96.     unsigned int i;
  97.     
  98.     /*如果led_count > 3,则led_count = 0*/
  99.     led_count &= 0x3;
  100.     
  101.     for(i = 0; i < led_count; i++)
  102.       s3c2410_gpio_setpin(led_table[i],1);
  103.     
  104.     s3c2410_gpio_setpin(led_table[led_count],0);
  105.     
  106.     led_count ++;
  107.     kerneltimer_registertimer();
  108.     
  109. }
  110. static int
  111. LED_open(struct inode *inode, struct file *filp)
  112. {
  113.     struct LED_dev *dev;
  114.     
  115.     if(led_inc > 0)return -ERESTARTSYS;
  116.         
  117.     LED_TIMER = kmalloc(sizeof(struct timer_list), GFP_KERNEL);
  118.     
  119.     if(!LED_TIMER)
  120.      return -ENOMEM;
  121.         
  122.     kerneltimer_registertimer();
  123.     
  124.     led_inc++;
  125.   
  126.     dev = container_of(inode->i_cdev, struct LED_dev, cdev);
  127.     filp->private_data = dev;
  128.     
  129.     return 0;
  130. }
  131. static int
  132. LED_release(struct inode *inode, struct file *filp)
  133. {
  134.     led_inc--;
  135.     return 0;
  136. }
  137. static struct file_operations LED_fops = {
  138.     .owner   =    THIS_MODULE,
  139.     .open    =    LED_open,
  140.     .release =    LED_release,
  141. };
  142. static void
  143. LED_cleanup_module(void)
  144. {
  145.     unsigned int i = 0;    
  146.     dev_t devno = MKDEV(LED_MAJOR, LED_MINOR);
  147.     if (LED_devices) {
  148.         cdev_del(&LED_devices->cdev);
  149.         kfree(LED_devices);
  150.     }
  151.     
  152.     if(LED_TIMER)
  153.         kfree(LED_TIMER);
  154.     for(; i < 4; i ++)
  155.      s3c2410_gpio_setpin(led_table[i], 1);
  156.     unregister_chrdev_region(devno,1);
  157. }
  158. static int
  159. LED_init_module(void)
  160. {
  161.     int result;
  162.     unsigned int i;
  163.     dev_t dev = 0;
  164.     dev = MKDEV(LED_MAJOR,LED_MINOR);
  165.     result = register_chrdev_region(dev,1,"Led_Test");
  166.     
  167.     if (result < 0)
  168.         return result;
  169.     
  170.     LED_devices = kmalloc(sizeof(struct LED_dev), GFP_KERNEL);
  171.     
  172.     if (!LED_devices){
  173.         result = -ENOMEM;
  174.         goto fail;
  175.     }
  176.     
  177.     memset(LED_devices, 0, sizeof(struct LED_dev));
  178.     
  179.     cdev_init(&LED_devices->cdev, &LED_fops);
  180.     LED_devices->cdev.owner = THIS_MODULE;
  181.     LED_devices->cdev.ops = &LED_fops;
  182.     result = cdev_add (&LED_devices->cdev, dev, 1);
  183.     if(result)
  184.         goto fail;
  185.   for (i = 0; i < 4; i++) {
  186.         s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  187.         s3c2410_gpio_setpin(led_table[i], 1);
  188.     }
  189.     
  190.     return 0;
  191. fail:
  192.     LED_cleanup_module();
  193.     return result;
  194. }
  195. module_init(LED_init_module);
  196. module_exit(LED_cleanup_module);
  197. #endif
  198. /**********************************************************
  199. ||
  200. ||
  201. ||
  202. ||                    END
  203. ||
  204. ||
  205. ||
  206. **********************************************************/
  207. 如果不方便,可以进行一下调试,MAJOR = 240;MINOR = 0; 驱动加载为: mknod /dev/Led_Test c 240 0



由于程序比较简单,所以没有必要加注释,

这个程序有点问题,也就是注销驱动模块或者运行一段时间后,会死机。小弟不知道是什么原因。