Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

In either of setup or loop, if I were to add an exit(0) call, where would control be passed to? What would the next state of the microcontroller be? Would it stop execution and power down?

I am using a revision 2 Arduino Uno.

share|improve this question
    
I think it just stops. It wouldn't shut down the clock, or power down. –  TheDoctor Feb 14 at 1:34
    
The rest of the memory should be filled with NOP assembly statements, which just pause for a couple clock cycles –  TheDoctor Feb 14 at 1:35
add comment

2 Answers 2

up vote 8 down vote accepted

My initial guess is wrong. I would have thought it would simply return from loop and the core library would just call loop() again. However, I see the following code was created. Noticing that __stop_program is a hard loop...

An extract of Blink.ino's listing, with exit(0) added:

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  exit(0);
}

The disassembly of the above:

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 100:   80 91 00 01     lds r24, 0x0100
 104:   61 e0           ldi r22, 0x01   ; 1
 106:   0e 94 ca 01     call    0x394   ; 0x394 <digitalWrite>
  delay(1000);               // wait for a second
 10a:   68 ee           ldi r22, 0xE8   ; 232
 10c:   73 e0           ldi r23, 0x03   ; 3
 10e:   80 e0           ldi r24, 0x00   ; 0
 110:   90 e0           ldi r25, 0x00   ; 0
 112:   0e 94 f7 00     call    0x1ee   ; 0x1ee <delay>
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 116:   80 91 00 01     lds r24, 0x0100
 11a:   60 e0           ldi r22, 0x00   ; 0
 11c:   0e 94 ca 01     call    0x394   ; 0x394 <digitalWrite>
  delay(1000);               // wait for a second
 120:   68 ee           ldi r22, 0xE8   ; 232
 122:   73 e0           ldi r23, 0x03   ; 3
 124:   80 e0           ldi r24, 0x00   ; 0
 126:   90 e0           ldi r25, 0x00   ; 0
 128:   0e 94 f7 00     call    0x1ee   ; 0x1ee <delay>
  exit(0);
 12c:   80 e0           ldi r24, 0x00   ; 0
 12e:   90 e0           ldi r25, 0x00   ; 0
 130:   0e 94 1e 02     call    0x43c   ; 0x43c <_exit>

...

0000043c <_exit>:
 43c:   f8 94           cli

0000043e <__stop_program>:
 43e:   ff cf           rjmp    .-2         ; 0x43e <__stop_program>

Note that if _exit had not called cli, interrupts would be able to do stuff. But that is not the case.

share|improve this answer
    
avr-objdump -S {compiled *.elf file} produces a file that includes the C code that lead to each section of assembly code. It's much easier to follow. –  Connor Wolf Feb 17 at 4:02
    
aaaand I just tried it, and it's not correctly emitting inline C code for the loop function. What the hell? –  Connor Wolf Feb 17 at 4:08
    
Whoa, extremely strange. I compiled the project with Stino instead of the arduino editor, decompiled the *.elf from that, and then I get the proper debugging symbols. I think the Arduino text-editor/button-macro (I refuse to call it an IDE since it's not) thing is stripping the debug info from just the compiled main C++ file, for some bizarre and stupid reason. –  Connor Wolf Feb 17 at 4:15
add comment

Well I just tested it with my Arduino Uno and it just completely stopped the code and left all the outputs as they were when the code stopped running (so it left an LED I had on on). There seems to not be a IO cleanup when you call exit. This was what I expected because the Arduino IDE provides the setup and loop functions, if you program the ATMEGA*28 with with any other AVR IDE you start with the main function like all C/C++ programs. The setup and loop functions are not standard on AVR MCU's.

Note: The press of the reset button restarts the code, if you were wondering.

share|improve this answer
    
Good to know. I was looking for something more detailed and at a lower level. On calling exit(0) the disassembled instructions are (IIRC) __stop_program, cli and a spinlock. I wanted to verify if that is correct with an explanation of how the control is passed i.e. call stack pop?, ISR call? –  AsheeshR Feb 14 at 2:05
    
Ah, well I haven't looked into arduino on such a low level, for that information you might want to check the atmel website. –  jamolnng Feb 14 at 2:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.