Challenge - 5 Problems
Queue ISR Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of xQueueSendFromISR with full queue
What will be the output of the following code snippet when the queue is already full?
FreeRTOS
#include "FreeRTOS.h" #include "queue.h" #include <stdio.h> QueueHandle_t queue; BaseType_t xHigherPriorityTaskWoken = pdFALSE; void ISR_Handler() { int value = 10; BaseType_t result = xQueueSendFromISR(queue, &value, &xHigherPriorityTaskWoken); if(result == pdPASS) { printf("Sent\n"); } else { printf("Failed\n"); } } int main() { queue = xQueueCreate(1, sizeof(int)); int initial = 5; xQueueSend(queue, &initial, 0); // Fill the queue ISR_Handler(); return 0; }
Attempts:
2 left
💡 Hint
Think about what happens when you try to send to a full queue from an ISR.
✗ Incorrect
xQueueSendFromISR returns errQUEUE_FULL if the queue is full and cannot accept new data. Since the queue size is 1 and already contains an item, the send fails and prints "Failed".
🧠 Conceptual
intermediateUnderstanding the xHigherPriorityTaskWoken parameter
What is the purpose of the xHigherPriorityTaskWoken parameter in xQueueSendFromISR?
Attempts:
2 left
💡 Hint
Consider what happens if sending to the queue unblocks a higher priority task.
✗ Incorrect
xHigherPriorityTaskWoken is set to pdTRUE by xQueueSendFromISR if sending data to the queue causes a higher priority task to unblock. This allows the ISR to request a context switch after the ISR completes.
🔧 Debug
advancedWhy does xQueueSendFromISR cause a runtime error?
Given this ISR code snippet, why does the system crash at runtime?
void ISR_Handler() {
int value = 20;
BaseType_t result = xQueueSendFromISR(NULL, &value, NULL);
if(result == pdPASS) {
// do something
}
}
Attempts:
2 left
💡 Hint
Check the parameters passed to xQueueSendFromISR carefully.
✗ Incorrect
Passing NULL as the queue handle causes the function to dereference a null pointer, leading to a runtime crash.
📝 Syntax
advancedIdentify the syntax error in xQueueSendFromISR usage
Which option contains a syntax error in the usage of xQueueSendFromISR?
FreeRTOS
QueueHandle_t queue = xQueueCreate(5, sizeof(int)); int data = 42; BaseType_t xHigherPriorityTaskWoken = pdFALSE;
Attempts:
2 left
💡 Hint
Check the type of the second argument expected by xQueueSendFromISR.
✗ Incorrect
The second argument must be a pointer to the data to send. Option D passes data by value, causing a syntax or type error.
🚀 Application
expertDetermining the number of items in the queue after ISR sends
Assume a queue of length 3 is initially empty. The following ISR is called three times in quick succession without the main task removing items:
void ISR_Handler() {
static int count = 0;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(queue, &count, &xHigherPriorityTaskWoken);
count++;
}
After these three ISR calls, how many items are in the queue?
Attempts:
2 left
💡 Hint
Consider if the queue can hold all three items without removal.
✗ Incorrect
The queue has space for 3 items and starts empty. Each ISR call sends one item successfully, so after three calls, the queue holds 3 items.
