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)
Previous article:UCOSIII event flag groups and simultaneous waiting for multiple kernel objects
Next article:UCOSIII interrupt and time management
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Can the silkscreen on the board be deleted if there is insufficient space? What are the key files required for patch?
- [Mill MYB-YT507 development board trial experience] tkinterLabel learning
- [Automatic clock-in walking timing system based on face recognition] Node communication - LoRa technology
- TI: Development platform compatible with multiple wireless communication protocols
- What is this error?
- I want to use CD4051 as the RX expansion of USART serial port, but how to calculate the baud rate that CD4051 can meet...
- TI analog temperature sensor lmt88
- A 39-year-old single programmer moves into a nursing home? The happiness of a programmer is beyond your imagination!
- Recruiting part-time MCU engineers (Dalian area)
- Import HFSS devices into AD to generate PCB files