Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Changing timer period at runtime in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Changing timer period at runtime
Create Timer with initial period
↓
Timer starts running
↓
At runtime, decide to change period
↓
Call API to change timer period
↓
Timer period updated
↓
Timer continues with new period
↓
Timer callback executes
This flow shows how a FreeRTOS timer is created with an initial period, then at runtime its period is changed using an API call, and the timer continues running with the new period.
Execution Sample
FreeRTOS
TimerHandle_t timer = xTimerCreate("MyTimer", pdMS_TO_TICKS(1000), pdTRUE, NULL, TimerCallback);
xTimerStart(timer, 0);
// Later in runtime
xTimerChangePeriod(timer, pdMS_TO_TICKS(500), 0);
Creates a timer with 1 second period, starts it, then changes its period to 0.5 seconds at runtime.
Execution Table
StepActionTimer Period (ms)API CalledResult
1Create timer1000xTimerCreateTimer created with 1000 ms period
2Start timer1000xTimerStartTimer started with 1000 ms period
3Timer runs1000NoneTimer callback called every 1000 ms
4Change period1000 -> 500xTimerChangePeriodTimer period changed to 500 ms
5Timer runs500NoneTimer callback called every 500 ms
6End--Execution stops or continues with new period
💡 Timer period changed at step 4; timer continues running with new period.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
timer_period_msN/A10001000500500
timer_stateN/ACreatedStartedStartedStarted
Key Moments - 3 Insights
Why does the timer callback run at the old period before changing?
Before step 4 in the execution_table, the timer period is still 1000 ms, so the callback runs every 1000 ms until xTimerChangePeriod is called.
Does changing the timer period stop the timer?
No, as shown in step 4 and 5, xTimerChangePeriod updates the period but the timer remains started and continues running.
What happens if you change the period to a longer time?
The timer callback will be called less frequently, as the period is updated to the new longer value in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the timer period after step 2?
A0 ms
B1000 ms
C500 ms
DNot set
💡 Hint
Check the 'Timer Period (ms)' column at step 2 in the execution_table.
At which step does the timer period change?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the 'API Called' column showing xTimerChangePeriod in the execution_table.
If you call xTimerChangePeriod with a larger period, what happens to the timer callback frequency?
ACallback runs less often
BCallback runs more often
CCallback stops
DCallback runs immediately
💡 Hint
Refer to key_moments about changing period affecting callback frequency.
Concept Snapshot
FreeRTOS timers run with a set period.
Use xTimerChangePeriod(timer, newPeriod, blockTime) to change period at runtime.
Timer keeps running; callback frequency changes.
No need to stop timer before changing period.
New period applies immediately after API call.
Full Transcript
This visual execution shows how a FreeRTOS timer is created with an initial period of 1000 milliseconds and started. The timer callback runs every 1000 ms. Later, the timer period is changed to 500 milliseconds using xTimerChangePeriod. The timer continues running without stopping, and the callback now runs every 500 ms. Variables like timer_period_ms and timer_state track the timer's period and state through each step. Key moments clarify that changing the period does not stop the timer and affects callback frequency. The quiz tests understanding of timer period changes and their effects.

Practice

(1/5)
1.

What function is used in FreeRTOS to change the period of a timer while it is running?

easy
A. xTimerStop()
B. xTimerStart()
C. xTimerReset()
D. xTimerChangePeriod()

Solution

  1. Step 1: Understand timer period change in FreeRTOS

    FreeRTOS allows changing a timer's period without stopping it.
  2. Step 2: Identify the correct API function

    The function xTimerChangePeriod() is designed to change the timer period at runtime.
  3. Final Answer:

    xTimerChangePeriod() -> Option D
  4. Quick Check:

    Change timer period = xTimerChangePeriod() [OK]
Hint: Use xTimerChangePeriod() to update timer period on the fly [OK]
Common Mistakes:
  • Confusing xTimerStart() with changing period
  • Trying to stop and restart timer instead
  • Using xTimerReset() which resets but doesn't change period
2.

Which of the following is the correct syntax to change a FreeRTOS timer period to 100 ticks?

?
easy
A. xTimerChangePeriod(timerHandle, 0, 100);
B. xTimerChangePeriod(100, timerHandle, 0);
C. xTimerChangePeriod(timerHandle, 100, 0);
D. xTimerChangePeriod(0, 100, timerHandle);

Solution

  1. Step 1: Recall function parameter order

    The function xTimerChangePeriod() takes parameters as (TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xTicksToWait).
  2. Step 2: Match parameters to example

    To set period to 100 ticks, pass timerHandle, 100, and 0 for no wait.
  3. Final Answer:

    xTimerChangePeriod(timerHandle, 100, 0); -> Option C
  4. Quick Check:

    Correct parameter order = xTimerChangePeriod(timerHandle, 100, 0); [OK]
