Consider a FreeRTOS task using vTaskDelayUntil() to run every 100 ticks. What will be the output timestamps printed by the task?
TickType_t xLastWakeTime;
void vTaskFunction(void *pvParameters) {
xLastWakeTime = xTaskGetTickCount();
for (int i = 0; i < 3; i++) {
vTaskDelayUntil(&xLastWakeTime, 100);
printf("Tick: %lu\n", xTaskGetTickCount());
}
}Remember that vTaskDelayUntil() delays the task until the next fixed tick count based on xLastWakeTime.
The vTaskDelayUntil() function delays the task to maintain a fixed periodic rate. Starting from xLastWakeTime, it adds the delay period (100 ticks) each time. So the ticks printed are 100, 200, and 300.
Which reason best explains why vTaskDelayUntil() is preferred over vTaskDelay() for tasks that must run at precise intervals?
Think about how each function calculates the delay period relative to time.
vTaskDelayUntil() uses a reference time to keep the task running at fixed intervals, compensating for the time the task itself takes to run. vTaskDelay() delays relative to the current time, so small delays in execution accumulate causing drift.
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?
TickType_t xLastWakeTime;
void vTaskFunction(void *pvParameters) {
for (;;) {
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil(&xLastWakeTime, 50);
printf("Tick: %lu\n", xTaskGetTickCount());
}
}Check where xLastWakeTime is initialized and updated.
The variable xLastWakeTime must be initialized once before the loop and then passed to vTaskDelayUntil() each iteration. Resetting it inside the loop causes the delay to be relative to the current time every time, losing the fixed periodic timing.
Choose the code snippet that correctly implements a FreeRTOS task running every 200 ticks using vTaskDelayUntil().
Remember the correct way to initialize and update xLastWakeTime for vTaskDelayUntil().
Option C initializes xLastWakeTime once before the loop and passes its address to vTaskDelayUntil() each iteration, which is correct. Option C passes the value, not the pointer. Option C initializes to zero which may cause incorrect timing. Option C resets xLastWakeTime inside the loop causing drift.
A FreeRTOS task uses vTaskDelayUntil() with a delay of 120 ticks to run periodically. How many times will the task execute within 1000 ticks?
Divide the total ticks by the delay period and consider the first execution timing.
The task runs first after 120 ticks, then every 120 ticks. The number of executions in 1000 ticks is floor(1000 / 120) = 8, but since the first execution happens at 120 ticks, total executions are 8 + 1 = 9.