Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26851
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 82
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-30 14:36
文章分类

全部博文(12)

文章存档

2016年(7)

2015年(5)

我的朋友

分类: 嵌入式

2016-09-12 14:39:05


点击(此处)折叠或打开

  1. /**包含一个函数声明 : static char process_thread_blink_process(struct pt *process_thread_process_pt, process_event_t ev, process_data_t data)
  2.  * 定义一个进程结构 : struct process blink_process = { NULL, "Blink", process_thread_blink_process };
  3.  */
  4. PROCESS(blink_process, "Blink");
  5. /**定义一个进程数据组 : struct process blink_process = { NULL, "Blink", process_thread_blink_process };
  6.  * 这个进程数组是用户添加的进程,在后面的处理中,他会和系统进程一起使用进程链表管理
  7.  */
  8. AUTOSTART_PROCESSES(&blink_process);
  9. /**定义一个进程 : static char process_thread_blink_process(struct pt *process_thread_process_pt, process_event_t ev, process_data_t data)
  10.  * 
  11.  */ 
  12. PROCESS_THREAD(blink_process, ev, data)
  13. {
  14.   
  15.   PROCESS_BEGIN();
  16.   while(1)
  17.   {
  18.     static struct etimer et;
  19.     etimer_set(&et, CLOCK_SECOND);
  20.     PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  21.     
  22.     Bsp_RedLightOnImp();
  23.     
  24.     etimer_set(&et, CLOCK_SECOND);
  25.     PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  26.     
  27.     Bsp_RedLightOffImp();
  28.     
  29.   }
  30.    PROCESS_END();
  31. }

宏讲解


点击(此处)折叠或打开

  1. PROCESS(blink_process, "Blink");

  2. #define PROCESS(name, strname)                \
  3.   PROCESS_THREAD(name, ev, data);            \
  4.   struct process name = { NULL, strname,        \
  5.                           process_thread_##name }

  6. #define PROCESS_THREAD(name, ev, data) \
    static PT_THREAD(process_thread_##name(struct pt *process_pt, \
          process_event_t ev, \
          process_data_t data))



按照上面一步步展开即可得到注释部分的内容,关于##连接符的作用,如
#define A(x)  T_##x
则 int A(1) = 10; //等效于int T_1 = 10;

进程展开


点击(此处)折叠或打开

  1. static char process_thread_blink_process(struct pt *blink_process_process_pt, process_event_t ev, process_data_t data)
  2. {
  3.         {
  4.                 char PT_YIELD_FLAG = 1;
  5.                 switch((blink_process_process_pt)->lc)
  6.                 {
  7.                     case 0:
  8.                         ;
  9.                         while (1) {
  10.                                 do {
  11.                                         PT_YIELD_FLAG = 0;
  12.                                         (blink_process_process_pt)->lc = __LINE__;
  13.                                         case __LINE__:
  14.                                         ;
  15.                                         if((PT_YIELD_FLAG == 0) || !(etimer_expired(&et))) {
  16.                                                 return PT_YIELDED
  17.                                         } while(0)
  18.                                 }
  19.                                 Bsp_RedLightOnImp();
  20.                                 
  21.                                 etimer_set(&et, CLOCK_SECOND);
  22.                                 do {
  23.                                         PT_YIELD_FLAG = 0;
  24.                                         (blink_process_process_pt)->lc = __LINE__;
  25.                                         case __LINE__:
  26.                                         ;
  27.                                         if((PT_YIELD_FLAG == 0) || !(etimer_expired(&et))) {
  28.                                                 return PT_YIELDED
  29.                                         } while(0)
  30.                                 }
  31.                                 Bsp_RedLightOffImp();
  32.                                 
  33.                         }

  34.                 };
  35.              
  36.                 PT_YIELD_FLAG = 0;
  37.                 (blink_process_process_pt)->lc = 0;;
  38.                 return 3;
  39.         }
  40. }
进程代码展开如上图所示,在走一遍代码运行流程:
进程第一次运行:进程运行到12行保存运行的函数为12行?进程运行到15行后,条件为假,进程挂起。
假设定时事件到达,进程第二次运行:进程从头运行,运行到第五行系统跳转,这时运行从第上次保存的行开始运行,从第12行?此时就恢复上次运行的现场了。

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