zoukankan      html  css  js  c++  java
  • Linux 简单按键中断处理流程

    中断处理程序中不能延时、休眠之类的,一定要最快速、高效的执行完。


    // 功能:申请中断

    // 参数1:中断号码,通过宏 IRA_EINT(x) 获取

    // 参数2:中断的处理函数,填函数名

    // 参数3:中断的出发方式(下面的宏在不同内核中是不同的) 

    #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

    // 参数4:描述语句,给此中断起名,自定义

    // 参数5:传递给中断处理函数的参数,没有填NULL


    int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, const char *name, void *dev_id);

    // 功能:释放中断

    // 参数1:中断号,与 request_irq 函数的第一个参数一致

    // 参数2:一定要与 request_irq 中最后一个参数一致

    void free_irq(unsigned int irq, void *dev_id);


    用法:

    // 中断处理程序,多个按键可以根据 irqno 区分
    irqreturn_t key_irq_handler(int irqno, void *dev_id)
    {
    	int ret;
    
    	printk("------------%s-------------
    ", __FUNCTION__);
    	
    	ret = gpio_get_value(key_info[i].key_gpio);
    	ret ? (key_dev->key.value = 0) : (key_dev->key.value = 1);
    	printk("key = %d status = %d
    ", key_dev->key.name, key_dev->key.value);
    
    	// 返回值一定要是 IRQ_HANDLED
    	return IRQ_HANDLED;
    }
    
    static int __init key_drv_init(void)
    {
    	... ...
    	key_dev->irqno = IRQ_EINT(2);
    	ret = request_irq(key_dev->irqno, key_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "key_eint", NULL);
    	... ...
    }
    
    static int __exit key_drv_exit(void)
    {
    	... ...
    	free_irq(key_dev->irqno,NULL);
    	... ...
    }
  • 相关阅读:
    从零构建自己的远控•界面搭建(2)
    从零构建自己的远控•项目简介(1)
    特训99小游戏
    WritePrivateProfileSection 示例
    dll劫持
    c/c++创建动态库
    不传请求号修改测试系统或者正式系统代码
    ABAP 业务处理中的弹窗提示
    SAP MM 采购订单中货币单位怎么修改
    ABAP 屏幕字段无搜索帮助处理
  • 原文地址:https://www.cnblogs.com/lialong1st/p/7756673.html
Copyright © 2011-2022 走看看