Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Starting and stopping timers in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Timer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of starting a one-shot timer
What will be the output when this FreeRTOS timer code runs?
FreeRTOS
void vTimerCallback(TimerHandle_t xTimer) {
    printf("Timer expired\n");
}

int main() {
    TimerHandle_t xTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
    if (xTimer == NULL) {
        printf("Timer creation failed\n");
    } else {
        xTimerStart(xTimer, 0);
        vTaskDelay(pdMS_TO_TICKS(1500));
    }
    return 0;
}
ATimer expired
BTimer expired\nTimer expired
CNo output
DTimer creation failed
Attempts:
2 left
💡 Hint
Think about what pdFALSE means for the timer type and how long the delay is.
❓ Predict Output
intermediate
2:00remaining
Effect of stopping a timer before expiry
What will be printed when this FreeRTOS timer code runs?
FreeRTOS
void vTimerCallback(TimerHandle_t xTimer) {
    printf("Timer expired\n");
}

int main() {
    TimerHandle_t xTimer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
    if (xTimer == NULL) {
        printf("Timer creation failed\n");
    } else {
        xTimerStart(xTimer, 0);
        xTimerStop(xTimer, 0);
        vTaskDelay(pdMS_TO_TICKS(1500));
    }
    return 0;
}
ATimer expired
BTimer creation failed
CNo output
DTimer expired\nTimer expired
Attempts:
2 left
💡 Hint
Consider what happens if you stop a timer before it expires.
🔧 Debug
advanced
2:00remaining
Identify the error in timer restart code
This code tries to restart a periodic timer after stopping it. What error will it cause?
FreeRTOS
void vTimerCallback(TimerHandle_t xTimer) {
    printf("Timer expired\n");
}

int main() {
    TimerHandle_t xTimer = xTimerCreate("Periodic", pdMS_TO_TICKS(500), pdTRUE, NULL, vTimerCallback);
    if (xTimer == NULL) {
        printf("Timer creation failed\n");
    } else {
        xTimerStart(xTimer, 0);
        vTaskDelay(pdMS_TO_TICKS(600));
        xTimerStop(xTimer, 0);
        xTimerStart(xTimer, 0);
        vTaskDelay(pdMS_TO_TICKS(600));
    }
    return 0;
}
AThe timer callback prints "Timer expired" twice
BThe second xTimerStart call returns an error because the timer is stopped
CThe timer callback never runs
DThe program crashes with a null pointer dereference
Attempts:
2 left
💡 Hint
Think about how periodic timers behave when restarted after stopping.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in timer creation
Which option contains a syntax error in creating a FreeRTOS timer?
ATimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, vTimerCallback);
BTimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, &vTimerCallback);
CTimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback);
DTimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE NULL, vTimerCallback);
Attempts:
2 left
💡 Hint
Look carefully at the commas separating arguments.
🚀 Application
expert
3:00remaining
How many times does the callback run?
Given this FreeRTOS timer code, how many times will the callback print "Timer expired" during execution?
FreeRTOS
void vTimerCallback(TimerHandle_t xTimer) {
    static int count = 0;
    count++;
    printf("Timer expired %d\n", count);
    if (count == 3) {
        xTimerStop(xTimer, 0);
    }
}

