The idea: one timer, one loop, no task ever interrupts another
A small microcontroller program often has a handful of jobs that repeat: blink an LED, read a button, send a status line over the UART. A time-triggered co-operative scheduler, as Michael Pont described it, runs them with almost no machinery. One hardware timer, SysTick on an ARM Cortex-M, interrupts every 10 ms: one tick. Its interrupt handler only does bookkeeping in a small task array. The real work happens in the super loop in main(), which runs every task that is due, one after another, each to its end, and then puts the CPU to sleep until the next tick. Tasks never interrupt each other, so they need no locks, and the timing of every task is fixed by the tick count.
The task array and SCH_Add_Task
Each slot of sTask SCH_tasks[SCH_MAX_TASKS] (5 slots on the page) describes one task:
| field | meaning |
|---|---|
pTask | pointer to the task function, a void f(void); 0 = the slot is free |
Delay | ticks left until the task is due |
Period | ticks between runs; 0 = run once (a one-shot task) |
RunMe | how many runs are due and not yet done; set by the ISR, cleared by the loop |
SCH_Add_Task(pF, DELAY, PERIOD) puts the task into the first free slot and returns the slot number, or SCH_MAX_TASKS when the array is full. DELAY is when the task first runs and PERIOD how often it repeats, both in ticks. The page starts with SCH_Add_Task(LED_Toggle, 0, 50) (toggle every 500 ms), SCH_Add_Task(Button_Read, 0, 1) (read the pin every tick) and SCH_Add_Task(UART_Send, 3, 100) (a status line every second) in slots 0, 1 and 2 (Demo: SCH_Add_Task fills the array). The array is static: no memory is allocated at run time, which is why many safety standards accept this design.
SCH_Update: count in the interrupt
The SysTick handler calls SCH_Update(). For every slot with a task it decrements Delay; when Delay reaches 0 the task is due, so it adds 1 to RunMe and reloads Delay with Period. That is all. It does not call the task: an interrupt handler that ran an 8 ms UART transmit would block every other interrupt for 8 ms. Counting takes a few microseconds whatever the tasks do. With this code a task added with DELAY = 0 is due at tick 1, one with DELAY = 3 at tick 3, and then every PERIOD ticks (Demo: three ticks in detail: after tick 3, LED_Toggle has Delay 48, Button_Read 1 and UART_Send 100). A common variant tests Delay == 0 first and decrements in the else branch; then a task repeats every PERIOD + 1 ticks, an off-by-one worth checking in any copy of this code.
SCH_Dispatch_Tasks: run, then sleep
The super loop is while (1) SCH_Dispatch_Tasks();. One call is a pass: it walks slots 0 to 4 and runs each task whose RunMe is above 0, then decrements RunMe, and deletes the task if its Period is 0. A task always runs to its end: this is co-operative scheduling, the general idea of which is on preemptive vs cooperative scheduling. After the pass, SCH_Go_To_Sleep() executes __WFI() (wait for interrupt), which stops the core clock until the next interrupt, the next SysTick. In tick 1 the page's CPU runs LED_Toggle from 10 to 12 ms and Button_Read from 12 to 13 ms, then sleeps for 7 ms. Over a whole second the CPU is busy only about 11 % of the time (100 ms of Button_Read, 4 ms of LED_Toggle and 8 ms of UART_Send per 1000 ms) (Demo: LED every 50 ticks, UART every 100: LED_Toggle runs at ticks 1, 51 and 101, UART_Send at 3 and 103, Button_Read at every one of the 103 ticks). A one-shot task such as SCH_Add_Task(Beep_Once, 5, 0) runs once and its slot is free again afterwards (Demo: a one-shot task is deleted: Beep_Once runs at 51 ms in tick 5).
Offsets spread the load
The whole pass of a tick must end before the next tick. Its length is the sum of the tasks due in that tick, so the worst tick is the one where the most work is due together. Without an offset, LED_Toggle (2 ms), Button_Read (1 ms) and UART_Send (8 ms) are all due at tick 1 and again at tick 101, every second: 11 ms of work in a 10 ms tick. A first DELAY of 3 for UART_Send moves it to ticks 3, 103, 203, … where only Button_Read runs with it: the longest pass falls from 11 ms to 9 ms (Demo: offset 3 spreads the load). Pont's advice is to choose the offsets on paper, by listing which tasks are due in each tick over the major cycle (the least common multiple of the periods, 100 ticks here). A cyclic executive takes this one step further and fixes the whole table of what runs in each frame at design time.
Overruns and jitter
If a pass takes longer than a tick, the next SysTick arrives while a task is still running. The interrupt still fires, so SCH_Update keeps counting and nothing is lost from the tick count. But no new pass starts: when the long task returns, the dispatcher only finishes the slots after it and goes to sleep, and the tasks flagged in the slots before it wait for the following tick. With no offset (Demo: no offset, tick 1 overruns), UART_Send runs from 13 to 21 ms, tick 2 arrives at 20 ms and flags Button_Read, and Button_Read runs only at 30 ms: 10 ms late. The time between two runs of a task that should be fixed is now uneven: that unevenness is jitter. The same happens with a one-shot Flash_Write that takes 16 ms (Demo: a task overruns 10 ms (jitter)): Button_Read runs at 50 ms, then not in tick 6, then at 70 ms.
There is a second, quieter effect. The dispatcher runs each task at most once per pass, so the run that was missed is never made up: Button_Read's RunMe stays at 1 after the overrun, and every later run of it comes one tick after the tick that flagged it. Each new overrun adds one more owed run (after 103 ticks without offset, RunMe = 2 and Button_Read has run 101 times instead of 103). Pont's own code also reports errors through a status variable, and many designs add a watchdog timer that resets the chip if a task hangs for good.
Rules for task code
- Every task must return quickly, and the sum of the tasks that can be due in one tick must fit in the tick. Measure the worst-case execution time of each task, not the usual one.
- Split long jobs into short steps: send one UART byte per tick instead of a whole line, or write one flash page per tick from a small state machine. A button debounce state machine is a task of exactly this shape: one short step every 10 ms.
- Keep interrupts besides SysTick few and short, and let them only set flags or fill buffers for a task to handle. How the core orders several interrupts is on Cortex-M interrupt priorities and nesting; timers and memory-mapped I/O in general are on embedded I/O.
- When some tasks need a guaranteed response that a co-operative pass cannot give, a preemptive RTOS with fixed priorities is the next step; rate-monotonic analysis checks such a task set on paper.
See also EDF vs least slack for event-driven schedulers that pick the next job by deadline at run time.
See also Cortex-M exceptions for what the hardware does when SysTick fires: the 8-word stack frame, EXC_RETURN and the 12-cycle entry.
See also embedded software architectures, which builds one device as a polling loop, a loop with interrupts, a function queue and an RTOS and compares their worst-case response.
See also a PID speed loop, a periodic task whose integral and derivative terms rely on its fixed 10 ms period.
See also time-triggered communication, the same table-driven idea on a shared bus, where each node sends in a fixed TDMA slot and the nodes keep their clocks in step.
What the page leaves out
SysTick setup (SysTick_Config(SystemCoreClock / 100)), SCH_Init, SCH_Start and Pont's error reporting (SCH_Report_Status, error codes on a port) are left out, and SCH_Update takes no time here, while a real ISR costs a few microseconds per slot. Task run times are fixed whole milliseconds; real ones vary with the input. Pont's later O(1) designs that keep the tasks in a list sorted by due time, hybrid schedulers that allow one preemptive task, shared-data protection between the ISR and the loop (RunMe is changed by both; real code disables interrupts around the decrement or uses one byte that the core writes in one instruction), and low-power modes deeper than WFI are not shown.