美文网首页
四 . 树莓派A20 GPIO中断程序编写(2 按键消抖)

四 . 树莓派A20 GPIO中断程序编写(2 按键消抖)

作者: wit_yuan | 来源:发表于2017-08-01 18:21 被阅读0次

1 简介

这篇文章是接着 《四 . 树莓派A20 GPIO中断程序编写(1基本处理)》 链接 来说的。

再上篇文章中,并没有谈到按键的消抖问题,导致再读取按键的时候,可能会出现连续读取了多个值的情况。

这篇文章主要添加了一个timer,也可以看作是推后一个时间来读取数据。这是基于这样的一个事实:
按键按下,初始过程中,会有抖动,这个时候,会频繁进入中断,那么中断程序中就频繁调用这个timer,从而一直修改读取时间,也就是不给应用层汇报值,一直到键值稳定,再去推后一个时间读取稳定的按键值。

2 基本的定时器消抖程序

首先按照sys_config.fex文件,如下所示:


按键配置

然后是我们需要做的驱动程序key.c:

#include "linux/init.h"
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/leds.h>
#include <plat/sys_config.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/irqs.h>
#include <mach/system.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/gpio.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/wait.h>


/* EINT type PIO controller registers */
#define PIO_INT_CFG0_OFFSET 0x200
#define PIO_INT_CFG1_OFFSET 0x204
#define PIO_INT_CFG2_OFFSET 0x208
#define PIO_INT_CFG3_OFFSET 0x20c


#define PIO_INT_STAT_OFFSET (0x214)
#define PIO_INT_DATA_OFFSET (0x130)

#define IRQ_EINT22          22
#define IRQ_EINT23          23

#define PIO_BASE_ADDRESS    SW_PA_PORTC_IO_BASE
#define PIO_RANGE_SIZE      (0x400)
#define PIO_INT_CTRL_OFFSET (0x210)



/* EINT type defines */
#define POSITIVE_EDGE       0x0
#define NEGATIVE_EDGE       0x1
#define HIGH_LEVEL      0x2
#define LOW_LEVEL       0x3
#define DOUBLE_EDGE     0x4


static wait_queue_head_t    key_data_avail;
static unsigned int key_done = 0;


static int int_cfg_addr[] = {PIO_INT_CFG0_OFFSET,
                 PIO_INT_CFG1_OFFSET,
                 PIO_INT_CFG2_OFFSET,
                 PIO_INT_CFG3_OFFSET};


/* Setup GPIO irq mode (FALLING, RISING, BOTH, etc */
#define SUNXI_SET_GPIO_IRQ_TYPE(addr, offs, mode) ({ \
    __u32 reg_bit = offs % 8; \
    __u32 reg_num = offs / 8; \
    __u32 reg_val = readl(addr + int_cfg_addr[reg_num]); \
    reg_val &= (~(0xf << (reg_bit * 4))); \
    reg_val |= (mode << (reg_bit * 4)); \
    writel(reg_val, addr + int_cfg_addr[reg_num]); \
})


/* Enable GPIO interrupt for pin */
#define SUNXI_UNMASK_GPIO_IRQ(addr, irq) ({ \
    __u32 reg_val = readl(addr + PIO_INT_CTRL_OFFSET); \
    reg_val |= (1 << irq); \
    writel(reg_val, addr + PIO_INT_CTRL_OFFSET); \
})

/* Disable GPIO interrupt for pin */
#define SUNXI_MASK_GPIO_IRQ(addr, irq) ({ \
    __u32 reg_val = readl(addr + PIO_INT_CTRL_OFFSET); \
    reg_val &= ~(1 << irq); \
    writel(reg_val, addr + PIO_INT_CTRL_OFFSET); \
})



/* Set GPIO pin mode (input, output, etc)            */
/* GPIO port has 4 cfg 32bit registers (8 pins each) */
/* First port cfg register addr = port_num * 0x24    */
#define SUNXI_SET_GPIO_MODE(addr, port, pin, mode) ({ \
    __u32 reg_val = 0; \
    __u32 pin_idx = pin >> 3; \
    void *raddr = addr + (((port)-1)*0x24 + ((pin_idx)<<2) + 0x00); \
    reg_val = readl(raddr); \
    reg_val &= ~(0x07 << (((pin - (pin_idx<<3))<<2))); \
    reg_val |= mode << (((pin - (pin_idx<<3))<<2)); \
    writel(reg_val, raddr); \
})


