#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);//错误码