CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 4854|回复: 2

按键输入驱动程序~希望各位指点

[复制链接]
发表于 2015-11-13 16:25:06 | 显示全部楼层 |阅读模式
本帖最后由 简单侶途  于 2015-11-16 11:14 编辑

首先,说明我的程序是查询模式的按键输入驱动程序
先看看寄存器的配置吧:
配置为输入模式input  :000
pc_cfg2 &= ~( 7<<12 | 7<<16 );   /*  19--bit[14:12]  20--bit[18:16]*/
kkk.png
加内部上拉电阻,确保电平的稳定,按下为低电平,松开为高电平
pull.png
1.驱动程序部分
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/errno.h>
  4. #include <linux/poll.h>
  5. #include <linux/sched.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/slab.h>
  8. #include <linux/ipmi.h>
  9. #include <linux/mutex.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/cdev.h>
  13. #include <linux/compat.h>
  14. #include <asm/io.h>

  15. #include<linux/fs.h>
  16. #include<linux/kernel.h>
  17. #include<linux/uaccess.h>
  18. #include<linux/types.h>
  19. #include <asm/gpio.h>
  20. #include <linux/gpio.h>
  21. #include <plat/sys_config.h>
  22. #include <linux/delay.h>
  23. #include <asm/irq.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/ipmi.h>
  27. #include <linux/mutex.h>

  28. //#define PIO_BASE  0x01c20800
  29. #define PC_BASE   (PIO_BASE + 2*0x24)
  30. #define PC_CFG2   (PC_BASE  + 0x08)
  31. #define PC_DAT    (PC_BASE  + 0x10)
  32. #define PC_PULL1  (PC_BASE  + 0x20)

  33. static volatile unsigned int *pc_cfg2;
  34. static volatile unsigned int *pc_pull1;
  35. static volatile unsigned int *pc_dat;

  36. static struct class *keydrv_class;
  37. static struct class_device        *keydrv_class_dev;

  38. static int major;
  39. /*
  40. * 1. Configure register PG_CFG0 enable PG0 as input pin .
  41. * 2. Configure register PG_PULL0 enable PG0 pull-up .
  42. */

  43. static int key_open(struct inode *inode, struct file *file)
  44. {
  45.         *pc_cfg2 &= ~( 7<<12 | 7<<16 );   /*  19--bit[14:12] */
  46.         *pc_pull1 &= ~( 3<<6 | 3<<8 );    /*  19--bit[7:6]   */
  47.         *pc_pull1 |= ( 1<<6  | 1<<8 );     /*01 is pull up*/
  48.         return 0;
  49. }

  50. static int key_close(struct inode *inode, struct file *file)
  51. {
  52.         return 0;
  53. }

  54. static ssize_t key_read(struct file *file, char __user *buf,
  55.                                             size_t size,loff_t *ppos)
  56. {
  57.         static char keyval[2];
  58.     if (size != sizeof(keyval))
  59.                 return -EINVAL;
  60.         keyval[0] =        (*pc_dat & (1<<19))? 1:0;
  61.         keyval[1] =        (*pc_dat & (1<<20))? 1:0;
  62.         copy_to_user(buf, &keyval,sizeof(keyval));        
  63.         return sizeof(keyval);
  64. }

  65. static const struct file_operations key_fops = {
  66.         .owner              = THIS_MODULE,
  67.         .open         = key_open,
  68.         .release      = key_close,
  69.         .read         = key_read,
  70. };

  71. static int keys_init(void)
  72. {
  73.         major = register_chrdev(0,"key", &key_fops);
  74.         keydrv_class = class_create(THIS_MODULE, "key");
  75.         keydrv_class_dev = device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
  76.         /* ioremap*/
  77.         pc_cfg2  = ioremap(PC_CFG2,4);
  78.         pc_pull1 = ioremap(PC_PULL1,4);
  79.         pc_dat   = ioremap(PC_DAT,4);        
  80.         return 0;
  81. }

  82. static void keys_exit(void)
  83. {
  84.         device_destroy(keydrv_class,MKDEV(major, 0));
  85.         class_destroy(keydrv_class);
  86.         unregister_chrdev(major,"key");
  87.         //注销LED设备  
  88.         iounmap(pc_cfg2);
  89.         iounmap(pc_pull1);
  90.         iounmap(pc_dat);
  91. }

  92. module_init(keys_init);
  93. module_exit(keys_exit);
  94. MODULE_LICENSE("GPL");
复制代码
2.应用程序部分:
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>

  5. int main(int argc, char **argv)
  6. {
  7.         int fd;
  8.         unsigned char key_val[2];
  9.         int cnt = 0;

  10.         fd = open("/dev/buttons", O_RDWR);
  11.         if (fd < 0) {
  12.                 printf("can't open!\n");
  13.                 return -1;
  14.         }
  15.         while (1){
  16.                 read(fd,key_val, sizeof(key_val));
  17.                 if ( (key_val[0] == 0) || (key_val[1] == 0) )
  18.                         printf("%04d key pressed: %d , %d\n", cnt++, key_val[0],key_val[1]);
  19.         }
  20.         return 0;
  21. }
复制代码

key.png
回复

使用道具 举报

 楼主| 发表于 2015-11-16 11:13:22 | 显示全部楼层
用单片机思维来给按键消抖:
效果为:每按一次打印一次,只有按键松开另外的按键才可以按下
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>

  5. void delay(int t);

  6. int main(int argc, char **argv)
  7. {
  8.         int fd;
  9.         unsigned char key_val[2];
  10.         int cnt = 0;
  11.         char key_flag = 1;

  12.         fd = open("/dev/buttons", O_RDWR);
  13.         if (fd < 0) {
  14.                 printf("can't open!\n");
  15.                 return -1;
  16.         }
  17.         while (1){
  18.                 read(fd,key_val, sizeof(key_val));
  19.                 if ( (key_val[0] == 0) || (key_val[1] == 0) ) {
  20.                         delay(100);        //延时消抖
  21.                         if( (key_val[0] == 0) || (key_val[1] == 0) ) {
  22.                                 if(key_flag == 1)
  23.                                         printf("%04d key pressed: %d , %d\n", cnt++, key_val[0],key_val[1]);
  24.                                 key_flag = 2;
  25.                         }
  26.                 }
  27.                 else
  28.                         key_flag = 1;
  29.         }
  30.         return 0;
  31. }


  32. void delay(int t)
  33. {
  34.         int j;
  35.         for(t = 0;t<256;t++)
  36.                 for(j=0;j<256;j++);
  37. }
复制代码
本人能力有限,尝试用在驱动程序里面用定时器作按键消抖,但是不知道哪里配置问题,程序没反应。
所以只能作这样的修改了。


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-6-2 17:45 , Processed in 0.023957 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部