Chinaunix首页 | 论坛 | 博客
  • 博客访问: 35168
  • 博文数量: 16
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-26 14:56
文章分类

全部博文(16)

文章存档

2015年(1)

2014年(15)

我的朋友

分类: LINUX

2014-06-30 15:11:54


点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <linux/time.h>
  8. #include <asm/uaccess.h>
  9. #include <asm/irq.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/regs-gpio.h>
  12. #include <asm/hardware.h>

  13. #define DEVICE_NAME "buttons" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
  14. unsigned char key_val;
  15. static struct class *buttons_class;
  16. static struct class_device    *buttons_class_devs;

  17. /*
  18. volatile unsigned long *gpfcon;
  19. volatile unsigned long *gpfdat;

  20. volatile unsigned long *gpgcon;
  21. volatile unsigned long *gpgdat;
  22. */

  23. /* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
  24. static volatile int ev_press = 0;
  25. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);


  26.  
  27. static DECLARE_MUTEX(button_lock); // 定义赋值
  28. //static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1


  29. static struct timer_list buttons_timer;

  30. struct pin_desc{
  31.     int pin;        //4个引脚 GPF0 2 GPG3 GPG11
  32.     int key_val;        //引脚对应置key_val值
  33. };
  34. struct pin_desc pins_desc[4]= //结构体只是一个类型 , pins_desc是数组类似int a[3];
  35. {
  36.     {S3C2410_GPF0, 0x01},
  37.     {S3C2410_GPF2, 0x02},
  38.     {S3C2410_GPG3, 0x03},
  39.     {S3C2410_GPG11, 0x04},
  40. };

  41. struct pin_desc *pindesc;

  42. static irqreturn_t buttons_irq(int irq, void *dev_id)
  43. {

  44.         /* 10ms后启动定时器 */
  45.     pindesc=(struct pin_desc *)dev_id;
  46.     mod_timer(&buttons_timer, jiffies+HZ/100);
  47.     return IRQ_RETVAL(IRQ_HANDLED);    

  48. }
  49. static void buttons_timer_function(unsigned long data)
  50. {

  51. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  52. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  53. int getpin;
  54. getpin=s3c2410_gpio_getpin(pindesc->pin);//0按下 1放开
  55. if(getpin)
  56. {
  57. //放开
  58. key_val=0x80|pindesc->key_val;
  59. }
  60. else
  61. {
  62. //按下
  63. key_val=pindesc->key_val;
  64. }

  65.     ev_press = 1;                            /* 表示中断发生了 */
  66.     wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
  67. }

  68. static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
  69. {

  70. #if 0    
  71.     if (!atomic_dec_and_test(&canopen))
  72.     {
  73.         atomic_inc(&canopen);
  74.         return -EBUSY;
  75.     }
  76. #endif        

  77.     if (file->f_flags & O_NONBLOCK)
  78.     {
  79.         if (down_trylock(&button_lock))
  80.             return -EBUSY;
  81.     }
  82.     else
  83.     {
  84.         /* 获取信号量 */
  85.         down(&button_lock);
  86.     }
  87.     /* 配置GPF0,2,GPG3,11为输入引脚
  88.      注册时,内核自动设置中断引脚
  89.     */

  90.     /*注册中断
  91.      IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]分别为
  92.      外部中断0 要执行的中断函数 双边沿触发 注册的中断 dev_id
  93.     */
  94.     request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  95.     request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  96.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  97.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
  98.     return 0;

  99. }



  100. ssize_t s3c24xx_buttons_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  101. {
  102.     if (size != 1)
  103.     return -EINVAL;

  104.     if (file->f_flags & O_NONBLOCK)
  105.     {
  106.         if (!ev_press)
  107.             return -EAGAIN;
  108.     }
  109.     else
  110.     {
  111.         /* 如果没有按键动作, 休眠 */
  112.         wait_event_interruptible(button_waitq, ev_press);
  113.     }

  114.     copy_to_user(buf, &key_val, 1);
  115.     ev_press = 0;
  116.     return 1;
  117. }


  118. int s3c24xx_buttons_close(struct inode *inode, struct file *file)
  119. {
  120.     //atomic_inc(&canopen);
  121.     free_irq(IRQ_EINT0, &pins_desc[0]);
  122.     free_irq(IRQ_EINT2, &pins_desc[1]);
  123.     free_irq(IRQ_EINT11, &pins_desc[2]);
  124.     free_irq(IRQ_EINT19, &pins_desc[3]);
  125.     up(&button_lock);
  126.     return 0;
  127. }

  128. static struct file_operations s3c24xx_buttons_fops = {
  129.     .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  130.     .open = s3c24xx_buttons_open,
  131.     .read    =    s3c24xx_buttons_read,    
  132.     .release    =s3c24xx_buttons_close,    
  133. };

  134. int ret;
  135. static int __init s3c24xx_buttons_init(void)
  136. {
  137.     init_timer(&buttons_timer);
  138.     buttons_timer.function = buttons_timer_function;
  139.     //buttons_timer.expires = 0;
  140.     add_timer(&buttons_timer);

  141.     ret = register_chrdev(0, DEVICE_NAME, &s3c24xx_buttons_fops);
  142.     if (ret < 0) {
  143.       printk(DEVICE_NAME " can't register major number\n");
  144.       return ret;
  145.     }
  146.     printk("%d",ret);
  147.     buttons_class = class_create(THIS_MODULE, "buttons");
  148.     buttons_class_devs= class_device_create(buttons_class, NULL, MKDEV(ret, 0), NULL, "buttons");
  149.     //调试的时候老是出错就是因为MKDEV(ret, 0),ret 按照LED define BUTTON_MAJOR 0 写成BUTTON_MAJOR,然后令其为0以为可以
  150.     //自动配置主设备号,要注意此时MKDEV(ret, 0)不要写BUTTON_MAJOR ret不是0是249(eg)
  151. /*
  152.         gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  153.     gpfdat = gpfcon + 1;

  154.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  155.     gpgdat = gpgcon + 1;
  156.          */
  157.     printk(DEVICE_NAME " initialized\n");
  158.     return 0;
  159. }


  160. static void __exit s3c24xx_buttons_exit(void)
  161. {
  162.     unregister_chrdev(ret, DEVICE_NAME);

  163.     class_device_unregister(buttons_class_devs);
  164.     class_destroy(buttons_class);
  165. /*
  166.     iounmap(gpfcon);
  167.     iounmap(gpgcon);*/
  168. }


  169. module_init(s3c24xx_buttons_init);
  170. module_exit(s3c24xx_buttons_exit);
  171. MODULE_AUTHOR("");
  172. MODULE_VERSION("0.1.0");
  173. MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
  174. MODULE_LICENSE("GPL");

阅读(1069) | 评论(0) | 转发(0) |
0

上一篇:进程创建

下一篇:没有了

给主人留下些什么吧!~~