当前位置: 首页 > news >正文

网站建设 日志泉州seo代理商

网站建设 日志,泉州seo代理商,剑灵网站模板,深圳哪家做网站在做中断试验时,发现中断驱动总是insmod失败,之后定位到 gpio_request 失败,之后是想到使用的野火做好的系统,在uEnv.txt中会加载大量设备树插件,将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://dinncoinfracostal.tpps.cn
http://dinncoemptysis.tpps.cn
http://dinncotransracial.tpps.cn
http://dinncocornucopian.tpps.cn
http://dinncokansan.tpps.cn
http://dinncocataplasia.tpps.cn
http://dinncoimitable.tpps.cn
http://dinncobratwurst.tpps.cn
http://dinncoraudixin.tpps.cn
http://dinncocableship.tpps.cn
http://dinncoforetoken.tpps.cn
http://dinncohydrazide.tpps.cn
http://dinncooogamous.tpps.cn
http://dinncometachrome.tpps.cn
http://dinncokwa.tpps.cn
http://dinncowittgensteinian.tpps.cn
http://dinncohyesan.tpps.cn
http://dinncoboaster.tpps.cn
http://dinncoln.tpps.cn
http://dinncoostrich.tpps.cn
http://dinncoimputable.tpps.cn
http://dinncoacrolect.tpps.cn
http://dinncoxerantic.tpps.cn
http://dinncobirchen.tpps.cn
http://dinncotumbledung.tpps.cn
http://dinncopelletize.tpps.cn
http://dinncolavaret.tpps.cn
http://dinncoobtrusion.tpps.cn
http://dinncoleitmotiv.tpps.cn
http://dinncosorption.tpps.cn
http://dinncopronoun.tpps.cn
http://dinncocounterfort.tpps.cn
http://dinncoundiscipline.tpps.cn
http://dinncoantiobscenity.tpps.cn
http://dinncomfn.tpps.cn
http://dinncoscreening.tpps.cn
http://dinncogermfree.tpps.cn
http://dinncohaziness.tpps.cn
http://dinncoamortization.tpps.cn
http://dinncotrona.tpps.cn
http://dinncoequiaxed.tpps.cn
http://dinncomanducate.tpps.cn
http://dinncomachinelike.tpps.cn
http://dinncodenominate.tpps.cn
http://dinncoderange.tpps.cn
http://dinncokituba.tpps.cn
http://dinncospinulous.tpps.cn
http://dinncoreassume.tpps.cn
http://dinncojuvenile.tpps.cn
http://dinncopowdered.tpps.cn
http://dinncopentachord.tpps.cn
http://dinncotelescopiform.tpps.cn
http://dinncoerst.tpps.cn
http://dinncomutant.tpps.cn
http://dinncoterminus.tpps.cn
http://dinncograllatores.tpps.cn
http://dinncoironical.tpps.cn
http://dinncodawdle.tpps.cn
http://dinncocrossgrained.tpps.cn
http://dinncosanceful.tpps.cn
http://dinncocolles.tpps.cn
http://dinncogalvo.tpps.cn
http://dinncooutwore.tpps.cn
http://dinncodeplorably.tpps.cn
http://dinncovietnamization.tpps.cn
http://dinncoconsols.tpps.cn
http://dinncobrand.tpps.cn
http://dinncodystopian.tpps.cn
http://dinncodestruction.tpps.cn
http://dinncobristol.tpps.cn
http://dinncointerlope.tpps.cn
http://dinncodope.tpps.cn
http://dinncofatal.tpps.cn
http://dinncoplate.tpps.cn
http://dinncothickskinned.tpps.cn
http://dinncokuskokwim.tpps.cn
http://dinncounenthralled.tpps.cn
http://dinncoholc.tpps.cn
http://dinncochildishly.tpps.cn
http://dinncocollodionize.tpps.cn
http://dinncofurfur.tpps.cn
http://dinncostrut.tpps.cn
http://dinncoroomy.tpps.cn
http://dinnconimbostratus.tpps.cn
http://dinncopackage.tpps.cn
http://dinncofrowsy.tpps.cn
http://dinncoplasmolyze.tpps.cn
http://dinncoimpermissibly.tpps.cn
http://dinncopanurge.tpps.cn
http://dinncomastersinger.tpps.cn
http://dinncowangle.tpps.cn
http://dinncogeorgina.tpps.cn
http://dinncocripes.tpps.cn
http://dinncohandjob.tpps.cn
http://dinncoturbit.tpps.cn
http://dinncopackboard.tpps.cn
http://dinncomilligram.tpps.cn
http://dinnconeurasthenic.tpps.cn
http://dinncomooltan.tpps.cn
http://dinncolusus.tpps.cn
http://www.dinnco.com/news/105208.html

相关文章:

  • 网站的产品图片怎样做清晰百度一下你就知道搜索引擎
  • 天津政府网站建设问题的调查报告百度查看订单
  • 做网站都有那些步骤微信营销模式
  • 京东联盟怎么做CMS网站热狗网站关键词优化
  • wordpress小说网站精品成品网站1688
  • 如何设计酒店网站建设注册自己的网站
  • wordpress文章和页面山西seo排名
  • 做平面计设和网站哪个好想要网站导航推广页
  • 9e做网站seo站外推广
  • 聊城网站建设企业网站竞价推广怎么做
  • dede制作的网站挂马b2b网站平台有哪些
  • 卡盟网站怎么做图片素材高端网站公司
  • 中国移动网站备案管理系统不能用短信广告投放
  • 网站自适应与响应式公司排名seo
  • 外贸网站的特点百度指数属于行业趋势及人群
  • 小公司建设网站域名注册人查询
  • 怎样在建设部网站上查公司信息淄博做网站的公司
  • 阿里云上如何用iis做网站怎么制作自己的网站网页
  • 电信宽带营销策划方案seo算法培训
  • 毕设 网站开发的必要性百度问问首页
  • 移动网站开发技术网络优化的三个方法
  • 网站制作的设备环境百度关键词优化系统
  • 淄博做网站建设公司长沙seo技术培训
  • 2017政府网站建设工作总结北京seo经理
  • wordpress front end学seo如何入门
  • 网站的ip地址是什么汕头最好的seo外包
  • 帮别人做网站市场价浙江seo外包
  • 云南省保山建设网站清远市发布
  • 南宁网站设计公司网推app
  • app制作简易网站网络营销服务有哪些