Bird
Raised Fist0
FreeRTOSprogramming~10 mins

xQueueSend() and xQueueReceive() in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - xQueueSend() and xQueueReceive()
Prepare data to send
↓
Call xQueueSend()
↓
Is queue full?
Yes→Block or return error
No↓
Data added to queue
↓
Call xQueueReceive()
↓
Is queue empty?
Yes→Block or return error
No↓
Data removed from queue and returned
↓
Done
Data is sent to a queue with xQueueSend(), which adds it if space is available; xQueueReceive() removes data if available, otherwise blocks or returns error.
Execution Sample
FreeRTOS
QueueHandle_t q = xQueueCreate(2, sizeof(int));
int val = 10;
xQueueSend(q, &val, 0);
int received;
xQueueReceive(q, &received, 0);
Create a queue of 2 integers, send 10, then receive it back.
Execution Table
StepFunction CallQueue State BeforeCondition CheckedAction TakenQueue State AfterOutput/Return
1xQueueCreate(2, sizeof(int))EmptyN/ACreate queue with capacity 2Empty queue readyQueue handle returned
2xQueueSend(q, &val=10, 0)EmptyIs queue full? NoAdd 10 to queueQueue contains [10]pdPASS (success)
3xQueueSend(q, &val=20, 0)Queue contains [10]Is queue full? NoAdd 20 to queueQueue contains [10, 20]pdPASS (success)
4xQueueSend(q, &val=30, 0)Queue contains [10, 20]Is queue full? YesReturn error (no block)Queue unchanged [10, 20]errQUEUE_FULL
5xQueueReceive(q, &received, 0)Queue contains [10, 20]Is queue empty? NoRemove 10 from queueQueue contains [20]pdPASS, received=10
6xQueueReceive(q, &received, 0)Queue contains [20]Is queue empty? NoRemove 20 from queueEmpty queuepdPASS, received=20
7xQueueReceive(q, &received, 0)Empty queueIs queue empty? YesReturn error (no block)Empty queueerrQUEUE_EMPTY
💡 Execution stops after last receive returns empty queue error.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
Queue ContentEmpty[10][10, 20][10, 20][20]EmptyEmpty
Return ValueN/ApdPASSpdPASSerrQUEUE_FULLpdPASSpdPASSerrQUEUE_EMPTY
Received ValueN/AN/AN/AN/A1020N/A
Key Moments - 3 Insights
Why does xQueueSend() fail at step 4?
At step 4, the queue is full with 2 items (capacity reached), so xQueueSend() returns errQUEUE_FULL immediately without adding new data, as shown in execution_table row 4.
What happens when xQueueReceive() is called on an empty queue at step 7?
At step 7, the queue is empty, so xQueueReceive() returns errQUEUE_EMPTY immediately without blocking or returning data, as shown in execution_table row 7.
How does the queue content change after each send and receive?
Each xQueueSend() adds data to the end if space is available; each xQueueReceive() removes data from the front. This FIFO behavior is visible in variable_tracker under 'Queue Content'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the queue content after xQueueSend()?
A[10, 20]
B[20]
C[10]
DEmpty
💡 Hint
Check 'Queue State After' column in execution_table row 3.
At which step does xQueueSend() return errQUEUE_FULL?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Return Value' in variable_tracker and 'Output/Return' in execution_table.
If we increase queue size to 3, how would step 4 change?
AxQueueSend() would still fail with errQUEUE_FULL
BxQueueReceive() would fail instead
CxQueueSend() would succeed and queue would have 3 items
DQueue would be empty
💡 Hint
Step 4 fails because queue capacity is 2; increasing size to 3 allows one more item.
Concept Snapshot
xQueueSend(queue, &data, ticks) adds data to queue if space available.
xQueueReceive(queue, &data, ticks) removes data if available.
Both can block or return error if queue full/empty.
Queue is FIFO: first sent, first received.
Use return values to check success or failure.
Full Transcript
This visual trace shows how FreeRTOS queue functions xQueueSend() and xQueueReceive() work step-by-step. First, a queue of size 2 is created. Then, values 10 and 20 are sent successfully. A third send fails because the queue is full. Receiving removes items in order: first 10, then 20. A receive on empty queue returns error. The variable tracker shows queue content changes after each operation. Key moments explain why send fails when full and receive fails when empty. The quiz tests understanding of queue state and function results. This helps beginners see how data moves through a FreeRTOS queue.

Practice

(1/5)
1. What is the primary purpose of the xQueueSend() function in FreeRTOS?
easy
A. To remove an item from a queue
B. To add an item to a queue for other tasks to receive
C. To create a new queue
D. To delete an existing queue

