Using WatchDog in absence of Bootloader

Dear Everyone! Here I am try to summarize my study regarding "Using Watchdog features on Arduino (Nano) etc where Bootloader not present"

Please note that bootloaders have some initializing codes which missing in avr/wdt.h

The watchdog timer is managed by the WDTCSR register.

Two FUSE BITS are important WDTON and BOOTRST.
WDTON set (0) will generate system reset when Dog Bite else depends on WDTCSR register.
BOOTRST should not be programmed if no bootloader present, to get start from address zero.

// Definition of interrupt names
#include <avr/io.h>
// ISR interrupt service routine
#include <avr/interrupt.h>

#define wdt_reset() __asm__ __volatile__ ("wdr")
const int WD_LED = 13; // LED at Pin13
int counts;
// Jump to address zero
// void(* resetFunc) (void) = 0;//declare soft reset function at address 0

//Watchdog Interrupt
ISR(WDT_vect)
{
flash();
}
void setup()
{
Serial.begin(9600);
pinMode(WD_LED, OUTPUT);
digitalWrite(WD_LED, LOW);
cli();
wdt_reset();
// Enable Watchdog Configuration mode for 4 clock cycles only:
WDTCSR |= (1<<WDCE) | (1<<WDE); // WDTCSR |= B00011000;
// Configur Watchdog
// WDIE = 1: WDT Interrupt Enable
// WDE = 1 :System Reset Enable
WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (0<<WDP0); // 1-Sec WDT with Interrupt and System Reset

sei();
Serial.print("WDTCSR: ");
Serial.println(WDTCSR, HEX);
Serial.println("Setup finished.");
}
void loop()
{
Serial.println(counts++); // do anything
delay(1500); // change argument to 1500 -> watchdog will be active DOG-BITE
wdt_reset();
}
void flash()
{
static boolean output = HIGH;
digitalWrite(WD_LED, output);
output = !output;
// resetFunc();  //Jump to address / soft reset
}
1 Like

So you found the top secret documentation on Atmega328 and actually read the 4 pages on WDT. Congratulations.

3 Likes

As I understand it, WDTON fuse causes the WDT to be enabled at reset/poweron, so that if your code does nothing else with the WDT, your application will reset every 16ms or so (default WDT timeout.) Your code needs to start doing wdt_reset()s right away.

You don't NEED to set WDTON; you can also enable the WDT explicitly in your code. In that case, you'd have up to the time that you enable the WDT where no resets would occur (unless the chip was reset by a WDT event. In that case, the WDT stays enabled.)

Is the above statement correct?

By default, the BOOTRST fuse bit (of ATmega328P MCU) is unprogrammed (Fig-1, prepared/composed based on data sheets) which means that the MCU will boot from location 0x0000; else, the MCU will begin program execution from the starting address of "Boot Loader Space" determined by BOOTSZ0-BOOTSZ1 fuse bits:


Figure-1:

No.
If no bootloader is present, BOOTRST should be "set" (1).
There's a lot of confusing introduced by fuses whose "off" state is 1 (caused by electrical considerations, AFAIK. An "unprogrammed" Fuse/EEPROM/Flash cell is a 1.)

1 Like

Thanks dear! corrected.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.