/*
Last Update at 2010/08/03 [yyyy/mm/dd] By Tim Tsai.
*/
//------------------------------------------------------------------------------
#ifndef _OS7_COM_H_
#define _OS7_COM_H_

#define Txbuf	0x00     /* tx buffer */
#define Rxbuf	0x00     /* rx buffer */
#define Dll		0x00     /* baud lsb */
#define Dlh		0x01     /* baud msb */
#define Ier		0x01     /* int enable reg */
#define Fcr		0x02     /* FIFO control register */
#define Iir		0x02     /* Interrupt Identification Register */
#define Lcr		0x03     /* line control reg */
#define Dfr		0x03     /* Data format  reg */
#define Mcr		0x04     /* modem control reg */
#define Lsr		0x05     /* line status reg */
#define Msr		0x06     /* modem status reg */

#define Spr		0x07 
#define Efr		0x02
#define Icr		0x05 
#define Acr		0 

/*  for MSR of 16c550 */
#define _MSR_dCTS	0x01
#define _MSR_dDSR	0x02
#define _MSR_TERI	0x04
#define _MSR_dDCD	0x08
#define _MSR_CTS	0x10
#define _MSR_DSR	0x20
#define _MSR_RI		0x40
#define _MSR_DCD	0x80

/*  for MCR of 16c550 */
#define _MCR_DTR	0x01
#define _MCR_RTS	0x02
#define _MCR_OUT1	0x04
#define _MCR_OUT2	0x08
#define _MCR_LOOP	0x10
#define _MCR_AUTO_FLOW	0x20

/*  for ICR of 16c950 */
#define ICR_RAED_ENABLE 0x40 
#define ICR_RAED_DISABLE 0xBF 

#ifndef _OS7_COMPORT_
#define _OS7_COMPORT_
typedef struct os7_com OS7_COM, *pOS7_COM;

struct os7_com{
	int ComPort;
	int UartType;
	int ComPortType;
	long MaxBaudrate;
	long baudrate;
	int databit;
	int parity;
	int stopbit;
	int ComBase;
	int fifosize;
	int RxTriggerLevel;
	unsigned char UartTimeout;
	int InInIdx;
	int InOutIdx;
	int OutInIdx;
	int OutOutIdx;
	int _InBufSize;
	int _OutBufSize;
	unsigned char *InData;
	unsigned char *OutData;
	char ComErr;
	char _Stop2_;
	char _SetB8;
	char fTxMode;
	unsigned TxTimeout;
	unsigned char ComFcr;
	unsigned char CurACR;
	char fCtsControlMode;
	char fRtsControlMode;
	char fDtrControlMode;
	char fDsrControlMode;
	int CurCTS;
	int CurRTS;
	int CurDTR;
	int CurDSR;
	int CurMsr;
	int bMsrChanged;
	int bCtsChanged;
	int bGetBreak;
	int SizeToXoff;
	int SizeToXon;
    char ReceiveXoff;   // 1: Xoff received  0: Xon received 
    char NeedXoff;      // 1: needs to send Xoff 
    char SendXoff;      // 1: Xoff sent  0: Xon sent
    char bUseXonXoff;   // 1: Use Xon/Xoff flow control 
	int AutoDirFor485;
	int Cur485Dir;	// 0 for input, 1 for output
	int ST01_Base;
	unsigned char CurST01;
	long lLostDataNo;
	int LsrError;
	unsigned char	saveLcr,saveIer;
	unsigned saveBaud,saveIntCon;
	void (*SetRts)(OS7_COM *com,int mode);
	int (*_DoInputData)(unsigned char data);
	void (*SetRs485ToTxMode)(OS7_COM *com);
	void (*SetRs485ToRxMode)(OS7_COM *com);
	int bUseDMA;
	int DmaPage;
};
#endif

#define ASCII_XON	0x11
#define ASCII_XOFF	0x13