struct timer_list g_timer[2];



static script_gpio_set_t info;
static unsigned key_handler1;
static unsigned key_handler2;

static struct class *key_class;
static struct device *key_device;
static unsigned int key_major;

static unsigned int key_value;

static void *__iomem gpio_addr = NULL;

static int key_open(struct inode *inode, struct file *filp);
static ssize_t key_read (struct file *, char __user *, size_t, loff_t *);
static ssize_t key_write (struct file *filp, const char __user *buf, size_t len, loff_t *off);
static int key_close(struct inode *inode, struct file *filp);


struct file_operations key_operations = {
    .owner   = THIS_MODULE,
    .open    = key_open,
    .read    = key_read,
    .write   = key_write,
    .release = key_close,
};

struct key_str{
    char *name;
    int val;
};

struct key_str g_key_str[2]={{"key1",0x1},{"key2",0x02}};

static irqreturn_t key_irq_handler1(int irq, void *dev_id)
{
    int reg_val = 0;
    //clear the IRQ_EINT22 interrupt pending
    reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
    if (reg_val & (1 << (IRQ_EINT22))) {
        //printk("==IRQ_EINT22=\r\n");
        writel(reg_val & (1 << (IRQ_EINT22)),
           gpio_addr + PIO_INT_STAT_OFFSET);
        mod_timer(&g_timer[0],jiffies + HZ/100);
    }   
    return IRQ_HANDLED;
}


static irqreturn_t key_irq_handler2(int irq, void *dev_id)
{
    
    int reg_val;
    //clear the IRQ_EINT22 interrupt pending
    reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
#if 1
    if (reg_val & (1 << (IRQ_EINT23))) {
        writel(reg_val & (1 << (IRQ_EINT23)),
               gpio_addr + PIO_INT_STAT_OFFSET);
        mod_timer(&g_timer[1],jiffies + HZ/100);
    }
#endif

    return IRQ_HANDLED;
}

void key_timer_function1(unsigned long data)
{
    int ret_val;

    ret_val = readl(gpio_addr + PIO_INT_DATA_OFFSET);
    if(!(ret_val&(1<<10)))
    {
        //printk("key1 pressed \r\n");
        key_value |= g_key_str[0].val;
    }
    else
    {
        //printk("key1 released \r\n");
        key_value &= ~g_key_str[0].val;
    }   
    
    key_done = 1;
    wake_up_interruptible(&key_data_avail);
}

void key_timer_function2(unsigned long data)
{
    int ret_val = 0;
    ret_val = readl(gpio_addr + PIO_INT_DATA_OFFSET);
    if(!(ret_val&(1<<11)))
    {
        //printk("key2 pressed \r\n");
        key_value |= g_key_str[1].val;
    }
    else
    {
        //printk("key2 released \r\n");
        key_value &= ~g_key_str[1].val;
    }   
    key_done = 1;
    wake_up_interruptible(&key_data_avail);
}



static ssize_t key_read (struct file *file, char __user *buf, size_t len, loff_t *off)
{
    unsigned int value = 0;

    key_done = 0;
    wait_event_interruptible(key_data_avail,key_done);
    value = copy_to_user(buf,&key_value,4);
    return value;
}

