cancel
Showing results for 
Search instead for 
Did you mean: 

Does main() require an infinite loop?

The BOOM
Associate III

This page says:

"With a custom main() on Arduino, do not return from main(). As there is no operating system running on a standard Arduino, there is no where to return to. If you do return, abort() is called, then exit(). On the AVR-8 MCU ( Uno, Mega ) this results in global interrupts being disabled and execution enters an infinite loop. To overcome the eventual return, you can utilize an infinite loop of your own, which is not unlike what the Arduino core does itself."

Does this same rule apply to the STM32 MCUs?"

thx

21 REPLIES 21

Your Arduino model is this

void main(void)

{

setup();

while(1) loop();

}

You're not running an OS, so when you drop out of main() where does it go? It is a universe where there is no track, what happens to the train? No accommodation has been made unless you make it. In most cases it just crashes, it goes into a Hard Fault handler if you're lucky, and that's got a priority just below an NMI, so what interrupt is going to get serviced.

If you're doing high speed ADC, use a DMA, with a buffer large enough to decimate your interrupt loading to a point you want/have to deal with it. Hold 1ms of data, hold 100ms..

A while(1) __WFI(); loop halts the processor until the next interrupt comes a long, this is the least power consuming mode you can have while still being able to respond to an interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
@Tesla Your Arduino model is this
void main(void)

Really? I thought Arduino uses setup() and loop(), not main().
when you drop out of main() where does it go? It is a universe where there is no track, what happens to the train? No accommodation has been made unless you make it. In most cases it just crashes
unclear. You’re saying that when main() finishes, STM32 crashes? Really? All the time? So, every STM32 app in the world crashes when main() finishes?
If you're doing high speed ADC, use a DMA, with a buffer large enough to decimate your interrupt loading to a point you want/have to deal with it. Hold 1ms of data
Thx for that! But not sure DMA is the right fit for my application. I’m not trying to store the data, just sample it. After I examine the sample, I discard it.
A while(1) __WFI(); loop halts the processor until the next interrupt comes a long, this is the least power consuming mode
That was already mentioned, above. I’m not concerned with power-consumption, I just don’t like running infinite loops that don’t do anything.
Thx