文字堆积网站,一个企业网站ppt怎么做,网站备案有用,制作微信公众号的网站在做中断试验时#xff0c;发现中断驱动总是insmod失败#xff0c;之后定位到 gpio_request 失败#xff0c;之后是想到使用的野火做好的系统#xff0c;在uEnv.txt中会加载大量设备树插件#xff0c;将key相关的设备树插件屏蔽即可。
linux中断API函数
中断号
每个中断…在做中断试验时发现中断驱动总是insmod失败之后定位到 gpio_request 失败之后是想到使用的野火做好的系统在uEnv.txt中会加载大量设备树插件将key相关的设备树插件屏蔽即可。
linux中断API函数
中断号
每个中断都有一个中断号通过中断号即可区分不同的中断在 Linux 内核中使用一个 int 变量表示中断号
request_irq函数
在 Linux 内核中要想使用某个中断是需要申请的 request_irq 函数用于申请中断 request_irq函数可能会导致睡眠因此不能在中断上下文或者其他禁止睡眠的代码段中使用 request_irq 函数。 request_irq 函数会激活(使能)中断所以不需要我们手动去使能中断 request_irq 函数原型如下
int request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char *name,void *dev)irq要申请中断的中断号。 handler中断处理函数当中断发生以后就会执行此中断处理函数。 flags中断标志可以在文件 include/linux/interrupt.h 里面查看所有的中断标志这些标志可以通过“|”来实现多种组合。 name中断名字设置以后可以在/proc/interrupts 文件中看到对应的中断名字。 dev 如果将 flags 设置为 IRQF_SHARED 的话 dev 用来区分不同的中断一般情况下将dev 设置为设备结构体 dev 会传递给中断处理函数 irq_handler_t 的第二个参数。
free_irq函数
使用中断的时候需要通过 request_irq 函数申请使用完成以后就要通过 free_irq 函数释放掉相应的中断。
void free_irq(unsigned int irq,void *dev)irq 要释放的中断。 dev如果中断设置为共享(IRQF_SHARED)的话此参数用来区分具体的中断。共享中断只有在释放最后中断处理函数的时候才会被禁止掉。
中断处理函数
使用 request_irq 函数申请中断的时候需要设置中断处理函数中断处理函数格式如下所示
irqreturn_t (*irq_handler_t) (int, void *)第一个参数是要中断处理函数要相应的中断号。第二个参数是一个指向 void 的指针也就是个通用指针需要与 request_irq 函数的 dev 参数保持一致。用于区分共享中断的不同设备dev 也可以指向设备数据结构。中断处理函数的返回值为 irqreturn_t 类型 irqreturn_t 类型定义 如下所示 enum irqreturn {IRQ_NONE (0 0),IRQ_HANDLED (1 0),IRQ_WAKE_THREAD (1 1),};typedef enum irqreturn irqreturn_t;可以看出 irqreturn_t 是个枚举类型一共有三种返回值。一般中断服务函数返回值使用如下形式
return IRQ_RETVAL(IRQ_HANDLED)中断使能和禁止函数
void enable_irq(unsigned int irq)
void disable_irq(unsigned int irq)disable_irq函数要等到当前正在执行的中断处理函数执行完才返回因此使用者需要保证不会产生新的中断并且确保所有已经开始执行的中断处理程序已经全部退出。在这种情况下可以使用另外一个中断禁止函数
void disable_irq_nosync(unsigned int irq)关闭全局中断可以使用如下函数
local_irq_save(flags) //禁止中断
local_irq_restore(flags) //恢复中断上半部与下半部
上半部上半部就是中断处理函数那些处理过程比较快不会占用很长时间的处理就可以放在上半部完成 下半部如果中断处理过程比较耗时那么就将这些比较耗时的代码提出来交给下半部去执行这样中断处理函数就会快进快出。
tasklet
tasklet 是利用软中断来实现的另外一种下半部机制在软中断和 tasklet 之间建议大家使用 tasklet。 Linux 内核使用 tasklet_struct 结构体来表示 tasklet
struct tasklet_struct
{
struct tasklet_struct *next; /* 下一个 tasklet */
unsigned long state; /* tasklet 状态 */
atomic_t count; /* 计数器记录对 tasklet 的引用数 */
void (*func)(unsigned long); /* tasklet 执行的函数 */
unsigned long data; /* 函数 func 的参数 */
};void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long),unsigned long data);t要初始化的 tasklet func tasklet 的处理函数。 data 要传递给 func 函数的参数 也 可 以 使 用 宏 DECLARE_TASKLET 来 一 次 性 完 成 tasklet 的 定 义 和 初 始 化 DECLARE_TASKLET 定义在 include/linux/interrupt.h 文件中定义如下:
DECLARE_TASKLET(name, func, data)其中 name 为要定义的 tasklet 名字这个名字就是一个 tasklet_struct 类型的时候变量 func就是 tasklet 的处理函数 data 是传递给 func 函数的参数。 在上半部也就是中断处理函数中调用 tasklet_schedule 函数就能使 tasklet 在合适的时间运行 tasklet_schedule 函数原型如下
void tasklet_schedule(struct tasklet_struct *t)t要调度的 tasklet也就是 DECLARE_TASKLET 宏里面的 name。 关于 tasklet 的参考使用示例如下所示
/* 定义 taselet */
struct tasklet_struct testtasklet;
/* tasklet 处理函数 */
void testtasklet_func(unsigned long data)
{
/* tasklet 具体处理内容 */
}
/* 中断处理函数 */
irqreturn_t test_handler(int irq, void *dev_id)
{
......
/* 调度 tasklet */
tasklet_schedule(testtasklet);
......
}
/* 驱动入口函数 */
static int __init xxxx_init(void)
{
......
/* 初始化 tasklet */
tasklet_init(testtasklet, testtasklet_func, data);
/* 注册中断处理函数 */
request_irq(xxx_irq, test_handler, 0, xxx, xxx_dev);
......
}工作队列
工作队列是另外一种下半部执行方式工作队列在进程上下文执行工作队列将要推后的工作交给一个内核线程去执行因为工作队列工作在进程上下文因此工作队列允许睡眠或重新调度。因此如果你要推后的工作可以睡眠那么就可以选择工作队列否则的话就只能选择软中断或 tasklet。 Linux 内核使用 work_struct 结构体表示一个工作内容如下(省略掉条件编译)
struct work_struct {
atomic_long_t data;
struct list_head entry;
work_func_t func; /* 工作队列处理函数 */
};简单创建工作很简单直接定义一个 work_struct 结构体 变量即可然后使用 INIT_WORK 宏来初始化工作 INIT_WORK 宏定义如下
#define INIT_WORK(_work, _func)_work 表示要初始化的工作 _func 是工作对应的处理函数。 也可以使用 DECLARE_WORK 宏一次性完成工作的创建和初始化宏定义如下
#define DECLARE_WORK(n, f)n 表示定义的工作(work_struct) f 表示工作对应的处理函数。 和 tasklet 一样工作也是需要调度才能运行的工作的调度函数为 schedule_work函数原型如下所示
bool schedule_work(struct work_struct *work)函数参数和返回值含义如下 work 要调度的工作。 返回值 0 成功其他值 失败。
/* 定义工作(work) */
struct work_struct testwork;
/* work 处理函数 */
void testwork_func_t(struct work_struct *work);
{
/* work 具体处理内容 */
}
/* 中断处理函数 */
irqreturn_t test_handler(int irq, void *dev_id)
{
......
/* 调度 work */
schedule_work(testwork);
......
}
/* 驱动入口函数 */
static int __init xxxx_init(void)
{
......
/* 初始化 work */
INIT_WORK(testwork, testwork_func_t);
/* 注册中断处理函数 */
request_irq(xxx_irq, test_handler, 0, xxx, xxx_dev);
......
}设备树修改
设备树修改如下
key{#address-cells 1;#size-cells 1;compatible fire,key;pinctrl-names default;pinctrl-0 pinctrl_key;key_gpio gpio5 1 GPIO_ACTIVE_LOW;interrupt-parent gpio5;interrupts 1 IRQ_TYPE_EDGE_FALLING; //1表示使用gpio5_io01status okay;};IRQ_TYPE_EDGE_FALLING 定义在文件 include/linux/irq.h 中定义如下
enum {
IRQ_TYPE_NONE 0x00000000,
IRQ_TYPE_EDGE_RISING 0x00000001,
IRQ_TYPE_EDGE_FALLING 0x00000002,
IRQ_TYPE_EDGE_BOTH (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING),
IRQ_TYPE_LEVEL_HIGH 0x00000004,
IRQ_TYPE_LEVEL_LOW 0x00000008,
IRQ_TYPE_LEVEL_MASK (IRQ_TYPE_LEVEL_LOW |
IRQ_TYPE_LEVEL_HIGH),
......
}获取中断号
编写驱动的时候需要用到中断号我们用到中断号中断信息已经写到了设备树里面因此可以通过 irq_of_parse_and_map 函数从 interupts 属性中提取到对应的设备号函数原型如下 unsigned int irq_of_parse_and_map(struct device_node *dev, int index) 函数参数和返回值含义如下 dev 设备节点。 index索引号 interrupts 属性可能包含多条中断信息通过 index 指定要获取的信息。 返回值中断号。 如果使用 GPIO 的话可以使用 gpio_to_irq 函数来获取 gpio 对应的中断号函数原型如 下
int gpio_to_irq(unsigned int gpio)函数参数和返回值含义如下 gpio 要获取的 GPIO 编号。 返回值 GPIO 对应的中断号。 文章转载自: http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.swzpx.cn.gov.cn.swzpx.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn