1,系统上电以后在main函数的最后会调用osal_start_system()方法来启动系统,这个方法是个死循环,只循环里面只做一件事:不断的检测任务,看任务里面有没有事件需要处理;
检测方法:如果tasksEvents[idx]不为0,则表示要事件需要处理。
2,事件是怎么的?
- /*********************************************************************
- * @fn osal_msg_send
- *
- * @brief
- *
- * This function is called by a task to send a command message to
- * another task or processing element. The sending_task field must
- * refer to a valid task, since the task ID will be used
- * for the response message. This function will also set a message
- * ready event in the destination tasks event list.
- *
- *
- * @param uint8 destination task - Send msg to? Task ID
- * @param uint8 *msg_ptr - pointer to new message buffer
- * @param uint8 len - length of data in message
- *
- * @return SUCCESS, INVALID_TASK, INVALID_MSG_POINTER
- */
- uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr )
- {
- .......
- //将message发进队列
- osal_msg_enqueue( &osal_qHead, msg_ptr );
- //设置事件,等待task来处理
- osal_set_event( destination_task, SYS_EVENT_MSG );
- return ( SUCCESS );
- }
看一下osal_set_event:
- /*********************************************************************
- * @fn osal_set_event
- *
- * @brief
- *
- * This function is called to set the event flags for a task. The
- * event passed in is OR'd into the task's event variable.
- *
- * @param uint8 task_id - receiving tasks ID
- * @param uint8 event_flag - what event to set
- *
- * @return SUCCESS, INVALID_TASK
- */
- uint8 osal_set_event( uint8 task_id, uint16 event_flag )
- {
- if ( task_id < tasksCnt )
- {
- ......
- tasksEvents[task_id] |= event_flag; // Stuff the event bit(s)
- return ( SUCCESS );
- }
- else
- {
- return ( INVALID_TASK );
- }
- }
3,在任务的处理函数中,将会从队列中取出消息来处理,比如:
- uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- (void)task_id;
- if ( events & SYS_EVENT_MSG )//判断事件类型
- {
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );//从消息队列中取出消息来处理
- }
- .....
- return 0;
- }