/*****************************************************

	reset_demo.c
	Z-World, 2000

	Demonstration of using Rabbit reset functions and
	protected variables.

	This program will print out to serial port B
	why the last reset occurred, then cause either a
	watchdog timeout or a soft reset according to a
	protected counter.

	To properly run this demo, compile it to flash,
	disconnect the programming cable, connect serial
	port B to your computer at 19200 baud, and hit the
	reset button (or cycle the board's power).  The
	controller will then start running the program.
	A hard (power fail) reset can be forced by pressing
	the reset button or cycling the power.
		
******************************************************/

#define	BINBUFSIZE	31
#define	BOUTBUFSIZE	31


main()
{
	static long	counter;
	char	s[256];

		
	_sysIsSoftReset();	// recover any protected variables

	serBopen(19200);		// open serial port B at 19200 baud

	// check why the controller was last reset
	if (chkHardReset()) {
		sprintf(s, "Hard reset (power fail) occurred.\r\n");
		serBputs(s);
	}

	if (chkWDTO()) {
		sprintf(s, "Watchdog timeout occurred.\r\n");
		serBputs(s);
	}

	if (chkSoftReset()) {
		sprintf(s, "'Soft' reset (by software) occurred.\r\n");
		serBputs(s);
	}

	counter++;				// increment counter

	if (counter & 0x01) {		// force a watchdog timeout
		// allocate one virtual watchdog, timing = 1/16 sec
		VdGetFreeWd(1);	
		delayMs(1000);
		
	} else {

		delayMs(1000);			 // force a software reset
		forceSoftReset();			
	}									
}



int delayMs(int ms)
{
	unsigned long	t0;
	
	t0 = MS_TIMER;
	while (MS_TIMER < (t0 + ms))
		/* do nothing */;
}
