Communication between UCOSIII tasks

Publisher:Xiaohan521Latest update time:2019-04-25 Source: eefocusKeywords:UCOSIII Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. UCOSIII inter-task communication

       1.1. Communication between tasks

Sometimes a task or an interrupt service routine needs to exchange information with another task. This is the process of message passing, which is called inter-task communication. There are two ways to pass messages between tasks: one is through global variables, and the other is by publishing messages.


       When using global variables, each task or interrupt service routine must ensure exclusive access to the global variables. Messages can also be published to tasks through message queues as intermediaries.


       1.2 What is a message?

       A message consists of the following parts: a pointer to the data, the length of the data, and a timestamp that records the time when the message was published. The pointer can point to a data area or even a function. The content of the message must always remain visible. Visibility means that the variable representing the message must be valid within the scope of the task code that receives the message. This is because the published data is passed by pointer, that is, by reference, not by value. In other words, the published message itself does not generate a copy. We can use dynamic memory allocation to allocate a memory block to the message, or we can pass a pointer to a global variable, global data structure, global array, or function.


2.UCOSIII message queue

2.1 Operation of message queue:

The interrupt service program can only use the OSQPost() function. In UCOSIII, the message queue is read in a first-in-first-out (FIFO) or last-in-first-out (LIFO) manner. Messages posted in a last-in-first-out (LIFO) manner will bypass all other messages in the message queue and be passed to the task first.


       The small hourglass indicates that the task has a specified timeout. If the task does not receive a message within this time, it will wake up the task and return an error code to tell UCOIII that the task was woken up due to a timeout rather than receiving a message. If the timeout is set to 0, the task will wait until it receives a message.


There is a list in the message queue, which records all tasks waiting to receive messages. When a message is published to the queue, the highest priority task receives the message. The publisher can also broadcast a message to all waiting tasks in the message queue.


2.2 The message queue is a kernel object in UCOSIII, which is a structure OS_Q.

struct os_q {

OS_OBJ_TYPE Type;

CPU_CHAR *NamePtr;

OS_PEND_LIST PendList;                         

#if OS_CFG_DBG_EN > 0u

    OS_Q *DbgPrevPtr;

    OS_Q *DbgNextPtr;

    CPU_CHAR *DbgNamePtr;

#endif

OS_MSG_Q MsgQ //Message list

};

OS_MSG_Q is also a structure, as follows:


struct os_msg_q {                                          

OS_MSG *InPtr;  

OS_MSG *OutPtr;

OS_MSG_QTY NbrEntriesSize;

OS_MSG_QTY NbrEntries;

OS_MSG_QTY NbrEntriesMax;

};

After the message is published, it will be stored in a data structure of type OS_MSG. OS_MSG is as follows:


struct os_msg {

OS_MSG *NextPtr;                           

void *MsgPtr;

OS_MSG_SIZE MsgSize;

CPU_TS MsgTS;

};

2.3 Message Queue API Functions


2.3.1 Create a message queue

void OSQCreate (OS_Q *p_q, //point to a message queue, use OS_Q Msg_Que to define a message queue

                 CPU_CHAR *p_name, //The name of the message queue

                 OS_MSG_QTY max_qty, //The length of the message queue must be greater than 0

                 OS_ERR *p_err) //Save error code

2.3.2 Waiting for the message queue

void *OSQPend (OS_Q *p_q, //points to a message queue

                OS_TICK timeout, //Timeout for waiting for message

                OS_OPT opt, //Choose whether to use blocking mode, there are 2 options

                OS_MSG_SIZE *p_msg_size, // points to a variable used to indicate the length of the received message

                CPU_TS *p_ts, // points to a timestamp indicating when the message was received

                OS_ERR *p_err)

2.3.3 Sending messages to the message queue

void OSQPost (OS_Q *p_q, //points to a message queue

               void *p_void, // points to the actual content sent

               OS_MSG_SIZE msg_size, //Set the message size, number of bytes

               OS_OPT opt, //Select the type of message sending operation, there are four basic types and they can also be combined

               OS_ERR *p_err)

3. UCOSIII task built-in message queue

Like task-embedded semaphores, each task in UCOSIII also has a built-in message queue. It is rare for multiple tasks to wait for the same message queue. If each task in UCOSIII has its own built-in message queue, users 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.


The function of the task's built-in message queue is in os_task.c

3.1 Waiting for task built-in message

void *OSTaskQPend (OS_TICK timeout, //Timeout for waiting for message

                    OS_OPT opt, //Whether to use blocking mode

                    OS_MSG_SIZE *p_msg_size, // variable to store message size

                    CPU_TS *p_ts, //timestamp

                    OS_ERR *p_err)

3.2 Sending task built-in messages

void OSTaskQPost (OS_TCB *p_tcb, //points to the TCB of the task receiving the message

                   void *p_void, //Message sent to a task

                   OS_MSG_SIZE msg_size, //Message size

                   OS_OPT opt, //Send operation type: LIFO and FIFO

                   OS_ERR *p_err)

 


Keywords:UCOSIII Reference address:Communication between UCOSIII tasks

Previous article:UCOSIII event flag groups and simultaneous waiting for multiple kernel objects
Next article:UCOSIII interrupt and time management

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号