1. After the system is powered on, the osal_start_system() method will be called at the end of the main function to start the system. This method is an infinite loop, and only one thing is done in the loop: constantly detect tasks to see if there are any events in the task that need to be processed;
Detection method: If tasksEvents[idx] is not 0, it means that the event needs to be processed.
2. What happened to the incident?
- /*********************************************************************
- * @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 )
- {
- .......
- //Send the message to the queue
- osal_msg_enqueue( &osal_qHead, msg_ptr );
- //Set the event and wait for the task to process it
- osal_set_event( destination_task, SYS_EVENT_MSG );
- return ( SUCCESS );
- }
Take a look at 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. In the processing function of the task, the message will be taken from the queue for processing, such as:
- uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- (void)task_id;
- if ( events & SYS_EVENT_MSG )//Determine event type
- {
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );//Get the message from the message queue for processing
- }
- .....
- return 0;
- }