The xQueueCreate() function is used to make a new queue in FreeRTOS. A queue helps tasks send and receive data safely.
xQueueCreate() function in FreeRTOS
QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize);
uxQueueLength is how many items the queue can hold.
uxItemSize is the size in bytes of each item stored in the queue.
QueueHandle_t myQueue = xQueueCreate(5, sizeof(int));
QueueHandle_t emptyQueue = xQueueCreate(0, sizeof(int));
QueueHandle_t singleItemQueue = xQueueCreate(1, sizeof(char));struct Data.QueueHandle_t largeQueue = xQueueCreate(10, sizeof(struct Data));This program creates a queue that can hold 3 Data items. It sends one item and then receives it back, printing messages to show success.
#include "FreeRTOS.h" #include "queue.h" #include "task.h" #include <stdio.h> // Simple struct to send via queue typedef struct { int id; char message[20]; } Data; int main(void) { // Create a queue to hold 3 Data items QueueHandle_t dataQueue = xQueueCreate(3, sizeof(Data)); if (dataQueue == NULL) { printf("Queue creation failed!\n"); return 1; } else { printf("Queue created successfully.\n"); } // Prepare a data item Data newData = {1, "Hello"}; // Send data to the queue if (xQueueSend(dataQueue, &newData, 0) == pdPASS) { printf("Data sent to queue: id=%d, message=%s\n", newData.id, newData.message); } else { printf("Failed to send data to queue.\n"); } // Receive data from the queue Data receivedData; if (xQueueReceive(dataQueue, &receivedData, 0) == pdPASS) { printf("Data received from queue: id=%d, message=%s\n", receivedData.id, receivedData.message); } else { printf("Failed to receive data from queue.\n"); } return 0; }
Time complexity of xQueueCreate() is generally O(1) because it just allocates memory and sets up the queue structure.
Space complexity depends on the queue length and item size: total memory = queue length x item size + overhead.
A common mistake is to create a queue with zero length or zero item size, which causes failure (returns NULL).
Use xQueueCreate() when you need a fixed-size queue for inter-task communication. For dynamic or more complex needs, consider other FreeRTOS mechanisms.
xQueueCreate() makes a new queue to hold data items safely between tasks.
You must specify how many items and the size of each item.
Check if the returned queue handle is NULL to know if creation succeeded.
Practice
xQueueCreate() function do in FreeRTOS?Solution
Step 1: Understand the purpose of xQueueCreate()
The function is designed to create a new queue in FreeRTOS for inter-task communication.Step 2: Differentiate from other queue functions
Functions like sending or receiving data operate on existing queues, not create them.Final Answer:
Creates a new queue to hold data items between tasks -> Option BQuick Check:
xQueueCreate() = create queue [OK]
- Confusing creation with sending or receiving data
- Thinking it deletes a queue
- Assuming it initializes queue data
Solution
Step 1: Recall xQueueCreate() parameters
The first parameter is the number of items, the second is the size of each item.Step 2: Match parameters to options
xQueueCreate(10, sizeof(int)); correctly uses 10 items and sizeof(int) as size per item.Final Answer:
xQueueCreate(10, sizeof(int)); -> Option AQuick Check:
Parameters = (number, size) [OK]
- Swapping the order of parameters
- Providing only one parameter
- Using raw numbers instead of sizeof()
QueueHandle_t q = xQueueCreate(5, sizeof(char));
if (q == NULL) {
printf("Queue creation failed\n");
} else {
printf("Queue created successfully\n");
}
What will be printed if the queue is created successfully?Solution
Step 1: Check the return value of xQueueCreate()
If queue creation succeeds, it returns a non-NULL handle.Step 2: Analyze the if condition
The code prints "Queue created successfully" when q is not NULL.Final Answer:
Queue created successfully -> Option CQuick Check:
q != NULL means success [OK]
- Assuming NULL means success
- Ignoring the if condition
- Confusing print messages
QueueHandle_t q = xQueueCreate(0, sizeof(int));
if (q == NULL) {
// handle error
}Solution
Step 1: Understand queue length parameter
The first parameter must be greater than zero to hold items.Step 2: Consequence of zero length
Passing zero causes xQueueCreate() to fail and return NULL.Final Answer:
Queue size cannot be zero, so creation fails -> Option AQuick Check:
Queue length > 0 required [OK]
- Using zero length queue
- Confusing size of item with queue length
- Ignoring return NULL check
SensorData. Which is the correct way to create this queue?Solution
Step 1: Identify queue length and item size
Queue length is 3, item size is size of one SensorData structure.Step 2: Match parameters to xQueueCreate()
First parameter is number of items (3), second is size of each item (sizeof(SensorData)).Final Answer:
xQueueCreate(3, sizeof(SensorData)); -> Option DQuick Check:
Length=3, size=sizeof(struct) [OK]
- Swapping length and size parameters
- Using pointer size instead of struct size
- Creating queue for one large combined item
Ready to prove it?
Pass the test and this pattern moves unknown
