Soft reset

Hi,

I wonder if someone could explain this code snippet to me? I stumbled across it while searching for a way to do a soft reset of the arduino. It works very well, but I get the feeling it is something that is not done in polite company!!

void(* resetFunc) (void) = 0;  // Soft reset function - Not sure how works!
void wakeUp()  
{ 
  resetFunc();
}

Call zero.
It isn't any kind of reset, but it is close.
A reset signal does stuff to the hardware and then goes via zero.
Most times it'll do almost the same things and work.

I'm under the impression that it resets the code to the beginning of loop() ?
What do you mean by call zero?
Thanks for the reply!

It calls the reset vector, which is quite a while before loop (or setup) is called.

Right... I'm getting there...
So does this function create a 'pointer' to memory address 0 (reset vector) and then begin executing that code? Ie. bootloader etc?
Is this a bad thing to do?

No, it is not a bad thing to do, it is just not exactly a processor reset, but, as I said earlier, most times, you won't notice the difference.

Thanks for that AWOL, it makes sense now!! :slight_smile:
Just out of curiosity if I changed the 0 to say a 8, I'm guessing strange things would happen?

Why would you go via an interrupt vector?

Not sure what you mean?

According to page 66 of the manual address 0 will contain:

jmp RESET

Thus jumping to 0 will then jump to wherever it normally goes on a hardware reset.

However address 8 contains:

jmp PCINT1

So that would take you to the handler for PCINT1.

I'm not sure why they would code a WakeUp like that, as you can make the watchdog timer do a system reset.

Plus you shouldn't really need to do a reset. That's like writing a Windows program that needs to power the PC off and on again.

Plus, the reset isn't a hardware reset, so assumptions made in the reset handler won't necessarily be valid.

I did not know you could jump to a memory address like that! Thats nifty!
The WakeUp() function is coded that way as I needed a way to get back to the main loop of my code and I wasn't sure how else to achieve that.
I needed a reset function as this stems from a project, receiving IR input from one remote and transmitting the appropriate IR signals to TV/XBMC etc.
The library I'm using for handling IR doesn't support receiving and sending of IR signals in the same sketch so would freeze, requiring a reset.
I should probably do the reset via a watchdog timer? I've stumbled over this, but the resetFunc seemed easier. I also tried connecting pin D2 to reset and pulling it high but that didn't work!
Thanks for your reply Nick Gammon, wasn't aware that you could jump to memory address' like that!

I did not know you could jump to a memory address like that! Thats nifty!

It isn't a jump, it is a call.

Ah - so that would cause the PCINT0 (interrupt handler) to run?

The C code is a call, the assembler is jump.

Ah - thank you!

AWOL is right, it calls address 0. One has to hope that the stack pointer gets reset during reset processing, otherwise each time you "call" reset you will use up some of the available stack.

inboxjason:
The library I'm using for handling IR doesn't support receiving and sending of IR signals in the same sketch so would freeze, requiring a reset.

Libraries can be changed, you know. I don't like the idea of deliberately resetting a sketch because "it would freeze". You should really address why it freezes and fix that.

However the watchdog timer can be set up to generate a reset.