Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185534
  • 博文数量: 87
  • 博客积分: 2975
  • 博客等级: 少校
  • 技术积分: 926
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-24 17:14
文章分类

全部博文(87)

文章存档

2010年(87)

我的朋友

分类: 嵌入式

2010-07-30 22:49:16

一、实现步骤

1. 硬件原理图分析。由原理图可知每个按键所用到的外部中断分别是EINT8、EINT11、EINT13、EINT14、EINT15、EINT19,所对应的IO口分别是GPG0、GPG3、GPG5、GPG6、GPG7、GPG11。再由按键的接口电路可知,当按键按下时按键接通,中断线上原有的VDD33V高电平被拉低,从而触发中断的产生。

 
2. 开始编写合适mini2440的按键驱动(含去抖动功能),文件名:my2440_buttons.c

1)按键驱动基本框架。这里我就指定主设备号为232,简单的注册为字符设备,另定义了一个结构体把按键要用到的资源组织起来

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>

#include <mach/regs-gpio.h>

#include <mach/hardware.h>

#define DEVICE_NAME    "my2440_buttons"
//设备名称
#define DEVICE_MAJOR   232             
//主设备号
struct button_irq_desc     //组织硬件资源结构体
{
    int irq;        
//中断号
    int pin;        
//对应的IO引脚
    int pin_setting;
//引脚配置
    char *name;     
//按键名称,注意这个名称,在后面的一个现象中会出现
};
//定义6个按键资源结构体数组
static struct button_irq_desc button_irqs[] =
{
    {IRQ_EINT8 , S3C2410_GPG0 , S3C2410_GPG0_EINT8  , "KEY0"},
    {IRQ_EINT11, S3C2410_GPG3 , S3C2410_GPG3_EINT11 , "KEY1"},
    {IRQ_EINT13, S3C2410_GPG5 , S3C2410_GPG5_EINT13 , "KEY2"},
    {IRQ_EINT14, S3C2410_GPG6 , S3C2410_GPG6_EINT14 , "KEY3"},
    {IRQ_EINT15, S3C2410_GPG7 , S3C2410_GPG7_EINT15 , "KEY4"},
    {IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, "KEY5"},
};


static int __init button_init(void)
{
    int ret;
    
//注册字符设备
    ret = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &buttons_fops);
    if(ret < 0)
    {
        printk(DEVICE_NAME " register faild!\n");
        return ret;
    }
    return 0;
}
static void __exit button_exit(void)
{
  
unregister_chrdev(DEVICE_MAJOR, DEVICE_NAME); //注销字符设备
}
module_init(button_init);
module_exit(button_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("apple");
MODULE_DESCRIPTION("My2440 button driver");

2)设备注册时用到的设备操作结构体buttons_fops的定义。这里由于按键是输入的设备,所以这里只有read,没有write,另因在应用程序中要实时监测哪个按键被按下,故这里就用poll在内核中遍历,来提供给应用中的select遍历判断资源是否可获取,可获取才来read

#include <linux/poll.h>    //poll要用到的头文件

//设备操作列表
static struct file_operations buttons_fops =
{
    .owner        = THIS_MODULE,
    .open         = buttons_open,
    .release      = buttons_close,
    .read         = buttons_read,
    .poll         = buttons_poll,
};


3)设备操作结构体中open的实现。 在open中分别实现了IO口的配置、中断触发的方式、申请中断、初始化6个按键的初始状态和初始化6个按键去抖动定时器。中断服务程序为buttons_interrupt,传过去的参数是当前的中断号和索引;定时器服务程序为buttons_timer,传过去的参数是当前定时器的索引。注意:这里有一个关键字volatile,为什么要用这个关键字呢?请看这里:http://blog.chinaunix.net/u1/41845/showart_2038284.html

//中断要用到的头文件

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <asm/irq.h>

 

#define KEY_DOWN            0   //按键按下                    
#define KEY_UP              1   //按键抬起                
#define KEY_UNCERTAIN       2   //按键不确定                    
#define KEY_COUNT           6   //6个按键

