Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 544 of 9367. Real-Time Operating Systems (RTOS)Free lesson

Architecture of RTOS Software Timer Services

Examine the underlying mechanisms of software timers, including the timer service task, tick-based management, and the timer list data structure.

In this lesson, you will learn how RTOS software timers abstract hardware constraints by delegating periodic or one-shot events to a dedicated background task.

The Timer Service Architecture

While hardware timers are limited by the number of physical compare registers on your MCU, software timers allow you to create an almost unlimited number of logical timers. The RTOS achieves this by offloading the timing logic from the tick interrupt (ISR) to a specialized Timer Service Task (or "Timer Daemon").

Tick-Based Management

The system tick interrupt acts as the heartbeat. When the tick ISR occurs, it increments a global tick counter. Crucially, the ISR does not execute the timer callback functions directly. If it did, long-running callbacks could cause the system to miss subsequent ticks, violating real-time determinism.

Instead, the tick ISR simply checks if any timers have expired. If one has, it sends a notification (or a message via a queue) to the Timer Service Task, signaling it to wake up and process the expired timers.

The Timer List Data Structure

To maintain efficiency, the RTOS keeps track of all active timers in a Timer List, typically implemented as a linked list sorted by expiry time.

typedef struct Timer {
    TickType_t xExpiryTime;   // When the timer should fire
    TickType_t xPeriod;       // Reload value for periodic timers
    void (*pvCallback)(void); // Pointer to function to execute
    struct Timer *pxNext;     // Link to the next timer in the list
} Timer_t;

Because the list is sorted by xExpiryTime, the Timer Service Task only needs to examine the head of the list. If the xExpiryTime of the first node hasn't been reached, the task knows that no other timers in the list have expired, allowing it to put itself into a blocked state until the next expected expiry. This minimizes CPU consumption, as the task only wakes up precisely when work needs to be done.

By decoupling the timing mechanism from the ISR and using a sorted list, the architecture ensures that your timing-sensitive application code executes in the context of a task rather than an interrupt, keeping your system responsive and predictable.

Key Takeaway: Software timers use a sorted linked list to manage deadlines and a dedicated service task to execute callbacks, ensuring that timing logic does not degrade system-wide interrupt latency.