Preface
link
- LiteOS Source link
- common problem
- Huawei developer community
- Huawei LiteOS The official tutorial
- my gitee-LiteOS-mcu
Reference resources
- Wildfire
- The link above
Draft notes
- I'm a little busy with my work recently , Far away? Reading delayed. Ha ha
Basic concepts
-
queue also called Message queue
- It is a data structure commonly used for inter task communication
- Ability to receive messages of variable length from tasks or interrupts
- According to API Choose whether the delivery message is stored in your own space
- That is The data transfer still Address
- The task can read messages from the queue
- When the message in the queue is empty
- Pending read task
- When there are new messages in the queue
- The pending read task is awakened
- And deal with new news .
- When the message in the queue is empty
-
LiteOS Queue properties
- Messages are queued first in, first out (FIFO), Support asynchronous read-write mode
- Both read and write queues support timeout mechanisms
- The type of message to be sent shall be agreed by both parties , Different lengths can be allowed ( No more than the maximum number of queue nodes ) news
- A task can receive and send messages from any message queue
- Multiple tasks can receive and send messages from the same message queue
- When the queue is finished , If it is dynamically applied memory , You need to recycle by freeing memory functions .
Queue operation mechanism
- Queue control block
usReadWriteableCnt
: The number of messages that can be read or writtenstReadWriteList
: Read or write message task wait listusQueueState
: Two kinds of state- OS_QUEUE_UNUSED : Not used
- OS_QUEUE_INUSED : Used
/**
* @ingroup los_queue
* Queue information block structure
*/
typedef struct tagQueueCB
{
UINT8 *pucQueue; /**< Pointer to a queue handle */
UINT16 usQueueState; /**< Queue state */
UINT16 usQueueLen; /**< Queue length */
UINT16 usQueueSize; /**< Node size */
UINT16 usQueueID; /**< usQueueID */
UINT16 usQueueHead; /**< Node head */
UINT16 usQueueTail; /**< Node tail */
UINT16 usReadWriteableCnt[2]; /**< Count of readable or writable resources, 0:readable, 1:writable */
LOS_DL_LIST stReadWriteList[2]; /**< Pointer to the linked list to be read or written, 0:readlist, 1:writelist */
LOS_DL_LIST stMemList; /**< Pointer to the memory linked list */
} QUEUE_CB_S;
/* queue state */
/**
* @ingroup los_queue
* Message queue state: not in use.
*/
#define OS_QUEUE_UNUSED 0
/**
* @ingroup los_queue
* Message queue state: used.
*/
#define OS_QUEUE_INUSED 1
How queues work
-
Create a queue
- Pass in The queue length and Message node size
- Open up the corresponding memory space
- return queue ID.
-
In line Header node Head and Message tail node Tail
- Head Indicates the starting position of the occupied message in the queue
- Tail Indicates the starting position of the idle message in the queue
- Both of them adopt the way of rewinding
-
usReadWriteableCnt[0]
Determine whether it is readable -
usReadWriteableCnt[1]
Determine whether it is writable -
When deleting a queue
- According to the incoming queue ID Find the corresponding queue
- Set the queue state to unused
- Free up the space occupied by the original queue
- The corresponding queue control header is set to the initial state .
-
The illustration :
Message queue transmission mode
-
LiteOS There are two ways to transmit messages
- Address mode
- advantage : Efficient
- shortcoming : Modify source file , The corresponding content of the message will also be modified
- Copy mode
- advantage : Data security , It's copying content directly into the message
- shortcoming : Efficiency is relative Address It's lower
- Address mode
-
Users can choose different ways according to their needs
- Main reference data size And Data importance To measure
Blocking mechanism of message queue
- Interrupt with blocking mechanism is not allowed API, So the interruption , The blocking values are taken as 0
- LiteOS The blocking mechanism has been implemented , Users can use it directly
Out of line jam
- Three ways : It mainly refers to whether there are messages in the queue
- Don't wait for :0
- When reading a message , Inside the queue
- News , Normal read
- No news , You don't wait , Directly execute the following code
- When reading a message , Inside the queue
- Wait for a limited time :
- Within the prescribed time , In the message queue
- News , Normal read ,
- No news , Is waiting for
- Overtime
- Then directly execute the following code
- Within the prescribed time , In the message queue
- Wait forever :LOS_WAIT_FOREVER
- When reading a message
- News , Normal read
- No news , Have been waiting for , Until there's a message in the queue
- When reading a message
- Don't wait for :0
Blocking in the queue
- Three ways : It mainly refers to whether the messages in the queue are full
- Don't wait for :0
- When writing a message
- The queue is under , The normal writing
- The queue is full , You don't wait , Directly execute the following code
- When writing a message
- Wait for a limited time :
- When writing a message
- The queue is under , The normal writing
- The queue is full , Wait for a limited time , Wait until the queue is full and write
- Overtime
- Then directly execute the following code
- When writing a message
- Wait forever :LOS_WAIT_FOREVER
- When writing a message
- The queue is under , The normal writing
- The queue is full , Have been waiting for , Until the queue is not full
- When writing a message
- Don't wait for :0
Task related function
The interface name | describe |
---|---|
LOS_QueueCreate | Create a message |
LOS_QueueRead | Read the data in the specified queue .(buff It contains the address of the queue node ) |
LOS_QueueWrite | Write data to the specified queue .( What is written to the queue node is buff The address of ) |
LOS_QueueReadCopy | Read the data in the specified queue .(buff It stores the data in the queue node )( Reserved interface ) |
LOS_QueueWriteCopy | Write data to the specified queue .( What is written to the queue node is buff Data in )( Reserved interface ) |
LOS_QueueWriteHead | Write data to the head of the specified queue |
LOS_QueueDelete | Delete a specified queue |
LOS_QueueInfoGet | Get the specified queue information |
The use of each function can see the source code or routine
Task development process
- Not copy The way ( copy In the same way )
- Create a message queue LOS_QueueCreate.
- Once created , You can get the message queue ID value .
- Write queue operation function LOS_QueueWrite.
- Read queue operation function LOS_QueueRead.
- Get queue information function LOS_QueueInfoGet.
- To delete a queue LOS_QueueDelete.
matters needing attention *
- The number of queue resources that can be configured by the system
- Refer to The total number of queue resources of the whole system
- Not the number of users can use .
- for example :
- The system software timer takes up more than one queue resource
- Then the queue resources that the system can configure will be reduced by one .
- call LOS_QueueCreate The queue name passed in by function is not used temporarily , As a future reserved parameter .
- The input parameter in the queue interface function uwTimeOut It means relative time .
- LOS_QueueReadCopy and LOS_QueueWriteCopy It's a set of interfaces
- LOS_QueueRead and LOS_QueueWrite It's a set of interfaces , Two sets of interfaces need to be used together .
- Whereas LOS_QueueWrite and LOS_QueueRead The actual operation of this group of interfaces is data address , The user must make sure to call LOS_QueueRead The obtained pointer points to the memory area during the read queue Not modified or released by exception , Otherwise, it may lead to unpredictable consequences .
- When using address mode , It must be ensured that the content of the address has not been modified , Local variables are not recommended
- When using address mode , It must be ensured that the content of the address has not been modified , Local variables are not recommended
- When using address mode , It must be ensured that the content of the address has not been modified , Local variables are not recommended