任务消息队列(API - Task Message Queues)
属于任务当中的一块,不需要单独创建。实现多对一的消息队列的传输。
OSTaskQPost
Description
OSTaskQPost() sends a message to a task through its local message queue. A message is a
pointer-sized variable, and its use is application specific. If the task’s message queue is full, an
error code is returned to the caller. In this case, OSTaskQPost() immediately returns to its caller,
and the message is not placed in the message queue.
If the task receiving the message is waiting for a message to arrive, it will be made
ready-to-run. If the receiving task has a higher priority than the task sending the message, the
higher-priority task resumes, and the task sending the message is suspended; that is, a context
switch occurs. A message can be posted as first-in first-out (FIFO), or last-in-first-out (LIFO),
depending on the value specified in the opt argument. In either case, scheduling occurs unless
opt is set to OS_OPT_POST_NO_SCHED.
Files
os.h/os_task.c
Prototype
void OSTaskQPost (OS_TCB *p_tcb,
void *p_void,
OS_MSG_SIZE msg_size,
OS_OPT opt,
OS_ERR *p_err)
Arguments
p_tcb
is a pointer to the TCB of the task. Note that it is possible to post a message to the calling
task (i.e., self) by specifying a NULL pointer, or the address of its TCB.
p_void
is the actual message sent to the task. p_void is a pointer-sized variable and its meaning is
application specific.
msg_size
specifies the size of the message posted (in number of bytes).
opt
determines the type of POST performed. Of course, it does not make sense to post LIFO
and FIFO simultaneously, so these options are exclusive:
OS_OPT_POST_FIFO
POST message to task and place at the end of the queue if the task is not waiting for
messages.
OS_OPT_POST_LIFO
POST message to task and place at the front of the queue if the task is not waiting
for messages.
OS_OPT_POST_NO_SCHED
This option prevents calling the scheduler after the post and therefore the caller is
resumed.
You should use this option if the task (or ISR) calling OSTaskQPost() will be doing
additional posts, the user does not want to reschedule until all done, and multiple
posts are to take effect simultaneously.
p_err
is a pointer to a variable that will contain an error code returned by this function.
OS_ERR_NONE
If the call was successful and the message was posted to the task’s message queue.
OS_ERR_INT_Q_FULL
If OS_CFG_ISR_POST_DEFERRED_EN is to DEF_ENABLED in os_cfg.h: If the deferred
interrupt post queue is full.
OS_ERR_MSG_POOL_EMPTY
If running out of OS_MSG to hold the message being posted.
OS_ERR_OPT_INVALID
If OS_CFG_ARG_CHK_EN is set to DEF_ENABLED in os_cfg.h: if a valid option is not
specified.
OS_ERR_OS_NOT_RUNNING
If OS_CFG_INVALID_OS_CALLS_CHK_EN is set to DEF_ENABLED in os_cfg.h: if µC/OS-III
is not running yet.
Required Configuration
OS_CFG_TASK_Q_EN must be enabled in os_cfg.h. Refer to µC-OS-III Configuration Manual.
OSTaskQPend
Description
OSTaskQPend() allows a task to receive messages directly from an ISR or another task, without
going through an intermediate message queue. In fact, each task has a built-in message queue
if the configuration constant OS_CFG_TASK_Q_EN is set to DEF_ENABLED. The messages received
are pointer-sized variables, and their use is application specific. If at least one message is
already present in the message queue when OSTaskQPend() is called, the message is retrieved
and returned to the caller.
If no message is present in the task’s message queue and OS_OPT_PEND_BLOCKING is specified for
the opt argument, OSTaskQPend() suspends the current task (assuming the scheduler is not
locked) until either a message is received, or a user-specified timeout expires. A pended task
that is suspended with OSTaskSuspend() can receive messages. However, the task remains
suspended until it is resumed by calling OSTaskResume().
If no message is present in the task’s message queue and OS_OPT_PEND_NON_BLOCKING is
specified for the opt argument, OSTaskQPend() returns to the caller with an appropriate error
code and returns a NULL pointer.
Files
os.h/os_task.c
Prototype
void *OSTaskQPend (OS_TICK timeout,
OS_OPT opt,
OS_MSG_SIZE *p_msg_size,
CPU_TS *p_ts,
OS_ERR *p_err)