Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342076
  • 博文数量: 61
  • 博客积分: 2816
  • 博客等级: 少校
  • 技术积分: 880
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 23:38
文章存档

2012年(1)

2011年(19)

2010年(41)

分类: LINUX

2010-09-05 23:40:21

/*************************************
按键的驱动,在TX2440A上做测试
维护记录: 2009-10-28 V1.0  by xgc
           2009-03-08 V1.1  by xgc 修改了release后,没有关中断的bug
linux内核:2.6.31
硬件接法:
  KEY1 --> GPF4
  KEY2 --> GPF5
  KEY3 --> GPF6
  KEY4 --> GPF7
*************************************/
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
//#include
#include
#include
#include
#include
#define BUFFER_SIZE 256
#define DEVICE_NAME "TX2440-button" /* 设备名称 */
static int BUTTON_MAJOR = 0;  /* 主设备号 */
static volatile int key_values [] = {0, 0, 0, 0};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
struct button_irq_desc
{
 int irq; //中断名
 int pin; //中断管脚
 int pin_setting;//中断管设置,其实就是把端口改成中断状态
 int number; 
 char *name; //名字
};
/* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */
static struct button_irq_desc button_irqs [] =
{
 {IRQ_EINT4, S3C2410_GPF(4), S3C2410_GPF4_EINT4, 0, "KEY1"}, /* K1 */
 {IRQ_EINT5, S3C2410_GPF(5), S3C2410_GPF5_EINT5, 1, "KEY2"}, /* K2 */
 {IRQ_EINT6, S3C2410_GPF(6), S3C2410_GPF6_EINT6, 2, "KEY3"}, /* K3 */
 {IRQ_EINT7, S3C2410_GPF(7), S3C2410_GPF7_EINT7, 3, "KEY4"}, /* K4 */
};request_irq
static void open_irqs(void)
{
 int i;
 
 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) //其实就搞4次
 {
  enable_irq(button_irqs[i].irq); //允许单个中断
 }
}
static void close_irqs(void)
{
 int i;
 
 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) //4次
 {
  disable_irq(button_irqs[i].irq); //禁止单个中断
 }
}
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
 struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;//把中断的管脚转换成button_irq_desc
 int key_press = s3c2410_gpio_getpin(button_irqs->pin);//读去管脚的数据
 if (key_press == 0) //检查是否按下
 {
  mdelay(5); //延时函数
  key_press = s3c2410_gpio_getpin(button_irqs->pin); //再次读去管脚的数据
  if (key_press == 0)//检查是否按下
   key_values[button_irqs->number] = (button_irqs->number + 1);//得到数据
 }
 ev_press = 1;
 wake_up_interruptible(&button_waitq);//唤醒队列
 return IRQ_RETVAL(IRQ_HANDLED);
}
static void register_irqsrv(void)
{
 int i;
 int err;
 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
 {
  // 注册中断处理函数
  s3c2410_gpio_cfgpin(button_irqs[i].pin,button_irqs[i].pin_setting);//使用中断模式
  err = request_irq(button_irqs[i].irq, buttons_interrupt, NULL, button_irqs[i].name, (void *)&button_irqs[i]);
  if (err)
   break;
 }
 if (err)
 {
  // 释放已经注册的中断
  i--;
  for (; i >= 0; i--)
  {
   disable_irq(button_irqs[i].irq);
   free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);//释放分配给已定中断的内存
  }
  return -EBUSY;
 } 
}
static void unregister_irqsrv(void)
{
 int i;
 
 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
 {
  free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);//释放分配给已定中断的内存
 }
}
static int TX2440_button_open(struct inode *inode, struct file *file)
{
 open_irqs();  //开中断
 printk("TX2440-BUTTON Driver Open Called!\n"); 
 return 0;
}
static int TX2440_button_close(struct inode *inode, struct file *file)
{
 close_irqs();  //关中断
 printk("TX2440-BUTTON Driver Release Called!\n"); 
 return 0;
}
static int TX2440_button_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
 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));//把内核数据写入用户空间
 memset((void *)key_values, 0, sizeof(key_values));//清零内核数据
 
 return err ? -EFAULT : min(sizeof(key_values), count);//那个小返回那个
}
/*这个我不明白。请大侠解释*/
static unsigned int TX2440_button_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 TX2440_button_fops =
{
 .owner = THIS_MODULE,
 .open = TX2440_button_open,
 .release = TX2440_button_close,
 .read = TX2440_button_read,
 .poll = TX2440_button_poll,
};
static struct class *button_class;
static int __init TX2440_button_init(void)
{
 printk("TX2440 BUTTON DRIVER MODULE INIT\n");
 
 register_irqsrv();
 close_irqs();
 BUTTON_MAJOR = register_chrdev(0, DEVICE_NAME, &TX2440_button_fops);//注册一个设备号
 if (BUTTON_MAJOR < 0)
 {
  printk(DEVICE_NAME " can't register major number\n");
  return BUTTON_MAJOR;
 }
 printk("register TX2440-Button Driver OK! Major = %d\n", BUTTON_MAJOR);
 button_class = class_create(THIS_MODULE, DEVICE_NAME);//在dev目录下注册设备号。
 if(IS_ERR(button_class))
 {
  printk("Err: failed in TX2440-Button class. \n");
  return -1;
 }
 device_create(button_class, NULL, MKDEV(BUTTON_MAJOR, 0), NULL, DEVICE_NAME);//创建设备号
 printk(DEVICE_NAME " initialized\n");
 return 0;
}
static void __exit TX2440_button_exit(void)
{
 printk("TX2440 BUTTON DRIVER MODULE EXIT\n");
 
 close_irqs(); //关闭所有中断
 unregister_irqsrv();////释放分配给已定中断的内存
 
 unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
 device_destroy(button_class, MKDEV(BUTTON_MAJOR, 0));
 class_destroy(button_class);     
}
module_init(TX2440_button_init);//告诉内初始化程序 这个是宏定义
module_exit(TX2440_button_exit);
MODULE_AUTHOR("www.txmcu.com");
MODULE_DESCRIPTION("TX2440 Button Driver");
MODULE_LICENSE("GPL");
 
阅读(1849) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-07 08:23:13

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com