What is Super Loop in Embedded C programming language?
-
The infinite loop which services all the requests.. – Haris Jun 08 '17 at 07:38
-
It is an endless loop where tasks are called sequentially – LPs Jun 08 '17 at 07:38
-
To begin with, the C programming language is the C programing language. It doesn't matter if you're targeting small embedded systems or big super-computers. There's no special "embedded C programming language", it's just plain C. As for the term "super loop", where did you hear about it? Where did you read about it? It's not a common term in C. And have you tried *searching* for it? – Some programmer dude Jun 08 '17 at 07:38
-
10There's no need to "bandwagon-whine" at the OP. No clarification is needed, since the term is well-known to those with domain knowledge. https://en.wikibooks.org/wiki/Embedded_Systems/Super_Loop_Architecture. If you haven't heard of it, then that might be because you don't have the domain knowledge needed to respond to questions tagged embedded. Off you go, to find other tags. – Lundin Jun 08 '17 at 07:42
-
1@Someprogrammerdude There is a technical report ([ISO/IEC TR 18037:2008](https://www.iso.org/standard/51126.html)) for extensions to the C programming language to support embedded processors. – Jun 08 '17 at 07:49
4 Answers
This refers to the eternal loop usually located in main()
of a "bare metal" system (no OS), since such systems can never return from main. A typical bare metal embedded system looks like this:
void main (void)
{
// various initializations
for(;;) // "super loop" or "main loop"
{
// do stuff
}
}

- 195,001
- 40
- 254
- 396
MCU is device which runs continuously or better, it executes instructions when power is on (in general).
So while loop is here to force MCU to do something, even if loop is empty, it will just circle around.
But it must do something as it is not the same as PC program where you have return
at the end of main function.
If you wouldn't have super loop then MCU can get instruction from FLASH/RAM (whatever..) and do something stupid things as MCU don't know what it is executing. It just executes code you provide him.
By using super loop, you guarantee MCU won't just uncontrollable execute some instructions and maybe go to fail-safe area. Of course this can happen even if you have super loop but this is other topic.
int main() {
//Init if you have something
while (1) {
//DO stuff always
}
return 0; //This should never happen!
}

- 7,246
- 2
- 25
- 40
-
The loop would be the reason why `int main()` is most likely an incorrect form to use for most compilers. Most compilers, like for example gcc, use `void main (void)` when compiling for a freestanding system. – Lundin Jun 08 '17 at 07:45
-
It is not incorrect, it may just look odd. But it does not mean it is not correct. – unalignedmemoryaccess Jun 08 '17 at 07:47
-
It will generate an incorrect calling convention when main() is called from the "CRT" start-up code, pointlessly consuming extra stack memory. So yeah it is incorrect, on any freestanding system I know. – Lundin Jun 08 '17 at 07:49
-
I somehow believe to static code analyzer tools that `int main` is not incorrect way of doing it. Most compilers know (even *gcc* is that smart) than it won't give you a warning if you use `int main` without return statement. – unalignedmemoryaccess Jun 08 '17 at 07:53
-
@Lundin When I do `void main` I got warning *Return type of main is not **int** [-Wmain]* in GCC. – unalignedmemoryaccess Jun 08 '17 at 08:00
-
GCC defaults to a hosted system compiler. You need to compile for freestanding systems `-ffreestanding`. Overall, lots of brainwashing have been done towards programmers when it comes to the format of main. The format is actually quite flexible, as far as the standard is concerned. https://stackoverflow.com/a/31263079/584518 – Lundin Jun 08 '17 at 08:02
Super loop is an infinite loop which is suitable only in embedded c programming because there you have to run your code for very very long time and wants explicitly terminate when the behavior is change for your robot or whatever. Superloop be like
while(1){
// Your code
// exit condition
}
for(;;) {
}

- 1,329
- 2
- 15
- 40

- 768
- 3
- 9
- 25
-
1You 'while' -loop looks like a superloop, except there will be no exit. Weird 'for' -loop that in your example captures control flow after it exists the superloop, is unnecessary since as a rule superloops in embedded devices do not exit. – Tammi May 21 '20 at 09:15
Super loop (or superloop) is a design pattern that often is chosen by embedded real-time firmware developers for very small systems (especially, "bare metal" systems) in which simplicity is an important design goal.
The super loop ("super" means "above" in Latin) is the program's top-level loop. An especially simple implementation might look like this:
int main() {
initializeGlobalState();
while (1) {
sampleInputs();
updateGlobalStateBasedOnNewInputs();
updateOutputs();
}
// can't ever get here but the compiler may require a `return` statement.
return 0;
}
Simplicity is the goal, but the pattern allows for small complications. For example, one could implement a rudimentary priority scheme:
initializeGlobalState();
while (1) {
sampleHighPriorityInputs();
if (updateStateBasedOnNewHighPriorityInputs()) {
updateOutputs();
continue;
}
sampleOtherInputs();
updateGlobalStateBasedOnNewOtherInputs();
updateOutputs();
}
One can do scheduling:
initializeGlobalState();
while (1) {
timeNow=readFreeRunningTimer();
sampleInputs();
updateGlobalState();
if (timeNow > task1_dueDate) {
doTask1();
task1_dueDate += task1_fixedPeriod;
}
if (timeNow > task2_dueDate) {
doTask2();
task2_dueDate = computeTask2DueDateBasedOnGlobalStateOrInputs();
}
...
updateOutputs();
}
One can slow things down to save power:
initializeGlobalState();
enableHeartbeatInterrupt();
while (1) {
...
WAIT_FOR_INTERRUPT;
}
Other complications are possible, but the down side of superloop is, it becomes unwieldy and brittle if you try to add too many of them. When that happens, the developer may find themselves wishing they had built the project on a Real-time OS and/or an application framework.

- 25,130
- 5
- 37
- 57