阻塞型驱动设计

来源:互联网 发布:淘宝情趣内衣模特 编辑:程序博客网 时间:2024/06/03 15:33
阻塞型驱动设计
1.阻塞的必要性
当一个设备无法立刻满足用户的读写请求时应当如何处理? 例如:调用read时,设备没有数据提供, 但以后可能会有;或者一个进程试图向设备写入数据,但是设备暂时没有准备好接收数据。当上述情况发生的时候,驱动程序应当(缺省地)阻塞进程,使它进入等待(睡眠)状态,直到请求可以得到满足

A准备对B(驱动程序)读取数据,B说现在还没数据。请A先进候车室等待,B之后有了数据。B就将A唤醒。

2.内核等待队列
在实现阻塞驱动的过程中,也需要有一个“候车室”来安排被阻塞的进程“休息”,当唤醒它们的条件成熟时,则可以从“候车室”中将这些进程唤醒。而这个“候车室”就是等待队列

1、定义等待队列
wait_queue_head_t my_queue
2、初始化等待队列
init_waitqueue_head(&my_queue);
3、定义+初始化等待队列
DECLARE_WAIT_QUEUE_HEAD(my_queue);

3.进入等待队列,睡眠
  wait_event(queue,condition)
当condition(布尔表达式)为真时,立即返回;否则让进程进入TASK_UNINTERRUPTIBLE模式的睡眠,并挂在queue参数所指定的等待队列上。

 
wait_event_interruptible(queue,condition)
当condition(布尔表达式)为真时,立即返回;否则让进程进入TASK_INTERRUPTIBLE的睡眠,并挂在queue参数所指定的等待队列上。

 
int wait_event_killable(queue, condition)
当condition(一个布尔表达式)为真时,立即返回;否则让进程进入TASK_KILLABLE的睡眠,并挂在queue参数所指定的等待队列上。

4.从等待队列中唤醒进程
  wake_up(wait_queue_t *q)
从等待队列q中唤醒状态为TASK_UNINTERRUPTIBLE,TASK_INTERRUPTIBLE,TASK_KILLABLE 的所有进程
  wake_up_interruptible(wait_queue_t *q)
从等待队列q中唤醒状态为TASK_INTERRUPTIBLE 的进程。

5.对按键程序进行改造
key.c
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/miscdevice.h>
  4. #include <linux/fs.h>
  5. #include <asm/uaccess.h>
  6. #include <linux/io.h>
  7. #include <linux/irqreturn.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/uaccess.h>

  10. #define GPFCON (volatile unsigned long*) 0x56000050
  11. #define GPFDAT (volatile unsigned long*) 0x56000054

  12. unsigned int *gpio_data;

  13. struct work_struct *work1;
  14. struct timer_list key_timer;
  15. wait_queue_head_t key_q;

  16. unsigned int key_num = 0;

  17. void key_timer_func(unsigned long data)
  18. {
  19.     unsigned int key_val;
  20.     key_val = readw(gpio_data);
  21.     if((key_val & 0x01) == 0)
  22.     {
  23.         key_num = 4;
  24.     }
  25.     else if((key_val& 0x02) == 0)
  26.     {
  27.         key_num = 1;
  28.     }
  29.     else if((key_val & 0x04) == 0)
  30.     {
  31.         key_num = 3;
  32.     }
  33.     else if((key_val & 0x10) == 0)
  34.     {
  35.         key_num = 2;
  36.     }

  37.     wake_up(&key_q);                                 //唤醒进程
  38. }

  39. void work1_func(struct work_struct *work)
  40. {
  41.     mod_timer(&key_timer, jiffies + HZ/10);
  42. }

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

  51.     return 0;
  52. }

  53. void key_hw_init()
  54. {
  55.     unsigned int *gpio_config;
  56.     unsigned short data;
  57.     
  58.     gpio_config = ioremap(GPFCON, 4);
  59.     gpio_data = ioremap(GPFDAT, 4);
  60.     data = readw(gpio_config);
  61.     data &= ~0x33f;
  62.     data |= 0x22a;
  63.     writew(data, gpio_config);
  64. }

  65. int key_open(struct inode *inode, struct file *filp)
  66. {
  67.     return 0;
  68. }

  69. ssize_t key_read(struct file *filp, char __user *buf, size_t size, loff_t *pos)
  70. {
  71.     wait_event(key_q, key_num);                                 //进入睡眠

  72.     printk("in kernel :key num is %d\n",key_num);
  73.     copy_to_user(buf, &key_num, 4);

  74.     key_num = 0;

  75.     return 4;
  76. }

  77. struct file_operations key_fops = {
  78.     .open = key_open,
  79.     .read = key_read,
  80. };

  81. struct miscdevice key_miscdev = {
  82.     .minor = 200,
  83.     .name = "tq2400key",
  84.     .fops = &key_fops,
  85. };

  86. static int button_init()
  87. {
  88.     int ret;
  89.     ret = misc_register(&key_miscdev);
  90.     if(ret != 0)
  91.     {
  92.         printk(KERN_ERR"misc_register failed\n");
  93.     }
  94.     //按键初始化
  95.     key_hw_init();

  96.     //注册中断处理程序
  97.     ret = request_irq(IRQ_EINT0, &key_int, IRQF_TRIGGER_FALLING, "tq2440key", 0);
  98.     ret = request_irq(IRQ_EINT1, &key_int, IRQF_TRIGGER_FALLING, "tq2440key", 0);
  99.     ret = request_irq(IRQ_EINT2, &key_int, IRQF_TRIGGER_FALLING, "tq2440key", 0);
  100.     ret = request_irq(IRQ_EINT4, &key_int, IRQF_TRIGGER_FALLING, "tq2440key", 0);

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

  104.     //初始化定时器
  105.     init_timer(&key_timer);
  106.     key_timer.function = key_timer_func;

  107.     //注册定时器
  108.     add_timer(&key_timer);

  109.     //初始化等待队列
  110.     init_waitqueue_head(&key_q);                                //初始化等待队列
  111.     return 0;
  112. }

  113. static void button_exit()
  114. {
  115.     misc_deregister(&key_miscdev);
  116.     //注销中断处理函数
  117.     free_irq(IRQ_EINT0, 0);
  118.     free_irq(IRQ_EINT1, 0);
  119.     free_irq(IRQ_EINT2, 0);
  120.     free_irq(IRQ_EINT4, 0);
  121. }

  122. module_init(button_init);
  123. module_exit(button_exit);

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(35) | 评论(0) | 转发(0) |
0

上一篇:中断分层和按键去抖

下一篇:总线设备驱动模型和平台设备模型

相关热门文章
  • SHTML是什么_SSI有什么用...
  • 查看linux中某个端口(port)...
  • 卡尔曼滤波的原理说明...
  • shell中字符串操作
  • 关于java中的“错误:找不到或...
给主人留下些什么吧!~~
原创粉丝点击