天天看點

工作隊列的使用過程:

工作隊列的使用過程:

工作隊列相關函數介紹:

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

————————————————

繼續閱讀