关于_linux外部中断驱动异步通知应用程序

元气小坏坏 提交于 2019-12-12 05:18:54

1,file 1,-> ent_test.c

/*

  • file ent_test.c
  • Copyright by jiangdou QQ:344283973
  • RK3188 ENT test 20140902
  • how to compile: $ arm-linux-gnueabihf-gcc -o ent_test ENT_test.c -static
    */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int fd;

//信号处理函数
void my_signal_fun(int signum)
{

    printf("by jiangdou............\n");

}

int main(void)
{
unsigned char key_val;
int ret;
int Oflags;
signal(SIGIO, my_signal_fun);
fd = open("/dev/key", O_RDWR);
if (fd < 0)
{
printf(“can’t open!\n”);
}
ret = fcntl(fd, F_SETOWN, getpid());
if(ret < 0)
{
printf(“can’t fcntl_1!\n”);
}

    Oflags = fcntl(fd, F_GETFL); 
    printf("Oflags is : %d\n", Oflags);//is 2
    if(Oflags < 0)
    {
            printf("can't fcntl_2!\n");
    }
    ret = fcntl(fd, F_SETFL, Oflags | FASYNC);
    if(ret < 0)
    {
            printf("can't fcntl_3!\n");
    }
    while (1)
    {
            sleep(1000);
    }
    
    return 0;

}

2,file 2,-> irq_.c

/*

  • file ent_test.c
  • Driver for irq on GPIO lines capable of generating interrupts.
  • Copyright by jiangdou

*/

#include <linux/module.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/adc.h>

#include <asm/gpio.h>
#include <mach/board.h>
#include <plat/key.h>
#include <linux/poll.h>

static struct class *keydrv_class;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static struct fasync_struct *button_async;

//static volatile int ev_press = 0;

#define ENT0 RK30_PIN1_PB7
#define GPIO_IRQ 207

static irqreturn_t irq_isr(int irq, void *dev_id)
{
//printk(“jiangdou…irq_isr…\n”);

    ev_press = 1;

wake_up_interruptible(&button_waitq);  

    
    kill_fasync(&button_async, SIGIO, POLL_IN);
    
    return IRQ_RETVAL(IRQ_HANDLED);
    
    //return 0;

}

static int irq_open(struct inode*inode, struct file *file)
{
int error;

    printk("jiangdou....irq_open...\n");
    if(gpio_request(ENT0,NULL) != 0){
  gpio_free(ENT0);
  printk("gpio_request error\n");
  return -EIO;
}
    error = request_irq(GPIO_IRQ, irq_isr, IRQF_TRIGGER_FALLING, "irq_dou", 1);
    if(error){
            printk("failed to register irq_isr interrupt\n");
    }
    return error;

}

int irq_close(struct inode *inode,struct file *file)
{
free_irq(GPIO_IRQ,1);
return 0;
}

static unsigned irq_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); //

    if (ev_press)
            mask |= POLLIN | POLLRDNORM;

    return mask;

}

static int irq_fasync (int fd, struct file *filp, int on)
{
printk(“driver: fifth_drv_fasync\n”);

    return fasync_helper(fd, filp, on, &button_async);

}

static struct file_operations irq_drv_fops= {

    .owner        =        THIS_MODULE,    
    .open        =        irq_open,             
    .release        =        irq_close,
    //.fasync         =  irq_fasync,
    //.poll    =  irq_poll,

};

int major;

static int irq_init(void)
{
printk(“jiangdou…irq_init…\n”);
major = register_chrdev(0, “irq_drv”, &irq_drv_fops);
keydrv_class = class_create(THIS_MODULE,“irq_drv”);
device_create(keydrv_class,NULL,MKDEV(major,0),NULL,“key”);

    return 0;

}
static void irq_drv_exit(void)

{
unregister_chrdev(major, “irq_drv”);
device_destroy(keydrv_class,MKDEV(major,0));
class_destroy(keydrv_class);

}

module_init(irq_init);

module_exit(irq_drv_exit);

MODULE_LICENSE(“GPL”);

3,

4,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!