Consider a FreeRTOS software timer created with a 1000 ms period. The timer callback increments a counter. After starting the timer, the period is changed to 500 ms using xTimerChangePeriod. What will be the value of the counter after approximately 1500 ms?
static int counter = 0; void vTimerCallback(TimerHandle_t xTimer) { counter++; } void main_task(void *params) { TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback); xTimerStart(xTimer, 0); vTaskDelay(pdMS_TO_TICKS(700)); xTimerChangePeriod(xTimer, pdMS_TO_TICKS(500), 0); vTaskDelay(pdMS_TO_TICKS(800)); // Check counter value here }
Think about how many timer callbacks happen before and after the period change.
The timer first fires after 1000 ms, incrementing the counter to 1. Then the period changes to 500 ms. After 500 ms more (total 1500 ms), the timer fires again, incrementing the counter to 2.
If a FreeRTOS software timer is created but not started, what happens when xTimerChangePeriod is called on it?
Consider if changing the period also starts the timer.
xTimerChangePeriod changes the timer period but does not start the timer if it is stopped. The timer remains stopped until xTimerStart is called.
Given this code snippet, the timer period change does not seem to take effect immediately. What is the most likely cause?
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback);
xTimerStart(xTimer, 0);
vTaskDelay(pdMS_TO_TICKS(500));
xTimerChangePeriod(xTimer, pdMS_TO_TICKS(2000), 0);Think about how FreeRTOS timers handle period changes during an active timer cycle.
In FreeRTOS, changing the timer period with xTimerChangePeriod schedules the new period to take effect after the current timer period completes. It does not reset the timer immediately.
Which option contains a syntax error when trying to change a FreeRTOS timer period?
Check the commas separating function arguments.
Option A is missing a comma between xTimer and pdMS_TO_TICKS(500), causing a syntax error.
A FreeRTOS timer is created with a 1000 ms period and started at time 0. At 700 ms, the period is changed to 400 ms. At 1300 ms, the period is changed again to 600 ms. Assuming the timer callback increments a counter each time it fires, what is the counter value at 2500 ms?
static int counter = 0; void vTimerCallback(TimerHandle_t xTimer) { counter++; } void main_task(void *params) { TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback); xTimerStart(xTimer, 0); vTaskDelay(pdMS_TO_TICKS(700)); xTimerChangePeriod(xTimer, pdMS_TO_TICKS(400), 0); vTaskDelay(pdMS_TO_TICKS(600)); xTimerChangePeriod(xTimer, pdMS_TO_TICKS(600), 0); vTaskDelay(pdMS_TO_TICKS(1200)); // Check counter value here }
Calculate each timer firing interval carefully after each period change.
Timer firing timeline (assuming period changes take effect after the current period expires):
- t=1000 ms: first firing, counter=1. Period now 400 ms (change at 700 ms effective here), next at 1400 ms.
- t=1300 ms: period changed to 600 ms, effective after next firing (1400 ms).
- t=1400 ms: firing, counter=2. Period now 600 ms, next at 2000 ms.
- t=2000 ms: firing, counter=3. Next at 2600 ms (>2500 ms).
At 2500 ms, counter=3.
