.TH ": Watchdog timer handling" 3avr "Wed Jun 4 2014" "Version 1.8.0svn" "avr-libc" \" -*- nroff -*- .ad l .nh .SH NAME : Watchdog timer handling \- .SS "Macros" .in +1c .ti -1c .RI "#define \fBwdt_reset\fP() __asm__ __volatile__ ('wdr')" .br .ti -1c .RI "#define \fBwdt_enable\fP(value)" .br .ti -1c .RI "#define \fBwdt_disable\fP()" .br .ti -1c .RI "#define \fBWDTO_15MS\fP 0" .br .ti -1c .RI "#define \fBWDTO_30MS\fP 1" .br .ti -1c .RI "#define \fBWDTO_60MS\fP 2" .br .ti -1c .RI "#define \fBWDTO_120MS\fP 3" .br .ti -1c .RI "#define \fBWDTO_250MS\fP 4" .br .ti -1c .RI "#define \fBWDTO_500MS\fP 5" .br .ti -1c .RI "#define \fBWDTO_1S\fP 6" .br .ti -1c .RI "#define \fBWDTO_2S\fP 7" .br .ti -1c .RI "#define \fBWDTO_4S\fP 8" .br .ti -1c .RI "#define \fBWDTO_8S\fP 9" .br .in -1c .SH "Detailed Description" .PP .PP .nf #include .fi .PP .PP This header file declares the interface to some inline macros handling the watchdog timer present in many AVR devices\&. In order to prevent the watchdog timer configuration from being accidentally altered by a crashing application, a special timed sequence is required in order to change it\&. The macros within this header file handle the required sequence automatically before changing any value\&. Interrupts will be disabled during the manipulation\&. .PP \fBNote:\fP .RS 4 Depending on the fuse configuration of the particular device, further restrictions might apply, in particular it might be disallowed to turn off the watchdog timer\&. .RE .PP Note that for newer devices (ATmega88 and newer, effectively any AVR that has the option to also generate interrupts), the watchdog timer remains active even after a system reset (except a power-on condition), using the fastest prescaler value (approximately 15 ms)\&. It is therefore required to turn off the watchdog early during program startup, the datasheet recommends a sequence like the following: .PP .PP .nf #include #include uint8_t mcusr_mirror __attribute__ ((section ("\&.noinit"))); void get_mcusr(void) \ __attribute__((naked)) \ __attribute__((section("\&.init3"))); void get_mcusr(void) { mcusr_mirror = MCUSR; MCUSR = 0; wdt_disable(); } .fi .PP .PP Saving the value of MCUSR in \fCmcusr_mirror\fP is only needed if the application later wants to examine the reset source, but in particular, clearing the watchdog reset flag before disabling the watchdog is required, according to the datasheet\&. .SH "Macro Definition Documentation" .PP .SS "#define wdt_disable()" \fBValue:\fP .PP .nf __asm__ __volatile__ ( \ "in __tmp_reg__, __SREG__" "\n\t" \ "cli" "\n\t" \ "out %0, %1" "\n\t" \ "out %0, __zero_reg__" "\n\t" \ "out __SREG__,__tmp_reg__" "\n\t" \ : /* no outputs */ \ : "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)), \ "r" ((uint8_t)(_BV(_WD_CHANGE_BIT) | _BV(WDE))) \ : "r0" \ ) .fi Disable the watchdog timer, if possible\&. This attempts to turn off the Enable bit in the watchdog control register\&. See the datasheet for details\&. .SS "#define wdt_enable(value)" \fBValue:\fP .PP .nf __asm__ __volatile__ ( \ "in __tmp_reg__,__SREG__" "\n\t" \ "cli" "\n\t" \ "wdr" "\n\t" \ "out %0,%1" "\n\t" \ "out __SREG__,__tmp_reg__" "\n\t" \ "out %0,%2" \ : /* no outputs */ \ : "I" (_SFR_IO_ADDR(_WD_CONTROL_REG)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ _BV(WDE) | (value & 0x07)) ) \ : "r0" \ ) .fi Enable the watchdog timer, configuring it for expiry after \fCtimeout\fP (which is a combination of the \fCWDP0\fP through \fCWDP2\fP bits to write into the \fCWDTCR\fP register; For those devices that have a \fCWDTCSR\fP register, it uses the combination of the \fCWDP0\fP through \fCWDP3\fP bits)\&. .PP See also the symbolic constants \fCWDTO_15MS\fP et al\&. .SS "#define wdt_reset() __asm__ __volatile__ ('wdr')" Reset the watchdog timer\&. When the watchdog timer is enabled, a call to this instruction is required before the timer expires, otherwise a watchdog-initiated device reset will occur\&. .SS "#define WDTO_120MS 3" See \fCWDT0_15MS\fP .SS "#define WDTO_15MS 0" Symbolic constants for the watchdog timeout\&. Since the watchdog timer is based on a free-running RC oscillator, the times are approximate only and apply to a supply voltage of 5 V\&. At lower supply voltages, the times will increase\&. For older devices, the times will be as large as three times when operating at Vcc = 3 V, while the newer devices (e\&. g\&. ATmega128, ATmega8) only experience a negligible change\&. .PP Possible timeout values are: 15 ms, 30 ms, 60 ms, 120 ms, 250 ms, 500 ms, 1 s, 2 s\&. (Some devices also allow for 4 s and 8 s\&.) Symbolic constants are formed by the prefix \fCWDTO_\fP, followed by the time\&. .PP Example that would select a watchdog timer expiry of approximately 500 ms: .PP .nf wdt_enable(WDTO_500MS); .fi .PP .SS "#define WDTO_1S 6" See \fCWDT0_15MS\fP .SS "#define WDTO_250MS 4" See \fCWDT0_15MS\fP .SS "#define WDTO_2S 7" See \fCWDT0_15MS\fP .SS "#define WDTO_30MS 1" See \fCWDT0_15MS\fP .SS "#define WDTO_4S 8" See \fCWDT0_15MS\fP Note: This is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48, ATmega88, ATmega168, ATmega48P, ATmega88P, ATmega168P, ATmega328P, ATmega164P, ATmega324P, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, ATmega256RFR2, ATmega128RFR2, ATmega64RFR2, ATmega2564RFR2, ATmega1284RFR2, ATmega644RFR2, AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88\&. .SS "#define WDTO_500MS 5" See \fCWDT0_15MS\fP .SS "#define WDTO_60MS 2" \fCWDT0_15MS\fP .SS "#define WDTO_8S 9" See \fCWDT0_15MS\fP Note: This is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48, ATmega48A, ATmega48PA, ATmega88, ATmega168, ATmega48P, ATmega88P, ATmega168P, ATmega328P, ATmega164P, ATmega324P, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, ATmega256RFR2, ATmega128RFR2, ATmega64RFR2, ATmega2564RFR2, ATmega1284RFR2, ATmega644RFR2, AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88, ATxmega16a4u, ATxmega32a4u, ATxmega16c4, ATxmega32c4, ATxmega128c3, ATxmega192c3, ATxmega256c3\&. .SH "Author" .PP Generated automatically by Doxygen for avr-libc from the source code\&.