📖 Scenario: You are working on a simple embedded system project using FreeRTOS. You need to create a queue to hold integer data that tasks will send and receive.
🎯 Goal: Learn how to create a queue using the xQueueCreate() function and verify its creation by checking the returned handle.
📋 What You'll Learn
Create a queue to hold 5 integers using xQueueCreate()
Store the queue handle in a variable named myQueue
Check if the queue was created successfully by verifying myQueue is not NULL
Print a message indicating success or failure
💡 Why This Matters
🌍 Real World
Queues are used in embedded systems to safely pass data between tasks without conflicts.
💼 Career
Understanding how to create and manage queues is essential for embedded software developers working with FreeRTOS or similar real-time operating systems.
Progress0 / 4 steps
1
Create a queue to hold 5 integers
Write a line of code to create a queue called myQueue that can hold 5 items, each of size sizeof(int), using the xQueueCreate() function.
FreeRTOS
Hint
Use xQueueCreate(5, sizeof(int)) and assign it to myQueue.
2
Add a check for successful queue creation
Add an if statement to check if myQueue is not NULL, indicating the queue was created successfully.
FreeRTOS
Hint
Use if (myQueue != NULL) to check the queue handle.
3
Add print statements for success or failure
Inside the if block, add a line to print "Queue created successfully.". Add an else block that prints "Failed to create queue.".
FreeRTOS
Hint
Use printf() inside both if and else blocks.
4
Run the program and check the output
Run the program and observe the printed output. It should display Queue created successfully. if the queue was created.
FreeRTOS
Hint
If you see Queue created successfully., your queue was created correctly.
Practice
(1/5)
1. What does the xQueueCreate() function do in FreeRTOS?
easy
A. Sends data to a queue
B. Creates a new queue to hold data items between tasks
C. Deletes an existing queue
D. Receives data from a queue
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 B
Quick Check:
xQueueCreate() = create queue [OK]
Hint: xQueueCreate() always makes a new queue [OK]
Common Mistakes:
Confusing creation with sending or receiving data
Thinking it deletes a queue
Assuming it initializes queue data
2. Which of the following is the correct syntax to create a queue for 10 items, each of size sizeof(int)?
easy
A. xQueueCreate(10, sizeof(int));
B. xQueueCreate(sizeof(int), 10);
C. xQueueCreate(10);
D. xQueueCreate(sizeof(int));
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 A
Quick Check:
Parameters = (number, size) [OK]
Hint: First param = count, second = size [OK]
Common Mistakes:
Swapping the order of parameters
Providing only one parameter
Using raw numbers instead of sizeof()
3. Consider this code snippet:
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?
medium
A. Queue creation failed
B. Compilation error
C. Queue created successfully
D. No output
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 C
Quick Check:
q != NULL means success [OK]
Hint: Check if queue handle is NULL for failure [OK]