原创 两个linux按键驱动之一 poll(未去抖动)

2009-9-4 16:08 4617 9 10 分类: 软件与OS

//驱动


//未加去抖动


//通过poll判断是否可读来得到中断值


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/cdev.h>


#define DEVICE_NAME "buttons_my"
#define BUTTON_MAJOR 0                      // 定义0使用自动分配设备号
unsigned int button_major = BUTTON_MAJOR;


struct button_irq_desc
{
 int irq; /*中断号*/ 
 int pin; /*中断控制的寄存器*/  
 int pin_setting; /*中断的引脚*/ 
 int number; /*编号*/
 char *name; /*名称*/
};


static struct button_irq_desc button_irqs [] =
{
 {IRQ_EINT11,S3C2410_GPG3, S3C2410_GPG3_EINT11, 1,"KEY1"}, /*K1*/
 {IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0,  2,"KEY2"}, /*K2*/
 {IRQ_EINT19,S3C2410_GPG11,S3C2410_GPG11_EINT19,3,"KEY3"}, /*K3*/
 {IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2,  4,"KEY4"}, /*K4*/
 {IRQ_EINT6, S3C2410_GPF6, S3C2410_GPF6_EINT6,  5,"KEY5"}, /*K5*/
 {IRQ_EINT5, S3C2410_GPF5, S3C2410_GPF5_EINT5,  6,"KEY6"}, /*K6*/
};


static volatile int key_values [] = {0,0,0,0,0,0};/*key_values数组中是否有数据的标志,0表示无数据可读,1表示有数据可读*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0; /*初始为0*/
// 中断处理
static irqreturn_t buttons_interrupt(int irq,void *dev_id)
{
 struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
// int up = s3c2410_gpio_getpin(button_irqs->pin); //读I/O
//没去抖动  
 key_values[button_irqs->number - 1] = button_irqs->number;
 ev_press = 1; /*表示数组已经可读了*/
 wake_up_interruptible(&button_waitq); 
 return IRQ_RETVAL(IRQ_HANDLED);
}
//open 注册中断
static int buttons_open(struct inode *inode,struct file *file)
{
 int i; 
 int err;
 for(i = 0;i < sizeof(button_irqs)/sizeof(button_irqs[0]);i++)
 {
  s3c2410_gpio_cfgpin(button_irqs.pin,button_irqs.pin_setting);
  err = request_irq(button_irqs.irq,buttons_interrupt,NULL,button_irqs.name,(void *)&button_irqs);
  set_irq_type(button_irqs.irq,IRQT_FALLING); //IRQT_BOTHEDGE 双边沿触发
  if(err)
   break;
 }


 if(err)
 {
  i--;
  for(;i >=0; i--)
  {
   disable_irq(button_irqs.irq);
   free_irq(button_irqs.irq,(void *)&button_irqs);
  }
    return -EBUSY;
 }
 return 0;
}
//close 释放中断
static int buttons_close(struct inode *inode,struct file *file)
{
 int i;
 for(i = 0;i < sizeof(button_irqs)/sizeof(button_irqs[0]);i++)
 {
    disable_irq(button_irqs.irq);
    free_irq(button_irqs.irq,(void *)&button_irqs);
 }
 return 0;
}
//read
static int buttons_read(struct file *filp,char __user *buff,size_t count,loff_t *offp)
{
 unsigned long err; 
 
 if(!ev_press) /*标志位为0,即无数据时*/
 {
  if(filp->f_flags & O_NONBLOCK) //不能以非阻塞读取
   return -EAGAIN;
  else
   wait_event_interruptible(button_waitq,ev_press);
 }


 err = copy_to_user(buff,(const void *)key_values,min(sizeof(key_values),count));
 memset((void *)key_values,0,sizeof(key_values));
 ev_press = 0;
 if(err)
  return -EFAULT;
 else
  return min(sizeof(key_values),count);
}
//buttons_poll
static unsigned int buttons_poll(struct file *file,struct poll_table_struct *wait)
{
 unsigned int mask = 0;


 poll_wait(file,&button_waitq,wait);
 if(ev_press)
    mask |= POLLIN | POLLRDNORM;
 return mask;
}


