Linux内核中断底半部处理--工作队列

工作队列的使用过程:

Linux内核中断底半部处理--工作队列_第1张图片


工作队列相关函数介绍:

#include /*头文件包含*/

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 );

/*返回值为真表示正在运行,假表示停止*/

总结:

Linux内核中断底半部处理--工作队列_第2张图片Linux内核中断底半部处理--工作队列_第3张图片

实例代码:

驱动端:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include   		/*S5PV210_GPH3_BASE*/

#include 

#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 
#include 
#include 
#include 

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);
}


你可能感兴趣的:(Linux,驱动)