/****************************************************************************/
/* Copyright 2002 MBARI                                                     */
/****************************************************************************/
/* Summary  : I/O and serial comm support for OASIS mooring controller	    */
/* Filename : io.c							    */
/* Author   : Robert Herlien (rah)					    */
/* Project  : OASIS Mooring Replacement (OASIS3)			*/
/* Revision: 0.1							*/
/* Created  : 10/14/2002						*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/****************************************************************************/
/* Modification History:						    */
/* 10oct2002 rah - created from OASIS io.c				    */
/****************************************************************************/

#include <mbariTypes.h>			/* MBARI type definitions	    */
#include <mbariConst.h>			/* MBARI constants		    */
#include <io.h>				/* OASIS I/O definitions	    */
#include <cfxbios.h>			/* Persistor BIOS definitions	    */
#include <cfxpico.h>			/* Persistor PicoDOS definitions    */


/********************************/
/*	External Data		*/
/********************************/

//Extern Int16	task_tick;		/* Ticks since last dispatch()	    */
//Extern Reg MBool	ext_wake;	/* TRUE if woke by /wake pin	    */
//Extern Reg TimeOfDay	tod;		/* Current time in TimeOfDay format */
//Extern Nat16		xoff_tmout;	/* Timeout for xoff		    */


/********************************/
/*	Global Data		*/
/********************************/

Global volatile Nat32	tick;		/* 10 ms ticker			    */
Global Nat32		pwr_perm;	/* Devices to leave powered on	    */
Global Nat32		iopwrbits;	/* Devices to leave powered on	    */
Global Word		error;		/* Error vector			    */

//Global Byte		tx_status[NSER_PORTS];	/* UART Transmitter status  */
Global Byte		do_xon[NSER_PORTS];	/* TRUE to do XON/XOFF	    */
//Global Byte		modem_ctrl[NSER_PORTS];	/* Modem control bits	    */
#define XMITOFF		1		/* tx_status bit shows no data	    */
#define XOFFSTS		2		/* tx_status bit shows XOFF	    */
//Global Reg Byte	xoff_timer[NSER_PORTS];	/* To time out XOFF status  */


/********************************/
/*	Module Local Data	*/
/********************************/


/************************************************************************/
/* Function    : ioTick							*/
/* Purpose     : Handler for PIT ticker					*/
/* Inputs      : None							*/
/* Outputs     : None							*/
/************************************************************************/
Void	ioTick(Void)
{
    tick++;				/* Increment global ticker	*/
//    task_tick++;			/* Increment task delay ticker	*/

} /* ioTick() */


Void testPinFuncs(Void);
Void testPinFuncs(Void)
{
  PinClear(35);
  PinRead(35);
  PinTestIsItBus(35);
  PinIO(35);
  PinBus(35);
}

#if 0
/************************************************************************/
/* Function    : ioCheckXoffStatus					*/
/* Purpose     : Check if any port has timed out on XOFF		*/
/* Input       : None							*/
/* Output      : None							*/
/* Comment     : Called once/second from oasis task main loop		*/
/************************************************************************/
	Void
ioCheckXoffStatus( Void )
{
    Reg Nat16	i;
    
    for ( i = 0; i < NSER_PORTS; i++ )	/* XOFF timeouts		*/
	if ( tx_status[i] & XOFFSTS )	/* If port is XOFF'd		*/
	    if ( ++xoff_timer[i] > xoff_tmout )
	    {
		tx_status[i] &= ~XOFFSTS;
		enbl_xmit(i);
	    }

} /* ioCheckXoffStatus() */
#endif

/************************************************************************/
/* Function    : ser_init						*/
/* Purpose     : Initialize one serial port				*/
/* Inputs      : Serial port to init (0 - 2)				*/
/*		 Initialization word 					*/
/*		 Bit 12 = TTL/RS-232 mode: 0 = RS-232, 1 = TTL		*/
/*			  NOTE - UARTs A & B only!  Can't do TTL on CPU ser*/
/*		 Bits 10-11 = Newline mode - what to send on \n to output:*/
/*			      00 = \n, 01 = \r\n, 02 = \r, 03 = none	*/
/*		 Bit 9 = Echo mode: 1 to do local echo, 0 for none	*/
/*		 Bit 8 = Flow control: 1 for XON/XOFF, 0 for none	*/
/*		 Bit 7 = Stop bits: 0 for 1 stop bit, 1 for 2 stop bits */
/*		 Bit 6 = Character len: 0 for 7 bits, 1 for 8 bits	*/
/*		 Bits 4-5 = Parity: 00 = none, 01 = Even, 10 = odd, 11 rsvd*/
/*		 Bits 0-3 = Baud rate: 0 = 150, 1 = 300, 2 = 1200	*/
/*			    3 = 2400, 4 = 4800, 5 = 9600, 6 = 19200 	*/
/*			    7 = 38400, 8 - 15 reserved			*/
/* Outputs     : MBool, TRUE if OK					*/
/************************************************************************/
	Void