Hint: Remember: timer handle first, then new period, then wait ticks [OK]
Common Mistakes:
  • Swapping timer handle and period parameters
  • Passing wait time before period
  • Using wrong parameter types or order
3.

Given the code snippet below, what will be the output if the timer callback prints the timer period each time it runs?

TimerHandle_t timer = xTimerCreate("MyTimer", pdMS_TO_TICKS(500), pdTRUE, NULL, TimerCallback);
xTimerStart(timer, 0);
xTimerChangePeriod(timer, pdMS_TO_TICKS(1000), 0);
// TimerCallback prints current period in ms
medium
A. The callback prints 500 ms once, then 1000 ms repeatedly
B. The callback prints 1000 ms repeatedly from start
C. The callback prints 500 ms repeatedly without change
D. The callback never runs due to period change

Solution

  1. Step 1: Analyze timer creation and start

    The timer is created with 500 ms period and started, scheduling first callback in 500 ms.
  2. Step 2: Effect of period change

    xTimerChangePeriod() removes the pending expiration, updates the period to 1000 ms, and schedules the next callback in 1000 ms from the change time. Thus, all callbacks print 1000 ms repeatedly.
  3. Final Answer:

    The callback prints 1000 ms repeatedly from start -> Option B
  4. Quick Check:

    Period change cancels pending expiry = prints 1000 ms repeatedly [OK]
Hint: xTimerChangePeriod cancels pending expiry and starts new full period [OK]
Common Mistakes:
  • Thinking first callback uses old 500 ms period
  • Assuming period change preserves pending expiration time
  • Believing the timer prints 500 ms first then switches
4.

Identify the error in the following code that attempts to change a timer period at runtime:

TimerHandle_t timer = xTimerCreate("Timer", pdMS_TO_TICKS(200), pdTRUE, NULL, TimerCallback);
xTimerChangePeriod(200, timer, 0);
medium
A. Parameters to xTimerChangePeriod are in wrong order
B. Timer was not started before changing period
C. Timer callback function is missing
D. pdMS_TO_TICKS() cannot be used in timer period

Solution

  1. Step 1: Check function parameter order

    xTimerChangePeriod() expects (TimerHandle_t, TickType_t, TickType_t), but code passes (TickType_t, TimerHandle_t, TickType_t).
  2. Step 2: Confirm other code parts

    Timer is created correctly and callback is provided; pdMS_TO_TICKS() is valid for period conversion.
  3. Final Answer:

    Parameters to xTimerChangePeriod are in wrong order -> Option A
  4. Quick Check:

    Correct parameter order is timer handle first [OK]
Hint: Timer handle always comes first in xTimerChangePeriod() [OK]
Common Mistakes:
  • Swapping timer handle and period parameters
  • Not starting timer before changing period (not required)
  • Misunderstanding pdMS_TO_TICKS usage
5.

You have a FreeRTOS timer running with a 200 ms period. You want to double the period each time the timer callback runs, up to a maximum of 1600 ms. Which code snippet correctly changes the timer period at runtime inside the callback?

void TimerCallback(TimerHandle_t xTimer) {
    TickType_t currentPeriod = xTimerGetPeriod(xTimer);
    TickType_t newPeriod = currentPeriod * 2;
    if (newPeriod <= pdMS_TO_TICKS(1600)) {
        ?
    }
}
hard
A. xTimerChangePeriod(xTimer, newPeriod, 0);
B. xTimerStart(xTimer, 0);
C. xTimerStop(xTimer, 0); xTimerChangePeriod(xTimer, newPeriod, 0);
D. xTimerReset(xTimer, 0); xTimerChangePeriod(xTimer, newPeriod, 0);

Solution

  1. Step 1: Understand timer period update inside callback

    Inside the callback, you can call xTimerChangePeriod() to update the timer period dynamically.
  2. Step 2: Choose correct function usage

    Calling xTimerChangePeriod(xTimer, newPeriod, 0); changes the period without stopping or restarting the timer.
  3. Final Answer:

    xTimerChangePeriod(xTimer, newPeriod, 0); -> Option A
  4. Quick Check:

    Change period directly inside callback = xTimerChangePeriod(xTimer, newPeriod, 0); [OK]
Hint: Use xTimerChangePeriod inside callback to update period safely [OK]
Common Mistakes:
  • Stopping timer before changing period unnecessarily
  • Starting timer again inside callback
  • Resetting timer which restarts count instead of changing 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