您的位置:首页 > 运维架构 > Linux

Linux中断下半部——工作队列

2018-01-25 17:00 316 查看
1 参考书籍

《Linux设备驱动开发详解》 宋宝华 机械工业出版社

《嵌入式Linux驱动开发教程》 华清远见嵌入式学院 电子工业出版社

2 工作队列

下面的代码用于定义一个工作队列和一个底半部执行函数

struct work_struct my_wq; /*定义一个工作队列*/
void my_wq_func(struct work_struct *work); /*定义一个处理函数*/


通过INIT_WORK()可以初始化这个工作队列并将工作队列与处理函数绑定

INIT_WORK(&my_wq,my_wq_func);


用于调度工作队列执行的函数为schedule_work()

schedule_work(&my_wq);


工作队列使用模版

/*混杂设备驱动模型*/
/*使用工作队列改写按键中断*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>

#define GPNCON 0x7F008830

#define DEVICE_NAME     "my_buttons"
#define MISC_DYNAMIC_MINOR 200 /*次设备号*/

struct work_struct my_wq; /*定义一个工作队列*/
void my_wq_func(struct work_struct *work);

/*中断处理,下半部函数*/
void my_wq_func(struct work_struct *work)
{
...
}

/*中断处理顶半部函数*/
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
...
schedule_work(&my_wq);
...
return IRQ_HANDLED;
}

/*模块加载函数*/
static int __init dev_init()
{
...
...
/*申请中断*/        err=request_irq(IRQ_EINT(0),buttons_interrupt,IRQF_TRIGGER_FALLING,"mykey",0);

/*初始化工作队列*/
INIT_WORK(&my_wq,my_wq_func);
...
return 0;
}

/*设备驱动模块卸载函数*/
static void __exit dev_exit(void)
{
...
/*释放中断*/
...

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: