avr_watchdog(3avr)

πŸ’– avr_watchdog β€” the avr/wdt.h watchdog timer page (3avr) πŸ’–

okay bestie, sit down, we're talking about the watchdog timer today and honestly? it's giving security guard energy. this is the avr_watchdog(3avr) page from AVR-LibC, so let's get into it πŸ•βœ¨


🌸 NAME 🌸

avr_watchdog β€” <avr/wdt.h>: Watchdog timer handling


✨ SYNOPSIS ✨

Macros

#define wdt_reset()
#define wdt_enable(timeout)
#define WDTO_15MS   0
#define WDTO_30MS   1
#define WDTO_60MS   2
#define WDTO_120MS   3
#define WDTO_250MS   4
#define WDTO_500MS   5
#define WDTO_1S   6
#define WDTO_2S   7
#define WDTO_4S   8
#define WDTO_8S   9

πŸ¦‹ Detailed Description πŸ¦‹

#include <avr/wdt.h>

so like. this header file declares the interface to some inline macros that handle the watchdog timer, and the watchdog timer is a thing that lives inside many AVR devices. πŸ’…

now listen, the watchdog config is protected on purpose: to keep a crashing application from going feral and accidentally messing with the configuration, you need a special timed sequence to actually change it. the macros in this header file do that whole sequence for you automatically before they change any value. and yeah β€” interrupts get disabled during the manipulation. 🫢

Note β€” depending on the fuse configuration of your particular device, extra restrictions might apply, and in particular it might be disallowed to turn off the watchdog timer. so check your fuses, bestie, no cap.

and here's a big one: for newer devices (ATmega88 and newer β€” effectively any AVR that has the option to also generate interrupts), the watchdog timer stays active even after a system reset (except a power-on condition), and it uses the fastest prescaler value (approximately 15 ms). 😭 so you literally have to turn off the watchdog early during program startup. the datasheet recommends a sequence like this one:

#include <stdint.h>
#include <avr/wdt.h>

uint8_t mcusr_mirror __attribute__ ((section (".noinit")));

__attribute__((used, unused, naked, section(".init3")))
static void get_mcusr (void);

void get_mcusr (void)
{
  mcusr_mirror = MCUSR;
  MCUSR = 0;
  wdt_disable();
}

saving the value of MCUSR in mcusr_mirror is only needed if the application later wants to examine the reset source. but β€” and this is the important part β€” clearing the watchdog reset flag before disabling the watchdog is required, according to the datasheet. periodt. πŸ‘‘


πŸ‘‘ Macro Definition Documentation πŸ‘‘

#define wdt_enable( timeout)

turn the watchdog timer on, and configure it so it expires after timeout. that timeout is a combination of the WDP0 through WDP2 bits that get written into the WDTCR register. for those devices that have a WDTCSR register instead, it uses the combination of the WDP0 through WDP3 bits. πŸ’«

also see the symbolic constants WDTO_15MS et al.


#define wdt_reset()

Value:

__asm__ __volatile__ ("wdr")

reset the watchdog timer. once the watchdog timer is enabled, you have to call this instruction before the timer expires β€” otherwise a watchdog-initiated device reset will occur. so like, do not forget, i'm begging you πŸ™ˆ


#define WDTO_120MS 3

see WDTO_15MS


#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 they 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. 🌈

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 WDTO_, followed by the time. iykyk πŸ’…

here's an example that would select a watchdog timer expiry of approximately 500 ms:

wdt_enable(WDTO_500MS);

#define WDTO_1S 6

see WDTO_15MS


#define WDTO_250MS 4

see WDTO_15MS


#define WDTO_2S 7

see WDTO_15MS


#define WDTO_30MS 1

see WDTO_15MS


#define WDTO_4S 8

see WDTO_15MS

Note: this is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48*, ATmega88*, ATmega168*, ATmega328*, ATmega164P, ATmega324P, ATmega324PB, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88.
Note: this value does not match the bit pattern of the respective control register. it is solely meant to be used together with wdt_enable().

#define WDTO_500MS 5

see WDTO_15MS


#define WDTO_60MS 2

see WDTO_15MS


#define WDTO_8S 9

see WDTO_15MS

Note: this is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48*, ATmega88*, ATmega168*, ATmega328*, ATmega164P, ATmega324P, ATmega324PB, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, ATmega2564RFR2, ATmega256RFR2, ATmega1284RFR2, ATmega128RFR2, ATmega644RFR2, ATmega64RFR2 AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88, ATxmega16a4u, ATxmega32a4u, ATxmega16c4, ATxmega32c4, ATxmega128c3, ATxmega192c3, ATxmega256c3.
Note: this value does not match the bit pattern of the respective control register. it is solely meant to be used together with wdt_enable().

🧚 Author 🧚

generated automatically by Doxygen for AVR-LibC from the source code. it's giving ✨ auto-generated but make it fashion ✨


aaaand that's the whole page, bestie!! πŸŽ€ this was AVR-LibC, Version 2.2.1, and yeah the date field is literally just the manual page name avr_watchdog(3avr) hanging out down there in the footer πŸ’€ go forth and pet your watchdog responsibly πŸ•πŸ’– slay!!

πŸ“¦ where this page comes from