static struct file_operations buttons_fops =
{
 .owner = THIS_MODULE,
 .open = buttons_open, /*open()*/
 .release = buttons_close, /*release()*/
 .read = buttons_read, /*read()*/
 .poll = buttons_poll /*poll()*/
};
struct cdev button_cdev;                       // cdev结构体
/* 初始化并注册cdev */
static void buttons_setup_cdev(void)
{
    int err,devno = MKDEV(button_major,0);
    cdev_init(&button_cdev,&buttons_fops);
    button_cdev.owner = THIS_MODULE;
    button_cdev.ops = &buttons_fops;
    err = cdev_add(&button_cdev, devno, 1);
    if (err)
        printk(KERN_NOTICE "Error %d adding utukey", err);
}
//init
static int __init buttons_init(void)
{
 int result;
 dev_t devno = MKDEV(button_major,0);    // 用主次设备号生成设备号
 
    /* 申请设备号 */
    if (button_major)
        result = register_chrdev_region(devno, 1, DEVICE_NAME);
    else    /* 动态申请设备号 */
    {
        result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
        button_major = MAJOR(devno);
        printk(KERN_INFO "Todo: mknod /dev/%s c %d 0\n", DEVICE_NAME, button_major);
    } 
 if (result < 0)
        return result;
 
 buttons_setup_cdev();
 
 printk(DEVICE_NAME " initialized\n");
 return 0;
}
//buttons_exit()
static void __exit buttons_exit(void)
{
 cdev_del(&button_cdev);                             // 注销cdev
 unregister_chrdev_region(MKDEV(button_major, 0), 1);    // 释放设备号
}


module_init(buttons_init);
module_exit(buttons_exit);


MODULE_AUTHOR("http://www.xxx.net");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL");


 


//测试代码


/*按键测试程序*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>


int main(void)
{
 int i;
 int buttons_fd;
 int key_value[6];
 fd_set rds;
 int ret;
 
 buttons_fd = open("/dev/buttons_my",0);
 if(buttons_fd < 0)
 {
  perror("open device buttons");
  exit(1);
 }


 while(1)
 {
  FD_ZERO(&rds);
  FD_SET(buttons_fd,&rds);


  ret = select(buttons_fd + 1,&rds,NULL,NULL,NULL);
  if(ret < 0)
  {
   perror("select");
   exit(1);
  }    
  if(ret == 0)
  {
   printf("Timeout.\n");
  }
  else if(FD_ISSET(buttons_fd,&rds))
  {
   int ret = read(buttons_fd,key_value,sizeof(key_value));
   if(ret != sizeof(key_value))
   {
    if(errno != EAGAIN)  
     perror("read buttons\n");
    continue;
   }
   else
   {
    for(i = 0; i < 6 ; i++)
    {
     if(key_value != 0)
      printf("You pressed buttons %d\n", key_value);
    }    
   }
  }
 }
 /*关闭设备*/
 close(buttons_fd);
 return 0;
}


 


没去抖动导致误报频繁。

PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

用户539229 2009-9-6 10:12

可以参考的程序,用定时器去抖动的 http://blog.ednchina.com/gurongjiang/255217/message.aspx 里面有源代码
相关推荐阅读
用户1679196 2009-09-04 16:17
两个linux按键驱动之二 read(定时器去抖动)
//相比之下poll更为实用,但是这个代码实在太具有代表性了...涉及了下面几个知识。代码很大部分是linux设备驱动开发详解的内容。/*1、阻塞读取,队列概念2、睡眠等待中断产生3、定时器产生及相应...
用户1679196 2009-09-04 09:39
select 使用
以下来自网络搜索:Linux下select调用的过程:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:offi...
用户1679196 2009-09-03 14:52
Linux设备驱动之定时器
Linux内核中定义了一个timer_list结构,我们在驱动程序中可以利用之: #include<linux/timer.h> struct timer_list { struct li...
用户1679196 2009-08-28 17:38
loff_t *ppos是什么东东
ssize_t generic_file_read(struct file * filp, char * buf, size_t count, loff_t *ppos) 这是一个文件读函数 我们很容...
用户1679196 2009-08-12 17:30
linux 延时函数几个资料
http://www.cppblog.com/CppExplore/archive/2008/04/02/46111.html一、 基础知识1、时间类型。Linux下常用的时间类型有4个:time_t...
EE直播间
更多
我要评论
1
9
关闭 站长推荐上一条 /3 下一条