Bird
Raised Fist0
FreeRTOSprogramming~20 mins

One-shot vs auto-reload timers in FreeRTOS - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Timer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Timer behavior: One-shot vs Auto-reload

Consider a FreeRTOS timer created as one-shot with a 1000 ms period. The timer callback increments a global counter each time it runs. The timer is started once. What will be the value of the counter after 3 seconds?

FreeRTOS
volatile int counter = 0;

void TimerCallback(TimerHandle_t xTimer) {
    counter++;
}

int main() {
    TimerHandle_t timer = xTimerCreate("OneShotTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
    xTimerStart(timer, 0);
    vTaskDelay(pdMS_TO_TICKS(3000));
    // What is counter now?
    return 0;
}
Acounter == 3
Bcounter == 1
Ccounter == 0
Dcounter == 2
Attempts:
2 left
💡 Hint

One-shot timers run only once after the period expires.

❓ Predict Output
intermediate
2:00remaining
Auto-reload timer callback count

A FreeRTOS auto-reload timer is created with a 500 ms period. The callback increments a global variable each time it runs. The timer is started once. After 2 seconds, what is the expected value of the variable?

FreeRTOS
volatile int count = 0;

void TimerCallback(TimerHandle_t xTimer) {
    count++;
}

int main() {
    TimerHandle_t timer = xTimerCreate("AutoReloadTimer", pdMS_TO_TICKS(500), pdTRUE, NULL, TimerCallback);
    xTimerStart(timer, 0);
    vTaskDelay(pdMS_TO_TICKS(2000));
    // What is count now?
    return 0;
}
Acount == 3
Bcount == 2
Ccount == 4
Dcount == 1
Attempts:
2 left
💡 Hint

Auto-reload timers run repeatedly every period.

🧠 Conceptual
advanced
2:00remaining
Timer type effect on system behavior

Which statement correctly describes the difference between one-shot and auto-reload timers in FreeRTOS?

AOne-shot timers execute their callback once after the period, auto-reload timers execute repeatedly at each period.
BOne-shot timers cannot be stopped once started, auto-reload timers can be stopped anytime.
CBoth timers execute their callbacks repeatedly, but auto-reload timers have shorter periods.
DOne-shot timers execute repeatedly, auto-reload timers execute only once.
Attempts:
2 left
💡 Hint

Think about how many times the callback runs for each timer type.

❓ Predict Output
advanced
2:00remaining
Timer stop effect on auto-reload timer

An auto-reload timer with a 1000 ms period is started. After 2500 ms, the timer is stopped. How many times has the callback run?

FreeRTOS
volatile int callbackCount = 0;

void TimerCallback(TimerHandle_t xTimer) {
    callbackCount++;
}

int main() {
    TimerHandle_t timer = xTimerCreate("AutoReload", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
    xTimerStart(timer, 0);
    vTaskDelay(pdMS_TO_TICKS(2500));
    xTimerStop(timer, 0);
    // What is callbackCount now?
    return 0;
}
AcallbackCount == 2
BcallbackCount == 3
CcallbackCount == 1
DcallbackCount == 0
Attempts:
2 left
💡 Hint

Count how many full periods fit in 2500 ms before stopping.

🔧 Debug
expert
3:00remaining
Unexpected behavior with one-shot timer restart

A one-shot timer with a 1000 ms period is started. Inside its callback, the timer is restarted. What will be the behavior of the callback executions?

FreeRTOS
volatile int execCount = 0;

void TimerCallback(TimerHandle_t xTimer) {
    execCount++;
    xTimerStart(xTimer, 0);
}

int main() {
    TimerHandle_t timer = xTimerCreate("OneShot", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
    xTimerStart(timer, 0);
    vTaskDelay(pdMS_TO_TICKS(3500));
    // What is execCount now?
    return 0;
}
AexecCount == 4
BexecCount == 1
CexecCount == 0
DexecCount == 3
Attempts:
2 left
💡 Hint

Consider that restarting a one-shot timer inside its callback schedules it again.

Practice

(1/5)
1. What is the main difference between a one-shot timer and an auto-reload timer in FreeRTOS?
easy
A. One-shot timers run once after a delay; auto-reload timers run repeatedly at fixed intervals.
B. One-shot timers run repeatedly; auto-reload timers run only once.
C. One-shot timers are faster than auto-reload timers.
D. Auto-reload timers cannot be stopped once started.

Solution

  1. Step 1: Understand timer behavior

    One-shot timers execute their callback function only once after the specified delay.
  2. Step 2: Compare with auto-reload timers

    Auto-reload timers automatically restart after each expiration, running repeatedly at fixed intervals.
  3. Final Answer:

    One-shot timers run once after a delay; auto-reload timers run repeatedly at fixed intervals. -> Option A
  4. Quick Check:

    One-shot = single run, Auto-reload = repeated runs [OK]
Hint: One-shot = once, Auto-reload = repeat [OK]
Common Mistakes:
  • Confusing which timer runs repeatedly
  • Thinking one-shot timers repeat automatically
  • Assuming auto-reload timers cannot be stopped
2. Which of the following is the correct way to create an auto-reload timer in FreeRTOS using xTimerCreate?
easy
A. xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
B. xTimerCreate("Timer", 1000, pdTRUE, NULL, TimerCallback);
C. xTimerCreate("Timer", 1000, pdFALSE, NULL, TimerCallback);
D. xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);

Solution

  1. Step 1: Identify auto-reload parameter

    The third parameter of xTimerCreate is uxAutoReload, which should be pdTRUE for auto-reload timers.
  2. Step 2: Check time parameter format

    Use pdMS_TO_TICKS(1000) to convert milliseconds to ticks correctly.
  3. Final Answer:

    xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback); -> Option D
  4. Quick Check:

    Auto-reload needs pdTRUE and tick conversion [OK]
Hint: Auto-reload = pdTRUE, use pdMS_TO_TICKS for time [OK]
Common Mistakes:
  • Using pdFALSE for auto-reload timers
  • Passing raw milliseconds instead of ticks
  • Confusing parameter order
3. Consider this FreeRTOS timer code snippet:
TimerHandle_t timer = xTimerCreate("MyTimer", pdMS_TO_TICKS(500), pdFALSE, NULL, TimerCallback);
xTimerStart(timer, 0);

What will happen when this timer expires?
medium
A. The TimerCallback function will be called once after 500 ms.
B. The TimerCallback function will be called repeatedly every 500 ms.
C. The timer will never start because pdFALSE is invalid.
D. The TimerCallback function will be called immediately and repeatedly.

Solution

  1. Step 1: Analyze timer creation parameters

    The timer is created with pdFALSE for auto-reload, meaning it is a one-shot timer.
  2. Step 2: Understand timer start and expiration

    After starting, the timer waits 500 ms, then calls TimerCallback once and stops.
  3. Final Answer:

    The TimerCallback function will be called once after 500 ms. -> Option A
  4. Quick Check:

    pdFALSE means one-shot = single callback [OK]
Hint: pdFALSE means one-shot, callback runs once [OK]
Common Mistakes:
  • Thinking pdFALSE means repeated calls
  • Assuming timer starts immediately
  • Confusing pdFALSE with invalid parameter
4. You wrote this code to create an auto-reload timer but it only runs once:
TimerHandle_t timer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdFALSE, NULL, TimerCallback);
xTimerStart(timer, 0);

