OSAL message mechanism triggers the event Process
The timer trigger event is generally triggered inside the program. If an external trigger event is triggered, the system message can be used to trigger the event. During initialization, the key callback function HalKeyConfig (OnboardKeyIntEnable, OnBoard_KeyCallback) is registered in InitBoard (). The function registers the key callback function to pHalKeyProcessFunction and starts the timer event, the ID is the HAL layer ID, and the event is a key event. Call HalKeyPoll () to start the key Round Robin and wait for the key to be triggered. HalProcessKeyInterrupt () is registered to the OSAL interrupt mechanism through HAL_ISR_FUNCTION (). This part of the code is not made public. It may be that the underlying key triggers the interrupt and then calls halProcessKeyInterrupt () to report the event, HalKeyPoll () call the key callback function (pHalKeyProcessFunction) () registered during initialization to process. In the callback function, call the OnBoard_SendKeys () function to send system messages. The message structure is as follows:
typedef struct{ void *next; uint16 len; uint8 dest_id;} osal_msg_hdr_t;typedef struct{ uint8 event; uint8 status;} osal_event_hdr_t;typedef struct{ osal_event_hdr_t hdr; uint8 state; uint8 keys; } keyChange_t;
The prototype of the OnBoard_SendKeys () callback function is as follows:
1 /********************************************************************* 2 3 * @fn OnBoard_SendKeys 4 5 * 6 7 * @brief Send "Key Pressed" message to application. 8 9 *10 11 * @param keys - keys that were pressed12 13 * state - shifted14 15 *16 17 * @return status18 19 *********************************************************************/20 21 uint8 OnBoard_SendKeys( uint8 keys, uint8 state )22 23 {24 25 keyChange_t *msgPtr;26 27 if ( registeredKeysTaskID != NO_TASK_ID )28 29 {30 31 // Send the address to the task32 33 msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );34 35 if ( msgPtr )36 37 {38 39 msgPtr->hdr.event = KEY_CHANGE;40 41 msgPtr->state = state;42 43 msgPtr->keys = keys;44 45 46 osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );47 48 }49 50 return ( SUCCESS );51 52 }53 54 else55 56 return ( FAILURE );57 58 }
In the OnBoard_SendKeys () function, call the osal_msg_allocate () function to apply for a piece of memory,
1 /********************************************************************* 2 3 * @fn osal_msg_allocate 4 5 * 6 7 * @brief 8 9 *10 11 * This function is called by a task to allocate a message buffer12 13 * into which the task will encode the particular message it wishes14 15 * to send. This common buffer scheme is used to strictly limit the16 17 * creation of message buffers within the system due to RAM size18 19 * limitations on the microprocessor. Note that all message buffers20 21 * are a fixed size (at least initially). The parameter len is kept22 23 * in case a message pool with varying fixed message sizes is later24 25 * created (for example, a pool of message buffers of size LARGE,26 27 * MEDIUM and SMALL could be maintained and allocated based on request28 29 * from the tasks).30 31 *32 33 *34 35 * @param uint8 len - wanted buffer length36 37 *38 39 *40 41 * @return pointer to allocated buffer or NULL if allocation failed.42 43 */44 45 uint8 * osal_msg_allocate( uint16 len )46 47 {48 49 osal_msg_hdr_t *hdr;50 51 52 if ( len == 0 )53 54 return ( NULL );55 56 57 58 hdr = (osal_msg_hdr_t *) osal_mem_alloc( (short)(len + sizeof( osal_msg_hdr_t )) );59 60 if ( hdr )61 62 {63 64 hdr->next = NULL;65 66 hdr->len = len;67 68 hdr->dest_id = TASK_NO_TASK;69 70 return ( (uint8 *) (hdr + 1) );71 72 }73 74 else75 76 return ( NULL );77 78 }
The memory layout is as follows: after the application is successful, the return value is the first address of the keyChange_t part. Therefore, the struct pointer is reduced by one in subsequent Message check and fill operations. After the message is created, call osal_msg_send () to send the message. This function calls osal_msg_enqueue_push () to send the message to the OSAL message linked list and call osal_set_event (destination_task, SYS_EVENT_MSG ); send a system message event to the target task, call the callback function of the target task in the main cycle, and enter the system Message Processing Branch to receive and parse the message.