FreeRTOS(queue)

queue

What is a queue?

Queue, also known as message queue, is a data structure commonly used for communication between tasks. Queue can transfer information between tasks, interrupts and tasks.

Why not use global variables? If global variables are used, the rabbit (task 1) modifies the variable a and waits for the sloth (task 3) to process it, but the sloth processing speed is very slow. During the process of processing the data, the fox (task 2) may modify the variable again. a, causing Sloth to possibly get incorrect data. In this case, queues can be used. The data generated by rabbits and foxes are placed on the assembly line, and the sloth can be processed one by one slowly.

Queue, also known as message queue, is a data structure commonly used for communication between tasks. Queue can transfer information between tasks, interrupts and tasks.

A few terms about queues:

Queue item: every data in the queue;

Queue length: The maximum number of queue items that the queue can store;

When creating a queue, you need to specify the queue length and queue item size.

Queue characteristics

1. Data enqueue and dequeue method

Usually a first-in-first-out (FIFO) data storage buffer mechanism is used, that is, the data that is queued first will be read from the queue first. It can also be configured in last-in-first-out (LIFO) mode, but it is less commonly used.

2. Data transmission method

Actual value transfer is used, that is, the data is copied to the queue for transfer. Pointers can also be passed. Pointer transfer is used when transferring larger data.

3. Multi-tasking access

The queue does not belong to a certain task. Any task and interrupt can send/read messages to the queue.

4. Dequeue and enqueue blocking

When a task sends a message to a queue, you can specify a blocking time, assuming that the queue is full and cannot be added to the queue. If the blocking time is set to:

  • 0: Return directly without waiting;
  • 0~port_MAX_DELAY: Wait for the set blocking time. If it cannot join the queue within this time, it will return directly after timeout and no longer wait;
  • port_MAX_DELAY: Wait until you can join the queue. Dequeuing blocking is similar to enqueuing blocking;

Queue related API functions

1. Create a queue

QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength, UBaseType_t uxItemSize );

parameter:

  • uxQueueLength: The maximum number of items that the queue can hold simultaneously.
  • uxItemSize: The size in bytes required to store each data item in the queue.
  • Return value: If the queue is created successfully, the handle of the created queue is returned. If the memory required to create the queue cannot be allocated, NULL is returned.

2. Write queue

The write queue has the following functions in total:

 BaseType_t xQueueSend( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait );

parameter:

  • xQueue: Handle of the queue to which data items will be sent.
  • pvItemToQueue: data to be written
  • xTicksToWait: blocking timeout
  • Return value: If the data is successfully written, pdTRUE is returned, otherwise errQUEUE_FULL is returned.

3. Read queue

The read queue has the following functions:

BaseType_t xQueueReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait );

parameter:

  • xQueue: Queue to be read
  • pvItemToQueue: data reading buffer
  • xTicksToWait: blocking timeout
  • Return value: pdTRUE is returned successfully, otherwise pdFALSE is returned.

 Practical operation

Experimental requirements: Create a queue, press KEY1 to send data to the queue, and press KEY2 to read data from the queue.

cubeMX configuration

 

 

 

Guess you like

Origin blog.csdn.net/m0_70987863/article/details/131691500