static int key_open(struct inode *inode, struct file *filp)
{
    int err = 0;
    int key_test_enabled = 0;

    err = script_parser_fetch("key_test_para", "key_test_enable", &key_test_enabled,
                    sizeof(key_test_enabled)/sizeof(int));

    if(!err){
        printk("---script.bin key get ok,value:%d----\n",key_test_enabled);
    }
    else
    {
        printk("---script.bin key get false----\n");    
        return -1;
    }

    err = script_parser_fetch("key_test_para", "key1",
                (int *)&info,
                sizeof(script_gpio_set_t));
    if (err) {
        printk("----script.bin get io error----\r\n");
        return -1;
    }

    err = script_parser_fetch("key_test_para", "key2",
                (int *)&info,
                sizeof(script_gpio_set_t));
    if (err) {
        printk("----script.bin get io error----\r\n");
        return -1;
    }



    /* reserve gpio for led */
    key_handler1 = gpio_request_ex("key_test_para", "key1");
    if (!key_handler1) {
        printk("----script.bin can't requst handler----\r\n");
        return -1;
    }

    
    /* reserve gpio for led */
    key_handler2 = gpio_request_ex("key_test_para", "key2");
    if (!key_handler2) {
        printk("----script.bin can't requst handler----\r\n");
        return -1;
    }

#if 1
    /*设置为输入,没有上下拉*/
    err = gpio_set_one_pin_io_status(key_handler1,0,"key1");
    if (err) {
        printk("----set io input 1 error----\r\n");
        return -1;
    }   
    err = gpio_set_one_pin_io_status(key_handler2,0,"key2");
    if (err) {
        printk("----set io input 2 error----\r\n");
        return -1;
    }   

#endif

    err = gpio_set_one_pin_pull(key_handler1,1,"key1");
    if (err) {
        printk("----set io pull error----\r\n");
        return -1;
    }
    
    err = gpio_set_one_pin_pull(key_handler2,1,"key2");
    if (err) {
        printk("----set io pull error----\r\n");
        return -1;
    }

    if (!gpio_addr) {
        gpio_addr = ioremap(PIO_BASE_ADDRESS, PIO_RANGE_SIZE);
    }

    if(!gpio_addr)
    {
        printk("-----address error-----\r\n");
    }

    err = request_irq(SW_INT_IRQNO_PIO, key_irq_handler1,
              IRQF_SHARED, "gpio_pin_1", &g_key_str[0]);

    if (err < 0) {
        printk(" request irq 1 error:%d\n",err);
        return -1;
    }
    
    err = request_irq(SW_INT_IRQNO_PIO, key_irq_handler2,
              IRQF_SHARED, "gpio_pin_2", &g_key_str[1]);

    if (err < 0) {
        printk(" request irq error:%d\n",err);
        return -1;
    }

    /*set the gpio 1 register*/
    SUNXI_SET_GPIO_IRQ_TYPE(gpio_addr,IRQ_EINT22,DOUBLE_EDGE);  
    SUNXI_UNMASK_GPIO_IRQ(gpio_addr,IRQ_EINT22);
    SUNXI_SET_GPIO_MODE(gpio_addr,9,10,6);/*PI10 EINT22 settiings*/

    /*set the gpio 2 register*/
    SUNXI_SET_GPIO_IRQ_TYPE(gpio_addr,IRQ_EINT23,DOUBLE_EDGE);  
    SUNXI_UNMASK_GPIO_IRQ(gpio_addr,IRQ_EINT23);
    SUNXI_SET_GPIO_MODE(gpio_addr,9,11,6);/*PI11 EINT23 settiings*/


    init_timer(&g_timer[0]);
    g_timer[0].function = key_timer_function1;
    g_timer[0].expires = jiffies + HZ / 100;
    add_timer(&g_timer[0]);

    init_timer(&g_timer[1]);
    g_timer[1].function = key_timer_function2;
    g_timer[1].expires = jiffies + HZ / 100;
    add_timer(&g_timer[1]);


    return 0;
}

static ssize_t key_write (struct file *filp, const char __user *buf, size_t len, loff_t *off)
{

    return 0;
}

static int key_close(struct inode *inode, struct file *filp)
{
    SUNXI_MASK_GPIO_IRQ(gpio_addr,IRQ_EINT22);
    SUNXI_MASK_GPIO_IRQ(gpio_addr,IRQ_EINT23);


    free_irq(SW_INT_IRQNO_PIO, &g_key_str[0]);
    free_irq(SW_INT_IRQNO_PIO, &g_key_str[1]);
    printk("----key close----\r\n");


    return 0;
}




static int __init key_test_init(void)
{
    key_major = register_chrdev(0, "key_chrdev", &key_operations);
    key_class = class_create(THIS_MODULE, "key_class");

    if(!key_class){
        unregister_chrdev(key_major, "key_chrdev");
        printk("----key_chrdev error----\r\n");
        return -1;
    }
    key_device = device_create(key_class, NULL, MKDEV(key_major,0),
                          NULL, "key_device");
    if(!key_device){
        class_destroy(key_class);
        unregister_chrdev(key_major, "key_chrdev");
        printk("----key_device error----\r\n");
        return -1;
    }

    printk("----key init ok----\r\n");

    init_waitqueue_head(&key_data_avail);
    return 0;
}


