0
0
FreeRTOSprogramming~3 mins

Why vTaskDelayUntil() for precise timing in FreeRTOS? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your task could run perfectly on time, every time, without drifting?

The Scenario

Imagine you have a task in your embedded system that must run exactly every 100 milliseconds, like checking a sensor or updating a display.

You try to do this by just delaying for 100 ms each time using a simple delay function.

The Problem

But the problem is, the delay time adds up with the task execution time, so the task drifts and doesn't run exactly every 100 ms.

This causes timing errors, making your system unreliable and unpredictable.

The Solution

Using vTaskDelayUntil() lets you specify the exact time when the task should run next.

This keeps the timing precise and consistent, no matter how long the task takes to run.

Before vs After
Before
while(1) {
  doTaskWork();
  vTaskDelay(100 / portTICK_PERIOD_MS);
}
After
TickType_t xLastWakeTime = xTaskGetTickCount();

while(1) {
  doTaskWork();
  vTaskDelayUntil(&xLastWakeTime, 100 / portTICK_PERIOD_MS);
}
What It Enables

This makes your real-time tasks run like clockwork, improving system stability and predictability.

Real Life Example

For example, a heart rate monitor needs to sample data exactly every 100 ms to give accurate readings.

Using vTaskDelayUntil() ensures the sampling happens on time, every time.

Key Takeaways

Simple delays cause timing drift in periodic tasks.

vTaskDelayUntil() keeps task timing precise and consistent.

Precise timing improves reliability in real-time systems.