#ifdef __cplusplus
extern "C" {
#endif
//------------------------------------------------------------------------------
int OS7_SetComPortBufferSize(int port,int in_size,int out_size);
OS7_COM *OS7_InstallCom(int port,long baud, int databit,int parity,int stopbit);
int OS7_RestoreCom(OS7_COM *com);

OS7_COM *OS7_InstallCom_DMA(int port,long baud, int databit,int parity,int stopbit);
int OS7_RestoreCom_DMA(OS7_COM *pCom);

int OS7_GetTxBufferFreeSize(OS7_COM *com);
void OS7_ClearTxBuffer(OS7_COM *com);
int OS7_GetInputBufFreeSize(OS7_COM *com);
int OS7_SetComFifoTriggerLevel(OS7_COM *com,int level);
int OS7_GetComFifoTriggerLevel(OS7_COM *com);

//void OS7_EnableTxInterrupt(OS7_COM *com);
//void OS7_DisableTxInterrupt(OS7_COM *com);

int OS7_SetBaudrate(OS7_COM *com,unsigned long baud);
int OS7_SetDataFormat(OS7_COM *com,int databit,int parity,int stopbit);

int OS7_IsCom(OS7_COM *com);
int OS7_ReadComn(OS7_COM *com,unsigned char *buf,int maxno);
int OS7_ReadCom(OS7_COM *com);
int OS7_ToComBufn(OS7_COM *com,char *buf,int no);
int OS7_ToCom(OS7_COM *com,int data);
int OS7_ToComStr(OS7_COM *com,char *str);
int OS7_printCom(OS7_COM *com,char *fmt,...);
int OS7_ClearCom(OS7_COM *com);

void OS7_SetDtrActive(OS7_COM *com);
void OS7_SetDtrInactive(OS7_COM *com);
int OS7_GetDsrStatus(OS7_COM *com);
void OS7_SetDsrControlMode(OS7_COM *com,int mode);
int OS7_GetDsrControlMode(OS7_COM *com);
void OS7_SetDtrControlMode(OS7_COM *com,int mode);
int OS7_GetDtrControlMode(OS7_COM *com);
void OS7_SetRtsActive(OS7_COM *com);
void OS7_SetRtsInactive(OS7_COM *com);
int OS7_GetCtsStatus(OS7_COM *com);
void OS7_SetCtsControlMode(OS7_COM *com,int mode);
int OS7_GetCtsControlMode(OS7_COM *com);
void OS7_SetRtsControlMode(OS7_COM *com,int mode);
int OS7_GetRtsControlMode(OS7_COM *com);
void OS7_SetXonXoffControlMode(OS7_COM *com,int mode);

#define OS7_GetMSR	OS7_GetMsr
int OS7_GetMsr(OS7_COM *com);
int OS7_GetCurMsr(OS7_COM *com);
int OS7_GetMsrChanged(OS7_COM *com);
void OS7_ClrMsrChanged(OS7_COM *com);

#define		OS7_SetComTimeout	OS7_SetComTxTimeout 
void OS7_SetComTxTimeout(OS7_COM *com,unsigned t);

int OS7_DataSizeInCom(OS7_COM *com);

int OS7_SetMCR(OS7_COM *com,int mcr);
void OS7_SetMCR_Bit(OS7_COM *com,int mcr_bit);
void OS7_ClearMCR_Bit(OS7_COM *com,int mcr_bit);

void OS7_SendBreak(OS7_COM *com,unsigned timems);
void OS7_SetBreakMode(OS7_COM *com,int mode);
int OS7_IsDetectBreak(OS7_COM *com);

int OS7_IsTxBufEmpty(OS7_COM *com);
int OS7_WaitTransmitOver(OS7_COM *com);
int OS7_IsComOutBufEmpty(OS7_COM *com);

void OS7_InstallComInputData(OS7_COM *com,int (*fun)(unsigned char));/*[2010/03/31]*/

int OS7_SendCmdTo7000(OS7_COM *com, unsigned char *cCmd, int iChksum); /*[2010/03/31]*/

int OS7_ReceiveResponseFrom7000(OS7_COM *com, unsigned char *cCmd, long lTimeout, int iChksum);/*[2010/03/31]*/
/* timeout unit is check count */

int OS7_ReceiveResponseFrom7000_1(OS7_COM *com, unsigned char *cCmd, long lTimeout, int iChksum);/*[2010/03/31]*/
/* timeout unit is millisecond*/

void OS7_COM_GetVersion(char *ver); /*[2010/04/02]*/
/*
	return version message, the first version is :"1.0.0"
*/
void OS7_COM_GetLibDate(char *date);/*[2010/04/02]*/
#ifdef __cplusplus
}
#endif

//------------------------------------------------------------------------------

/* com port for iPAC-5000,i-8000,iPAC-8000 */
#define	COM_0		0

/* com port for 7188XB/7188E/7186E */
#define	COM_1		1
#define	COM_2		2
#define	COM_3		3
#define	COM_4		4
#define	COM_5		5
#define	COM_6		6
#define	COM_7		7
#define	COM_8		8

/* com port for iPAC-5000 */
#define	COM_GPRS	9
#define	COM_GPS		10
#define	COM_PIC		11

/* com port for 8112/8114/8142/8144 on i-8000/iPAC-8000 slot 0~3 */
#define	COM_S0P0	5
#define	COM_S0P1	6
#define	COM_S0P2	7
#define	COM_S0P3	8

#define	COM_S1P0	9
#define	COM_S1P1	10
#define	COM_S1P2	11
#define	COM_S1P3	12

#define	COM_S2P0	13
#define	COM_S2P1	14
#define	COM_S2P2	15
#define	COM_S2P3	16

#define	COM_S3P0	17
#define	COM_S3P1	18
#define	COM_S3P2	19
#define	COM_S3P3	20

/* com port for 8112/8114/8142/8144 on iPAC-8000 slot 4~7 */
#define	COM_S4P0	21
#define	COM_S4P1	22
#define	COM_S4P2	23
#define	COM_S4P3	24

#define	COM_S5P0	25
#define	COM_S5P1	26
#define	COM_S5P2	27
#define	COM_S5P3	28

#define	COM_S6P0	29
#define	COM_S6P1	30
#define	COM_S6P2	31
#define	COM_S6P3	32

#define	COM_S7P0	33
#define	COM_S7P1	34
#define	COM_S7P2	35
#define	COM_S7P3	36

#ifndef OS7_COM_INFO
#define OS7_COM_INFO 
struct com_info {
	int port;
	int type;
	int int_no;
	int base;
	char *name;
	OS7_COM *pCom;
};
typedef struct com_info	COM_INFO, *pCOM_INFO;
#endif
extern int MAX_COM_PORT_NUMBER;
/*
extern COM_INFO ComBaseData[];
*/

#endif
