UCOSIII學習筆記--任務內嵌消息隊列

#define OS_CFG_TASK_Q_EN                1u   /* Include code for OSTaskQXXXX()                                        */

要使用內嵌消息隊列,將宏OS_CFG_TASK_Q_EN設置爲1。

任務內嵌消息隊列,顧名思義,就是使用任務內部的內嵌消息隊列,無需創建新的消息隊列,發送的是任務內嵌的消息隊列,發送函數需要接收消息任務的任務控制塊,接收消息的任務接收的就是發送給這個任務的消息。

發送消息API:

函數原型:

void  OSTaskQPost (

      OS_TCB       *p_tcb,
                   void         *p_void,
                   OS_MSG_SIZE   msg_size,
                   OS_OPT        opt,
                   OS_ERR       *p_err);

以下是UCOSIII關於OSTaskQPost ()API函數的官方註釋以及個人理解

/*
************************************************************************************************************************
*                                               POST MESSAGE TO A TASK // 給一個任務發送消息
*
* Description: This function sends a message to a task //  描述:這個函數發送一個消息給一個任務
*
* Arguments  : p_tcb      is a pointer to the TCB of the task receiving a message.  If you specify a NULL pointer then
*                         the message will be posted to the task's queue of the calling task.  In other words, you'd be
*                         posting a message to yourself. 

//一個指針,指向接收消息的任務控制塊,如果設置爲NULL,你將給自己發送一個消息
*
*              p_void     is a pointer to the message to send.

//一個指針,指向要發送的消息
*
*              msg_size   is the size of the message sent (in #bytes)

//發送消息的大小
*
*              opt        specifies whether the post will be FIFO or LIFO://發送選項
*
*                             OS_OPT_POST_FIFO       Post at the end   of the queue  //first in first out 先進先出
*                             OS_OPT_POST_LIFO       Post at the front of the queue //first in last out 先進後出
*
*                             OS_OPT_POST_NO_SCHED   Do not run the scheduler after the post  //不進行任務調度
*
*                          Note(s): 1) OS_OPT_POST_NO_SCHED can be added with one of the other options.//可以添加其他選項
*
*
*              p_err      is a pointer to a variable that will hold the error code associated
*                         with the outcome of this call.  Errors can be:  //錯誤碼
*
*                             OS_ERR_NONE            The call was successful and the message was sent
*                             OS_ERR_Q_MAX           If the queue is full
*                             OS_ERR_MSG_POOL_EMPTY  If there are no more OS_MSGs available from the pool
*
* Returns    : none  //返回值
************************************************************************************************************************

接收消息API:

函數原型:

void  *OSTaskQPend (OS_TICK       timeout,
                    OS_OPT        opt,
                    OS_MSG_SIZE  *p_msg_size,
                    CPU_TS       *p_ts,
                    OS_ERR       *p_err)

 /*
************************************************************************************************************************
*                                                  WAIT FOR A MESSAGE   //接收一個消息
*
* Description: This function causes the current task to wait for a message to be posted to it.

//函數功能是讓當前的任務等待消息的發送
*
* Arguments  : timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will wait for a
*                            message to arrive up to the amount of time specified by this argument.
*                            If you specify 0, however, your task will wait forever or, until a message arrives.

//超時時間:     如果不是0,等待指定的時間(時間片)  如果是0,任務將會一直等待,直到接收到一個消息
*              opt           determines whether the user wants to block if the task's queue is empty or not:
*
*                                OS_OPT_PEND_BLOCKING
*                                OS_OPT_PEND_NON_BLOCKING
*
*              p_msg_size    is a pointer to a variable that will receive the size of the message

//接收到的消息大小的指針
*
*              p_ts          is a pointer to a variable that will receive the timestamp of when the message was
*                            received.  If you pass a NULL pointer (i.e. (CPU_TS *)0) then you will not get the
*                            timestamp.  In other words, passing a NULL pointer is valid and indicates that you don't
*                            need the timestamp.

//接收到消息的時間戳 如果是0,就不管時間戳
*
*              p_err         is a pointer to where an error message will be deposited.  Possible error
*                            messages are:
*//錯誤碼
*                                OS_ERR_NONE               The call was successful and your task received a message.
*                                OS_ERR_PEND_ABORT
*                                OS_ERR_PEND_ISR           If you called this function from an ISR and the result
*                                OS_ERR_PEND_WOULD_BLOCK   If you specified non-blocking but the queue was not empty
*                                OS_ERR_Q_EMPTY
*                                OS_ERR_SCHED_LOCKED       If the scheduler is locked
*                                OS_ERR_TIMEOUT            A message was not received within the specified timeout
*                                                          would lead to a suspension.
*
* Returns    : A pointer to the message received or a NULL pointer upon error.

*返回值:接收到消息的指針
*
* Note(s)    : 1) It is possible to receive NULL pointers when there are no errors.
************************************************************************************************************************
*/

使用示例:

p_voice=OSTaskQPend(   (OS_TICK        )0,//一直等待
                                                     (OS_OPT        )OS_OPT_PEND_BLOCKING,//選項
                                                     (OS_MSG_SIZE*    )&size,//保存接收到的消息的大小   size已定義
                                                     (CPU_TS*        )0,//不管時間戳
                                                     (OS_ERR*      )&err );//錯誤碼

//p_voice就是接收到的消息的首地址

 

                                OSTaskQPost( (OS_TCB                *)&BEEPTaskTCB,// 任務控制塊
                                                         (void                  *)&alarm_find_cards,//真正的消息(全局或靜態變量傳遞的是地址)
                                                         (OS_MSG_SIZE            )1,//消息大小
                                                         (OS_OPT                 )OS_OPT_POST_FIFO,//發送選項
                                                         (OS_ERR                *)err);//錯誤碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章