ATmega328 Watchdog

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys&gals,

I have recently started playing around with an Arduino Pro Mini board, and I intend to upload code remotely via bluetooth. I have a bluetooth module and everything works fine, but I still have to manually press the reset button in order to start the upload. This is obviously annoying, so I want to trigger the reset via software using the watchdog.

However, once the watchdog is triggered, it seems that the chip enters an endless reset loop. I understand that this usually happens because the watchdog is still active after the reset, and I tried to enter the code to disable it. This is the minimal example I tried to run, it should turn on the LED and then reset after 2 seconds:

/*
 * WatchdogResetTest.cpp
 *
 * Created: 26.11.2013 12:34:54
 *  Author: Christoph
 */


#include 
#include 
#include 

/*Disable the watchdog to prevent infinite reset loop */
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
  
void wdt_init(void)
{
    MCUSR = 0;
    wdt_disable();
    return;
}

int main(void)
{
    _delay_ms(2000);
    PORTB ^= (1 << 5);   
    _delay_ms(2000);
    wdt_enable(WDTO_15MS);
   
    while(1)
    {
       
    }
} 

However, after the first reset it apparently enters an infinite reset loop. I also tried moving it to .init1, but both options don't seem to work. Does anyone have an idea where my problem might lie?

Thanks in advance, Christoph

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

it apparently enters an infinite reset loop

How are you determining that?

If you have another LED available I'd consider doing something with it to know when you are in that wdt_init code.

Also I don't see you setting DDRB do how is your existing PORTB.5 code working anyway?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately that's the only LED. However, it gets turned on nicely the first time the program runs.

I assume that it's a reset loop because the LED always flashes briefly after a reset, and it's blinking very rapidly after the first time the watchdog resets the chip, and only a power cycle stops this behaviour.

There are some unlisted dependencies that I don't have to explicitly include in the file (I'm using Atmel Studio), that's why PORTB is working fine.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also just tried sending a small message to my terminal via USART at the beginning of main, but it only gets sent the first time the program runs.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To see if there is something bad with wdt_disable() I would try

void wdt_init(void)
{
   asm volatile("cli");               
   asm volatile("wdr");               //watchdog reset  
   MCUSR &= (1<<WDRF);                
   WDTCSR |= (1<<WDCE) | (1<<WDE);    
   WDTCSR = 0;
   asm volatile("sei");               
}

Or

void wdt_init(void)
{
    MCUSR = 0;
    wdt_enable(WDTO_4S);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unchanged behaviour for both suggestions :/

Might the following be the problem? The Arduino Pro Mini is from Sparkfun and already has a bootloader installed, I'm using a USB to Serial converter to program it. Is it possible that the watchdog kicks in before the bootloader has finished and my code never gets executed?
I can't burn the bootloader as I don't have the equipment yet unfortunately, so I can't verify this hypothesis, but does it sound reasonable? :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

already has a bootloader installed, I'm using a USB to Serial converter to program it. Is it possible that the watchdog kicks in before the bootloader has finished and my code never gets executed?

Extremely possible. I bet the bootloader waits to see some trigger and I bet it waits longer than 15ms.

Also check the source of the bootloader - it's often the case that a bootloader already uses WDT for its own purposes (to reset before entering the app) so that could further complicate things.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bah, I should have stayed with TI, stupid Arduino :P

Thanks for the hints, I'll check it out. I just noticed that I got another Arduino Pro Mini here that can act as an ISP to burn the bootloader, will report back if it works or not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yup you can load the ISP sketch into any Arduino and use it as an ISP programmer for a standalone chip or another Arduino.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah, now I remember I had the same problem and I had to disable watchdog in my AVR109 bootloader code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:
already has a bootloader installed, I'm using a USB to Serial converter to program it. Is it possible that the watchdog kicks in before the bootloader has finished and my code never gets executed?
Extremely possible. I bet the bootloader waits to see some trigger and I bet it waits longer than 15ms.
It's been well over a year since I touched a pro mini, but I had similar issues with the bootloader not playing nice with the watchdog.

The source for the Pro Mini's bootloader includes this:

#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

... but examination of the pre-built .hex files for the pro mini line (of which there are 4) suggests that only one of them was built with -DWATCHDOG_MODS:

Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168

The remaining three lack any reference to MCUSR or WDTCSR:

Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328

To the OP: which Pro Mini do you have? You could use avrdude to read flash and then wash it through avr-objdump to confirm that the bootloader's handling (or not) of MCUSR/WDTCSR.

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but examination of the pre-built .hex files for the pro mini line (of which there are 4) suggests that only one of them was built with -DWATCHDOG_MODS:

oh wow, that explains why the precompiled bootloader didn't change anything, stupid that see your answer only now :/ I tried various bootloaders this afternoon, but none worked (they either didn't solve the problem or didn't start the program at all). Unfortunately it seems to be rather hard to recompile the bootloaders on Windows, and I don't have a Linux right here.

I have an Arduino Pro Mini 328, 3.3V/8MHz

Could you maybe compile the correct file for me and put it online? And if you could also compile a version in which the bootloader is not skipped after a watchdog reset, I would be very grateful :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ushtarador wrote:
Could you maybe compile the correct file for me and put it online?
Attached.
Quote:
And if you could also compile a version in which the bootloader is not skipped after a watchdog reset...
Also attached. I wrapped the relevant code with an #if/#endif:
#ifndef ALWAYS_BOOTLOAD
	// 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

JJ

Attachment(s): 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thousand thanks, it worked like a charm, you're awesome! :) Now onwards to the next problem :P