</>EmbeddedInterview

What are software timers in an RTOS?

EasyRTOS & Real-Time Systems

Short answer: Software timers run a callback function after a delay, either once (one-shot) or repeatedly (auto-reload), without needing a dedicated task per timer. In FreeRTOS all timer callbacks execute in a single timer service (daemon) task, so callbacks must be short and must never block, otherwise every other timer is delayed.

static void led_timer_cb(TimerHandle_t t) { (void)t; toggle_led(); }

TimerHandle_t led_timer = xTimerCreate("led", pdMS_TO_TICKS(500), pdTRUE /* auto-reload */,
                                       NULL, led_timer_cb);
xTimerStart(led_timer, 0);

How it works

  • API calls (xTimerStart, xTimerStop, xTimerChangePeriod, xTimerReset) send commands through the timer command queue to the daemon task, so they can fail if the queue is full; check the return value and use a block time.
  • The daemon task keeps active timers in a list sorted by expiry and blocks until the next one expires.
  • Its priority (configTIMER_TASK_PRIORITY) decides how promptly callbacks run relative to other tasks; set it appropriately (often high, with very short callbacks).

Good uses: LED blinking, timeouts for protocols (e.g. "no response in 2 s"), debouncing, periodic housekeeping, retry back-off, watchdog supervision.

Avoid: heavy processing or blocking calls in callbacks (send an event to a worker task instead), and using software timers for precise, jitter-free timing (use a hardware timer interrupt for that).

Resolution: limited to the tick period.

Zephyr: k_timer runs its expiry function in the system timer interrupt context (so it must be ISR-safe and very short); use k_work_delayable for work that should run in a thread.