/*  TT8 specific includes  */
#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        <dio332.h>              /* 68332 Digital I/O Port Pin Definitions */
#include        <tt8lib.h>              /* definitions and prototypes for Model 8 library */

/*  general C includes  */
#include        <stdio.h>
#include        <stdlib.h>
#include        <userio.h>

/*  includes specific to the ad7716 routines  */
#include		"power.h"

void AnalogPowerON (void)  {
	//  Turn the power on; this does not configure the output
	//  lines used by the AD7716.  That must be done seperately.
	AnalogPositivePowerON ();
	AnalogNegativePowerON ();
}



void AnalogPowerOFF (void)  {
	//  when depowering the analog section we must also be sure that we
	//	configure the output lines connected to zero output so that we
	//  don't power the chip through the digital I/O lines.  In fact,
	//  we we always power it through the SPI interface lines when we
	//  access the MAX186, but, oh well.

	//  PCS3 idles high
	_QPDR->PCS3 = 1;

	//  Make sure PCS2, 1, and 0 are not assigned to the SPI
	//  We want to control them independently so we can force them
	//  to zero.  We can leave PCS3 (for the MAX186) assigned
	//  in case the software tries to use it while the power to
	//  this A/D is off
	_QPAR->PCS2 = 0;
	_QPAR->PCS1 = 0;
	_QPAR->PCS0 = 0;
	
	//  REVIEW
	//  Configure the three chip selects as outputs; configure
	//  MOSI as an output.  There is some question about MISO.
	//  It should be an input when the QSPI is active (if and
	//  only if we are talking to the MAX186), but an output
	//  otherwise.  Here we leave it as an input, which may cause
	//  it to float, thereby drawing excess power.  Perhaps we
	//  should add a high value pull down to the hardware (?)
	_QDDR->PCS2 = 1;
	_QDDR->PCS1 = 1;
	_QDDR->PCS0 = 1;
	_QDDR->MOSI = 1;
	_QDDR->MISO = 0;

	//  All the outputs should idle low
	_QPDR->PCS2 = 0;
	_QPDR->PCS1 = 0;
	_QPDR->PCS0 = 0;
	_QPDR->MOSI = 0;

	//  finally, actually turn the power off
	AnalogNegativePowerOFF ();
	AnalogPositivePowerOFF ();
	return;
}




void SensorPowerON (void)  {
	SensorPositivePowerON ();
	SensorNegativePowerON ();
}



void SensorPowerOFF (void)  {
	SensorNegativePowerOFF ();
	SensorPositivePowerOFF ();
}



/*************************************************************************
**      AnalogPositivePowerON()
**      CLEARS TPU0 Output which is connected to MAX649 SHDN.This turns
**      ON MAX649 (+5 V) output.
**************************************************************************/

void AnalogPositivePowerON(void)
	{
	TPUSetPin(5,CLR);
	} /*AnalogPositivePowerON()*/

/*************************************************************************
**      AnalogPositivePowerOFF()
**      SETS TPU0 Output which is connected to MAX649 SHDN.This turns
**      OFF MAX649 (+5 V) output.
**************************************************************************/

void AnalogPositivePowerOFF(void)
	{
	TPUSetPin(5,SET);
	} /*AnalogPositivePowerOFF()*/

/*************************************************************************
**      AnalogNegativePowerON()
**      CLEARS TPU1 Output which is connected to MAX764 SHDN.This turns
**      ON MAX764 (-5 V) output.
**************************************************************************/

void AnalogNegativePowerON(void)
	{
	TPUSetPin(4,CLR);
	} /*AnalogNegativePowerON()*/

/*************************************************************************
**      AnalogNegativePowerOFF()
**      SETS TPU1 Output which is connected to MAX764 SHDN.This turns
**      OFF MAX764 (-5 V) output.
**************************************************************************/

void AnalogNegativePowerOFF(void)
	{
	TPUSetPin(4,SET);
	} /*AnalogNegativePowerOFF()*/

/*************************************************************************
**      SensorPositivePowerON()
**      SETS TPU2 Output which is connected to MAX649 -SHDN.This turns
**      ON MAX649 (+5 V) output.
**************************************************************************/

void SensorPositivePowerON(void)
	{
	TPUSetPin(3,CLR);
	} /*SensorPositivePowerON()*/

/*************************************************************************
**      SensorPositivePowerOFF()
**      CLEARS TPU2 Output which is connected to MAX649 -SHDN.This turns
**      OFF MAX649 (+5 V) output.
**************************************************************************/

void SensorPositivePowerOFF(void)
	{
	TPUSetPin(3,SET);
	} /*SensorPositivePowerOFF()*/

/*************************************************************************
**      SensorNegativePowerON()
**      CLEARS TPU3 Output which is connected to MAX764 SHDN.This turns
**      ON MAX764 (-5 V) output.
**************************************************************************/

void SensorNegativePowerON(void)
	{
	TPUSetPin(2,CLR);
	} /*SensorNegativePowerON()*/

/*************************************************************************
**      SensorNegativePowerOFF()
**      SETS TPU1 Output which is connected to MAX764 SHDN.This turns
**      OFF MAX764 (-5 V) output.
**************************************************************************/

void SensorNegativePowerOFF(void)
	{
	TPUSetPin(2,SET);
	} /*SensorNegativePowerOFF()*/
