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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27312943/article/details/79164115

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)
{
    ...
    /*释放中断*/
    ...

}


猜你喜欢

转载自blog.csdn.net/qq_27312943/article/details/79164115