Bird
Raised Fist0
FreeRTOSprogramming~5 mins

vTaskDelay() for periodic tasks in FreeRTOS - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of vTaskDelay() in FreeRTOS?

vTaskDelay() pauses a task for a specified number of tick periods, allowing other tasks to run. It helps in creating delays or periodic behavior in tasks.

Click to reveal answer
beginner
How does vTaskDelay() help in creating periodic tasks?

By delaying a task for a fixed number of ticks, vTaskDelay() makes the task wait before running again, effectively creating a simple periodic execution pattern.

Click to reveal answer
intermediate
What is a limitation of using vTaskDelay() for periodic tasks?

vTaskDelay() delays relative to when the function is called, so small variations in execution time can cause drift in the task's period over time.

Click to reveal answer
intermediate
Which FreeRTOS function is better than vTaskDelay() for precise periodic tasks?

vTaskDelayUntil() is better because it delays a task until a fixed point in time, preventing drift and keeping the task period consistent.

Click to reveal answer
beginner
Example: What does this code do?
TickType_t xLastWakeTime = xTaskGetTickCount();
vTaskDelay(100 / portTICK_PERIOD_MS);

This code delays the task for 100 milliseconds from the moment vTaskDelay() is called. It does not consider when the task last ran, so it may cause timing drift if used repeatedly for periodic tasks.

