What is a super loop (foreground/background) architecture?
Short answer: A super loop is the simplest firmware structure: main() initialises the hardware and then runs an infinite loop that calls each task function in turn. Interrupts (the foreground) handle time-critical events quickly and set flags or fill buffers; the loop (the background) does the processing. It needs no RTOS and is easy to understand, but the response time of every task depends on the total time of one pass of the loop.
int main(void)
{
clock_init();
gpio_init();
uart_init();
adc_init();
for (;;) {
if (uart_rx_available()) handle_command(); /* each task must return quickly */
if (adc_flag) { adc_flag = false; filter_sample(); }
update_display();
kick_watchdog_if_healthy();
__WFI(); /* sleep until the next interrupt */
}
}
Rules for making it work
- Every task must be non-blocking: do a small amount of work and return; long operations become state machines.
- ISRs do the minimum (capture data, set flags).
- Use timestamps for periodic work instead of delay loops.
- Measure the worst-case loop time; the worst-case response time of a task is about one full loop.
Advantages: tiny code size, no RTOS licensing or learning curve, fully deterministic order, easy to debug, no stack-per-task overhead.
Disadvantages and when to move on
- A slow task (writing to flash, a long calculation) delays all others.
- Priorities are only possible through ISRs or by reordering/skipping work.
- Complex timing requirements, many independent activities, or blocking protocol stacks (TCP/IP, USB, Bluetooth) become hard to manage.
Next steps up: a cooperative scheduler (time-triggered task table, each task run at its period), event-driven frameworks (active objects with event queues, e.g. QP), or a pre-emptive RTOS (FreeRTOS, Zephyr).