Linux内核中断底半部处理--工作队列
2017-06-01 16:05
471 查看
工作队列的使用过程:
工作队列相关函数介绍:
#include <workqueue.h> /*头文件包含*/1.工作队列的创建及销毁:
定义一个工作队列结构体指针
static struct workqueue_struct *key_workqueue;
创建工作队列
struct workqueue_struct *create_workqueue(char *);
参数:工作队列的名称(字符串)
返回值:创建好的工作队列
销毁工作队列,参数为待销毁的工作队列
void destroy_workqueue(struct workqueue_struct *);
2.工作的创建、初始化
创建一个不带延时的工作(加入队列立即执行)
struct work_struct work;
工作初始化宏
INIT_WORK(&work, func);
参数1:用户已定义的work_struct变量(工作)
参数2:任务处理函数,用户实现(中断底半部)
3.添加工作到工作队列
int queue_work(struct workqueue_struct*wq,struct work_struct *work);
参数1:第1部创建的工作队列
参数2:第2部创建的工作
4.延迟工作的创建、初始化及添加:
定义一个带延时的工作
struct delayed_work dwork;
初始化带延迟的工作
INIT_DELAYED_WORK(&dwork, func);
添加带延迟的工作到工作队列
int queue_delayed_work(struct workqueue_struct *wq,
struct delayed_work *dwork, unsigned long delay);
参数delay:延迟时间(时钟滴答数)
【注】对于我们的开发板,一个时钟滴答=5ms
终止队列中的工作(即使处理程序已经在处理该任务)
int cancel_work_sync(struct work_struct *work);
int cancel_delayed_work_sync(struct delayed_work *dwork);
判断任务项目是否在进行中
int work_pending(struct work_struct work );
int delayed_work_pending(structdelayed_work work );
/*返回值为真表示正在运行,假表示停止*/
总结:
实例代码:
驱动端:
#include <linux/device.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/irq.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <mach/gpio.h> #include <mach/regs-gpio.h> /*S5PV210_GPH3_BASE*/ #include <linux/workqueue.h> #define EINT_DEVICE_ID 1 #define DRIVER_NAME "key1_eint" #define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME) #define __debug(fmt, arg...) printk(KERN_DEBUG fmt, ##arg) #define GPH0_CONREG (S5PV210_GPH0_BASE + 0x00) #define GPH0_DATREG (S5PV210_GPH0_BASE + 0x04) #define GPH0_UPREG (S5PV210_GPH0_BASE + 0x08) #define GPH0_DRVREG (S5PV210_GPH0_BASE + 0x0c) #define GPH3CON (unsigned long)(S5PV210_GPH3_BASE+ 0x00) #define GPH3DAT (unsigned long)(S5PV210_GPH3_BASE + 0x04) #define GPH2UP (unsigned long)(S5PV210_GPH2_BASE + 0x08) static int major = 0; /* Driver Major Number */ static int minor = 0; /* Driver Minor Number */ struct class *key_class; static struct device *key_device; static unsigned char key; /* 定义工作队列指针变量,并定义工作变量。 * 在此例中我们采用自定义的方式(我们还可以 * 使用内核提供的工作队列,请参考ppt) */ static struct workqueue_struct *key_workqueue; //static struct work_struct key_work; static struct delayed_work key_work; static struct work_struct key_work1; /* 工作处理函数work_handler * 该函数完成点亮/熄灭led灯的功能 */ static void work_handler(struct work_struct *work) { unsigned long reg_data; reg_data = readl(GPH0_DATREG); reg_data ^= 0x01<<3; writel(reg_data, GPH0_DATREG); } static void work_handler1(struct work_struct *work) { printk("haha you done\n"); } irqreturn_t buttons_interrupt(int irq, void *dev_id) { key = (unsigned int)dev_id; //__debug("in eint function...\n"); /*将work添加到工作队列workqueue中*/ //queue_work(key_workqueue, &key_work); queue_delayed_work(key_workqueue,&key_work,1000); queue_work(key_workqueue, &key_work1); return IRQ_HANDLED; } /* the initialization of IO ports * @prepare for key device and led device */ static void led_io_port_init(void) { unsigned long con_val, up_val; con_val = readl(GPH0_CONREG); con_val &= ~(0x0f<<12); con_val |= (0x01<<12); writel(con_val, GPH0_CONREG); up_val = readl(GPH0_UPREG); up_val &= ~(0X03<<6); up_val |= (0X02<<6); writel(up_val, GPH0_UPREG); } static void key_io_port_init(void) { unsigned long reg_val; reg_val = readl(GPH3CON); reg_val &= ~((0x0f<<0) | (0x0f<<4)); reg_val |= ((0x01<<0) | (0x01<<4)); writel(reg_val, GPH3CON); reg_val = readl(GPH3DAT); reg_val &= ~((0x01<<0) | (0x01<<1)); writel(reg_val, GPH3DAT); reg_val = readl(GPH2UP); reg_val &= ~(0x03<<8); reg_val |= 0x02<<8; writel(reg_val, GPH2UP); } static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { int key_num; int cpy_len; int retval; key_num = key; //读取键值 key = 0; //清除键值 cpy_len = min(sizeof(key_num), count); retval = copy_to_user(buf, &key_num, cpy_len); return (cpy_len - retval); } static struct file_operations key_fops = { .owner = THIS_MODULE, .read = key_read, }; static int __init key_eint_init(void) { int retval; key_io_port_init(); led_io_port_init(); /* 创建工作队列--workqueue*/ key_workqueue = create_workqueue("key_workqueue"); if(IS_ERR(key_workqueue)){ err("ceate workqueue failed!"); return PTR_ERR(key_workqueue); } /*初始化工作--work, 指定处理函数work_handler*/ //INIT_WORK(&key_work, work_handler); INIT_DELAYED_WORK(&key_work,work_handler); INIT_WORK(&key_work1, work_handler1); retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING); if(retval){ err("IRQ_EINT20 set irq type failed"); goto error; } retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_DISABLED, "KEY1", (void *)EINT_DEVICE_ID); if(retval){ err("request eint20 failed"); goto error; } /* Driver register */ major = register_chrdev(major, DRIVER_NAME, &key_fops); if(major < 0){ err("register char device fail"); retval = major; goto error_register; } key_class=class_create(THIS_MODULE,DRIVER_NAME); if(IS_ERR(key_class)){ err("class create failed!"); retval = PTR_ERR(key_class); goto error_class; } key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME); if(IS_ERR(key_device)){ err("device create failed!"); retval = PTR_ERR(key_device); goto error_device; } __debug("register myDriver OK! Major = %d\n", major); return 0; error_device: class_destroy(key_class); error_class: unregister_chrdev(major, DRIVER_NAME); error_register: free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID); error: return retval; } static void __exit key_eint_exit(void) { /*清空工作队列*/ flush_workqueue(key_workqueue); /*释放创建的工作队列,资源回收*/ destroy_workqueue(key_workqueue); free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID); unregister_chrdev(major, DRIVER_NAME); device_destroy(key_class,MKDEV(major, minor)); class_destroy(key_class); return; } module_init(key_eint_init); module_exit(key_eint_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Eric");
应用层:
#include <stdio.h>#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char *devname = "/dev/key1_eint";
int fd;
unsigned char key;
fd = open(devname, O_RDWR);
while(1)
{
read(fd, &key, sizeof(key));
if(key)
printf("the key = %d\n",key);
usleep(100*1000);
}
close(fd);
}
相关文章推荐
- Linux2.6内核--中断下半部实现方法 工作队列
- Linux2.6内核--中断下半部实现方法 工作队列
- 《Linux设备驱动开发详解》-- Linux中断处理底半部机制(tasklet、工作队列和软中断)
- Linux中断下半部——工作队列
- linux驱动开发之输入子系统编程(一)使用工作队列实现中断下半部
- 【Linux开发】linux设备驱动归纳总结(六):3.中断的上半部和下半部——工作队列
- linux中断处理下文: 工作队列分析
- Linux 驱动之中断下半部之工作队列
- 把握linux内核设计思想(五):下半部机制之工作队列及几种机制的选择
- 把握linux内核设计思想(五):下半部机制之工作队列及几种机制的选择
- 中断的下半部处理机制(2)工作队列
- 13 linux内核里实现中断底半部处理的新方法
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】中断服务下半部之工作队列详解
- linux 触摸屏驱动中断下半部实现-工作队列
- 关于linux内核中断处理和内核抢占的两个说明
- linux0.11内核中断处理
- linux内核里的工作队列及内核通知链
- Linux下半部处理之软中断
- 中断服务下半部之工作队列【转】
- Linux 内核中工作队列的操作