/************************************************************************/
/* Copyright 200-2018 MBARI												*/
/************************************************************************/
/* Summary	: Definitions for serial I/O on BEDS						*/
/* Filename : serial.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Benthic Event Detection System (BEDS)						*/
/* Revision : 1.0														*/
/* Created	: 10/02/2012													*/
/*																			*/
/* 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:												*/
/* 02Oct2012 rah - created												*/
/* $Log$
*/
/************************************************************************/

#ifndef INCserialh
#define INCserialh		1

#include <buffer.h>


/********************************************/
/*      Typedefs and Constants              */
/********************************************/

#define PIC_UARTS       2
#define RX_BUFF_SIZE    1024
#define TX_BUFF_SIZE    512

#define SER_PORT_1		1
#define SER_PORT_2		2

#define UxMODE          0x8800
#define UxSTA           0x1400

/* data/parity settings for PIC24/32 */
#define D8_NONE         0
#define D8_EVEN         1
#define D8_ODD          2
#define D9_NONE         3

#define FastBRG(baud)   (unsigned int)((PBCLK/(4*baud)) - 1)
#define StdBRG(baud)    (unsigned int)((PBCLK/(16*baud)) - 1)

#define XON				0x11			/* Xon ASCII character			*/
#define XOFF			0x13			/* Xoff ASCII character			*/
#define CTRLC			0x03			/* Control-C ASCII character	*/


/********************************************/
/*		Serial Port Data Structures			*/
/********************************************/

typedef unsigned int	SerPort;
typedef unsigned int	Port_t;

typedef struct					/****************************************/
{								/* Struct to hold serial data and state	*/
	ByteBuffer	rxBuff;			/* Receive buffer struct				*/
	Byte		rxBuffData[RX_BUFF_SIZE];  /* Rcv buffer data			*/
	ByteBuffer	txBuff;			/* Receive buffer struct				*/
	Byte		txBuffData[TX_BUFF_SIZE];  /* Tx buffer data			*/
	Nat32		baudRate;		/* Baud Rate							*/
	Nat32		rxOverFlowCnt;	/* Rx Overflow count					*/
} SerStruct;					/****************************************/


/****************************************/
/* Function Declarations				*/
/****************************************/

Errno	serInit(void);
Errno	setConsole(SerPort port);
SerPort	getConsole(void);
Errno	setSerialBaud(SerPort port, Nat32 baud);
MBool	ser_putc(SerPort port, Byte b);
int		ser_getc(SerPort port);
int		ser_getc_tmout(SerPort port, Nat32 ticks);
void	_mon_putc(char c);
int		_mon_getc(int canblock);
//short	gets_lpt(char *linebuf, short len);
int		serRxCount(SerPort port);
int		kbhit(void);
void	serRxFlush(SerPort port);
void	serTxFlush(SerPort port);
void	serWaitTxComplete(int port);
Nat32	serGetRxOvflCnt(Port_t port);

#endif	/* INCserialh */