ser_init( Nat16 port, Nat16 mode )
{
    Reg Word	umode, baud;

    baud = mode & BAUD_MASK;		/* Save baud rate		*/
    umode = mode & PTY_LEN_STOP;	/* Save mode			*/
    do_xon[port] = (mode & FLOW) ? TRUE : FALSE;

} /* ser_init() */


#if 0
/************************************************************************/
/* Function    : ser_putc						*/
/* Purpose     : Write one character to serial port			*/
/* Inputs      : Port number, character					*/
/* Output      : MBool, TRUE if character sent				*/
/************************************************************************/
	MBool
ser_putc( Nat16 port, Byte byte )
{

} /* ser_putc() */


/************************************************************************/
/* Function    : ser_getc						*/
/* Purpose     : Read one character from serial port			*/
/* Inputs      : Port number						*/
/* Output      : Character, or ERROR if none				*/
/************************************************************************/
	Int16
ser_getc( Nat16 port )
{

} /* ser_getc() */


/************************************************************************/
/* Function    : ser_getn						*/
/* Purpose     : Read N characters from serial port			*/
/* Inputs      : Port number, ptr to put chars, number of chars to get	*/
/* Output      : Number of chars read					*/
/************************************************************************/
	Int16
ser_getn( Nat16 port, char *p, Int16 nchars )
{

} /* ser_getn() */


/************************************************************************/
/* Function    : ser_sndsts						*/
/* Purpose     : Get number characters left to send on serial port	*/
/* Inputs      : Port number						*/
/* Output      : Number characters left to send				*/
/* Comments    : If ring buffer is empty, checks UART to see if	it's	*/
/*		 still sending a character.  This is to ensure that the */
/*		 last byte is sent before this	function returns a 0.	*/
/************************************************************************/
	Int16
ser_sndsts( Nat16 port )
{

} /* ser_sndsts() */


/************************************************************************/
/* Function    : ser_flush						*/
/* Purpose     : Discard input on a serial line				*/
/* Inputs      : Port number						*/
/* Output      : None							*/
/************************************************************************/
	Void
ser_flush( Nat16 port )
{
    
} /* ser_flush() */


/************************************************************************/
/* Function    : ser_break						*/
/* Purpose     : Send 1000 ms break signal to a serial port		*/
/* Inputs      : Port number						*/
/* Output      : None							*/
/* Comment     : Increased from 200 ms 2/11/2000 rah			*/
/*               Added variable break period 7/5/00 klh                 */
/************************************************************************/
	Void
ser_break( Nat16 port, Int16 count )
{
} /* ser_break() */


/************************************************************************/
/* Function    : io_atod						*/
/* Purpose     : A/D Conversion Routine					*/
/* Inputs      : A/D Port number					*/
/* Outputs     : 10 bit A/D value, or ERROR				*/
/************************************************************************/
	Nat16
io_atod( Nat16 channel )
{

} /* io_atod() */


/************************************************************************/
/* Function    : io_power						*/
/* Purpose     : Turn on/off power to selected instruments		*/
/* Inputs      : Bit vector of instruments to turn on			*/
/* Outputs     : None							*/
/************************************************************************/
	Void
io_power( Word pwrbits )
{
} /* io_power() */


/************************************************************************/
/* Function    : io_pwron						*/
/* Purpose     : Turn on power to selected instruments			*/
/* Inputs      : Bit vector of instruments to turn on			*/
/* Outputs     : None							*/
/************************************************************************/
	Void
io_pwron( Word pwrbits )
{
    io_power( iopwrbits | pwrbits );

} /* io_pwron() */


/************************************************************************/
/* Function    : io_pwroff						*/
/* Purpose     : Turn off power to selected instruments			*/
/* Inputs      : Bit vector of instruments to turn off			*/
/* Outputs     : None							*/
/************************************************************************/
	Void
io_pwroff( Word pwrbits )
{
    io_power( iopwrbits & ~pwrbits );

} /* io_pwroff() */

#endif


/************************************************************************/
/* Function    : io_init						*/
/* Purpose     : Initialize I/O system					*/
/* Inputs      : None							*/
/* Outputs     : None							*/
/************************************************************************/
        Void
io_init( Void )
{
    Reg Nat16	i;

    BIOSInit();			// just in case we're NOT running from PicoDOS
    TMGSetSpeed(CPU_CLOCK);		/* Set system clock speed	*/
    tick = 0L;				/* Initialize ticker		*/
    PITSet100usPeriod(10000/TICKS_PER_SECOND);
					/* Set our tick interval	*/
    PITAddChore(ioTick, TICK_INT_LEVEL); /* Set tick interrupt handler	*/

    for ( i = 0; i < NSER_PORTS; i++ )	/* Init all ser ports to 96,N,8,1 */
      ser_init( i, (BAUD_9600 | NO_PTY | BIT8 | STOP1) );

//    io_power( pwr_perm );		/* Turn on perm powered instruments*/

} /* io_init() */


#if 0
/************************************************************************/
/* Function    : io_term						*/
/* Purpose     : Prepare I/O system for powerdown			*/
/* Inputs      : None							*/
/* Outputs     : None							*/
/************************************************************************/
        Void
io_term( Void )
{
    io_power( pwr_perm );		/* Leave on the permanent pwr bits*/

} /* io_term() */
#endif
