UCOS operating system - task embedded message queue (thirteen)

UCOS operating system

foreword

Like the task-embedded semaphore, each task of UCOSIII also has a built-in message queue. Moreover, the application of multiple tasks waiting for the same message queue is rare. If each task in UCOSIII has its own built-in message queue, the user can directly publish messages to the task without going through an external message queue.
If you need to use the built-in message queue function of the task, you need to set the macro OS_CFG_TASK_Q_EN to 1 to enable the related code.

1. Correlation function

insert image description here

2. How to use

1. Wait for task built-in message

The function OSTaskQPend() is used to request messages. This function allows tasks to directly receive messages sent from other tasks or ISRs
without going through an intermediate message queue. The function prototype is as follows:

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

timeout: The timeout for waiting for a message. If no message is received within the specified time, the task will be woken up and run. This parameter can also be set to 0, which means that the task will wait until a message is received.
opt: Used to choose whether to use blocking mode, there are two options to choose from.
OS_OPT_PEND_BLOCKING Blocks the task if no messages exist, waiting until a message is received.
OS_OPT_PEND_NON_BLOCKING If the message queue has no messages, the task returns directly.
p_msg_size: Point to the variable that holds the message size.
p_ts: Points to a timestamp indicating when the message was received. If this pointer is assigned NULL, the user did not ask for a timestamp.
p_err: used to save the error code returned after calling this function.
insert image description here

2. Send task built-in messages

The function OSTaskQPost() can send a message to a task through the built-in message queue of a task. Like the external message queue, a message is a pointer. The function prototype is as follows:

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

p_tcb: Points to the TCB of the task receiving the message. You can send a message to the caller of the function by specifying a NULL pointer or the TCB address of the caller of the function.
p_void: message sent to a task.
msg_size: Specifies the size (bytes) of the message sent. opt: Specify the type of sending operation, only one of LIFO and FIFO can be selected.
OS_OPT_POST_FIFO The message to be sent is stored at the end of the message queue
OS_OPT_POST_LIFO The message to be sent is stored at the beginning of the message queue The
above two options can be used with the following options.
OS_OPT_POST_NO_SCHED When this option is specified, no task scheduling will be done after sending, so the caller of this function can continue to run.
p_err: used to save the error code returned after calling this function
insert image description here

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/123937142