0
0
FreeRTOSprogramming~20 mins

vTaskDelayUntil() for precise timing in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
vTaskDelayUntil Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS task timing code?

Consider a FreeRTOS task using vTaskDelayUntil() to run every 100 ticks. What will be the output timestamps printed by the task?

FreeRTOS
TickType_t xLastWakeTime;

void vTaskFunction(void *pvParameters) {
    xLastWakeTime = xTaskGetTickCount();
    for (int i = 0; i < 3; i++) {
        vTaskDelayUntil(&xLastWakeTime, 100);
        printf("Tick: %lu\n", xTaskGetTickCount());
    }
}
ATick: 101\nTick: 201\nTick: 301
BTick: 100\nTick: 200\nTick: 300
CTick: 100\nTick: 201\nTick: 302
DTick: 0\nTick: 100\nTick: 200
Attempts:
2 left
💡 Hint

Remember that vTaskDelayUntil() delays the task until the next fixed tick count based on xLastWakeTime.

🧠 Conceptual
intermediate
1:30remaining
Why use vTaskDelayUntil() instead of vTaskDelay() for periodic tasks?

Which reason best explains why vTaskDelayUntil() is preferred over vTaskDelay() for tasks that must run at precise intervals?

AvTaskDelayUntil() maintains a fixed periodic rate by accounting for task execution time, while vTaskDelay() delays relative to the current time causing drift.
BvTaskDelayUntil() uses less CPU than vTaskDelay() because it disables interrupts during delay.
CvTaskDelayUntil() allows tasks to run faster than the specified delay, while vTaskDelay() enforces a strict minimum delay.
DvTaskDelayUntil() automatically adjusts task priority during delay, vTaskDelay() does not.
Attempts:
2 left
💡 Hint

Think about how each function calculates the delay period relative to time.

🔧 Debug
advanced
2:30remaining
Identify the bug causing timing drift in this FreeRTOS task

This task is intended to run every 50 ticks using vTaskDelayUntil(). However, the timing drifts and the task runs slower than expected. What is the bug?

FreeRTOS
TickType_t xLastWakeTime;

void vTaskFunction(void *pvParameters) {
    for (;;) {
        xLastWakeTime = xTaskGetTickCount();
        vTaskDelayUntil(&xLastWakeTime, 50);
        printf("Tick: %lu\n", xTaskGetTickCount());
    }
}
AxLastWakeTime is reset inside the loop each iteration, so the delay is always relative to the current tick count causing drift.
BxLastWakeTime is not declared as volatile, so the compiler optimizes it incorrectly.
CThe task does not call vTaskDelay() before vTaskDelayUntil(), causing starvation.
DvTaskDelayUntil() is called with the wrong delay value; it should be 100 instead of 50.
Attempts:
2 left
💡 Hint

Check where xLastWakeTime is initialized and updated.

📝 Syntax
advanced
2:00remaining
Which option correctly uses vTaskDelayUntil() for a 200 tick periodic task?

Choose the code snippet that correctly implements a FreeRTOS task running every 200 ticks using vTaskDelayUntil().

A
TickType_t xLastWakeTime = 0;
for (;;) {
    vTaskDelayUntil(&amp;xLastWakeTime, 200);
    // Task code
}
B
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
for (;;) {
    vTaskDelayUntil(xLastWakeTime, 200);
    // Task code
}
C
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
    vTaskDelayUntil(&amp;xLastWakeTime, 200);
    // Task code
}
D
TickType_t xLastWakeTime;
for (;;) {
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil(&amp;xLastWakeTime, 200);
    // Task code
}
Attempts:
2 left
💡 Hint

Remember the correct way to initialize and update xLastWakeTime for vTaskDelayUntil().

🚀 Application
expert
1:30remaining
Calculate the number of task executions in 1000 ticks using vTaskDelayUntil()

A FreeRTOS task uses vTaskDelayUntil() with a delay of 120 ticks to run periodically. How many times will the task execute within 1000 ticks?

A7
B8
C10
D9
Attempts:
2 left
💡 Hint

Divide the total ticks by the delay period and consider the first execution timing.