Go Down

Topic: Never returning for loop()? (Read 1 time) previous topic - next topic

enderw88

New to Arduino, but pretty experienced coder.  The idea of the Arduino calling loop() repeatedly looks a lot like cooperative multitasking (ala Win3.1 and early MacOS).  So my question is this: if loop() calls a function that never returns, what do I lose?  I assume there is some housekeeping going in the "background". For example:

void setup(){
  // some stuff
}

void loop(){
  goawayforever();
}

void goawayforever(){
  while(1){
    //do some stuff;
  }
}


I have looked for the source code that runs in the Arduino but have been stymied.  

Craig

Korman

#1
Sep 25, 2010, 04:29 pm Last Edit: Sep 25, 2010, 04:32 pm by Korman Reason: 1
Craig,

your thoughts are too complicated, it's far more primitive than that. Look into arduino/hardware/arduino/cores/arduino/main.cpp and you'll see this:
Code: [Select]
int main(void)
{
       init();
       setup();
       for (;;)
               loop();
       return 0;
}


This should answer all your questions.

Korman

deSilva

Also, it would not need co-operative multitasking (which is not so difficult on theArduino), but pre-emptive multitasking.

However there is an implementation of a FreeRTOS (DuinOS)
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256745982/150

enderw88

@Korman,
   Indeed, I was overthinking...thanks for the concise answer!


Andy Brown

The only stuff going on in the background is the interrupt service routine that keeps track of clock ticks. You can see that in wiring.c. Everything else that happens is because you made it so, whether you meant to or not  ;)
Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

Go Up