/*

TSTRTCKS.C

This program tests to make sure the RTC can wake up the entire system. The
program sets the alarm time to be 5 seconds later and turns off power after
turning on the LED for a short while. When the system powers down, the LED
will go off, too. 5 seconds later, power should be reenabled and the
process repeats.

The program should be compiled, then the controller should be set to run
mode. You should see the LED blink every 5 seconds or so.

*/

#use eziolp31.lib
#use vdriver.lib

main() {
	struct tm t1, t2;
	unsigned long secs;
	unsigned int u;

	rtcRead(0xc);			//	read to clear alarm flag
	rtcSwWIE(0);			//	switch off wake up interrupt
	rtcSwWF(0);				//	clear the wake up flag, otherwise /PWR remains high
	eioBrdDO(24,1);		//	turn on LED
	rtcInit();				//	initialize RTC operational parameters
	tm_rd(&t1);				//	read the current time
	secs = mktime(&t1);	//	compute the current time
	secs += 5;				//	want to wake up in 10secs
	mktm(&t2,secs);		//	put wakeup time in t2
	rtcSetAlmTime(&t2);	//	set the alarm time
	rtcSwAIE(1);			//	set alarm enable
	for (u = 0; u < 10000; ++u) hitwd();	//	put a slight delay here
	rtcSwWIE(1);			//	enable wake up interrupt
	rtcSwABE(1);			//	enable aux. battery (Vbaux)
	swEnbPower(0);			//	turn off main power now (via the latch)
	rtcSwPAB(1);			//	need to switch the RTC pin as well
}