/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use the watchdog for reset.
 */

#include <stdio.h>
#include <conio.h>
#include <target.h>

#define LED	0x10000				// LED =P0.16

/*
 * The watchdog restarts the program every 10 seconds unless
 * a key is pressed which resets the timeout. The counter is
 * clocked with pclk/4 i.e. one second equals pclk/4 counts.
 *
 * The LED lights when there are less than 3s until restart.
 */
int
main(void)
{
	puts(Intern_wdmod&4? "\nRESTART (watchdog reset)\7\n"	// start
			   : "\nSTART (external reset)\n");	//  message

	Intern_wdtc   = _PCLK/4*10;				// 10s timeout (pclk/4 =1s)
	Intern_wdmod  = 3;					// enable watchdog reset
	Intern_wdfeed = 0xaa;					// start
	Intern_wdfeed = 0x55;					//  watchdog

	while (1) {
		Intern_iodir &= ~LED;							// LED off
		puts("Restart in 10s... (hit any key to reset timeout!)");
		while (!kbhit()) if (Intern_wdtv < _PCLK/4*3) Intern_iodir |= LED;	// < 3s? LED on

		getch();					// remove pending char
		Intern_wdfeed = 0xaa;				// reset
		Intern_wdfeed = 0x55;				//  timeout
	}
}
