/*		H2ocon_init.c

	This is a short initialization file the the H2O controller CF2.
	Its primary purpose is to initialize the CF2 pins and peripheral
	chips.  Most importantly, the program shuts down the DC/DC converters
	that power the cameras and current meter.  This is important since
	the controller won't boot if the converters draw more than the current
	limit of 0.5A.
	
	This program should be compiled and the file H2ocon_init.APP stored
	in the CF2 flash using Motocross.  Execute the command "BOOT APP" to
	force the CF2 to always boot this program before starting PicoDOS.
	
	RCG 8-12-03


*/

#include	<cfxbios.h>		// Persistor BIOS and I/O Definitions
#include	<cfxpico.h>		// Persistor PicoDOS Definitions

#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	<dirent.h>		// PicoDOS POSIX-like Directory Access Defines
#include	<dosdrive.h>	// PicoDOS DOS Drive and Directory Definitions
#include	<fcntl.h>		// PicoDOS POSIX-like File Access Definitions
#include	<stat.h>		// PicoDOS POSIX-like File Status Definitions
#include	<termios.h>		// PicoDOS POSIX-like Terminal I/O Definitions
#include	<unistd.h>		// PicoDOS POSIX-like UNIX Function Definitions

static QPBDev U10_DevTemplate = 
{
	NMPCS2,				// qslot, non-multiplexed connection to PCS3
	"U10",				// devName			C string with device name (15 max)
	2000000,			// maxBaud			maximum baud rate in Hz for device
	8,					// bits				Bits Per Transfer
	iaLowSCK,			// clockPolar		SPI Clock Polarity
	captLead,			// clockPhase		SPI Clock Phase
	false,				// contCSMulti		Continue CS assert between mult xfrs
	true,				// autoTiming		Auto adjust timing to clock flag
	1000000,			// psDelaySCK		Min. Delay Before SCK (picoSecs)
	1000000,			// psDelayTXFR		Min. Delay After Transfer
	0,					// *rcvData			pointer to received data buffer
	0					// xfrCount			words transferred
};

static QPB		*QPB_U10_Slot = NULL;
static ushort 	g_U10_byte = 0;		// Power control byte

/******************************************************************************\
**	main
\******************************************************************************/
void main(void)
	{

	// Identify the progam and build
	printf("\nProgram: %s: %s %s \n", __FILE__, __DATE__, __TIME__);
	// Identify the device and its firmware
	printf("Persistor CF%d SN:%ld   BIOS:%d.%d   PicoDOS:%d.%d\n", CFX,
		BIOSGVT.CFxSerNum, BIOSGVT.BIOSVersion, BIOSGVT.BIOSRelease, 
		BIOSGVT.PICOVersion, BIOSGVT.PICORelease);


	// Init U10 and shutdown external instrument power 
	QPB_U10_Slot = QPBInitSlot(&U10_DevTemplate);	
	if(QPB_U10_Slot == NULL)
	{
		printf("\nError opening QPB_U10_Slot");
		return;
	}
	QPBTransact(QPB_U10_Slot, 0, 1, &g_U10_byte);	// All off
	QPBFreeSlot(&U10_DevTemplate);
	

	PinClear(19);	// PCS1
	PinClear(21);	// PCS0
	PinClear(22);	// TPU1
	PinClear(24);	// TPU3
	PinClear(26);	// TPU5
	PinClear(28);	// TPU7
	PinClear(30);	// TPU9
	PinClear(32);	// TPU11
	PinClear(34);	// TPU13
	PinRead(23);	// TPU2
	PinRead(25);	// TPU4
	PinRead(27);	// TPU6
	PinRead(29);	// TPU8
	PinRead(31);	// TPU10
	PinRead(33);	// TPU12
	PinRead(35);	// TPU14
	
	// RS-422 COM port stuff
	EIAForceOff(true);			// Shutdown RS-232 chip on CF2
	SCIRxSetBuffered(true);		// Make port buffered
	SCITxSetBuffered(true);	


	// Exit to PicoDOS
	BIOSResetToPicoDOS();	// full reset, but jump to 0xE10000 (PicoDOS)

	}	//____ main() ____//