static void __exit  key_test_exit(void)
{
    del_timer(&g_timer[0]);
    del_timer(&g_timer[1]);
    
    if (gpio_addr) {
        iounmap(gpio_addr);
    }

    if (key_handler1)
        gpio_release(key_handler1, 1);
    if (key_handler2)
        gpio_release(key_handler2, 1);  
    
    device_destroy(key_class, MKDEV(key_major, 0));
    class_destroy(key_class);
    unregister_chrdev(key_major, "key_chrdev");

    printk("---driver exit---\r\n");
}

module_init(key_test_init);
module_exit(key_test_exit);

MODULE_DESCRIPTION("Driver for key");
MODULE_AUTHOR("wit_yuan");
MODULE_LICENSE("GPL");

再就是测试程序,key_test.c:

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
    int fd;
    int val;
    fd = open("/dev/key_device",O_RDWR);
    if(fd < 0){
        printf("---open file error----\r\n");
        return -1;
    }

    while(1)
    {
        read(fd,&val,1);

        if(val!=0)
            printf("val:%0x\r\n",val);

    }
}

然后是Makefile文件:

ifeq ($(KERNELRELEASE),)
    KERNEL_DIR=/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi
    PWD=$(shell pwd)

modules:
    $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
    arm-linux-gnueabihf-gcc -o key key.c

modules_install:
    $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules_install
clean:
    rm -rf *.ko *.o .tmp_versions *.mod.c modules.order Module.symvers .*.cmd
else
    obj-m:=key.o

endif

最后是测试程序key_test.c:

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
    int fd;
    int val;
    fd = open("/dev/key_device",O_RDWR);
    if(fd < 0){
        printf("---open file error----\r\n");
        return -1;
    }
    printf("---application run----\r\n");
    while(1)
    {
        read(fd,&val,1);

        if(val!=0)
            printf("val:%0x\r\n",val);

    }
    return 0;
}

可实现的程序效果如下:


按键测试

3 poll消抖程序

我们仍然写驱动程序,名称为key.c,如下:

#include "linux/init.h"
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/leds.h>
#include <plat/sys_config.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/irqs.h>
#include <mach/system.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/gpio.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fcntl.h>

/* EINT type PIO controller registers */
#define PIO_INT_CFG0_OFFSET 0x200
#define PIO_INT_CFG1_OFFSET 0x204
#define PIO_INT_CFG2_OFFSET 0x208
#define PIO_INT_CFG3_OFFSET 0x20c

#define PIO_INT_STAT_OFFSET (0x214)
#define PIO_INT_DATA_OFFSET (0x130)

#define IRQ_EINT22          22
#define IRQ_EINT23          23

#define PIO_BASE_ADDRESS    SW_PA_PORTC_IO_BASE
#define PIO_RANGE_SIZE      (0x400)
#define PIO_INT_CTRL_OFFSET (0x210)

static DEFINE_SEMAPHORE(key_lock);


/* EINT type defines */
#define POSITIVE_EDGE       0x0
#define NEGATIVE_EDGE       0x1
#define HIGH_LEVEL      0x2
#define LOW_LEVEL       0x3
#define DOUBLE_EDGE     0x4

static wait_queue_head_t    key_wait_q;

static int int_cfg_addr[] = {PIO_INT_CFG0_OFFSET,
                 PIO_INT_CFG1_OFFSET,
                 PIO_INT_CFG2_OFFSET,
                 PIO_INT_CFG3_OFFSET};

/* Setup GPIO irq mode (FALLING, RISING, BOTH, etc */
#define SUNXI_SET_GPIO_IRQ_TYPE(addr, offs, mode) ({ \
    __u32 reg_bit = offs % 8; \
    __u32 reg_num = offs / 8; \
    __u32 reg_val = readl(addr + int_cfg_addr[reg_num]); \
    reg_val &= (~(0xf << (reg_bit * 4))); \
    reg_val |= (mode << (reg_bit * 4)); \
    writel(reg_val, addr + int_cfg_addr[reg_num]); \
})

