按键驱动--中断的例子

作为中断的一个例子,按键驱动最具有代表性了。

 

1、建立key文件夹

 

2、在key文件夹下建立驱动程序buttons.c

 

#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 <linux/irq.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/sched.h>

#define DEVICE_NAME "wjbButtons"

struct button_irq_desc{
 int irq;
 int pin;
 int number;
 char *name;
};

static struct button_irq_desc button_irqs[] = {
 {IRQ_EINT8, S3C2410_GPG(0), 0, "KEY0"},
 {IRQ_EINT11, S3C2410_GPG(3), 1, "KEY1"},
 {IRQ_EINT13, S3C2410_GPG(5), 2, "KEY2"},
 {IRQ_EINT14, S3C2410_GPG(6), 3, "KEY3"},
 {IRQ_EINT15, S3C2410_GPG(7), 4, "KEY4"},
 {IRQ_EINT19, S3C2410_GPG(11), 5, "KEY5"},
};

static volatile char key_values[] = {'0', '0', '0', '0', '0', '0'};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

static irqreturn_t buttons_interrupt(int irq, void* dev_id)
{
 struct button_irq_desc *button_irqs = (struct button_irq_desc*)dev_id;
 int down;
 
 down = !s3c2410_gpio_getpin(button_irqs->pin);

 if(down != (key_values[button_irqs->number] & 1))
 {
  key_values[button_irqs->number] = '0' + down;

  ev_press = 1;

  wake_up_interruptible(&button_waitq);
 }
 
 return IRQ_RETVAL(IRQ_HANDLED);
}

static int s3c24xx_buttons_open(struct inode* inode, struct file* file)
{
 int i;
 int err;
 
 for(i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
 {
  if(button_irqs[i].irq < 0)
  {
   continue;
  }

  err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH, button_irqs[i].name, (void *)&button_irqs[i]);
  if(err)
  {
   break;
  }
 }
 if(err)
 {
  i--;
  for(; i >= 0; i--)
  {
   if(button_irqs[i].irq < 0)
   {
    continue;
   }
   disable_irq(button_irqs[i].irq);
   free_irq(button_irqs[i].irq, (void*)&button_irqs[i]);
  }
  return -EBUSY;
 }

 ev_press = 1;

 return 0;
}

static int s3c24xx_buttons_close(struct inode* inode, struct file* file)
{
 int i;
 
 for(i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
 {
  if(button_irqs[i].irq < 0)
  {
   continue;
  }
  free_irq(button_irqs[i].irq, (void*)&button_irqs[i]);
 }

 return 0;
}

static int s3c24xx_buttons_read(struct file* filp, char __user *buff, size_t count, loff_t *offset)
{
 unsigned long err;
 
 if(!ev_press)
 {
  if(filp->f_flags & O_NONBLOCK)
  {
   return -EAGAIN;
  }
  else
  {
   wait_event_interruptible(button_waitq, ev_press);
  }
 }

 ev_press = 0;

 err = copy_to_user(buff, (const void*)key_values, min(sizeof(key_values), count));
 
 return err ? -EFAULT : min(sizeof(key_values), count);
}

static unsigned int s3c24xx_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 dev_fops = {
 .owner = THIS_MODULE,
 .open = s3c24xx_buttons_open,
 .release = s3c24xx_buttons_close,
 .read = s3c24xx_buttons_read,
 .poll = s3c24xx_buttons_poll,
};

static struct miscdevice misc = {
 .minor = MISC_DYNAMIC_MINOR,
 .name = DEVICE_NAME,
 .fops = &dev_fops,
};

static int __init dev_init(void)
{
 int ret;
 ret = misc_register(&misc);
 printk(DEVICE_NAME"/tinitialize/n");
 
 return ret;
}

static void __exit dev_exit(void)
{
 misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("sjwangjinbao");

 

3、Makefile文件

 

ifneq ($(KERNELRELEASE), )

obj-m := buttons.o

else

KDIR := /usr/src/linux-2.6.32.2

all:
 make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
 rm -f *.ko *.o *.mod.o *.mod.c *.symvers

endif

 

4、执行make

会生成buttons.ko。

 

5、建立应用程序1-使用select

 

#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 buttons_fd;
 char key_value[6] = {'0', '0', '0', '0', '0', '0'};

 buttons_fd = open("/dev/wjbButtons", 0);
 if(buttons_fd < 0)
 {
  perror("open device buttons");
  exit(1);
 }

 for(;;)
 {
  fd_set rds;
  int ret;
  int i = 0;
  
  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(0 == ret)
  {
   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 < sizeof(key_value); ++i)
     printf("%d buttons_value : %d/n",i, key_value[i]);
   }
  }
 }
 return 0;
}

6、建立应用程序2-普通读

 

#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 buttons_fd;
 char buttons[6] = {'0', '0', '0', '0', '0', '0'};

 buttons_fd = open("/dev/wjbButtons",0);
 if(buttons_fd < 0)
 {
  perror("open device buttons");
  exit(1); 
 }

 while(1)
 {
  char current_buttons[6] = {'0', '0', '0', '0', '0', '0'};
  int count_of_changed_key;
  int i;
  int readsize = 0;

  printf("before read!/n");

  readsize = read(buttons_fd, current_buttons, sizeof(current_buttons));
  if(readsize < 0)
  {
   perror("read buttons");
   exit(1); 
  }
  
  printf("after read! readsize : %d/n", readsize);

  for(i = 0, count_of_changed_key = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++)
  {
   printf("buttons[%d] : %d/n", i, buttons[i]);
   printf("current_buttons[%d] : %d /n", i, current_buttons[i]);
   if(buttons[i] != current_buttons[i])
   {
    buttons[i] = current_buttons[i];
    printf("%s key %d is %s/n", count_of_changed_key ? "," : "", i + 1, buttons[i] == '0' ? "up" : "down");
    count_of_changed_key++;
   }
   
  }

  if(count_of_changed_key)
  {
   printf("/n");
  }
 }
 
 close(buttons_fd);
 return 0;
}

7、交叉编译

使用arm-linux-gcc 交叉编译出两个应用程序btnselect和btnapp。

 

8、将btnselect、btnapp和buttons.ko一起拷贝到开发板上。

1)执行insmod buttons.ko添加驱动

2)执行btnselect。一开始会把6个按键的默认值输出来:48,其实就是‘0’。当有按键按下的时候,相应的按键的值会变为49;但是当安检落下的时候,其值又恢复为48。

          3)用Ctl+C停掉刚才的程序,再执行btnapp。当有按键按下或抬起的时候,都会有相应的输出,告诉我们哪个按键按下或抬起了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值