int main() {
    TimerHandle_t xTimer = xTimerCreate("Periodic", pdMS_TO_TICKS(400), pdTRUE, NULL, vTimerCallback);
    if (xTimer == NULL) {
        printf("Timer creation failed\n");
    } else {
        xTimerStart(xTimer, 0);
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
    return 0;
}
A4 times
B3 times
C5 times
D2 times
Attempts:
2 left
💡 Hint
Consider the timer period, delay, and when the timer stops itself.

Practice

(1/5)
1. What does the function xTimerCreate do in FreeRTOS?
easy
A. Creates a timer but does not start it automatically
B. Starts a timer immediately after creation
C. Stops a running timer
D. Deletes a timer permanently

Solution

  1. Step 1: Understand the purpose of xTimerCreate

    This function is used to create a software timer in FreeRTOS but it does not start the timer automatically.
  2. Step 2: Differentiate from other timer functions

    Starting a timer requires xTimerStart, stopping requires xTimerStop, and deleting requires xTimerDelete.
  3. Final Answer:

    Creates a timer but does not start it automatically -> Option A
  4. Quick Check:

    xTimerCreate only creates timer [OK]
Hint: Remember: create ≠ start; start needs separate call [OK]
Common Mistakes:
  • Thinking xTimerCreate starts the timer automatically
  • Confusing xTimerStart with creation
  • Assuming timer is running immediately after creation
2. Which of the following is the correct way to start a FreeRTOS timer named xTimer?
easy
A. xTimerDelete(xTimer, 0);
B. xTimerCreate(xTimer, 0);
C. xTimerStop(xTimer, 0);
D. xTimerStart(xTimer, 0);

Solution

  1. Step 1: Identify the function to start a timer

    The function xTimerStart is used to start a timer in FreeRTOS.
  2. Step 2: Check the syntax

    The correct syntax is xTimerStart(xTimer, 0); where the second parameter is the block time.
  3. Final Answer:

    xTimerStart(xTimer, 0); -> Option D
  4. Quick Check:

    Start timer = xTimerStart call [OK]
Hint: Start timers with xTimerStart, not create or stop [OK]
Common Mistakes:
  • Using xTimerCreate to start a timer
  • Confusing stop and delete functions with start
  • Missing the block time parameter
3. Given the following code snippet, what will be the output if the timer callback increments a counter each time it runs and the timer is started and stopped immediately?
int counter = 0;
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, timerCallback);
xTimerStart(xTimer, 0);
xTimerStop(xTimer, 0);
// Assume timerCallback increments counter
medium
A. counter will remain 0
B. counter will be incremented at least once
C. Compilation error due to missing parameters
D. Runtime error due to invalid timer handle

Solution

  1. Step 1: Analyze timer start and stop sequence

    The timer is started and then immediately stopped before it can expire and call the callback.
  2. Step 2: Understand timer callback execution

    Since the timer period is 1000 ms and it is stopped immediately, the callback does not get a chance to run, so the counter stays 0.
  3. Final Answer:

    counter will remain 0 -> Option A
  4. Quick Check:

    Immediate stop prevents callback = counter 0 [OK]
Hint: Timer callback runs only after timer expires, not immediately [OK]
Common Mistakes:
  • Assuming callback runs immediately on start
  • Ignoring timer period delay
  • Confusing start and stop timing effects
4. Identify the error in the following code snippet that attempts to stop a timer:
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(500), pdFALSE, NULL, timerCallback);
xTimerStop(xTimer, 1000);
medium
A. The block time parameter in xTimerStop is too large
B. The timer was never started before stopping
C. The timer handle is NULL
D. The timer period is set incorrectly

Solution

  1. Step 1: Check timer lifecycle

    The timer is created but never started before calling xTimerStop.
  2. Step 2: Understand stopping a timer

    Stopping a timer that is not running has no effect and may cause unexpected behavior.
  3. Final Answer:

    The timer was never started before stopping -> Option B
  4. Quick Check:

    Stop requires running timer first [OK]
Hint: Always start timer before stopping it [OK]
Common Mistakes:
  • Stopping timer without starting it
  • Assuming block time causes error
  • Misunderstanding timer handle validity
5. You want to create a periodic timer in FreeRTOS that starts automatically and stops after 5 executions. Which approach correctly achieves this behavior?
hard
A. Create the timer with pdTRUE, start it automatically by xTimerCreate, and delete after 5 executions
B. Create the timer with pdFALSE, start it manually, and stop it after 5 seconds
C. Create the timer with pdTRUE for auto-reload, start it manually, and in the callback count executions and stop after 5
D. Create the timer with pdFALSE, start it automatically, and restart it 5 times

Solution

  1. Step 1: Understand timer creation parameters

    Setting pdTRUE makes the timer periodic (auto-reload), but it does not start automatically; you must call xTimerStart.
  2. Step 2: Implement stopping after 5 executions

    Use a counter inside the callback function to track executions and call xTimerStop after the 5th execution.
  3. Final Answer:

    Create the timer with pdTRUE for auto-reload, start it manually, and in the callback count executions and stop after 5 -> Option C
  4. Quick Check:

    Periodic timer + manual start + callback stop after 5 [OK]
Hint: Use callback counter to stop periodic timer after N runs [OK]
Common Mistakes:
  • Assuming xTimerCreate starts timer automatically
  • Using one-shot timer for periodic behavior
  • Not counting executions in callback
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