Chinaunix首页 | 论坛 | 博客
  • 博客访问: 619039
  • 博文数量: 75
  • 博客积分: 988
  • 博客等级: 准尉
  • 技术积分: 1269
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-10 15:44
文章分类

全部博文(75)

文章存档

2022年(1)

2019年(1)

2018年(1)

2016年(9)

2015年(7)

2013年(6)

2012年(40)

2011年(10)

分类: LINUX

2012-03-12 23:20:12

从之前的查表方式检测按键是否被按下的情况来看,完全霸占着CPU,不能让CPU干其他的事。如果用中断检测的方式来检测按键则可以避免这个问题,当有按键被按下的时候,就会产生中断。

点击(此处)折叠或打开

  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 <asm/uaccess.h>
  8. #include <asm/irq.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/regs-gpio.h>
  11. #include <asm/hardware.h>


  12. static struct class *thirddrv_class;
  13. static struct class_device    *thirddrv_class_dev;

  14. volatile unsigned long *gpfcon;
  15. volatile unsigned long *gpfdat;

  16. volatile unsigned long *gpgcon;
  17. volatile unsigned long *gpgdat;


  18. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  19. /* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
  20. static volatile int ev_press = 0;


  21. struct pin_desc{
  22.     unsigned int pin;
  23.     unsigned int key_val;
  24. };


  25. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  26. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  27. static unsigned char key_val;

  28. struct pin_desc pins_desc[4] = {
  29.     {S3C2410_GPF0, 0x01},
  30.     {S3C2410_GPF2, 0x02},
  31.     {S3C2410_GPG3, 0x03},
  32.     {S3C2410_GPG11, 0x04},
  33. };


  34. /*
  35.   * 确定按键值
  36.   */
  37. static irqreturn_t buttons_irq(int irq, void *dev_id)
  38. {
  39.     struct pin_desc * pindesc = (struct pin_desc *)dev_id;
  40.     unsigned int pinval;
  41.     
  42.     pinval = s3c2410_gpio_getpin(pindesc->pin);

  43.     if (pinval)
  44.     {
  45.         /* 松开 */
  46.         key_val = 0x80 | pindesc->key_val;
  47.     }
  48.     else
  49.     {
  50.         /* 按下 */
  51.         key_val = pindesc->key_val;
  52.     }

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

  55.     
  56.     return IRQ_RETVAL(IRQ_HANDLED);
  57. }

  58. static int third_drv_open(struct inode *inode, struct file *file)
  59. {
  60.     /* 配置GPF0,2为输入引脚 */
  61.     /* 配置GPG3,11为输入引脚 */ /* 有中断请求时,会自动将能设置为中断模式的引脚设为中断模式,所以这里不用配置引脚模式,都自动配置成了中断模式 11 */
  62. /* request_irq参数可以参考Linux源代码里面的其他代码,其中IRQT_BOTHEDGE表示是双边沿触发 */
  63.     request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  64.     request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  65.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  66.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    

  67.     return 0;
  68. }

  69. ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  70. {
  71.     if (size != 1) // 强制为1
  72.         return -EINVAL;

  73.     /* 如果没有按键动作, 休眠 */
  74.     wait_event_interruptible(button_waitq, ev_press);

  75.     /* 如果有按键动作, 返回键值 */
  76.     copy_to_user(buf, &key_val, 1);
  77.     ev_press = 0;   // 清零
  78.     
  79.     return 1;
  80. }


  81. int third_drv_close(struct inode *inode, struct file *file)
  82. {
  83.     free_irq(IRQ_EINT0, &pins_desc[0]);   // 释放中断,两个参数,中断号和引脚描述
  84.     free_irq(IRQ_EINT2, &pins_desc[1]);
  85.     free_irq(IRQ_EINT11, &pins_desc[2]);
  86.     free_irq(IRQ_EINT19, &pins_desc[3]);
  87.     return 0;
  88. }


  89. static struct file_operations sencod_drv_fops = {
  90.     .owner   = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
  91.     .open    = third_drv_open,
  92.     .read    = third_drv_read,    
  93.     .release = third_drv_close,    
  94. };


  95. int major;
  96. static int third_drv_init(void)
  97. {
  98.     major = register_chrdev(0, "third_drv", &sencod_drv_fops);  // 自动分配主设备号

  99.     thirddrv_class = class_create(THIS_MODULE, "third_drv");

  100.     thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  101.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);  // 地址映射
  102.     gpfdat = gpfcon + 1;

  103.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  104.     gpgdat = gpgcon + 1;

  105.     return 0;
  106. }

  107. static void third_drv_exit(void)
  108. {
  109.     unregister_chrdev(major, "third_drv");
  110.     class_device_unregister(thirddrv_class_dev);
  111.     class_destroy(thirddrv_class);
  112.     iounmap(gpfcon);
  113.     iounmap(gpgcon);
  114.     return 0;
  115. }


  116. module_init(third_drv_init);

  117. module_exit(third_drv_exit);

  118. MODULE_LICENSE("GPL");
测试程序:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>

  5. /* thirddrvtest
  6.   */
  7. int main(int argc, char **argv)
  8. {
  9.     int fd;
  10.     unsigned char key_val;
  11.     
  12.     fd = open("/dev/buttons", O_RDWR);
  13.     if (fd < 0)
  14.     {
  15.         printf("can't open!\n");
  16.     }

  17.     while (1)
  18.     {
  19.         read(fd, &key_val, 1);                  //进入内核态的read函数
  20.         printf("key_val = 0x%x\n", key_val);
  21.     }
  22.     
  23.     return 0;
  24. }
也是通过一个死循环,如果没有按键按下就会永远在在那里等待。但占用CPU资源得到很好的改善。


阅读(2301) | 评论(3) | 转发(2) |
给主人留下些什么吧!~~

huicpc08662012-03-18 10:01:05

貌似很强大

jiaweijing2012-03-14 10:24:37

夏冰软件: 路过,支持.....
谢谢!

夏冰软件2012-03-13 15:46:04

路过,支持