Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2118673
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2015-11-20 18:45:12

1. 程序说明
将GPIO口设为外部中断模式,当有外部中断触发时,进入中断处理函数中
中断处理函数中使用了工作队列
工作队列的处理函数中使用了wake_lock
2.
2.1工作队列的使用
struct work_struct wakeup_frome1408_wq;
INIT_WORK(&wakeup_frome1408_wq,(void (*)(void*))wakeup_frome1408_wq_func);   //工作队列
schedule_work(&wakeup_frome1408_wq);
void wakeup_frome1408_wq_func(struct work_struct *work)
{
    wake_lock_timeout(&wakeup6572_wlock, msecs_to_jiffies(10*1000));
}
工作队列的作用是延后处理中断
2.2 wake_lock
  1. void wake_lock_init(struct wake_lock *lock, int type, const char *name);
  2. void wake_lock_destroy(struct wake_lock *lock);
  3. void wake_lock(struct wake_lock *lock);
  4. void wake_lock_timeout(struct wake_lock *lock, long timeout);   //在timeout时间内系统无法休眠
  5. void wake_unlock(struct wake_lock *lock);
static struct wake_lock wakeup6572_wlock;
wake_lock_init(&wakeup6572_wlock,WAKE_LOCK_SUSPEND,"wakeup6572_wlock");      //wakelock
wake_lock_timeout(&wakeup6572_wlock, msecs_to_jiffies(10*1000));
wakelock在android的电源管理系统中扮演一个核心的角色,wakelock是一种锁的机制, 只要有task拿着这个锁, 系统就无法进入休眠


3.代码如下
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/ioport.h>
  5. #include <linux/errno.h>
  6. #include <linux/spi/spi.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irqreturn.h>
  12. #include <linux/types.h>
  13. #include <linux/delay.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/sched.h>
  19. #include <linux/wakelock.h>
  20. #include <mach/irqs.h>
  21. #include <mach/mt_gpio.h>
  22. #include <mach/eint.h>


  23. #define GPIO_1408_WAKEUP_6572 GPIO145 //1408 pending interrupt

  24. #define WAKEUP_FROM1408_NAME "wakeup_from1408"

  25. #define WAKEUP_FROM1408_EINT_NUM 8

  26. struct work_struct wakeup_frome1408_wq;

  27. static struct wake_lock wakeup6572_wlock;

  28. void wakeup_frome1408_wq_func(struct work_struct *work)
  29. {
  30.     printk(KERN_NOTICE "cong: %s:%s[%d]: \n", __FILE__,__FUNCTION__, __LINE__);
  31.     wake_lock_timeout(&wakeup6572_wlock, msecs_to_jiffies(10*1000));
  32. }

  33. static void wakeup_from1408_irq_handler(void)
  34. {
  35.     schedule_work(&wakeup_frome1408_wq);
  36.     return ;
  37. }

  38. static int __init mt6261_init(void)
  39. {
  40.     int rc;
  41.     struct mt6261_data_t * pmt6261;
  42.     pmt6261 = devm_kzalloc(&pdev->dev, sizeof(*pmt6261), GFP_KERNEL);   //初始化私有数据
  43.     if (!pmt6261) {
  44.         dev_err(&pdev->dev, "failed to allocate mt6261\n");
  45.         return -ENOMEM;
  46.     }

  47.     //1. 将GPIO145设为MOD3即INT8
  48.     mt_set_gpio_mode(GPIO_1408_WAKEUP_6572, GPIO_MODE_03);              //MOD3
  49.     mt_set_gpio_dir(GPIO_1408_WAKEUP_6572, GPIO_DIR_IN);                //输入状态
  50.     mt_set_gpio_pull_enable(GPIO_1408_WAKEUP_6572, GPIO_PULL_DISABLE);  //禁止上拉,输入一般是需要上拉的,但这儿不需要
  51.     mt_eint_set_hw_debounce(WAKEUP_FROM1408_EINT_NUM, 1);               //延时吧?
  52.     mt_eint_registration(WAKEUP_FROM1408_EINT_NUM, EINTF_TRIGGER_RISING, wakeup_from1408_irq_handler, true); //注册中断
  53.     mt_eint_unmask(WAKEUP_FROM1408_EINT_NUM);

  54.     INIT_WORK(&wakeup_frome1408_wq,(void (*)(void*))wakeup_frome1408_wq_func);   //工作队列

  55.     wake_lock_init(&wakeup6572_wlock,WAKE_LOCK_SUSPEND,"wakeup6572_wlock");      //wakelock

  56.     platform_set_drvdata(pdev,pmt6261);                         //设置私有数据

  57.     return 0;
  58. fail:
  59.     return rc;
  60. }

  61. static void __init mt6261_exit(void)
  62. {
  63.     platform_driver_unregister(&mt6261_driver);
  64. }

  65. module_init(mt6261_init);
  66. module_exit(mt6261_exit);

  67. MODULE_DESCRIPTION("MT6261 Controller Driver");
  68. MODULE_AUTHOR("wangcong");
  69. MODULE_LICENSE("GPL");


阅读(2161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~