/* TT8RMLIB.C */

#include	"tt8rmlib.h"

#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	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<fcntl.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<sgtty.h>
#include	<signal.h>
#include	<stat.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>


/*******************************************************************************
**	TPUSetupPWM			Setup TPU channel for Pulse-Width Modulation
**	
**	Notes:
**		pwmper = period in tcr1 cycles [call TPUGetTCR1() for current value]
**		pwmhi = time high in tcr1 cycles
**		priority = LowPrior, MiddlePrior, or HighPrior [defined in tpu.h]
**		read about value limitations in the TPU Reference Manual
*******************************************************************************/
void TPUSetupPWM(short chan, short pwmhi, short pwmper, short priority)
	{
	
	CHANPRIOR(chan, Disabled);			/* stop what its doing */
	if (priority == Disabled)			/* just shutting it down */
		return;
	*CIER &= ~(1 << chan);				/* don't want interrupts enabled */
	FUNSEL(chan, PWM);					/* channel function select */
	PRAM[chan][0] = OutputChan | NoChangePAC | (pwmhi ? ForceHigh : ForceLow);
	PRAM[chan][2] = pwmhi;				/* time hi */
	PRAM[chan][3] = pwmper;				/* period */

	HOSTSERVREQ(chan, 2);				/* issue request */
	CHANPRIOR(chan, priority);			/* set channel priority */
	while (HOSTSERVSTAT(chan) & 3)		/* await reply */
		;
	
	}	/* TPUSetupPWM() */


/*******************************************************************************
**	TPUChangePWM			Change PWM values for inited TPU channel
**	
**	Notes:
**		pwmper = period in tcr1 cycles [call TPUGetTCR1() for current value]
**		pwmhi = time high in tcr1 cycles
*******************************************************************************/
void TPUChangePWM(short chan, short pwmhi, short pwmper)
	{

/*
**	NEED TO DO THIS WRITE COHERENTLY (AS DOUBLE WRITE)
*/
	* (ulong *) &PRAM[chan][2] = ((ulong) pwmhi << 16L) | (ulong) pwmper;
	HOSTSERVREQ(chan, 1);				/* issue request */
	while (HOSTSERVSTAT(chan) & 3)		/* await reply */
		;

	}	/* TPUChangePWM() */

/****************************************/

int TPUGetPinES(short chan, short edge){

#ifdef AZTEC_C
  CHANPRIOR(chan, Disabled);
  FUNSEL(chan, DIO);
  PARAMRAM(chan)[CHANNEL_CONTROL] = InputChan | NoForceState | edge;
  HOSTSEQ(chan, UpdtOnTrans); /* need to enter transistion mode first ! */
  HOSTSERVREQ(chan, Initialize);
  CHANPRIOR(chan, LowPrior);
  while (HOSTSERVSTAT(chan))
    ;
  HOSTSEQ(chan, UpdtOnHSRInit);
  HOSTSERVREQ(chan, Initialize);
  while (HOSTSERVSTAT(chan))
    ;
  return ((PARAMRAM(chan)[PIN_LEVEL] & 0x8000) == 0x8000);

#else
  return;

#endif

}   /* TPUGetPinES() */


/***********************************************************
  "delay_lp()" places the Model 8 in a short loop
  that produces no bus cycles.  Power consumption can be
  reduced when running out of flash memory by calling this function
  in a time delay loop.
************************************************************/

void delay_lp(void){

#asm
	move.w  #32000,d1    /* About 500 mS with 320KHz clock */
lp1:  	nop
	dbf	d1,lp1
#endasm

}


