/******************************************************************************\
**	tt8main.c				Tattletale Model 8 Starter File
**	
**	First release:			Saturday, March 16, 1996
*****************************************************************************
**	
**	Developed by: John H. Godley dba Peripheral Issues
**	P.O.Box 543, Mashpee, MA 02649-0543 USA
**	jhgodley@periph.com - http://www.periph.com
**	
**	Copyright (C) 1996-1997 Peripheral Issues
**	Portions (C) 1994,1995 Onset Computer Corp.
**	All rights reserved.
**	
*****************************************************************************
**	
**	Copyright and License Information
**	
**	Peripheral Issues (hereafter, Pii) grants you (hereafter, Licensee)
**	a non-exclusive, non-transferable license to use the software source
**	code contained in this single source file. Licensee may distribute
**	binary derivative works using this software to third parties without
**	fee or restrictions.
**	
**	PII MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
**	BINARY SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
**	THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
**	PURPOSE, OR NON-INFRINGEMENT. PII SHALL NOT BE LIABLE FOR ANY DAMAGES
**	SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
**	BINARY SOFTWARE OR ITS DERIVATIVES.
**	
**	By using or copying this Software, Licensee agrees to abide by the
**	copyright law and all other applicable laws of the U.S. including, but
**	not limited to, export control laws, and the terms of this license. Pii
**	shall have the right to terminate this license immediately by written
**	notice upon Licensee's breach of, or non-compliance with, any of its
**	terms. Licensee may be held legally responsible for any copyright
**	infringement or damages resulting from Licensee's failure to abide by
**	the terms of this license. 
**	
\******************************************************************************/

#include	<TT8.h>			/* Tattletale Model 8 Definitions */
#include	<tat332.h>		/* 68332 Tattletale (7,8) Hardware Definitions */
#include	<sim332.h>		/* 68332 System Integration Module Definitions */
#include	<qsm332.h>		/* 68332 Queued Serial Module Definitions */
#include	<tpu332.h>		/* 68332 Time Processing Unit Definitions */
#include	<dio332.h>		/* 68332 Digital I/O Port Pin Definitions */
#include	<tt8pic.h>		/* Model 8 PIC Parallel Slave Port Definitions */

#include	<tt8lib.h>		/* definitions and prototypes for Model 8 library */
#include	<userio.h>		/* common convenient user I/O routines */

#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<signal.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include	"newsleep.h"

void GoToSleep(time_tt);


/******************************************************************************\
**	main		Tattletale Model 8 Main Entry Point
**		
**	The first action code in a Model 8 program must be a call to InitTT8(),
**	usually with the default arguments (NO_WATCHDOG, TT8_TPU) which disable
**	the software watchdog, and install the TPU code tailored for the TT8.
**	
**	The last action code in a Model 8 program should be a return from main(),
**	or a call to Reset() or ResetToMon(). Return or Reset() will act like
**	a conventional power up or reset and run whatever program is in rom.
**	ResetToMon() will force the Model 8 to restart in the TOM8 monitor.
\******************************************************************************/
main()
	{

/*
**	STANDARD TATTLETALE MODEL 8 LIBRARY & HARDWARE INITIALIZATION
*/
	InitTT8(NO_WATCHDOG, TT8_TPU);	/* setup Model 8 for running C programs */

/*
**	<<< YOUR STUFF HERE >>>
*/
	printf("\nSleep test\n");


/* Put the system to sleep in low-power mode */
void GoToSleep(time_tt wakeTime)
{
	SLPResult result;							/* reason for waking up */

// this ReadClkTime() call will cause a crash on wakeup from sleep - don't know why
//	printf("Entering low-power sleep mode on ");
//	ReadClkTime();
#if 0
	printf("\n\nCurrent Options:\n");
	printf("  3V Sleep %sabled\n", SleepOpts.sleepAt3V
		? "En" : "Dis");
	printf("  Key Wake %sabled\n", SleepOpts.sleepHandleIRQ1
		? "En" : "Dis");
	printf("  Spurious Wake %sabled\n", SleepOpts.sleepSpuriousRupt
		 ? "En" : "Dis");
	printf("  Sleep Zero Seconds %sabled\n", SleepOpts.sleepZeroEvenSec
		 ? "En" : "Dis");
	printf("  Handle IRQ1 %sabled\n", SleepOpts.sleepHandleIRQ1
		 ? "En" : "Dis");
	printf("  Handle IRQ2 %sabled\n", SleepOpts.sleepHandleIRQ2
		 ? "En" : "Dis");
	printf("  Handle IRQ4 %sabled\n", SleepOpts.sleepHandleIRQ4
		 ? "En" : "Dis");
	printf("  Handle IRQ6 %sabled\n", SleepOpts.sleepHandleIRQ6
		 ? "En" : "Dis");
#endif

	CloseSerPorts();				// turn off TPU serial ports
	PrepPinsLowPower();
	SetPeriodicTimer(0);			// stop PIT interrupts
	result = LowPowerSleepTill(wakeTime); /* sleep until specified time */
	// we are sleeping now...

	// we just woke up from sleep
	SetPeriodicTimer(PITDLY);		// restart PIT interrupts
	TPUSetupPWM(MTR_PWM, 0, PWM_PER, MiddlePrior); /* restart pwm output at 0%*/
	InitSerPorts();					// open TPU serial ports to talk to instruments
	printf("wakeup result = %d\n", result);
	printf("Waking from low-power sleep mode on ");
	ReadClkTime();
} /* end function GoToSleep() */


/*
**	STANDARD TATTLETALE MODEL 8 PROGRAM EXIT
*/
//	ResetToMon();
//	Reset();
	return (0);
	
	}	/* main() */