Click to reveal answer
What does vTaskDelay() do in FreeRTOS?
ADeletes a task
BImmediately resumes a task
CChanges task priority
DPauses a task for a fixed number of tick periods
Why might vTaskDelay() cause timing drift in periodic tasks?
ABecause it delays relative to when called, not a fixed time
BBecause it uses interrupts
CBecause it deletes the task after delay
DBecause it changes the system clock
Which function is better for precise periodic timing in FreeRTOS?
AvTaskDelayUntil()
BvTaskDelete()
CvTaskSuspend()
DvTaskResume()
If you want a task to run every 500 ms exactly, which is best?
AUse vTaskDelay() with 500 ms delay
BUse vTaskDelayUntil() with a 500 ms period
CUse vTaskDelete() after 500 ms
DUse vTaskSuspend() for 500 ms
What does portTICK_PERIOD_MS represent?
ATask priority level
BTicks per second
CMilliseconds per tick in FreeRTOS
DStack size of a task
Explain how vTaskDelay() works and why it might cause timing drift in periodic tasks.
Think about when the delay starts counting.
You got /4 concepts.
    Describe the difference between vTaskDelay() and vTaskDelayUntil() for periodic task scheduling.
    Consider how each function measures delay time.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the primary purpose of vTaskDelay() in FreeRTOS?
      easy
      A. To pause a task for a specific number of ticks
      B. To create a new task
      C. To delete a task
      D. To send data between tasks

      Solution

      1. Step 1: Understand the function role

        vTaskDelay() is designed to pause or block a task for a set number of tick counts, allowing other tasks to run.
      2. Step 2: Compare with other options

        Creating or deleting tasks and sending data are done by other FreeRTOS functions, not vTaskDelay().
      3. Final Answer:

        To pause a task for a specific number of ticks -> Option A
      4. Quick Check:

        vTaskDelay() pauses task = A [OK]
      Hint: vTaskDelay() always pauses task execution for ticks [OK]
      Common Mistakes:
      • Confusing vTaskDelay() with task creation functions
      • Thinking vTaskDelay() sends data
      • Assuming vTaskDelay() deletes tasks
      2. Which of the following is the correct way to delay a task for 100 milliseconds using vTaskDelay()?
      easy
      A. vTaskDelay(100);
      B. vTaskDelayMs(100);
      C. vTaskDelay(pdMS_TO_TICKS(100));
      D. vTaskDelayTicks(100);

      Solution

      1. Step 1: Understand tick conversion

        FreeRTOS uses ticks, not milliseconds directly. To convert milliseconds to ticks, use pdMS_TO_TICKS().
      2. Step 2: Check syntax correctness

        Only vTaskDelay(pdMS_TO_TICKS(100)); correctly converts 100 ms to ticks and delays the task.
      3. Final Answer:

        vTaskDelay(pdMS_TO_TICKS(100)); -> Option C
      4. Quick Check:

        Use pdMS_TO_TICKS() inside vTaskDelay() [OK]
      Hint: Always convert ms to ticks with pdMS_TO_TICKS() [OK]
      Common Mistakes:
      • Passing milliseconds directly without conversion
      • Using non-existent functions like vTaskDelayMs()
      • Confusing ticks and milliseconds
      3. Given the following code snippet, what will be the approximate delay between each print statement?
      void TaskFunction(void *pvParameters) {
          while(1) {
              printf("Tick\n");
              vTaskDelay(pdMS_TO_TICKS(500));
          }
      }
      medium
      A. Approximately 1 second
      B. Approximately 500 milliseconds
      C. No delay, prints continuously
      D. Approximately 50 milliseconds

      Solution

      1. Step 1: Analyze vTaskDelay parameter

        The delay is set to pdMS_TO_TICKS(500), which converts 500 milliseconds to ticks.
      2. Step 2: Understand loop timing

        Each loop iteration prints "Tick" then delays for about 500 ms, so prints occur roughly every 500 ms.
      3. Final Answer:

        Approximately 500 milliseconds -> Option B
      4. Quick Check:

        vTaskDelay(pdMS_TO_TICKS(500)) = 500 ms delay [OK]
      Hint: vTaskDelay(pdMS_TO_TICKS(x)) delays about x milliseconds [OK]
      Common Mistakes:
      • Ignoring pdMS_TO_TICKS conversion
      • Assuming no delay in loop
      • Confusing milliseconds with ticks
      4. Identify the error in this code snippet intended to delay a task for 200 milliseconds:
      void Task(void *params) {
          while(1) {
              doWork();
              vTaskDelay(200);
          }
      }
      medium
      A. vTaskDelay cannot be used inside a while loop
      B. doWork() must be called after vTaskDelay()
      C. The task function must return a value
      D. vTaskDelay should use pdMS_TO_TICKS(200) to convert milliseconds

      Solution

      1. Step 1: Check vTaskDelay argument

        The argument 200 is treated as ticks, not milliseconds. To delay 200 ms, convert using pdMS_TO_TICKS(200).
      2. Step 2: Verify other code parts

        Calling doWork() before delay is valid. The task function returns void, which is correct.
      3. Final Answer:

        vTaskDelay should use pdMS_TO_TICKS(200) to convert milliseconds -> Option D
      4. Quick Check:

        Convert ms to ticks with pdMS_TO_TICKS() [OK]
      Hint: Always convert ms to ticks before passing to vTaskDelay() [OK]
      Common Mistakes:
      • Passing milliseconds directly to vTaskDelay()
      • Thinking vTaskDelay can't be in loops
      • Expecting task functions to return values
      5. You want a task to run exactly every 1 second, but your system tick rate is 1000 Hz (1 tick = 1 ms). Which is the best way to implement this periodic delay using vTaskDelay()?
      hard
      A. Use vTaskDelayUntil() instead of vTaskDelay() for precise periodic timing
      B. Call vTaskDelay(pdMS_TO_TICKS(1000)); inside the task loop
      C. Call vTaskDelay(pdMS_TO_TICKS(1000)); and then call vTaskDelay(0);
      D. Call vTaskDelay(1000); inside the task loop

      Solution

      1. Step 1: Understand vTaskDelay limitations

        vTaskDelay() delays relative to current time, so small variations accumulate causing drift in periodic tasks.
      2. Step 2: Identify precise periodic method

        vTaskDelayUntil() uses an absolute wake time, preventing drift and ensuring exact periodic execution.
      3. Final Answer:

        Use vTaskDelayUntil() instead of vTaskDelay() for precise periodic timing -> Option A
      4. Quick Check:

        vTaskDelayUntil() prevents drift in periodic tasks [OK]
      Hint: Use vTaskDelayUntil() for exact periodic task timing [OK]
      Common Mistakes:
      • Using vTaskDelay() causes timing drift
      • Assuming tick count matches ms without conversion
      • Adding extra delays unnecessarily
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard