ZigBee小系统的运行原理

一、main开始

进入  osal_start_system(); // No Return from here执行操作系统,这是一个无线的循环

void osal_start_system( void )
{
#if !defined ( ZBIT ) && !defined ( UBIT )
  for(;;)  // Forever Loop
#endif
  {
    osal_run_system();
  }
}

二、

进入for的无线循环,也就是循环执行osal_run_system();。

void osal_run_system( void )
{
  uint8 idx = 0;


  osalTimeUpdate();
  Hal_ProcessPoll();


  do {
    if (tasksEvents[idx])  // Task is highest priority that is ready.
    {
      break;
    }
  } while (++idx < tasksCnt);


  if (idx < tasksCnt)
  {
    uint16 events;
    halIntState_t intState;


    HAL_ENTER_CRITICAL_SECTION(intState);
    events = tasksEvents[idx];//tasksEvents是用来存放事件的标志,一旦有事件准备就绪,相应的标志就会置1。

然后取出相应的标志。并根据相应的标志取出对应的事件。
    tasksEvents[idx] = 0;  // Clear the Events for this task.//清标志位
    HAL_EXIT_CRITICAL_SECTION(intState);


    activeTaskID = idx;
  如果事件的ID是9,也就是对应的应用层事件SampleApp_ProcessEvent

下面的这二句话是相同的的意义。

  //events =SampleApp_ProcessEvent( idx, events );
    events = (tasksArr[idx])( idx, events );

    activeTaskID = TASK_NO_TASK;


    HAL_ENTER_CRITICAL_SECTION(intState);
    tasksEvents[idx] |= events;  // Add back unprocessed events to the current task.

如果事件没有来得及处理,就放回相应的事件组。让下一个循环来执行事件
    HAL_EXIT_CRITICAL_SECTION(intState);
  }
#if defined( POWER_SAVING )
  else  // Complete pass through all task events with no activity?
  {
    osal_pwrmgr_powerconserve();  // Put the processor/system into sleep
  }
#endif


  /* Yield in case cooperative scheduling is being used. */
#if defined (configUSE_PREEMPTION) && (configUSE_PREEMPTION == 0)
  {
    osal_task_yield();
  }
#endif
}

最后进入事件处理函数来处理相应的事件

uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;  // Intentionally unreferenced parameter


  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
        case CMD_SERIAL_MSG://新添加接收串口信息
          SampleApp_SerialCMD((mtOSALSerialData_t *)MSGpkt);
          break;
        // Received when a key is pressed
        case KEY_CHANGE://按键触发
          SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
          break;


        // Received when a messages is received (OTA) for this endpoint
        case AF_INCOMING_MSG_CMD://有无线数据到来
          SampleApp_MessageMSGCB( MSGpkt );
          break;


        // Received whenever the device changes state in the network
        case ZDO_STATE_CHANGE://当网络状态改变,如从未连接上到连上网络
          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          if ( (SampleApp_NwkState == DEV_ZB_COORD)//协调器、路由器、或者终端都执行
              || (SampleApp_NwkState == DEV_ROUTER)
              || (SampleApp_NwkState == DEV_END_DEVICE) )
          {
            // Start sending the periodic message in a regular interval.//打开定时器
            osal_start_timerEx( SampleApp_TaskID,//任务ID号
                              SAMPLEAPP_SEND_PERIODIC_MSG_EVT,//同一个任务下可以有多个事件,这个是事件的号码。事件号码16位必须只占1位,所以只能定义16个事件
                              SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );//时间
            osal_start_timerEx( SampleApp_TaskID,//任务ID号
                              SAMPLEAPP_SEND_PERIODIC_MSG_EVT2,//同一个任务下可以有多个事件,这个是事件的号码。事件号码16位必须只占1位,所以只能定义16个事件
                              SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );//时间
          }
          else
          {
            // Device is no longer in the network
          }
          break;


        default:
          break;
      }


      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );


      // Next - if one is available
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    }


    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }


  // Send a message out - This event is generated by a timer
  //  (setup in SampleApp_Init()).
  if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )//周期性发数
  {
    // Send the periodic message
    SampleApp_SendPeriodicMessage();//周期性发送函数


    // Setup to send message again in normal period (+ a little jitter)
 //   osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
   //     (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );


    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  }

//在这样一个周期性的函数里可以添加时钟事件和外部Flash事件
  if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT2 )//周期性发数
  {
    // Send the periodic message
    //SampleApp_SendPeriodicMessage();//周期性发送函数


    // Setup to send message again in normal period (+ a little jitter)
 //   osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
   //     (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );


    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT2);
  }
  SAMPLEAPP_SEND_PERIODIC_MSG_EVT2


  // Discard unknown events
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值