Raspberry Linux GPIO interrupt

2021-09-12  本文已影响0人  star_walker

1. Learn before read

https://blog.csdn.net/droidphone/article/details/7445825
http://blog.chinaunix.net/uid-17188120-id-4187440.html

2. The main functions need to use

// include/linux/gpio.h
static inline int gpio_to_irq(unsigned int gpio)

// include/linux/interrupt.h
/*
 * These correspond to the IORESOURCE_IRQ_* defines in
 * linux/ioport.h to select the interrupt line behaviour.  When
 * requesting an interrupt without specifying a IRQF_TRIGGER, the
 * setting should be assumed to be "as already configured", which
 * may be as per machine or firmware initialisation.
 */
#define IRQF_TRIGGER_NONE   0x00000000
#define IRQF_TRIGGER_RISING 0x00000001
#define IRQF_TRIGGER_FALLING    0x00000002
#define IRQF_TRIGGER_HIGH   0x00000004
#define IRQF_TRIGGER_LOW    0x00000008
#define IRQF_TRIGGER_MASK   (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
#define IRQF_TRIGGER_PROBE  0x00000010

static inline int __must_check request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, const char *name, void *dev)
extern const void *free_irq(unsigned int, void *);
extern void disable_irq(unsigned int irq);
extern void enable_irq(unsigned int irq);

3. Sample code to show it works

#include <linux/init.h>             
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/sysfs.h>
#include <linux/interrupt.h>

MODULE_LICENSE("GPL");

#define LIGHTSENSORGPIO 23
int lightsensor_irq;

static irqreturn_t lightsensor_irq_handler(int irq, void *dev_id)
{
    printk(KERN_INFO "lightsensor irq, got it\n");
    return IRQ_HANDLED;
}

static int __init hello_world_init(void)
{
    int ret;

    printk(KERN_ERR "hello world!!!\n");

    ret = gpio_request(LIGHTSENSORGPIO, "LightSensorIrq");
    if (ret < 0) {
        printk(KERN_ERR "hello: request gpio failed\n");
        goto error;
    }

    ret = gpio_direction_input(LIGHTSENSORGPIO);
    if (ret < 0) {
        printk(KERN_ERR "hello: set gpio direction failed\n");
        goto error;
    }

    lightsensor_irq = gpio_to_irq(LIGHTSENSORGPIO);
    ret = request_irq(lightsensor_irq, lightsensor_irq_handler, IRQF_TRIGGER_RISING, "LightSensorIrq", NULL);
    if (ret < 0) {
        printk(KERN_ERR "hello: request irq failed\n");
        goto error;
    }

    return 0;
error:
    gpio_free(LIGHTSENSORGPIO);
    return -1;
}

static void __exit hello_world_exit(void)
{
    free_irq(lightsensor_irq, NULL);
    gpio_free(LIGHTSENSORGPIO);
    printk(KERN_ALERT "hell_exit\r\n");
}

module_init(hello_world_init);
module_exit(hello_world_exit);

4. Result after execute

root@l:/home/l# [  353.343011] lightsensor irq, got it
[  353.712772] lightsensor irq, got it
[  353.953947] lightsensor irq, got it
[  354.274983] lightsensor irq, got it
[  354.497476] lightsensor irq, got it
[  355.094600] lightsensor irq, got it

root@l:/home/l# 
root@l:/home/l# 
root@l:/home/l# cat /proc/interrupts 
           CPU0       CPU1       CPU2       CPU3       
  9:          0          0          0          0     GICv2  25 Level     vgic
 12:      26787      33227      33012      27719     GICv2  30 Level     arch_timer
 13:          0          0          0          0     GICv2  27 Level     kvm guest vtimer
 19:          0          0          0          0     GICv2 107 Level     fe004000.txp
 20:       5126          0          0          0     GICv2  65 Level     fe00b880.mailbox
 23:       2098          0          0          0     GICv2 153 Level     uart-pl011
 24:          0          0          0          0     GICv2 150 Level     fe204000.spi
 25:          0          0          0          0     GICv2 129 Level     vc4 hvs
 26:          0          0          0          0     GICv2 149 Level     fe804000.i2c
 27:          2          0          0          0     GICv2 105 Level     fe980000.usb, fe980000.usb, dwc2_hsotg:usb3
 31:          0          0          0          0     GICv2 114 Level     DMA IRQ
 33:          0          0          0          0     GICv2 116 Level     DMA IRQ
 34:        502          0          0          0     GICv2 117 Level     DMA IRQ
 35:          7          0          0          0     GICv2 118 Level     DMA IRQ
 36:          0          0          0          0     GICv2 119 Level     DMA IRQ
 38:          0          0          0          0     GICv2 141 Level     vc4 crtc
 39:          0          0          0          0     GICv2 142 Level     vc4 crtc, vc4 crtc
 40:       3101          0          0          0     GICv2 133 Level     vc4 crtc
 41:          0          0          0          0     GICv2 138 Level     vc4 crtc
 42:          1          0          0          0  interrupt-controller@7ef00100   0 Edge      vc4 hdmi cec tx
 43:          0          0          0          0  interrupt-controller@7ef00100   1 Edge      vc4 hdmi cec rx
 48:          0          0          0          0  interrupt-controller@7ef00100   8 Edge      vc4 hdmi cec tx
 49:          0          0          0          0  interrupt-controller@7ef00100   7 Edge      vc4 hdmi cec rx
 54:       8644          0          0          0     GICv2  66 Level     VCHIQ doorbell
 55:      46652          0          0          0     GICv2 158 Level     mmc1, mmc0
 56:          0          0          0          0     GICv2  48 Level     arm-pmu
 57:          0          0          0          0     GICv2  49 Level     arm-pmu
 58:          0          0          0          0     GICv2  50 Level     arm-pmu
 59:          0          0          0          0     GICv2  51 Level     arm-pmu
 61:        123       1595          0          0     GICv2 189 Level     eth0
 62:          0          0          0          0     GICv2 190 Level     eth0
 68:       5436          0          0          0     GICv2 106 Level     v3d
 69:          0          0          0          0     GICv2 175 Level     PCIe PME, aerdrv
 70:       4094          0          0          0  BRCM STB PCIe MSI 524288 Edge      xhci_hcd
 71:         35          0          0          0  pinctrl-bcm2835  23 Edge      LightSensorIrq
IPI0:     18993      36960      37471      28383       Rescheduling interrupts
IPI1:      4076       3668       3282       2370       Function call interrupts
IPI2:         0          0          0          0       CPU stop interrupts
IPI3:         0          0          0          0       CPU stop (for crash dump) interrupts
IPI4:         0          0          0          0       Timer broadcast interrupts
IPI5:       912        434        638        402       IRQ work interrupts
IPI6:         0          0          0          0       CPU wake-up interrupts
Err:          0
root@l:/home/l# 

上一篇下一篇

猜你喜欢

热点阅读