Solution

  1. Step 1: Understand the role of xQueueSend()

    xQueueSend() is used to place data into a queue so that other tasks can retrieve it later.
  2. Step 2: Differentiate from other queue functions

    Functions like xQueueReceive() remove data, while xQueueCreate() and xQueueDelete() manage queue lifecycle, not data transfer.
  3. Final Answer:

    To add an item to a queue for other tasks to receive -> Option B
  4. Quick Check:

    xQueueSend() adds data [OK]
Hint: Remember: Send puts data in, Receive takes data out [OK]
Common Mistakes:
  • Confusing sending with receiving
  • Thinking it creates or deletes queues
  • Ignoring the function's data transfer role
2. Which of the following is the correct syntax to send data to a queue using xQueueSend()?
easy
A. xQueueSend(queueHandle, data, 0);
B. xQueueSend(&queueHandle, data, portMAX_DELAY);
C. xQueueSend(queueHandle, &data, portMAX_DELAY);
D. xQueueSend(queueHandle, &data);

Solution

  1. Step 1: Check parameter types for xQueueSend()

    The function expects the queue handle, a pointer to the data, and a timeout value.
  2. Step 2: Match the correct syntax

    xQueueSend(queueHandle, &data, portMAX_DELAY); correctly passes the queue handle, address of data, and a timeout constant portMAX_DELAY.
  3. Final Answer:

    xQueueSend(queueHandle, &data, portMAX_DELAY); -> Option C
  4. Quick Check:

    Queue handle + &data + timeout = correct syntax [OK]
Hint: Always pass data by address and include timeout [OK]
Common Mistakes:
  • Passing data by value instead of pointer
  • Omitting the timeout parameter
  • Passing queue handle by address
3. Consider the following code snippet:
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?
medium
A. 10
B. 0
C. No output (queue empty)
D. Compilation error

Solution

  1. Step 1: Analyze xQueueSend() call

    The value 10 is sent to the queue with zero wait time. If the queue is not full, it returns pdPASS.
  2. Step 2: Analyze xQueueReceive() call

    The code tries to receive from the queue immediately. Since the value was just sent, it will receive 10 and print it.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Send 10, receive 10, print 10 [OK]
Hint: Send then receive immediately prints sent value [OK]
Common Mistakes:
  • Assuming queue is empty at receive
  • Confusing return values with data
  • Ignoring zero timeout behavior
4. What is wrong with this code snippet?
int data = 5;
if (xQueueSend(queue, data, 10) != pdPASS) {
    // handle error
}
medium
A. Data should be passed by pointer, not by value
B. Timeout value 10 is invalid
C. Return value should not be compared to pdPASS
D. Queue handle is missing

Solution

  1. Step 1: Check parameter types in xQueueSend()

    The second parameter must be a pointer to the data, but here data is passed by value.
  2. Step 2: Identify the error cause

    Passing data by value causes a type mismatch and likely a compilation error or incorrect behavior.
  3. Final Answer:

    Data should be passed by pointer, not by value -> Option A
  4. Quick Check:

    Pass &data, not data [OK]
Hint: Always pass address of data to queue functions [OK]
Common Mistakes:
  • Passing data directly instead of pointer
  • Misunderstanding timeout parameter
  • Ignoring return value check
5. You want to send multiple integers to a queue and then receive them in order. Which approach correctly uses xQueueSend() and xQueueReceive() to achieve this without losing data?
hard
A. Use xQueueReceive() to send data and xQueueSend() to receive data
B. Send all integers at once by passing an array pointer to xQueueSend(), then receive with one xQueueReceive()
C. Send integers without checking return values, then receive once expecting all data
D. Send each integer with xQueueSend() using a timeout, then receive each with xQueueReceive() using a timeout, checking return values each time

Solution

  1. Step 1: Understand queue data handling

    Queues handle one item at a time, so each integer must be sent individually with xQueueSend().
  2. Step 2: Properly receive data

    Each item must be received individually with xQueueReceive(), checking return values to avoid errors or data loss.
  3. Step 3: Eliminate incorrect options

    Send all integers at once by passing an array pointer to xQueueSend(), then receive with one xQueueReceive() 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. Use xQueueReceive() to send data and xQueueSend() to receive data reverses function roles.
  4. Final Answer:

    Send each integer with xQueueSend() using a timeout, then receive each with xQueueReceive() using a timeout, checking return values each time -> Option D
  5. Quick Check:

    Send and receive items one by one, check returns [OK]
Hint: Send and receive items individually, always check returns [OK]
Common Mistakes:
  • Trying to send multiple items at once
  • Ignoring return values causing lost data
  • Mixing up send and receive functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard