Go Down

Topic: ATMEGA 328 watchdog....new bootloader or re-code the actual ? (Read 2977 times) previous topic - next topic

FrankRadio

Hi all!

I'm working with a ATMEGA328 and the watchdog functions. My code is this:

Code: [Select]


#include <MsTimer2.h>
#include <avr/wdt.h>
#define LED_GREEN 6
#define LED_RED 7


void setup(){
      MCUSR = 0;
      wdt_disable();
      MsTimer2::set(500, SecondInt);
      MsTimer2::start();
      pinMode(LED_GREEN,OUTPUT);
      pinMode(LED_RED,OUTPUT);
      digitalWrite(LED_RED,HIGH);
     
      wdt_enable(WDTO_4S);

   
  }


void loop(){
// nothing yet!!
}

void SecondInt(){
   wdt_reset();
   digitalWrite(LED_GREEN,!digitalRead(LED_GREEN));
}



Reading a little about the WATCHDOG issue, Atmel saids that we must clear the MSUSR register, because if not the watchdog counter remains until the next power-on condition. This mean that the uP do not run-up until the power will be removed and apply it again....nice thing!! a slave reset technician is need it!  :smiley-yell:

So, I think that someone forget this little detail because that is what is wrong with my cpu, it do not reset until I turn it off, wait some seconds and apply power again, i check too the default fuses values and looks great. (Note: jumping into the reset button do not do nothing) :P

I think that this MSUSR = 0 must be the firts line at the bootloader.

Best Regards!
Frank

johnwasser

The watchdog timer works fine for me on an Arduino UNO R2 with the bootloader from Arduino 1.0.  Does this example not work on your Arduino?

Code: [Select]

//  Watchdog Timer Example
#include <avr/wdt.h>

unsigned long ToggleDelay;  //  When this delay grows to longer than the WDT interval the WDT resets the Arduino

const int LEDpin = 13;

void toggle_led()
{
  digitalWrite(LEDpin, !digitalRead(LEDpin));
}

void setup()
{
  wdt_disable();
  ToggleDelay = 1;   // Start with a very short delay
  pinMode(LEDpin, OUTPUT);
  wdt_enable(WDTO_250MS);  // Set watchdog to 1/4 second
}

void loop()
{
  wdt_reset();
  toggle_led();   // Blinking goes slower and slower until the WDT causes reset, then starts over.
  delay(ToggleDelay);
  ToggleDelay += 5;   // Increase the delay by 5 milliseconds.
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon

The Optiboot loader has it here:

Code: [Select]
/* main program starts here */
int main(void) {
 // After the zero init loop, this is the first code to run.
 //
 // This code makes the following assumptions:
 //  No interrupts will execute
 //  SP points to RAMEND
 //  r1 contains zero
 //
 // If not, uncomment the following instructions:
 // cli();
 // SP=RAMEND;  // This is done by hardware reset
 // asm volatile ("clr __zero_reg__");

 uint8_t ch;

#if LED_START_FLASHES > 0
 // Set up Timer 1 for timeout counter
 TCCR1B = _BV(CS12) | _BV(CS10); // div 1024
#endif
#ifndef SOFT_UART
 UCSR0A = _BV(U2X0); //Double speed mode USART0
 UCSR0B = _BV(RXEN0) | _BV(TXEN0);
 UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
 UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
#endif

 // Adaboot no-wait mod
 ch = MCUSR;
 MCUSR = 0;


So assuming you are using a hard UART that is about the third instruction. But the fastest you can have the watchdog timer reset is 16 mS, so 3 instructions should be plenty of time to disable it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

FrankRadio

The Watchdog in itself works, at my code the timer2 reset the watchdog timer, the issue is when it do not perform the task, I mean if y comment the wdt_reset() call the watchdog acts and reset the board, but because the timer keeps counting the uP do not start again.

I think that this is because the watchdog timer do not reset itself.

I hope that is because we do not clears this counter at the boot-loader functions. It only start again if I recycle the power source.

My boot is a pro 8 Mhz and 3V3, now i will see this boot-loader code and I will change to run at 8Mhz and 3V3, I think that this is the best option.... I'm right??

Best Regards
Frank

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

FrankRadio

I'm using this: ATmegaBOOT_168_atmega328_pro_8MHz.hex, it's inside de atmega folder.

Best Regards!
Frank

FrankRadio

At the bootloader code and makefile i found this:

'-DWATCHDOG_MODS'

making reference to the C code:

Code: [Select]
/* main program starts here */
int main(void)
{
uint8_t ch,ch2;
uint16_t w;

#ifdef WATCHDOG_MODS
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
#else
asm volatile("nop\n\t");
#endif


and the makefile script:
Code: [Select]
atmega328_pro8: TARGET = atmega328_pro_8MHz
atmega328_pro8: MCU_TARGET = atmega328p
atmega328_pro8: CFLAGS += '-DMAX_TIME_COUNT=F_CPU>>4' '-DNUM_LED_FLASHES=1' -DBAUD_RATE=57600 -DDOUBLE_SPEED
atmega328_pro8: AVR_FREQ = 8000000L
atmega328_pro8: LDSECTION  = --section-start=.text=0x7800
atmega328_pro8: $(PROGRAM)_atmega328_pro_8MHz.hex


hemm... i'm confused if de -D directive is #define rigth? so at the latest line we must add '-DWATCHDOG_MODS'
i leave the make as it and add into the C this:
#define WATCHDOG_MODS true

I got the HEX so i will burn to test, I try to decode the C code comment...
// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot....Means that they do not waits for the loading sketch time...because restart without bootloader is not a nice idea!

Best Regards!
Frank

Nick Gammon

As far as I can tell, it was not compiled with WATCHDOG_MODS. But it's hard to be certain.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

FrankRadio

Hi Nick!!

Was that missing #define, now works great and the uP reset by itself and runs again without any issues, i will let it with the watchdog reset mechanism to test if it reset and restart correctly for a several hours.

Best Regards!
Frank

FrankRadio

Hi Nick!
Is the opti ready to upload it a 328 with 3V3 PS and 8Mhz as clock?
The source code of this bootloader are in the download section of the site right?

Best Regards!
Frank

Nick Gammon

I'm not sure, you need to quote the file name. I thought it was on GitHub.

You would need to compile it so it knows you are running at 8 MHz or the timings will be out.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Jack Christensen

Not reading this too closely, but if you're looking for an 8MHz version of Optiboot, I've been using this one:

http://arduino.cc/forum/index.php/topic,101084.msg758248.html#msg758248
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Nick Gammon

What's the board choice for that? Arduino Pro Mini at 8 MHz? Or something else?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

FrankRadio

Something else is the correct answer!.
And the ELSE is the best description to my boards, I really love this platform so every time that I like to start some project I design and then build my own pcb.

Is less headaches with the wires and connections, for little wild time I'm using 8MHz and sometimes 3V3 and others 5V as PS

In fact I think that is not a good idea use a 16MHz crystal I prefer use a TCXO or similar for higher frequencies.

It's what I like, if the application don't control AC charges or similar do not happens nothing but.... why to test !!

I got some experience too with the Wiring platform doing some small mods to the bootladers about this PS and clocks.

Best Regards!!
Frank

Go Up