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.
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityMy 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.
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.
Feb 17, 2014 at 4:02
*.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.
Feb 17, 2014 at 4:15
avr-objdump -S -I/path/to/the/sketch/folder xxx.elf
. That is the sketch folder path, not the .ino file itself. Then you should get the C source in the dump.
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.
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?