Skip to content

Instantly share code, notes, and snippets.

@mcufreaks
Created May 26, 2013 08:29
//Some register definitions
#ifndef WDTCTRL
#define WDTCTRL (*((unsigned long*)0xFFFF0D30))
#endif // WDTCTRL defined
#ifndef WDTCLR
#define WDTCLR (*((unsigned long*)0xFFFF0D34))
#endif // WDTCLR defined
void myAvr32SoftwareReset(void)
{
//In AVR32, WDT module is clocked from internam 115KHz RC oscillator.
//We use PSEL field in WDTCTRL register to set a timeout period of 2^(PSEL+1) / 115000 seconds.
//In the code below we set a timeout period of 65536/115000 = 0.567 seconds
//Let's configure Watchdog as...
// WDTCTRL.KEY = [0x55, 0xAA]
// WDTCTRL.PSEL = 15 (timeout period to 2^16 / 115000 => 65536/115000 seconds => 0.567 seconds
// WDTCTRL.EN = 1 (enable WDT)
WDTCTRL = 0x55000F01;
WDTCTRL = 0xAA000F01;
//Endless loop until watchdog overflow.
while(1) {}
}
//And later, in your main function...
int main(void)
{
// ... Variable declarations ...
//Reset watchdog.
WDTCLR = 0x0000000f; //Any value is valid.
//Disable watchdog by setting EN field to '0'
WDTCTRL = 0x55000F00;
WDTCTRL = 0xAA000F00;
// ... continue with your program code ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment