These functions help tasks send and receive data safely between each other in FreeRTOS. They make sure data is passed without conflicts.
xQueueSend() and xQueueReceive() in FreeRTOS
/* Queue handle is created elsewhere */
BaseType_t xQueueSend(QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait);
BaseType_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait);xQueueSend() adds an item to the back of the queue.
xQueueReceive() removes an item from the front of the queue.
/* Sending an integer to the queue, wait up to 10 ticks if full */ int valueToSend = 42; xQueueSend(myQueue, &valueToSend, 10);
/* Receiving an integer from the queue, wait forever if empty */ int receivedValue; xQueueReceive(myQueue, &receivedValue, portMAX_DELAY);
/* What if queue is empty? xQueueReceive returns pdFALSE immediately if xTicksToWait is 0 */ int data; if(xQueueReceive(myQueue, &data, 0) == pdFALSE) { // No data available }
/* What if queue is full? xQueueSend returns errQUEUE_FULL if no space after waiting */ int newData = 100; if(xQueueSend(myQueue, &newData, 0) == errQUEUE_FULL) { // Queue is full, data not sent }
This program creates a queue that holds one integer. The sender task sends the number 10 to the queue. The receiver task waits and receives that number, printing it out. The output shows the order of sending and receiving.
#include "FreeRTOS.h" #include "queue.h" #include "task.h" #include <stdio.h> QueueHandle_t myQueue; void senderTask(void *params) { int valueToSend = 10; printf("Sender: Sending %d to queue\n", valueToSend); if(xQueueSend(myQueue, &valueToSend, pdMS_TO_TICKS(100)) == pdPASS) { printf("Sender: Sent successfully\n"); } else { printf("Sender: Failed to send\n"); } vTaskDelete(NULL); } void receiverTask(void *params) { int receivedValue = 0; printf("Receiver: Waiting to receive from queue\n"); if(xQueueReceive(myQueue, &receivedValue, portMAX_DELAY) == pdPASS) { printf("Receiver: Received %d from queue\n", receivedValue); } else { printf("Receiver: Failed to receive\n"); } vTaskDelete(NULL); } int main(void) { myQueue = xQueueCreate(1, sizeof(int)); if(myQueue == NULL) { printf("Failed to create queue\n"); return 1; } printf("Queue created\n"); xTaskCreate(senderTask, "Sender", 1000, NULL, 1, NULL); xTaskCreate(receiverTask, "Receiver", 1000, NULL, 1, NULL); vTaskStartScheduler(); // Should never reach here printf("Scheduler ended\n"); return 0; }
Time complexity: Both xQueueSend() and xQueueReceive() run in constant time O(1).
Space complexity: Depends on queue length and item size; each item uses memory in the queue buffer.
Common mistake: Not checking the return value to know if send or receive succeeded.
Use xQueueSend() when you want to add data safely to a queue; use xQueueReceive() when you want to get data from the queue, optionally waiting if empty.
xQueueSend() puts data into a queue safely for other tasks to get.
xQueueReceive() gets data from the queue, waiting if needed.
Always check return values to handle full or empty queues properly.
Practice
xQueueSend() function in FreeRTOS?Solution
Step 1: Understand the role of
xQueueSend()xQueueSend()is used to place data into a queue so that other tasks can retrieve it later.Step 2: Differentiate from other queue functions
Functions likexQueueReceive()remove data, whilexQueueCreate()andxQueueDelete()manage queue lifecycle, not data transfer.Final Answer:
To add an item to a queue for other tasks to receive -> Option BQuick Check:
xQueueSend()adds data [OK]
- Confusing sending with receiving
- Thinking it creates or deletes queues
- Ignoring the function's data transfer role
xQueueSend()?Solution
Step 1: Check parameter types for
The function expects the queue handle, a pointer to the data, and a timeout value.xQueueSend()Step 2: Match the correct syntax
xQueueSend(queueHandle, &data, portMAX_DELAY); correctly passes the queue handle, address of data, and a timeout constantportMAX_DELAY.Final Answer:
xQueueSend(queueHandle, &data, portMAX_DELAY); -> Option CQuick Check:
Queue handle + &data + timeout = correct syntax [OK]
- Passing data by value instead of pointer
- Omitting the timeout parameter
- Passing queue handle by address
int value = 10;
if (xQueueSend(queue, &value, 0) == pdPASS) {
int receivedValue = 0;
if (xQueueReceive(queue, &receivedValue, 0) == pdPASS) {
printf("%d", receivedValue);
}
}What will be printed when this code runs?
Solution
Step 1: Analyze
The value 10 is sent to the queue with zero wait time. If the queue is not full, it returnsxQueueSend()callpdPASS.Step 2: Analyze
The code tries to receive from the queue immediately. Since the value was just sent, it will receive 10 and print it.xQueueReceive()callFinal Answer:
10 -> Option AQuick Check:
Send 10, receive 10, print 10 [OK]
- Assuming queue is empty at receive
- Confusing return values with data
- Ignoring zero timeout behavior
int data = 5;
if (xQueueSend(queue, data, 10) != pdPASS) {
// handle error
}Solution
Step 1: Check parameter types in
The second parameter must be a pointer to the data, but here data is passed by value.xQueueSend()Step 2: Identify the error cause
Passing data by value causes a type mismatch and likely a compilation error or incorrect behavior.Final Answer:
Data should be passed by pointer, not by value -> Option AQuick Check:
Pass &data, not data [OK]
- Passing data directly instead of pointer
- Misunderstanding timeout parameter
- Ignoring return value check
xQueueSend() and xQueueReceive() to achieve this without losing data?Solution
Step 1: Understand queue data handling
Queues handle one item at a time, so each integer must be sent individually withxQueueSend().Step 2: Properly receive data
Each item must be received individually withxQueueReceive(), checking return values to avoid errors or data loss.Step 3: Eliminate incorrect options
Send all integers at once by passing an array pointer toxQueueSend(), then receive with onexQueueReceive()is wrong because queues do not send arrays as single items. Send integers without checking return values, then receive once expecting all data risks data loss by ignoring return values. UsexQueueReceive()to send data andxQueueSend()to receive data reverses function roles.Final Answer:
Send each integer withxQueueSend()using a timeout, then receive each withxQueueReceive()using a timeout, checking return values each time -> Option DQuick Check:
Send and receive items one by one, check returns [OK]
- Trying to send multiple items at once
- Ignoring return values causing lost data
- Mixing up send and receive functions
Ready to prove it?
Pass the test and this pattern moves unknown