static volatile int key_status[KEY_COUNT];      //记录6个按键的状态 
static struct timer_list key_timers[KEY_COUNT]; //6个按键去抖动定时器

static int buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int ret;
    for(i = 0; i < KEY_COUNT; i++)
    {
        //设置6个IO口为中断触发方式
        s3c2410_gpio_cfgpin(button_irqs[i].pin, button_irqs[i].pin_setting);
        //设置中断下降沿为有效触发
        set_irq_type(button_irqs[i].irq, IRQ_TYPE_EDGE_FALLING);   
        //申请中断(类型为快速中断,中断服务时屏蔽所有外部中断?)
        ret = request_irq(button_irqs[i].irq, buttons_interrupt, IRQF_DISABLED, button_irqs[i].name, (void *)i);
        if(ret)
        {
            break;
        }
        key_status[i] = KEY_UP;   //初始化6个按键的状态为抬起
        //初始化并设置6个去抖定时器
        setup_timer(&key_timers[i], buttons_timer, i);
    }
    if(ret)          //中断申请失败处理
    {
        i--;
        for(;i>= 0;i--)
        {
            //释放已注册成功的中断
            disable_irq(button_irqs[i].irq);
            free_irq(button_irqs[i].irq, (void *)i);
        }
        return -EBUSY;
    }
    return 0;
}


4)中断服务程序和去抖动定时器服务程序的实现。这里的中断服务和定时器服务互相的作用,首先中断触发后启动延时定时器,进入定时器服务后处理按键的状态,最后当前按键抬起后,中断服务又开始处理新的中断

#define KEY_TIMER_DELAY1  (HZ/50)       //按键按下去抖延时20毫秒        
#define KEY_TIMER_DELAY2  (HZ/10)      
//按键抬起去抖延时100毫秒

static volatile int ev_press = 0;      //按键按下产生标识,用于在读设备的时候来判断是否有数据可读,否则进程睡眠

static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //等待队列的定义并初始化
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    //获取当前按键资源的索引
    int key = (int)dev_id;

    //判断当前按键的状态已经抬起后才服务中断
    if(key_status[key] == KEY_UP)
    {
        //设置当前按键的状态为不确定
        key_status[key] = KEY_UNCERTAIN;
        //设置当前按键按下去抖定时器的延时并启动定时器
        key_timers[key].expires = jiffies + KEY_TIMER_DELAY1;
        add_timer(&key_timers[key]);
    }
    return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer(unsigned long arg)
{
    //获取当前按键资源的索引
    int key = arg;
    //获取当前按键引脚上的电平值来判断按键是按下还是抬起
    int up = s3c2410_gpio_getpin(button_irqs[key].pin);
    if(!up)//低电平,按键按下
    {
        if(key_status[key] == KEY_UNCERTAIN)
        {
            //标识当前按键状态为按下
            key_status[key] = KEY_DOWN;
            //标识当前按键已按下并唤醒等待队列让设备进行读取
            ev_press = 1;

            wake_up_interruptible(&button_waitq);
        }
        //设置当前按键抬起去抖定时器的延时并启动定时器
        key_timers[key].expires = jiffies + KEY_TIMER_DELAY2;
        add_timer(&key_timers[key]);
    }
    else      //高电平,按键抬起
    {
        key_status[key] = KEY_UP;    //标识当前按键状态为抬起
    }
}

5)读设备的实现。从电路图可以看出按键设备相对于CPU来说为输入设备,所以这里只有read,而没有write

static int buttons_read(struct file *file, char __user *buf, size_t count, loff_t *offp)
{
    unsigned long ret;
    if(!ev_press)
//按键按下发生标识,0没有发生
    {
        if(file->f_flags & O_NONBLOCK)
        {
            //应用程序若采用非阻塞方式读取则返回错误
            return -EAGAIN;
        }
        else
        {
            //以阻塞方式读取且按键没按下产生,让等待队列进入睡眠
            wait_event_interruptible(button_waitq, ev_press);
        }
    }
    //1为按键按下产生,并清除标识为0,准备给下一次判断用
   ev_press = 0;
    //将内核中的按键状态数据拷贝到用户空间给应用程序使用
    ret = copy_to_user(buf, (void *)key_status, min(sizeof(key_status), count));
    return ret ? -EFAULT : min(sizeof(key_status), count);
}