What is the likely error?
medium
A. The timer period is too short to see repeated calls.
B. xTimerStart was called with zero block time.
C. The auto-reload parameter is set to pdFALSE instead of pdTRUE.
D. The timer callback function is missing.

Solution

  1. Step 1: Check auto-reload parameter

    The timer was created with pdFALSE, which means it is a one-shot timer.
  2. Step 2: Understand effect on timer behavior

    Because of pdFALSE, the timer runs only once and does not auto-reload.
  3. Final Answer:

    The auto-reload parameter is set to pdFALSE instead of pdTRUE. -> Option C
  4. Quick Check:

    pdFALSE disables auto-reload, causing single run [OK]
Hint: Auto-reload needs pdTRUE, not pdFALSE [OK]
Common Mistakes:
  • Ignoring auto-reload parameter
  • Thinking block time affects reload
  • Assuming callback absence causes single run
5. You want to toggle an LED every 2 seconds indefinitely using a FreeRTOS timer. Which timer type and setup should you use?
hard
A. Create a one-shot timer with 2000 ms period and restart it manually each time.
B. Create an auto-reload timer with 2000 ms period to toggle LED automatically.
C. Create a one-shot timer with 1000 ms period and toggle LED twice per cycle.
D. Create an auto-reload timer with 1000 ms period and toggle LED every 2 cycles.

Solution

  1. Step 1: Identify requirement for repeated action

    Toggling LED every 2 seconds indefinitely means the action repeats automatically.
  2. Step 2: Choose timer type and period

    An auto-reload timer with 2000 ms period will call the toggle function every 2 seconds without manual restart.
  3. Final Answer:

    Create an auto-reload timer with 2000 ms period to toggle LED automatically. -> Option B
  4. Quick Check:

    Repeated action = auto-reload timer with correct period [OK]
Hint: Repeated tasks use auto-reload timers with correct period [OK]
Common Mistakes:
  • Using one-shot timer and forgetting to restart
  • Setting wrong timer period
  • Confusing toggle frequency with timer period
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