0
0
FreeRTOSprogramming~20 mins

vTaskDelay() for periodic tasks in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Periodic Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding vTaskDelay() behavior

What will be the output timing behavior of the following FreeRTOS task code snippet?

void vTaskFunction(void *pvParameters) {
    for (;;) {
        // Task code here
        vTaskDelay(pdMS_TO_TICKS(1000));
        // Print or toggle an LED
    }
}

Assuming the tick rate is 1ms, how often does the task unblock?

FreeRTOS
void vTaskFunction(void *pvParameters) {
    for (;;) {
        vTaskDelay(pdMS_TO_TICKS(1000));
        // action
    }
}
AThe task unblocks every 1000 milliseconds (1 second) after the delay.
BThe task unblocks immediately without any delay.
CThe task unblocks every 1 millisecond.
DThe task never unblocks because vTaskDelay blocks indefinitely.
Attempts:
2 left
💡 Hint

Remember that vTaskDelay() delays the task for the specified tick count.

Predict Output
intermediate
2:00remaining
Effect of vTaskDelay on task periodicity

Consider a FreeRTOS task that performs some work taking 200ms and then calls vTaskDelay(pdMS_TO_TICKS(1000)). How often will the task run?

FreeRTOS
void vTaskFunction(void *pvParameters) {
    for (;;) {
        // Work taking 200ms
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
AThe task runs every 1200 milliseconds (200ms work + 1000ms delay).
BThe task runs every 1000 milliseconds exactly.
CThe task runs every 200 milliseconds.
DThe task runs continuously without delay.
Attempts:
2 left
💡 Hint

Think about how vTaskDelay() affects the task after the work is done.

🔧 Debug
advanced
2:30remaining
Why does the task drift in timing?

A developer writes this FreeRTOS task to run every 1000ms:

void vTaskFunction(void *pvParameters) {
    for (;;) {
        vTaskDelay(pdMS_TO_TICKS(1000));
        // Do work
    }
}

But the task execution drifts over time, running later and later. Why?

ABecause vTaskDelay blocks the task forever.
BBecause the tick rate is too fast causing timing errors.
CBecause vTaskDelay delays from the time it is called, so work time adds to the period causing drift.
DBecause the task priority is too low to run on time.
Attempts:
2 left
💡 Hint

Consider when the delay starts counting relative to the work done.

🚀 Application
advanced
3:00remaining
Implementing a fixed periodic task with vTaskDelayUntil

Which code snippet correctly implements a task that runs exactly every 1000ms without drift?

A
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
    vTaskDelay(pdMS_TO_TICKS(1000));
    // work
}
B
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
    vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(1000));
    // work
}
C
for (;;) {
    vTaskDelayUntil(NULL, pdMS_TO_TICKS(1000));
    // work
}
D
for (;;) {
    vTaskDelay(1000);
    // work
}
Attempts:
2 left
💡 Hint

Use the function designed for fixed periodic delays.

🧠 Conceptual
expert
3:00remaining
Why is vTaskDelay not ideal for precise periodic tasks?

Why might vTaskDelay() be unsuitable for tasks that require precise periodic execution?

ABecause it disables interrupts during the delay period.
BBecause it blocks the task indefinitely without timeout.
CBecause it requires the task to have the highest priority to work.
DBecause it delays relative to when it is called, causing cumulative timing errors if task execution time varies.
Attempts:
2 left
💡 Hint

Think about how the delay start time relates to task execution time.