6)驱动中的轮询。这个与应用程序中的select的使用相对应
 

//驱动程序中的轮询,用于应用程序中的轮询查询是否可对设备进行访问
static int buttons_poll(struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;
    //添加等待队列到等待队列表中(poll_table)
    poll_wait(file, &button_waitq, wait);
    if(ev_press)
    {

      mask |= POLLIN | POLLRDNORM;   //标识数据可以获得
    }
    return mask;
}


7)设备的关闭。
 

static int buttons_close(struct inode *inode, struct file *file)
{
    int i;

    for(i = 0; i < KEY_COUNT; i++)   //释放6个定时器和中断
    {
        del_timer(&key_timers[i]);
        disable_irq(button_irqs[i].irq);
        free_irq(button_irqs[i].irq, (void *)i);
    }
    return 0;
}

3. 完整的按键驱动代码

/*===================================================
 Name            : my2440_buttons.c
 Author          : apple
 Date            : 09/11/2009
 Copyright       : Dual BSD/GPL
 Description     : my2440 buttons driver
 ===================================================*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>

#define DEVICE_NAME         "my2440_buttons"   //设备名称
#define DEVICE_MAJOR        232                //主设备号                
#define KEY_TIMER_DELAY1    (HZ/50)          //按键按下去抖延时20毫秒        
#define KEY_TIMER_DELAY2    (HZ/10)          //按键抬起去抖延时100毫秒
#define KEY_DOWN            0                 //按键按下                    
#define KEY_UP              1                //按键抬起                
#define KEY_UNCERTAIN       2                //按键不确定                    
#define KEY_COUNT           6                   //6个按键                    

static volatile int ev_press = 0;                //按键按下产生标志
static volatile int key_status[KEY_COUNT];       //记录6个按键的状态    
static struct timer_list key_timers[KEY_COUNT];  //定义6个按键去抖动定时器
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);    //定义并初始化等待队列
//组织硬件资源结构体
struct button_irq_desc    
{
    int irq;            //中断号
    int pin;            //引脚
    int pin_setting;    //引脚配置
    char *name;         //按键名称,注意这个名称,在后面的一个现象中会出现

};
//定义6个按键资源结构体数组
static struct button_irq_desc button_irqs[] =
{
    {IRQ_EINT8 , S3C2410_GPG0 , S3C2410_GPG0_EINT8 , "KEY0"},
    {IRQ_EINT11, S3C2410_GPG3 , S3C2410_GPG3_EINT11 , "KEY1"},
    {IRQ_EINT13, S3C2410_GPG5 , S3C2410_GPG5_EINT13 , "KEY2"},
    {IRQ_EINT14, S3C2410_GPG6 , S3C2410_GPG6_EINT14 , "KEY3"},
    {IRQ_EINT15, S3C2410_GPG7 , S3C2410_GPG7_EINT15 , "KEY4"},
    {IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, "KEY5"},
};
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    //获取当前按键资源的索引
    int key = (int)dev_id;
    if(key_status[key] == KEY_UP)
    {
        //设置当前按键的状态为不确定
        key_status[key] = KEY_UNCERTAIN;
        //设置当前按键按下去抖定时器的延时并启动定时器
        key_timers[key].expires = jiffies + KEY_TIMER_DELAY1;
        add_timer(&key_timers[key]);
    }
    return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer(unsigned long arg)
{
    //获取当前按键资源的索引
    int key = arg;
    //获取当前按键引脚上的电平值来判断按键是按下还是抬起
    int up = s3c2410_gpio_getpin(button_irqs[key].pin);
    if(!up)//低电平,按键按下
    {
        if(key_status[key] == KEY_UNCERTAIN)
        {
            //标识当前按键状态为按下
            key_status[key] = KEY_DOWN;
            //标识当前按键已按下并唤醒等待队列
            ev_press = 1;
            wake_up_interruptible(&button_waitq);
        }
        //设置当前按键抬起去抖定时器的延时并启动定时器
        key_timers[key].expires = jiffies + KEY_TIMER_DELAY2;
        add_timer(&key_timers[key]);
    }
    else//高电平,按键抬起
    {
        //标识当前按键状态为抬起
        key_status[key] = KEY_UP;
    }
}

static int buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int ret;
    for(i = 0; i < KEY_COUNT; i++)
    {
        //设置6个IO口为中断触发方式
        s3c2410_gpio_cfgpin(button_irqs[i].pin, button_irqs[i].pin_setting);
        //设置中断下降沿为有效触发
        set_irq_type(button_irqs[i].irq, IRQ_TYPE_EDGE_FALLING);   
        //申请中断(类型为快速中断,中断服务时屏蔽所有外部中断?)
        ret = request_irq(button_irqs[i].irq, buttons_interrupt, IRQF_DISABLED, button_irqs[i].name, (void *)i);
        if(ret)
        {
            break;
        }
        //初始化6个按键的状态为抬起
        key_status[i] = KEY_UP;
        //初始化并设置6个去抖定时器
        setup_timer(&key_timers[i], buttons_timer, i);
    }
    if(ret)
    {
        //中断申请失败处理
        i--;
        for(; i >= 0; i--)
        {
            //释放已注册成功的中断
            disable_irq(button_irqs[i].irq);
            free_irq(button_irqs[i].irq, (void *)i);
        }
        return -EBUSY;
    }
    return 0;
}

static int buttons_close(struct inode *inode, struct file *file)
{
    int i;
    //释放6个定时器和中断
    for(i = 0; i < KEY_COUNT; i++)
    {
        del_timer(&key_timers[i]);
        disable_irq(button_irqs[i].irq);
        free_irq(button_irqs[i].irq, (void *)i);
    }
    return 0;
}

static int buttons_read(struct file *file, char __user *buf, size_t count, loff_t *offp)
{
    unsigned long ret;
    if(!ev_press)
//判断按键按下产生标识,0没有产生
    {
        if(file->f_flags & O_NONBLOCK)
        {
            
//应用程序若采用非阻塞方式读取则返回错误
            return -EAGAIN;
        }
        else
        {
            //以阻塞方式读取且按键按下没有产生,让等待队列进入睡眠
            wait_event_interruptible(button_waitq, ev_press);
        }
    }
    //1为按键按下产生,并清除标识为0,准备给下一次判断用
    ev_press = 0;
    //将内核中的按键状态数据拷贝到用户空间给应用程序使用
    ret = copy_to_user(buf, (void *)key_status, min(sizeof(key_status), count));
    return ret ? -EFAULT : min(sizeof(key_status), count);
}

//驱动程序中的轮询,用于应用程序中的轮询查询是否可对设备进行访问
static int buttons_poll(struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;
    //添加等待队列到等待队列表中(poll_table)
    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,
    .release      = buttons_close,
    .read         = buttons_read,
    .poll         = buttons_poll,
};

static int __init button_init(void)
{
    int ret;
    //注册字符设备
    ret = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &buttons_fops);
    if(ret < 0)
    {
        printk(DEVICE_NAME " register faild!\n");
        return ret;
    }
    return 0;
}

static void __exit button_exit(void)
{
    //注销字符设备
    unregister_chrdev(DEVICE_MAJOR, DEVICE_NAME);
}
module_init(button_init);
module_exit(button_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("apple");
MODULE_DESCRIPTION("My2440 button driver");


4.编写Makefile文件,如下所示。
 
ifneq ($(KERNELRELEASE),)
obj-m := my2440button.o
else
KDIR := /home/study/driver/Kernel/linux-2.6.29
CROSS_COMPILE = /usr/local/arm/4.3.2/bin/arm-linux-
all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c.tmp_versions *.mod.c *.symvers *.order
.PHONY:modules clean
endif
5.编译,生成.ko文件

[root@localhost driver]# ls
Makefile  my2440button.c
[root@localhost driver]# make
make -C /home/study/driver/Kernel/linux-2.6.29 M=/home/study/driver/mini2440_driver/button/driver modules
make[1]: Entering directory `/home/study/driver/Kernel/linux-2.6.29'
  CC [M]  /home/study/driver/mini2440_driver/button/driver/my2440button.o
/home/study/driver/mini2440_driver/button/driver/my2440button.c:186: warning: initialization from incompatible pointer type
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/study/driver/mini2440_driver/button/driver/my2440button.mod.o
  LD [M]  /home/study/driver/mini2440_driver/button/driver/my2440button.ko
make[1]: Leaving directory `/home/study/driver/Kernel/linux-2.6.29'

6. 加载进入内核,查看已加载的设备:#cat /proc/devices,可以看到my2440_buttons的主设备号为232

[root@FriendlyARM mini2440]# ls                                                         
button_apply     my2440button.ko                                                        
[root@FriendlyARM mini2440]# insmod my2440button.ko                                     
[root@FriendlyARM mini2440]# cat /proc/devices                                          
Character devices:                                                                      
  1 mem                                                                                 
  4 /dev/vc/0                                                                           
  4 tty                                                                                 
  5 /dev/tty                                                                            
  5 /dev/console                                                                        
  5 /dev/ptmx                                                                           
  7 vcs                                                                                 
 10 misc                                                                                
 13 input                                                                               
 14 sound                                                                               
 29 fb                                                                                  
 81 video4linux                                                                         
 89 i2c                                                                                 
 90 mtd                                                                                 
116 alsa                                                                                
128 ptm                                                                                 
136 pts                                                                                 
180 usb                                                                                 
188 ttyUSB                                                                              
189 usb_device                                                                          
204 s3c2410_serial                                                                      
232 my2440_buttons                                                                      
253 usb_endpoint                                                                        
254 rtc  

7.编写应用程序测试按键驱动,文件名:buttons_test.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv)
{
    int fd;
    int key_status[6];
    //以阻塞方式打开设备文件,非阻塞时flags=O_NONBLOCK
    fd = open("/dev/my2440_buttons", 0);
    if(fd < 0)
    {
        printf("Open Buttons Device Faild!\n");
        exit(1);
    }
    while(1)
    {
        int i;
        int ret;
        fd_set rds;     
        FD_ZERO(&rds);
        FD_SET(fd, &rds);  
        //应用程序进行轮询,查询是否可对设备进行访问
        ret = select(fd + 1, &rds, NULL, NULL, NULL);
        if(ret < 0)
        {
            printf("Read Buttons Device Faild!\n");
            exit(1);
        }
        if(ret == 0)
        {
            printf("Read Buttons Device Timeout!\n");
        }
        else if(FD_ISSET(fd, &rds))
        {
          ret = read(fd, key_status, sizeof(key_status))//读设备
          if(ret != sizeof(key_status))
            {
                if(errno != EAGAIN)
                {
                    printf("Read Button Device Faild!\n");
                }
                continue;
            }
            else
            {
                for(i = 0; i < 6; i++)
                {
                    //对应驱动中按键的状态,为0即按键被按下
                    if(key_status[i] == 0)
                    {
                        printf("Key%d DOWN\n", i + 1);
                    }
                }
            }
        }
    }
    close(fd);
    return 0;
}


8.在开发主机上交叉编译测试应用程序。

#arm-linux-gcc -o buttons_apply buttons_apply.c


9. 在开发板上的文件系统中创建一个按键设备的节点,然后运行测试程序,效果图如下,观测按开发板上的按键时,在串口工具中会输出对应按键被按下的信息,也不会出现抖动现象(即按某个按键时,不会多次产生该按键按下的情况)

 

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