/* Enable GPIO interrupt for pin */
#define SUNXI_UNMASK_GPIO_IRQ(addr, irq) ({ \
    __u32 reg_val = readl(addr + PIO_INT_CTRL_OFFSET); \
    reg_val |= (1 << irq); \
    writel(reg_val, addr + PIO_INT_CTRL_OFFSET); \
})

/* Disable GPIO interrupt for pin */
#define SUNXI_MASK_GPIO_IRQ(addr, irq) ({ \
    __u32 reg_val = readl(addr + PIO_INT_CTRL_OFFSET); \
    reg_val &= ~(1 << irq); \
    writel(reg_val, addr + PIO_INT_CTRL_OFFSET); \
})

/* Set GPIO pin mode (input, output, etc)            */
/* GPIO port has 4 cfg 32bit registers (8 pins each) */
/* First port cfg register addr = port_num * 0x24    */
#define SUNXI_SET_GPIO_MODE(addr, port, pin, mode) ({ \
    __u32 reg_val = 0; \
    __u32 pin_idx = pin >> 3; \
    void *raddr = addr + (((port)-1)*0x24 + ((pin_idx)<<2) + 0x00); \
    reg_val = readl(raddr); \
    reg_val &= ~(0x07 << (((pin - (pin_idx<<3))<<2))); \
    reg_val |= mode << (((pin - (pin_idx<<3))<<2)); \
    writel(reg_val, raddr); \
})

struct timer_list g_timer[2];

static script_gpio_set_t info;
static unsigned key_handler1;
static unsigned key_handler2;

static struct class *key_class;
static struct device *key_device;
static unsigned int key_major;

static unsigned int key_value;

static void *__iomem gpio_addr = NULL;
static int key_press = 0;

static int key_open(struct inode *inode, struct file *filp);
static ssize_t key_read (struct file *, char __user *, size_t, loff_t *);
static ssize_t key_write (struct file *filp, const char __user *buf, size_t len, loff_t *off);
static int key_close(struct inode *inode, struct file *filp);
static unsigned int key_poll(struct file *, struct poll_table_struct *);

struct file_operations key_operations = {
    .owner   = THIS_MODULE,
    .open    = key_open,
    .read    = key_read,
    .write   = key_write,
    .release = key_close,
    .poll    = key_poll,
};

struct key_str{
    char *name;
    int val;
};

struct key_str g_key_str[2]={{"key1",0x1},{"key2",0x02}};

static irqreturn_t key_irq_handler1(int irq, void *dev_id)
{
    int reg_val = 0;
    //clear the IRQ_EINT22 interrupt pending
    reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
    if (reg_val & (1 << (IRQ_EINT22))) {
        //printk("==IRQ_EINT22=\r\n");
        writel(reg_val & (1 << (IRQ_EINT22)),
           gpio_addr + PIO_INT_STAT_OFFSET);
        mod_timer(&g_timer[0],jiffies + HZ/100);
    }   
    return IRQ_HANDLED;
}

static irqreturn_t key_irq_handler2(int irq, void *dev_id)
{
    
    int reg_val;
    //clear the IRQ_EINT22 interrupt pending
    reg_val = readl(gpio_addr + PIO_INT_STAT_OFFSET);
#if 1
    if (reg_val & (1 << (IRQ_EINT23))) {
        writel(reg_val & (1 << (IRQ_EINT23)),
               gpio_addr + PIO_INT_STAT_OFFSET);
        mod_timer(&g_timer[1],jiffies + HZ/100);
    }
#endif

    return IRQ_HANDLED;
}

void key_timer_function1(unsigned long data)
{
    int ret_val;

    ret_val = readl(gpio_addr + PIO_INT_DATA_OFFSET);
    if(!(ret_val&(1<<10)))
    {
        //printk("key1 pressed \r\n");
        key_value |= g_key_str[0].val;
    }
    else
    {
        //printk("key1 released \r\n");
        key_value &= ~g_key_str[0].val;
    }   
    
    key_press = 1;
    wake_up_interruptible(&key_wait_q);
}

void key_timer_function2(unsigned long data)
{
    int ret_val = 0;
    ret_val = readl(gpio_addr + PIO_INT_DATA_OFFSET);
    if(!(ret_val&(1<<11)))
    {
        //printk("key2 pressed \r\n");
        key_value |= g_key_str[1].val;
    }
    else
    {
        //printk("key2 released \r\n");
        key_value &= ~g_key_str[1].val;
    }   
    key_press = 1;
    wake_up_interruptible(&key_wait_q);
}

