Consider this FreeRTOS timer callback function. What will be the output when the timer expires?
static int count = 0; void vTimerCallback(TimerHandle_t xTimer) { count++; printf("Count is %d\n", count); } // Timer created and started elsewhere with 1 second period
Remember that timer callbacks run in the timer service task context, so accessing static variables is safe.
The timer callback runs in the timer service task context, so it can safely access and modify static variables. Each time the timer expires, count increments and prints the updated value.
What will happen if you call vTaskDelay(1000) inside a FreeRTOS timer callback function?
void vTimerCallback(TimerHandle_t xTimer) {
vTaskDelay(1000);
printf("Delayed inside timer callback\n");
}Consider the context in which timer callbacks run and what vTaskDelay requires.
Timer callbacks run in the timer service task context, which must not block or delay. Calling vTaskDelay inside a timer callback causes the timer service task to block, leading to system hang or deadlock.
A timer callback modifies a global variable, but the main task never sees the updated value. What is the most likely cause?
volatile int shared_var = 0; void vTimerCallback(TimerHandle_t xTimer) { shared_var = 1; } void main_task(void *params) { while(shared_var == 0) { // wait } printf("Shared var updated\n"); }
Think about how variables shared between tasks and callbacks should be handled.
Even though shared_var is volatile, without proper synchronization or memory barriers, the main task may read a cached stale value and never see the update made by the timer callback.
Which of the following is the correct function signature for a FreeRTOS timer callback?
Check the FreeRTOS API documentation for timer callback signatures.
The correct signature is void vTimerCallback(TimerHandle_t xTimer). The callback receives the timer handle as a parameter and returns void.
Which operation is safe to perform inside a FreeRTOS timer callback function?
Consider which FreeRTOS API functions are safe in the timer service task context.
Timer callbacks run in the timer service task context, not an ISR. Therefore, calling xSemaphoreGive() is safe. xQueueSendFromISR() is meant for ISR context and should not be called here. vTaskDelay() blocks the timer task and is unsafe. printf() may cause reentrancy issues and is generally unsafe.
