主题 : 基于中断的按键驱动中测试程序无法休眠? 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 113796
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2015-03-26
最后登录: 2015-04-07
楼主  发表于: 2015-03-26 12:41

 基于中断的按键驱动中测试程序无法休眠?

驱动代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <mach/gpio-fns.h>
#include <mach/gpio-nrs.h>


int major=0;
struct class *buttons_class;
struct device *buttons_dev;
unsigned int key_val=0;
wait_queue_head_t button_wait;


static volatile int flag_press=0;



static irqreturn_t buttons_ISR(int irq, void *dev_id)
{
        switch(irq)
        {
                case IRQ_EINT0:
                        if( s3c2410_gpio_getpin( S3C2410_GPF(0) ) )
                        {
                                key_val = 0x01;
                                printk("key_val = 0x%x\n", key_val);
                        }
                        else
                        {
                                key_val = 0x81;
                                printk("key_val = 0x%x\n", key_val);
                        }
                        break;
                case IRQ_EINT2:
                        if( s3c2410_gpio_getpin( S3C2410_GPF(2) ) )
                                key_val = 0x02;
                        else
                                key_val = 0x82;
                        break;
                case IRQ_EINT11:
                        if( s3c2410_gpio_getpin(S3C2410_GPG(3) ) )
                                key_val = 0x03;
                        else
                                key_val = 0x83;
                        break;
                case IRQ_EINT19:
                        if( s3c2410_gpio_getpin( S3C2410_GPG(11) ) )
                                key_val = 0x04;
                        else
                                key_val = 0x84;
                        break;        
                default:
                        break;
        }

        flag_press = 1;
        wake_up_interruptible(&button_wait);
        
        
        return IRQ_HANDLED;
}