static unsigned int key_poll(struct file *filep, struct poll_table_struct *wait)
{
    unsigned int mask = 0;

    poll_wait(filep,&key_wait_q,wait);

    if(key_press){
        mask |= POLLIN | POLLRDNORM;
    }
    return mask;
}

static ssize_t key_read (struct file *filep, char __user *buf, size_t len, loff_t *off)
{
    unsigned int value = 0;

    if(len != 4)    
        return -EINVAL;

    if(filep->f_flags & O_NONBLOCK){
        if(!key_press){
            return -EAGAIN;
        }
    }
    else
    {
        wait_event_interruptible(key_wait_q,key_press);
    }
    key_press = 0;
    value = copy_to_user(buf,&key_value,4);
    return value;
}

static int key_open(struct inode *inode, struct file *filep)
{
    int err = 0;
    int key_test_enabled = 0;


    if(filep->f_flags & O_NONBLOCK){
        if(down_trylock(&key_lock)){
            return -EBUSY;
        }
    }
    else
    {
        down(&key_lock);
    }
    err = script_parser_fetch("key_test_para", "key_test_enable", &key_test_enabled,
                    sizeof(key_test_enabled)/sizeof(int));
    if(!err){
        printk("---script.bin key get ok,value:%d----\n",key_test_enabled);
    }
    else
    {
        printk("---script.bin key get false----\n");    
        return -1;
    }

    err = script_parser_fetch("key_test_para", "key1",
                (int *)&info,
                sizeof(script_gpio_set_t));
    if (err) {
        printk("----script.bin get io error----\r\n");
        return -1;
    }

    err = script_parser_fetch("key_test_para", "key2",
                (int *)&info,
                sizeof(script_gpio_set_t));
    if (err) {
        printk("----script.bin get io error----\r\n");
        return -1;
    }

    /* reserve gpio for led */
    key_handler1 = gpio_request_ex("key_test_para", "key1");
    if (!key_handler1) {
        printk("----script.bin can't requst handler----\r\n");
        return -1;
    }
    /* reserve gpio for led */
    key_handler2 = gpio_request_ex("key_test_para", "key2");
    if (!key_handler2) {
        printk("----script.bin can't requst handler----\r\n");
        return -1;
    }
#if 1
    /*设置为输入,没有上下拉*/
    err = gpio_set_one_pin_io_status(key_handler1,0,"key1");
    if (err) {
        printk("----set io input 1 error----\r\n");
        return -1;
    }   
    err = gpio_set_one_pin_io_status(key_handler2,0,"key2");
    if (err) {
        printk("----set io input 2 error----\r\n");
        return -1;
    }   
#endif
    err = gpio_set_one_pin_pull(key_handler1,1,"key1");
    if (err) {
        printk("----set io pull error----\r\n");
        return -1;
    }
    err = gpio_set_one_pin_pull(key_handler2,1,"key2");
    if (err) {
        printk("----set io pull error----\r\n");
        return -1;
    }

    if (!gpio_addr) {
        gpio_addr = ioremap(PIO_BASE_ADDRESS, PIO_RANGE_SIZE);
    }

    if(!gpio_addr)
    {
        printk("-----address error-----\r\n");
    }

    err = request_irq(SW_INT_IRQNO_PIO, key_irq_handler1,
              IRQF_SHARED, "gpio_pin_1", &g_key_str[0]);

    if (err < 0) {
        printk(" request irq 1 error:%d\n",err);
        return -1;
    }
    
    err = request_irq(SW_INT_IRQNO_PIO, key_irq_handler2,
              IRQF_SHARED, "gpio_pin_2", &g_key_str[1]);

    if (err < 0) {
        printk(" request irq error:%d\n",err);
        return -1;
    }

    /*set the gpio 1 register*/
    SUNXI_SET_GPIO_IRQ_TYPE(gpio_addr,IRQ_EINT22,DOUBLE_EDGE);  
    SUNXI_UNMASK_GPIO_IRQ(gpio_addr,IRQ_EINT22);
    SUNXI_SET_GPIO_MODE(gpio_addr,9,10,6);/*PI10 EINT22 settiings*/

    /*set the gpio 2 register*/
    SUNXI_SET_GPIO_IRQ_TYPE(gpio_addr,IRQ_EINT23,DOUBLE_EDGE);  
    SUNXI_UNMASK_GPIO_IRQ(gpio_addr,IRQ_EINT23);
    SUNXI_SET_GPIO_MODE(gpio_addr,9,11,6);/*PI11 EINT23 settiings*/


    init_timer(&g_timer[0]);
    g_timer[0].function = key_timer_function1;
    g_timer[0].expires = jiffies + HZ / 100;
    add_timer(&g_timer[0]);

    init_timer(&g_timer[1]);
    g_timer[1].function = key_timer_function2;
    g_timer[1].expires = jiffies + HZ / 100;
    add_timer(&g_timer[1]);


    return 0;
}

