当前位置:网站首页>Liteos message queuing

Liteos message queuing

2020-11-08 16:10:00 Li Zhuming


Preface

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 .
  • 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 written
    • stReadWriteList : Read or write message task wait list
    • usQueueState : 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

    1. Address mode
      1. advantage : Efficient
      2. shortcoming : Modify source file , The corresponding content of the message will also be modified
    2. Copy mode
      1. advantage : Data security , It's copying content directly into the message
      2. shortcoming : Efficiency is relative Address It's lower
  • 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
    • 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
    • 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

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
    • 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
    • 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
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 )
    1. Create a message queue LOS_QueueCreate.
    2. Once created , You can get the message queue ID value .
    3. Write queue operation function LOS_QueueWrite.
    4. Read queue operation function LOS_QueueRead.
    5. Get queue information function LOS_QueueInfoGet.
    6. To delete a queue LOS_QueueDelete.

matters needing attention *

  1. 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 .
  2. call LOS_QueueCreate The queue name passed in by function is not used temporarily , As a future reserved parameter .
  3. The input parameter in the queue interface function uwTimeOut It means relative time .
  4. LOS_QueueReadCopy and LOS_QueueWriteCopy It's a set of interfaces
  5. LOS_QueueRead and LOS_QueueWrite It's a set of interfaces , Two sets of interfaces need to be used together .
  6. 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

actual combat

Actual address

版权声明
本文为[Li Zhuming]所创,转载请带上原文链接,感谢