static int buttons_open(struct inode *inode,struct file *file)
{
        request_irq(IRQ_EINT0, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button0", 1);
        request_irq(IRQ_EINT2, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button2", 1);
        request_irq(IRQ_EINT11, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button11", 1);
        request_irq(IRQ_EINT19, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button19", 1);

        init_waitqueue_head(&button_wait);

        return 0;
}

static int buttons_close(struct inode *inode,struct file *file)
{
        free_irq(IRQ_EINT0,1);
        free_irq(IRQ_EINT2,1);
        free_irq(IRQ_EINT11,1);
        free_irq(IRQ_EINT19,1);
        
        return 0;
}
ssize_t buttons_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
        if(size != sizeof(key_val))
        {
                return -EINVAL;
        }
        
        wait_event_interruptible(button_wait, flag_press);

        copy_to_user(buf, &key_val, 1);

        flag_press = 0;
        
        
        return sizeof(key_val);
}


const struct file_operations buttons_fops = {
        .owner        = THIS_MODULE,
        .open             = buttons_open,
        .read              = buttons_read,
        .release        = buttons_close,
};

static int buttons_init(void)
{
        major = register_chrdev(0,"buttons",&buttons_fops);
        buttons_class = class_create(THIS_MODULE,  "my_buttons");
        buttons_dev = device_create(buttons_class, NULL, MKDEV(major,0), NULL, "buttons");

/*        gpfcon = (volatile unsigned long *)ioremap(0x56000050 ,8);
        gpfdat = gpfcon + 1;
        
        gpgcon = (volatile unsigned long *)ioremap(0x56000060 ,8);
        gpgdat = gpgcon + 1;
*/        
        printk("buttons driver initing...\n");
        return 0;
}

static void buttons_exit(void)
{
        unregister_chrdev(0,"buttons");
        device_unregister(buttons_dev);
        class_destroy(buttons_class);
//        iounmap(gpfcon);
//        iounmap(gpgcon);
        printk("Bye,buttons.\n");
}

module_init(buttons_init);
module_exit(buttons_exit);
MODULE_LICENSE("GPL");





测试程序代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char **argv)
{
        int fd;
        int key_val=0;
        fd = open("/dev/buttons",O_RDWR);
        if(fd < 0)
        {
                printf("open error!\n");
        }
        
        while(1)
        {
                read(fd, &key_val, 1);
                
                printf("key_val = 0x%x \n",key_val);
        }
        
        return 0;
}


请高手给我看看啊,终端一直向外打印“key_val = 0x0”,看了半天没看出问题啊?
级别: 新手上路
UID: 113796
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2015-03-26
最后登录: 2015-04-07
1楼  发表于: 2015-03-26 16:34
要沉了  我顶一个啊
过程比结果重要,授之以鱼不如授之以渔。
级别: 新手上路
UID: 111193
精华: 0
发帖: 27
金钱: 135 两
威望: 27 点
综合积分: 54 分
注册时间: 2014-12-17
最后登录: 2018-08-28
2楼  发表于: 2015-03-27 10:20
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <mach/gpio-fns.h>
#include <mach/gpio-nrs.h>


int major=0;

struct class *buttons_class;
struct device *buttons_dev;

unsigned int key_val=0;

wait_queue_head_t button_wait;


static volatile int flag_press=0;


static irqreturn_t buttons_ISR(int irq, void *dev_id)
{
        switch(irq)
        {
                case IRQ_EINT0:
                        if( s3c2410_gpio_getpin( S3C2410_GPF(0) ) )
                        {
                                key_val = 0x01;
                                printk("key_val = 0x%x\n", key_val);
                        }
                        else
                        {
                                key_val = 0x81;
                                printk("key_val = 0x%x\n", key_val);
                        }
                        break;
                        
                case IRQ_EINT2:
                        if( s3c2410_gpio_getpin( S3C2410_GPF(2) ) )
                                key_val = 0x02;
                        else
                                key_val = 0x82;
                        break;
                        
                case IRQ_EINT11:
                    
                        if( s3c2410_gpio_getpin(S3C2410_GPG(3) ) )
                                key_val = 0x03;
                        else
                                key_val = 0x83;
                        break;
                        
                case IRQ_EINT19:                  
                        if( s3c2410_gpio_getpin( S3C2410_GPG(11) ) )
                                key_val = 0x04;
                        else
                                key_val = 0x84;
                        break;  
                            
                default:
                        break;
        }

        flag_press = 1;
        wake_up_interruptible(&button_wait);
        
        
        return IRQ_HANDLED;
}

static int buttons_open(struct inode *inode,struct file *file)
{
        request_irq(IRQ_EINT0, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button0", S3C2410_GPF(0));    /* modify */
        request_irq(IRQ_EINT2, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button2", S3C2410_GPF(2));    /* modify */
        request_irq(IRQ_EINT11, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button11", S3C2410_GPF(11));    /* modify */
        request_irq(IRQ_EINT19, buttons_ISR,  IRQ_TYPE_EDGE_BOTH, "button19", S3C2410_GPF(19));    /* modify */

        init_waitqueue_head(&button_wait);

        return 0;
}

static int buttons_close(struct inode *inode,struct file *file)
{
        free_irq(IRQ_EINT0,S3C2410_GPF(0));    /* modify */
        free_irq(IRQ_EINT2,S3C2410_GPF(2));    /* modify */
        free_irq(IRQ_EINT11,S3C2410_GPF(11));    /* modify */
        free_irq(IRQ_EINT19,S3C2410_GPF(19));    /* modify */
        
        return 0;
}
ssize_t buttons_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
        if(size != sizeof(key_val))
        {
                return -EINVAL;
        }
        
        wait_event_interruptible(button_wait, flag_press);

        copy_to_user(buf, &key_val, 1);

        flag_press = 0;
        
        
        return sizeof(key_val);
}


const struct file_operations buttons_fops = {
        .owner        = THIS_MODULE,
        .open         = buttons_open,
        .read         = buttons_read,
        .release      = buttons_close,
};

static int buttons_init(void)
{
        major = register_chrdev(0,"buttons",&buttons_fops);
        
        buttons_class = class_create(THIS_MODULE,  "my_buttons");
        
        buttons_dev = device_create(buttons_class, NULL, MKDEV(major,0), NULL, "buttons");


/*        gpfcon = (volatile unsigned long *)ioremap(0x56000050 ,8);
        gpfdat = gpfcon + 1;
        
        gpgcon = (volatile unsigned long *)ioremap(0x56000060 ,8);
        gpgdat = gpgcon + 1;
*/        
        printk("buttons driver initing...\n");
        return 0;
}

static void buttons_exit(void)
{
        unregister_chrdev(major,"buttons");    /* modify */
        device_unregister(buttons_dev);
        class_destroy(buttons_class);
//        iounmap(gpfcon);
//        iounmap(gpgcon);
        printk("Bye,buttons.\n");
}

module_init(buttons_init);
module_exit(buttons_exit);
MODULE_LICENSE("GPL");

驱动程序里面红色部分是我修改过的,你可以试试看。。我 也是刚学驱动,不太懂。
级别: 论坛版主
UID: 103400
精华: 0
发帖: 434
金钱: 2235 两
威望: 447 点
综合积分: 868 分
注册时间: 2014-04-24
最后登录: 2016-10-10
3楼  发表于: 2015-03-27 10:31

 回 楼主(a11en) 的帖子

应用程序只调用了读的函数,没有实现你的终端功能呀
过程比结果重要,授之以鱼不如授之以渔。
级别: 新手上路
UID: 111193
精华: 0
发帖: 27
金钱: 135 两
威望: 27 点
综合积分: 54 分
注册时间: 2014-12-17
最后登录: 2018-08-28
4楼  发表于: 2015-03-27 11:28

 回 3楼(嘉jjm) 的帖子

在驱动里面已经注册了按键中断,应用读函数,只要没有按键按下,底层驱动的读函数就一直在睡眠中。只要按键按下,就会调用中断处理函数,里面会给键值赋值,再通过驱动读函数把键值传到应用的读函数,再显示出来。
级别: 新手上路
UID: 113796
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2015-03-26
最后登录: 2015-04-07
5楼  发表于: 2015-04-02 14:34
按照二楼的方法改还是不行啊     大神再给看看啊
级别: 新手上路
UID: 113796
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2015-03-26
最后登录: 2015-04-07
6楼  发表于: 2015-04-03 19:06
帖子又沉了。。。就没有高手照顾下新人吗
级别: 侠客
UID: 113147
精华: 0
发帖: 107
金钱: 535 两
威望: 107 点
综合积分: 214 分
注册时间: 2015-03-05
最后登录: 2015-09-16
7楼  发表于: 2015-04-10 18:40
請問有人試過Tiny4412嗎?