static ssize_t key_write (struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
    return 0;
}

static int key_close(struct inode *inode, struct file *filp)
{
    SUNXI_MASK_GPIO_IRQ(gpio_addr,IRQ_EINT22);
    SUNXI_MASK_GPIO_IRQ(gpio_addr,IRQ_EINT23);

    free_irq(SW_INT_IRQNO_PIO, &g_key_str[0]);
    free_irq(SW_INT_IRQNO_PIO, &g_key_str[1]);
    printk("----key close----\r\n");

    return 0;
}

static int __init key_test_init(void)
{
    key_major = register_chrdev(0, "key_chrdev", &key_operations);
    key_class = class_create(THIS_MODULE, "key_class");

    if(!key_class){
        unregister_chrdev(key_major, "key_chrdev");
        printk("----key_chrdev error----\r\n");
        return -1;
    }
    key_device = device_create(key_class, NULL, MKDEV(key_major,0),
                          NULL, "key_device");
    if(!key_device){
        class_destroy(key_class);
        unregister_chrdev(key_major, "key_chrdev");
        printk("----key_device error----\r\n");
        return -1;
    }

    printk("----key init ok----\r\n");
    

    init_waitqueue_head(&key_wait_q);
    return 0;
}


static void __exit  key_test_exit(void)
{
    del_timer(&g_timer[0]);
    del_timer(&g_timer[1]);
    
    if (gpio_addr) {
        iounmap(gpio_addr);
    }

    if (key_handler1)
        gpio_release(key_handler1, 1);
    if (key_handler2)
        gpio_release(key_handler2, 1);  
    
    device_destroy(key_class, MKDEV(key_major, 0));
    class_destroy(key_class);
    unregister_chrdev(key_major, "key_chrdev");

    printk("---driver exit---\r\n");
}

module_init(key_test_init);
module_exit(key_test_exit);

MODULE_DESCRIPTION("Driver for key");
MODULE_AUTHOR("wit_yuan");
MODULE_LICENSE("GPL");

再就是Makefile文件:

ifeq ($(KERNELRELEASE),)
    KERNEL_DIR=/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi
    PWD=$(shell pwd)

modules:
    $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
    arm-linux-gnueabihf-gcc -o key key.c

modules_install:
    $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules_install
clean:
    rm -rf *.ko *.o .tmp_versions *.mod.c modules.order Module.symvers .*.cmd
else
    obj-m:=key.o

endif

最后是应用层程序key_test.c:

#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
int main(int argc,char *argv[])
{
    int fd;
    int val;
    int ret;
    struct pollfd fds[1];
    fd = open("/dev/key_device",O_RDWR);
    if(fd < 0){
        printf("---open file error----\r\n");
        return -1;
    }

    printf("---application run----\r\n");

    fds[0].fd = fd;
    fds[0].events = POLLIN;

    while(1)
    {
        ret = poll(fds,1,5000);
        if(ret == 0){
            printf("---time out---\r\n");
        }
        else
        {
            read(fd,&val,4);
            if(val!=0)
                printf("val:%0x\r\n",val);
        }
    }
    return 0;
}

最终驱动程序效果如下图所示:


poll方式测试程序

4 select消抖程序

相关文章

网友评论

      本文标题:四 . 树莓派A20 GPIO中断程序编写(2 按键消抖)

      本文链接:https://www.haomeiwen.com/subject/oehmkxtx.html