12.按键驱动程序设计(4)-中断分层

来源:互联网 发布:excel恢复之前数据 编辑:程序博客网 时间:2024/05/18 03:52

http://blog.csdn.net/mcgrady_tracy/article/details/42264995


1 中断嵌套

2 中断分层

上半部 :当中断发生时 , 它进行相应地硬件读写 ,并 “ 登记 ” 该中断。 通常由中断处理程序充当上半部 
下半部 :在系统空闲 的时候对上半部“登记”的中断进行后续 处理 

2.1 软中断

2.2 tasklet

2.3 工作队列

工作队列workqueue不是通过软中断实现的,它是通过内核进程实现的

3 使用工作队列实现分层

Linux内核使用struct workqueue_struct 来描述一个工作队列 :
struct workqueue_struct {
struct cpu_workqueue_struct *cpu_wq;
struct list_head list;
const char *name; /*workqueue name*/
const char *name; /*workqueue name*/
int singlethread;
int freezeable; /* Freeze threads during suspend */
int rt;
};

Linux内核使用struct work_struct来描述一个工作项 :
struct work_struct {
atomic_long_t data;
struct list_head entry;
work_func_t func;
};
typedef void (*work_func_t)(struct work_struct *work);


step1.  创建工作队列
create_workqueue


step2.  创建工作
INIT_WORK


step3.  提交工作
queue_work


queue1.c

#include <linux/init.h>
#include <linux/module.h>

struct workqueue_struct *my_wq;
struct work_struct *work1;
struct work_struct *work2;

MODULE_LICENSE("GPL");

void work1_func(struct work_struct *work)
{
    printk("this is work1->\n");
}

void work2_func(struct work_struct *work)
{
    printk("this is work2->\n");
}

int init_que(void)
{
    //1. 创建工作队列
    my_wq = create_workqueue("my_que");
   
    //2. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);
    
    //3. 挂载(提交)工作
    queue_work(my_wq,work1);
   
    //2. 创建工作
    work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work2, work2_func);
    
    //3. 挂载(提交)工作
    queue_work(my_wq,work2);
   
    return 0;
}

void clean_que()
{

}

module_init(init_que);
module_exit(clean_que);


在大多数情况下, 驱动并不需要自己建立工作队列 , 只需定义工作, 然后将工作提交到内核已经定义好 的 工作队列keventd_wq 中。
1.  提交工作 到默认 队列
schedule_work

queue2.c

#include <linux/init.h>
#include <linux/module.h>

struct work_struct *work1;
struct work_struct *work2;

MODULE_LICENSE("GPL");

void work1_func(struct work_struct *work)
{
    printk("this is work1->\n");
}

void work2_func(struct work_struct *work)
{
    printk("this is work2->\n");
}

int init_que(void)
{
    //2. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);
    
    //3. 挂载(提交)工作
    schedule_work(work1);
   
    //2. 创建工作
    work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work2, work2_func);
    
    //3. 挂载(提交)工作
    schedule_work(work2);
   
    return 0;
}

void clean_que()
{

}

module_init(init_que);
module_exit(clean_que);



key.c


#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/io.h>

#define GPFCON 0x56000050

struct work_struct *work1;

void work1_func(struct work_struct *work)
{
    printk("key down!\n");
}

irqreturn_t key_int(int irq, void *dev_id)
{
    //1. 检测是否发生了按键中断
    
    //2. 清除已经发生的按键中断
       
//3. 提交下半部
    schedule_work(work1);

    return 0;
}

void key_hw_init()
{
    unsigned short data; 
    unsigned int *gpio_config;
    
    gpio_config = ioremap(GPFCON,4);
    data = readw(gpio_config);
    data &= ~0b11;
    data |= 0b10;
    writew(data,gpio_config);
}

int key_open(struct inode *node,struct file *filp)
{
    return 0;
}

struct file_operations key_fops = 
{
    .open = key_open,
};

struct miscdevice key_miscdev = {
    .minor = 200,
    .name = "tq2440key",
    .fops = &key_fops,
};

static int button_init()
{
    misc_register(&key_miscdev);
    
    //按键硬件初始化
    key_hw_init();
    
    //注册中断处理程序
    request_irq(IRQ_EINT0,key_int,IRQF_TRIGGER_FALLING,"tq2440key",0);


//2. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);

    return 0;
}


static void button_exit()
{
    misc_deregister(&key_miscdev);
}


module_init(button_init);
module_exit(button_exit);




0 0
原创粉丝点击