挖坑网/填坑网 DebugDump Forum

别人挖坑默默填上,自己挖坑含泪填上。天下没有填不了的坑,只有不会填坑的人。来吧,加入我们,挖坑行,填坑你更行! 站长QQ: 516333132 点击这里给我发消息

您尚未登录。

#1 2018-06-05 15:55:10

晕哥
Administrator
注册时间: 1970-01-01
累计发帖: 2,505

写一个简单的V3s@linux4.13-y 中断驱动

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/input.h>
#include <linux/input-polldev.h>
#include <linux/input/touchscreen.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>

#define GPIO 192    //PG0
#define GPIO_INT_NAME  "pg0_int"

#define GPIO_HIGH gpio_get_value(GPIO)
#define GPIO_LOW (gpio_get_value(GPIO) == 0)
short int irq_any_gpio    = 0;
int count =0;

enum { falling, rising } type; 
static irqreturn_t r_irq_handler(int irq, void *dev_id)
 {
    count++;
    printk(KERN_DEBUG "interrupt received (irq: %d)\n", irq);
	if (irq == gpio_to_irq(GPIO)) 
    {

        type = GPIO_LOW ? falling : rising;

        if(type == falling)
        {
            printk("gpio pin is low\n");    
        }
        else
            printk("gpio pin is high\n");

    }

    return IRQ_HANDLED;
}

void r_int_config(void) {

   if (gpio_request(GPIO, GPIO_INT_NAME )) 
   {
      printk("GPIO request failure: %s\n", GPIO_INT_NAME );
      return;
   }

   if ( (irq_any_gpio = gpio_to_irq(GPIO)) < 0 ) {
      printk("GPIO to IRQ mapping failure %s\n",GPIO_INT_NAME );
      return;
   }

   printk(KERN_NOTICE "Mapped int %d\n", irq_any_gpio);

   if (request_irq(irq_any_gpio,(irq_handler_t ) r_irq_handler, IRQF_TRIGGER_FALLING , GPIO_INT_NAME, NULL)) 
   {
      printk("Irq Request failure\n");
      return;
   }

   return;
}

void r_int_release(void) {

   free_irq(gpio_to_irq(GPIO), NULL);
    gpio_free(GPIO);;
   return;
}

int init_module(void)
{
        printk("<1>Hello World\n"); 
    r_int_config();
        return 0;
}

module_init(init_module);    // Do some better naming
module_exit(r_int_release);

# dmesg
[   26.204637] interrupt received (irq: 71)
[   26.204660] gpio pin is low
[   26.919621] interrupt received (irq: 71)
[   26.919642] gpio pin is low
[   27.429350] interrupt received (irq: 71)
[   27.429359] gpio pin is low
[   27.694572] interrupt received (irq: 71)
[   27.694586] gpio pin is low
[   27.906767] interrupt received (irq: 71)
[   27.906779] gpio pin is low

把PG0 IO脚下拉就会进入中断, 输出上面的信息.


参考链接: https://stackoverflow.com/questions/25507750/detecting-interrupt-on-gpio-in-kernel-module

在线

#2 2018-06-05 16:05:51

晕哥
Administrator
注册时间: 1970-01-01
累计发帖: 2,505

Re: 写一个简单的V3s@linux4.13-y 中断驱动

QQ20180605160512.png

在线

#3 2018-06-14 14:44:25

beyondabcd
会员
注册时间: 2018-06-12
累计发帖: 32

Re: 写一个简单的V3s@linux4.13-y 中断驱动

现在成功了吗

离线

#4 2018-06-14 14:47:26

晕哥
Administrator
注册时间: 1970-01-01
累计发帖: 2,505

Re: 写一个简单的V3s@linux4.13-y 中断驱动

可以的,亲自试过。

在线

页脚