Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116764
  • 博文数量: 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)

分类: LINUX

2014-02-12 11:43:15

1. timer
每隔8个jiffies,打印一个hello
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. struct timer_list timer;
  5. static void do_timer_process(unsigned long arg)
  6. {
  7.     printk("%s\n", (char*)arg);
  8.     timer.expires    = jiffies +8;
  9.     add_timer(&timer);
  10. }

  11. static int __init scull_init(void)
  12. {
  13.     printk(KERN_ALERT "enter scull dirver\n");
  14.     setup_timer(&timer, do_timer_process, (unsigned long)"Hello");
  15.     timer.expires    = jiffies +8;
  16.     add_timer(&timer);
  17.     return 0;
  18. }

  19. static void __exit scull_exit(void)
  20. {
  21.     printk(KERN_ALERT "goodbye scull dirver\n");
  22.     return ;
  23. }

  24. MODULE_LICENSE("GPL");

  25. module_init(scull_init);
  26. module_exit(scull_exit);
2.高精度定时器:
  利用hrtimer延时8ms
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/hrtimer.h>
  6. #include <linux/time.h>
  7. #include <linux/timer.h>
  8. #include <linux/timex.h>
  9. #include <linux/rtc.h>
  10. #define dbmsg(fmt, args ...) printk(KERN_NOTICE "%s[%d]: "fmt"\n", __FUNCTION__, __LINE__,##args)
  11. static struct hrtimer vibe_timer;
  12. static int value = 8; //delay 8ms
  13. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  14. {     
  15.     struct timeval tv;
  16.     do_gettimeofday(&tv);
  17.     dbmsg("time %u:%u",tv.tv_sec,tv.tv_usec);
  18.     vibe_timer.function = vibrator_timer_func;
  19.     hrtimer_start(&vibe_timer, ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
  20.     return HRTIMER_NORESTART;
  21. }

  22. static int scull_init(void)
  23. {
  24.     dbmsg("scull_init");
  25.     hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  26.     vibe_timer.function = vibrator_timer_func;
  27.     hrtimer_start(&vibe_timer, ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
  28.     return 0;
  29. }

  30. static void scull_exit(void)
  31. {
  32.     while(hrtimer_cancel(&vibe_timer))
  33.     {
  34.         dbmsg("scull exit");
  35.     }
  36. }

  37. MODULE_LICENSE("GPL");

  38. module_init(scull_init);
  39. module_exit(scull_exit);
打印结果:
lcm3d: vibrator_timer_func[20]: time 87433:710
lcm3d: vibrator_timer_func[20]: time 87433:10708
lcm3d: vibrator_timer_func[20]: time 87433:20702
lcm3d: vibrator_timer_func[20]: time 87433:30706
lcm3d: vibrator_timer_func[20]: time 87433:40702
lcm3d: vibrator_timer_func[20]: time 87433:50702
注:从上述打印来看,大约每次执行了10ms,但是去掉打印从示波器上看的确是8ms

3. 工作队列延时
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/hrtimer.h>
  6. #include <linux/time.h>
  7. #include <linux/timer.h>
  8. #include <linux/timex.h>
  9. #include <linux/rtc.h>
  10. #define dbmsg(fmt, args ...) printk(KERN_NOTICE "lcm3d: %s[%d]: "fmt"\n", __FUNCTION__, __LINE__,##args)

  11. struct delayed_work dwork;
  12. static void dwork_func(void)
  13. {
  14.     unsigned long delay = msecs_to_jiffies(8);  //每8ms执行一次
  15.     schedule_delayed_work(&dwork, delay);
  16.     dbmsg("in dwork_func");
  17. }
  18. static int scull_init(void)
  19. {
  20.     dbmsg("scull_init");
  21.     INIT_DELAYED_WORK(&dwork, dwork_func);
  22.     schedule_work(&dwork); //by feng
  23.     return 0;
  24. }

  25. static void scull_exit(void)
  26. {
  27.     dbmsg("scull exit");
  28. }

  29. MODULE_LICENSE("GPL");

  30. module_init(scull_init);
  31. module_exit(scull_exit);
4. 工作队列的一个例子
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/workqueue.h>
  5. struct timer_list timer;
  6. struct work_struct my_work;
  7. struct workqueue_struct *my_work_queue;
  8. static void my_work_process(struct work_struct *work)
  9. {
  10.     printk("my_work_process \n");
  11.     return ;
  12. }

  13. static void do_timer_process(unsigned long arg)
  14. {  
  15.     //3.将工作提交到工作队列
  16.     queue_work(my_work_queue, &my_work);

  17.     timer.expires = jiffies +8;
  18.     add_timer(&timer);
  19. }
  20. static int __init scull_init(void)
  21. {
  22.     printk(KERN_ALERT "enter scull dirver\n");
  23.     //创建工作队列
  24.     //  1. 填充一个work_struct
  25.     //  2. 显式的创建一个工作队列
  26.     INIT_WORK(&my_work, my_work_process);
  27.     my_work_queue = create_workqueue("my_work");
  28.     
  29.     //setup timer    
  30.     setup_timer(&timer, do_timer_process, (unsigned long)"Hello");
  31.     timer.expires = jiffies +8;
  32.     add_timer(&timer);
  33.     return 0;
  34. }
  35. static void __exit scull_exit(void)
  36. {
  37.     printk(KERN_ALERT "goodbye scull dirver\n");
  38.     return ;
  39. }
  40. MODULE_LICENSE("GPL");
  41. module_init(scull_init);
  42. module_exit(scull_exit);
5. 工作队列中参数的传递
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/workqueue.h>
  5. #include <linux/slab.h>

  6. struct timer_list timer;
  7. struct _work_s
  8. {
  9.     struct work_struct wk;
  10.     struct workqueue_struct *wkq;
  11.     char data[20];
  12. };
  13. typedef struct _work_s work_s;
  14. static void my_work_process(struct work_struct *work)
  15. {
  16.     //利用container_of获取结构体,然后取得参数
  17.     work_s* my_work= container_of(work, work_s, wk);
  18.     printk("my_work_process data=%s\n", my_work->data);
  19.     return ;
  20. }

  21. static void do_timer_process(unsigned long arg)
  22. {
  23.     work_s * my_work = (work_s*)arg;    //获取work_s结构体
  24.     // 3.将工作提交到工作队列
  25.     queue_work(my_work->wkq, &my_work->wk);
  26.     timer.expires = jiffies +8;
  27.     add_timer(&timer);
  28. }
  29. static int __init scull_init(void)
  30. {
  31.     work_s* my_work = NULL;
  32.     my_work = kzalloc(sizeof(work_s), GFP_KERNEL);
  33.     printk(KERN_ALERT "enter scull dirver\n");
  34.     //创建工作队列
  35.     //  1. 填充一个work_struct 
  36.     //  2. 显式的创建一个工作队列
  37.     INIT_WORK(&my_work->wk, my_work_process);
  38.     my_work->wkq= create_workqueue("my_work");
  39.     memcpy(my_work->data, "hello", 6);    
  40.     //setup timer   
  41.     //将结构体当作参数传递出去
  42.     setup_timer(&timer, do_timer_process, (unsigned long)my_work); 
  43.     timer.expires = jiffies +8;
  44.     add_timer(&timer);
  45.     return 0;
  46. }
  47. static void __exit scull_exit(void)
  48. {
  49.     printk(KERN_ALERT "goodbye scull dirver\n");
  50.     return ;
  51. }
  52. MODULE_LICENSE("GPL");
  53. module_init(scull_init);
  54. module_exit(scull_exit);








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