Go Down

Topic: Reseting in Software (Read 2 times) previous topic - next topic

JanD

Hello,

is there a way to reset the Arduino (well, it is an ATmega644, programmed with the bootloader from here)?

I have read a bit and found out you can only use the WDT timer, but it seams the Arduino bootloader was in the way for this. Is it the same for the ATmega644 bootloader?

Jan

PaulS

It would be much better if you fixed the problem that requires you to reset the Arduino.

JanD

It's not a problem, it's a feature. Then I send a specific command via Serial, the board should "start from the beginning", with other words, reseting.

Jan

PaulS

Quote
Then I send a specific command via Serial, the board should "start from the beginning", with other words, reseting.

What is it that you think "start from the beginning" should do? Resetting the Arduino is almost certainly not what needs to be done. If it really is, just close and reopen the serial port. The Arduino will reset when you do that.

JanD

#4
Dec 01, 2011, 01:31 pm Last Edit: Dec 01, 2011, 01:36 pm by JanD Reason: 1
Quote
What is it that you think "start from the beginning" should do?

Reinit all variables, simply start everything from the beginning, as if the power just would have been connected.
Quote
If it really is, just close and reopen the serial port.

Ahh.. If it only would be so simple...

Ok, the problem is, I have to disable to serial reset, because it may happen the ATmega "changes master" (program controlling it). But, I still need to reset it at some points, so for that I need to have a reset command that executes a software reset.

Jan

EDIT: Small change: In my first post I sad the wrong link for the bootloader. I use the bootloader from here, it's the core files I'm using from avr-developers.

AWOL

You could do the same "call 0" that the bootloader does; as has been pointed out many times here before (I'm a little surprised your search didn't find them), it isn't exactly a reset, but it is as close as you can get with a software-only solution, unless you invoke the watchdog.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JanD

Do you mean "asm volatile ("jmp 0x0000");" when you say "call 0"?

If yes; that isn't enough, the variables wouldn't be reinited then.
If no; haven't found anything like that, what exactly does it do?

I don't have any problems with using the WDT timer, but:
Quote
I have read a bit and found out you can only use the WDT timer, but it seams the Arduino bootloader was in the way for this. Is it the same for the ATmega644 bootloader?


Jan

AWOL

#7
Dec 01, 2011, 01:53 pm Last Edit: Dec 01, 2011, 01:58 pm by AWOL Reason: 1
Quote
the variables wouldn't be reinited then

Why not?

Quote
Do you mean "asm volatile ("jmp 0x0000");" when you say "call 0"?

No, when I say "call", I mean "call".
Code: [Select]
void (*app_start)(void) = 0x0000;
//...
//...
app_start();
(Source: ATmegaBOOT_168.c)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JanD

Because:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242847196 Third post
http://answerpot.com/showthread.php?2498719-Fwd%3A++arduino+software+reset Second post

There are other, similar posts on the net too.

Jan

AWOL

I presume you're referring to this:
Quote
All of the chip's registers, settings and RAM will not be properly initialized.

As far as your sketch is concerned, it will appear as though it has just been booted for the first time, because it will have followed the same path through the pre-"main" initialisation of your sketch.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JanD

Ok, I didn't knew that. How is it with the registers?

JanD

Quote
Quote
All of the chip's registers, settings and RAM will not be properly initialized.


Ups... Didn't see that.

Ok, thanks!

Jan

bperrybap

#12
Dec 02, 2011, 01:38 am Last Edit: Dec 02, 2011, 04:58 am by bperrybap Reason: 1
Jumping to 0x0000, IMHO is a total kludge.
Why are people afraid of correcting the bootloader when there is a problem?
It is open source.....
Just fix the bootloader and be done with it so you can do a real software driven reset
if that is what you want.
Unlike jumping to 0x000, it allows everything to start up clean and fresh.

Jan, the 644 seems to be using an older version of the atmegaBOOT bootloader but the fix
should be the same:
In the bootloader:

Replace this:
Code: [Select]
#ifdef ADABOOT          // BBR/LF 10/8/2007 & 9/13/2008
   ch = MCUSR;
   MCUSR = 0;

   WDTCSR |= _BV(WDCE) | _BV(WDE);
   WDTCSR = 0;

   // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
   if (! (ch &  _BV(EXTRF))) // if its a not an external reset...
     app_start();  // skip bootloader
#endif




with this:
Code: [Select]
#ifdef ADABOOT
ch = MCUSR;
MCUSR = 0;
wdt_disable();

// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
if (! (ch &  _BV(EXTRF))) // if its a not an external reset...
app_start();  // skip bootloader
#else
asm volatile("nop\n\t");
MCUSR = 0;
wdt_disable();
#endif


Build a new bootloader and then burn it in your chip and enjoy.

Now if you have recent gcc tools, you may also find that your bootloader may no
longer compile.

If you have errors releated to EECR or EEPE,
you will need to also make this change:
Add this up at the top:
Code: [Select]
#include <avr/eeprom.h>
then change this:
Code: [Select]
                                       while(EECR & (1<<EEPE));
to
Code: [Select]
                                       while(!eeprom_is_ready());

Then it should compile again.
Once you get it in your AVR, you can use something like this to reset the AVR:
Sample sketch to demonstrate software control of reset:


Code: [Select]
#include <avr/wdt.h>

void setup()
{
Serial.begin(9600);
Serial.println("setup()");
}

void loop()
{
Serial.println("Top of loop()");
for(int x = 3; x ; x--)
{
Serial.println(x);
delay(1000);
}
Serial.println("Reseting Arduino");
#if ARDUINO >= 100
       delay(50); // wait for transmission to finish
#endif
resetArduino();
}

void resetArduino()
{
wdt_enable(WDTO_15MS);
noInterrupts();
while(1); // wait to die and be reborn....
}




If your watchdog reset is working, you will see the messages repeating.
If you have a broken WDT bootloader, you will see it the first time and
then the chip will hang in the bootloader until you manually reset it.
--- bill



JanD

Thanks, I will test that when I get home.

Jan

JanD

Hm.. it compiled without problems, but:
Code: [Select]
.....\hardware\atmega324_644\bootloaders\atmega644p>avrdude -cdragon_isp -patmega644 -P usb -U flash:w:ATmeg
aBOOT_644_NEW.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.18s

avrdude: Device signature = 0x1e9609
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed

         To disable this feature, specify the -D option.
avrdude: conflicting -e and -n options specified, NOT erasing chip
avrdude: reading input file "ATmegaBOOT_644_NEW.hex"
avrdude: input file ATmegaBOOT_644_NEW.hex auto detected as Intel Hex
avrdude: ERROR: address 0x10010 out of range at line 130 of ATmegaBOOT_644_NEW.h
ex
avrdude: write to file 'ATmegaBOOT_644_NEW.hex' failed

avrdude: safemode: Fuses OK

avrdude done.  Thank you.


Why this???

Jan

Go Up