/*
Last Update at 2010/05/04 [yyyy/mm/dd] By Tim Tsai.
*/
#ifndef __MINIOS7__
#define __MINIOS7__

#define _I7186E_

typedef unsigned int  uint;
typedef unsigned int  WORD;
typedef unsigned char uchar;
typedef unsigned char BYTE;
typedef unsigned long ulong;
typedef unsigned long DWORD;

#ifdef __TURBOC__
  #if (__TURBOC__ < 0x0300)
    #define inpw   inport
    #define outpw  outport
  #endif
#endif

#define    NoError	    0
#define    InitPinIsOpen    0
#define    InitPinIsNotopen 1
#define    QueueIsEmpty     0
#define    QueueIsNotEmpty  1
#define    PortError	   -1
#define    DataError	   -2
#define    ParityError	   -3
#define    StopError	   -4
#define    TimeOut	   -5
#define    QueueEmpty	   -6
#define    QueueOverflow   -7
#define    PosError	   -8
#define    AddrError	   -9
#define    BlockError	   -10
#define    WriteError	   -11
#define    SegmentError    -12
#define    BaudRateError   -13
#define    CheckSumError   -14
#define    ChannelError    -15
/*[2007/03/14] #define    BaudrateError   -16 */
#define    BaudrateError	BaudRateError   
#define    TriggerLevelError   -17
#define    DateError    -18
#define    TimeError    -19
#define    TimeIsUp        1
/*vic,[2007/7/24]*/
#define    XEECLKError     -25 
#define    XEESDAError     -26
#define    XEEWPError      -27

#ifdef __cplusplus
extern "C" {
#endif

/* FOR WDT */
void EnableWDT(void);
void DisableWDT(void);
void RefreshWDT(void);

/* FOR INIT* pin */
int ReadInitPin(void);

/* For SCLK pin */
void ClockHigh(void);
void ClockHighLow(void);
void ClockLow(void);

/* For red LED */
void LedOn(void);
void LedOff(void);
void LedToggle(void);

void Led1On(void);
void Led1Off(void);
void Led1Toggle(void);

void Led2On(void);
void Led2Off(void);
void Led2Toggle(void);

#define _NO_LED5_

#ifndef _NO_LED5_
/* FOR 5* 7-segments LED */
extern unsigned char ShowData[19];
void pascal Show5DigitLed(int pos, int data);
void pascal Show5DigitLedWithDot(int pos, int data);
void Init5DigitLed(void);
void pascal Show5DigitLedSeg(int pos, unsigned char data);
void pascal Set5DigitLedTestMode(int mode);
void pascal Set5DigitLedIntensity(int mode);
void Disable5DigitLed(void);
void Enable5DigitLed(void);
#endif

/* For STDIO */
void Putch(int data);
void Puts(char *str);
int Getch(void);
int Gets(char *str);
int Kbhit(void);
int LineInput(char *buf,int maxlen);
void ResetScanBuffer(void);
void SetScanBuffer(unsigned char *buf,int len);
int Scanf(char *fmt, ...); /* for TC/BC only */
int Print(const char *fmt, ...);
int _Printf(const char *fmt, ...); /* for TC/BC only */
int UngetchI(int key);
int Ungetch(int key);

/* For RTC/NVRAM */
void GetTime(int *hour,int *minute,int *sec);
void GetDate(int *year,int *month,int *day);
int GetWeekDay(void);
int SetDate(int year,int month,int day);
int SetTime(int hour,int minute,int sec);
int ReadNVRAM(int addr);
int WriteNVRAM(int addr, int data);

/* for Timer */
extern const unsigned long far *TimeTicks;

/* For old version EEPROM functions compatible */
#define WriteEEP	EE_RandomWrite
#define ReadEEP		EE_RandomRead
#define EnableEEP	EE_WriteEnable
#define ProtectEEP	EE_WriteProtect

/* for EEPROM(24WC128/24LC16)*/
void EE_WriteProtect(void);
void EE_WriteEnable(void);

unsigned char EE_RandomRead(int Block,unsigned Addr);
unsigned char EE_ReadNext(int Block);
int EE_MultiRead(int StartBlock,unsigned StartAddr,int no,char *databuf);
int EE_RandomWrite(int Block,unsigned Addr,int Data);
int EE_MultiWrite(int Block,unsigned Addr,int no,char *Data);
int EE_MultiWrite_A(int Block,unsigned Addr,unsigned no,char *Data);
int EE_MultiWrite_L(unsigned address,unsigned no,char *Data);
int EE_MultiRead_L(unsigned address,unsigned no,char *Data);
extern int const EE_Type;
extern int const EE_BlockNo;
extern int const EE_PageSize; 

/* for system */
extern unsigned long far *IntVect;
int IsMiniOS7(void);
int Is7186e(void);
int IsResetByPowerOn(void);
int IsResetByWatchDogTimer(void);

/* for FLASH MEMORY */
int FlashReadId(void);
int FlashErase(unsigned int FlashSeg);
int FlashWrite(unsigned int seg, unsigned int offset, char data);

#define FlashRead FlashReadB
unsigned char FlashReadB(unsigned seg, unsigned offset);
unsigned FlashReadI(unsigned seg, unsigned offset);
unsigned long FlashReadL(unsigned seg, unsigned offset);
void far *_MK_FP_(unsigned s,unsigned off);

/* Timer functions */
int TimerOpen(void);
int TimerClose(void);
void TimerResetValue(void);
unsigned long TimerReadValue(void);
int StopWatchReset(int channel);
int StopWatchStart(int channel);
int StopWatchStop(int channel);
int StopWatchPause(int channel);
int StopWatchContinue(int channel);
int StopWatchReadValue(int channel,unsigned long *value);
int CountDownTimerStart(int channel,unsigned long count);
int CountDownTimerReadValue(int channel,unsigned long *value);
void InstallUserTimer(void (*fun)(void));
void InstallUserTimer1C(void (*fun)(void));

/* StopWatch [計時碼表] */

#ifndef _T_STOPWATCH_
#define _T_STOPWATCH_
typedef struct {
 ulong ulStart,ulPauseTime;
 uint  uMode;  /* 0: pause, 1:run(start) */
} STOPWATCH;
#endif

/* CountDown Timer[倒數計時] */
#ifndef _T_COUNTDOWNTIMER_
#define _T_COUNTDOWNTIMER_
typedef struct {
 ulong ulTime,ulStartTime,ulPauseTime;
 uint  uMode;  /* 0: pause, 1:run(start) */
} COUNTDOWNTIMER;
#endif

void T_StopWatchStart(STOPWATCH *sw);
ulong T_StopWatchGetTime(STOPWATCH *sw);
void T_StopWatchPause(STOPWATCH *sw);
void T_StopWatchContinue(STOPWATCH *sw);

void T_CountDownTimerStart(COUNTDOWNTIMER *cdt,ulong timems);
void T_CountDownTimerPause(COUNTDOWNTIMER *cdt);
void T_CountDownTimerContinue(COUNTDOWNTIMER *cdt);
int T_CountDownTimerIsTimeUp(COUNTDOWNTIMER *cdt);
ulong T_CountDownTimerGetTimeLeft(COUNTDOWNTIMER *cdt);

/* Timer functions II */
void T2_UpdateCurrentTimeTicks(void); /* every loop must call T2_UpdateCurrentTimeTicks() to get new time.*/
void T2_StopWatchStart(STOPWATCH *sw);
ulong T2_StopWatchGetTime(STOPWATCH *sw);
void T2_StopWatchPause(STOPWATCH *sw);
void T2_StopWatchContinue(STOPWATCH *sw);

void T2_CountDownTimerStart(COUNTDOWNTIMER *cdt,ulong timems);
void T2_CountDownTimerPause(COUNTDOWNTIMER *cdt);
void T2_CountDownTimerContinue(COUNTDOWNTIMER *cdt);
int T2_CountDownTimerIsTimeUp(COUNTDOWNTIMER *cdt);
ulong T2_CountDownTimerGetTimeLeft(COUNTDOWNTIMER *cdt);

extern const unsigned long far *TimeTicks;
void Delay(unsigned ms); /* delay unit is ms, use CPU Timer 1. */
void Delay_1(unsigned ms); /* delay unit is 0.1 ms ,use CPU Timer 1.*/
void Delay_2(unsigned ms); /* delay unit is 0.01 ms ,use CPU Timer 1.*/
void DelayMs(unsigned t);/* delay unit is ms, use system timeticks. */

/* for MiniOS7 FLASH file system */
#ifndef __FILE_DATA__
#define __FILE_DATA__
typedef struct  {
  unsigned mark;   /* 0x7188 -> is file */
  unsigned char fname[12];
  unsigned char year;
  unsigned char month;
  unsigned char day;
  unsigned char hour;
  unsigned char minute;
  unsigned char sec;
  unsigned long size;
  char far *addr;
  unsigned CRC;
  unsigned CRC32;
} FILE_DATA;
#endif

#ifndef _DISK_AB_
#define _DISK_AB_

typedef struct {
  unsigned sizeA:3;
  unsigned sizeB:3;
  unsigned sizeC:3;
  unsigned sum:7;
} SIZE_AB;
#endif

extern SIZE_AB SizeAB;
extern FILE_DATA far *fdata;
extern unsigned DiskAStartSeg,DiskBStartSeg;

#define DISKA	0
#define DISKB	1

void far *AddFarPtrLong(void far * ptr1,unsigned long size);
void ReadSizeAB(void);

/* int GetFileNo(void); */
#define GetFileNo()	GetFileNo_AB(DISKA)

/* int GetFileName(int no,char *fname); */
#define GetFileName(no,fname)	GetFileName_AB(DISKA,no,fname)

/* FILE_DATA far * GetFileInfoByNo(int no) */
#define GetFileInfoByNo(no)	GetFileInfoByNo_AB(DISKA,no)

/* FILE_DATA far * GetFileInfoByName(char *fname) */
#define GetFileInfoByName(fname)	GetFileInfoByName_AB(DISKA,fname)

/* char far * GetFilePositionByNo(int no) */
#define GetFilePositionByNo(no)	GetFilePositionByNo_AB(DISKA,no)

/* char far * GetFilePositionByName(char *fname) */
#define GetFilePositionByName(fname)	GetFilePositionByName_AB(DISKA,fname)

int GetFileNo_AB(int disk);
int GetFileName_AB(int disk,int no,char *fname);
FILE_DATA far * GetFileInfoByNo_AB(int disk,int no);
FILE_DATA far *GetFileInfoByName_AB(int disk,char *fname);
char far * GetFilePositionByNo_AB(int disk,int no);
char far * GetFilePositionByName_AB(int disk,char *fname);

#define	COM1	0
#define	COM2	1
#define	COM3	2

#define FLOW_CONTROL_DISABLE	0
#define FLOW_CONTROL_ENABLE	1
#define FLOW_CONTROL_AUTO_BY_HW 2
#define FLOW_CONTROL_AUTO_BY_SW	3

/* for COM1 */
/* WITH CTS & RTS control */

#define	ClearTxBuffer1		ClearTxBuffer_1
#define GetTxBufferFreeSize1	GetTxBufferFreeSize_1
#define PushDataToCom1		PushDataToCom_1

#define CheckInputBufSize1	CheckInputBufSize_1
#define InstallCom1		InstallCom_1
#define RestoreCom1		RestoreCom_1
#define SetBaudrate1		SetBaudrate_1
#define SetDataFormat1		SetDataFormat_1
#define ClearCom1		ClearCom_1
#define DataSizeInCom1		DataSizeInCom_1
#define IsCom1			IsCom_1
#define IsCom1OutBufEmpty	IsComOutBufEmpty_1
#define ReadCom1		ReadCom_1
#define ToCom1Bufn		ToComBufn_1
#define ToCom1Str		ToComStr_1
#define SetCom1Timeout		SetComTimeout_1
#define ToCom1			ToCom_1
#define IsTxBufEmpty1		IsTxBufEmpty_1
#define WaitTransmitOver1	WaitTransmitOver_1
#define ReadCom1n		ReadComn_1
#define printCom1		printCom_1
#define SetBreakMode1		SetBreakMode_1
#define SendBreak1		SendBreak_1
#define IsDetectBreak1		IsDetectBreak_1

#define SetRtsActive1		SetRtsActive_1
#define SetRtsInactive1		SetRtsInactive_1
#define GetCtsStatus1		GetCtsStatus_1
#define CheckCtsStatus1		CheckCtsStatus_1
#define SetCtsControlMode1	SetCtsControlMode_1
#define SetRtsControlMode1	SetRtsControlMode_1

#define DataSizeInCom1_DMA	DataSizeInCom_DMA_1
#define ReadCom1n_DMA		ReadComn_DMA_1
#define InstallCom1_DMA		InstallCom_DMA_1
#define ClearCom1_DMA		ClearCom_DMA_1
#define IsCom1_DMA		IsCom_DMA_1
#define DataSizeInCom1_DMA	DataSizeInCom_DMA_1
#define ReadCom1_DMA		ReadCom_DMA_1

void ClearTxBuffer_1(void);
int GetTxBufferFreeSize_1(void);
int PushDataToCom_1(int data);
void CheckInputBufSize_1(void);
int InstallCom_1(unsigned long baud, int data, int parity,int stop);
int RestoreCom_1(void);
int SetBaudrate_1(unsigned long baud);
int SetDataFormat_1(int databit,int parity,int stopbit);
int ClearCom_1(void);
int ClearCom_DMA_1(void);
int DataSizeInCom_1(void);
int IsCom_1(void);
int IsComOutBufEmpty_1(void);
int ReadCom_1(void);
int ToComBufn_1(char *buf,int no);
int ToComStr_1(char *str);
void SetComTimeout_1(unsigned t);
int ToCom_1(int data);
int IsTxBufEmpty_1(void);
int WaitTransmitOver_1(void);
int ReadComn_1(unsigned char *buf,int no);
int printCom_1(const char *fmt,...);

int DataSizeInCom_DMA_1(void);
int ReadComn_DMA_1(unsigned char *buf,int maxsize);
int InstallCom_DMA_1(unsigned long baud, int data, int parity,int stop);
int ClearCom_DMA_1(void);
int IsCom_DMA_1(void);
int DataSizeInCom_DMA_1(void);
int ReadCom_DMA_1(void);

void SetBreakMode_1(int mode);
void SendBreak_1(unsigned TimeMs);
int IsDetectBreak_1(void);

void SetRtsActive_1(void);
void SetRtsInactive_1(void);
int GetCtsStatus_1(void);
void CheckCtsStatus_1(void);
void SetCtsControlMode_1(int mode);
void SetRtsControlMode_1(int mode);

void SetComPortBufferSize_1(int in_size,int out_size);
void InstallComInputData_1(int (*DoInputData)(unsigned char data));
#define bCtsChanged1	bCtsChanged_1
extern int bCtsChanged_1;

#define CurCTS1		CurCTS_1
extern int CurCTS_1;

#define CurRTS1		CurRTS_1
extern int CurRTS_1;

#define fCtsControlMode1	fCtsControlMode_1
extern int fCtsControlMode_1;

#define fRtsControlMode1	fRtsControlMode_1
extern int fRtsControlMode_1;


/* for COM2 (in normal RS-485)*/
/* WITHOUT CTS & RTS control */

#define	ClearTxBuffer2		ClearTxBuffer_2
#define GetTxBufferFreeSize2	GetTxBufferFreeSize_2
#define PushDataToCom2		PushDataToCom_2

#define CheckInputBufSize2	CheckInputBufSize_2
#define InstallCom2		InstallCom_2
#define RestoreCom2		RestoreCom_2
#define SetBaudrate2		SetBaudrate_2
#define SetDataFormat2		SetDataFormat_2
#define ClearCom2		ClearCom_2
#define DataSizeInCom2		DataSizeInCom_2
#define IsCom2			IsCom_2
#define IsCom2OutBufEmpty	IsComOutBufEmpty_2
#define ReadCom2		ReadCom_2
#define ToCom2Bufn		ToComBufn_2
#define ToCom2Str		ToComStr_2
#define SetCom2Timeout		SetComTimeout_2
#define ToCom2			ToCom_2
#define IsTxBufEmpty2		IsTxBufEmpty_2
#define WaitTransmitOver2	WaitTransmitOver_2
#define ReadCom2n		ReadComn_2
#define printCom2		printCom_2
#define SetBreakMode2		SetBreakMode_2
#define SendBreak2		SendBreak_2
#define IsDetectBreak2		IsDetectBreak_2

#define DataSizeInCom2_DMA	DataSizeInCom_DMA_2
#define ReadCom2n_DMA		ReadComn_DMA_2
#define InstallCom2_DMA		InstallCom_DMA_2
#define ClearCom2_DMA		ClearCom_DMA_2
#define IsCom2_DMA		IsCom_DMA_2
#define DataSizeInCom2_DMA	DataSizeInCom_DMA_2
#define ReadCom2_DMA		ReadCom_DMA_2

void ClearTxBuffer_2(void);
int GetTxBufferFreeSize_2(void);
int PushDataToCom_2(int data);
void CheckInputBufSize_2(void);
int InstallCom_2(unsigned long baud, int data, int parity,int stop);
int RestoreCom_2(void);
int SetBaudrate_2(unsigned long baud);
int SetDataFormat_2(int databit,int parity,int stopbit);
int ClearCom_2(void);
int ClearCom_DMA_2(void);
int DataSizeInCom_2(void);
int IsCom_2(void);
int IsComOutBufEmpty_2(void);
int ReadCom_2(void);
int ToComBufn_2(char *buf,int no);
int ToComStr_2(char *str);
void SetComTimeout_2(unsigned t);
int ToCom_2(int data);
int IsTxBufEmpty_2(void);
int WaitTransmitOver_2(void);
int ReadComn_2(unsigned char *buf,int no);
int printCom_2(const char *fmt,...);

int DataSizeInCom_DMA_2(void);
int ReadComn_DMA_2(unsigned char *buf,int maxsize);
int InstallCom_DMA_2(unsigned long baud, int data, int parity,int stop);
int ClearCom_DMA_2(void);
int IsCom_DMA_2(void);
int DataSizeInCom_DMA_2(void);
int ReadCom_DMA_2(void);

void SetBreakMode_2(int mode);
void SendBreak_2(unsigned TimeMs);
int IsDetectBreak_2(void);

void SetComPortBufferSize_2(int in_size,int out_size);
void InstallComInputData_2(int (*DoInputData)(unsigned char data));

/*
 Functions for COM3 (WITH RTS/CTS)
*/
#define InstallCom3	InstallCom_3
#define RestoreCom3	RestoreCom_3
#define IsCom3		IsCom_3
#define ToCom3		ToCom_3
#define ToCom3Str	ToComStr_3
#define ToCom3Bufn	ToComBufn_3
#define printCom3	printCom_3
#define ClearTxBuffer3	ClearTxBuffer_3
#define SetCom3FifoTriggerLevel		SetComFifoTriggerLevel_3
#define SetBaudrate3	SetBaudrate_3
#define ReadCom3	ReadCom_3
#define ClearCom3	ClearCom_3
#define DataSizeInCom3  DataSizeInCom_3
#define WaitTransmitOver3	WaitTransmitOver_3
#define IsTxBufEmpty3		IsTxBufEmpty_3
#define IsCom3OutBufEmpty	IsComOutBufEmpty_3
#define ReadCom3n		ReadComn_3
#define SetDataFormat3		SetDataFormat_3
#define SetRtsActive3		SetRtsActive_3
#define SetRtsInactive3		SetRtsInactive_3
#define GetCtsStatus3		GetCtsStatus_3
#define SetCtsControlMode3	SetCtsControlMode_3
#define SetRtsControlMode3	SetRtsControlMode_3
#define SetBreakMode3		SetBreakMode_3
#define SendBreak3		SendBreak_3
#define IsDetectBreak3		IsDetectBreak_3

int InstallCom_3(unsigned long baud, int data, int parity, int stop);
int RestoreCom_3(void);
int IsCom_3(void);
int ToCom_3(int data);
int ToComStr_3(char *str);
int ToComBufn_3(char *buf,int no);
int printCom_3(const char *fmt,...);
void ClearTxBuffer_3(void);
int GetTxBufferFreeSize_3(void);
int SetComFifoTriggerLevel_3(int level);
int SetBaudrate_3(unsigned long baud);
int ReadCom_3(void);
int ClearCom_3(void);
int DataSizeInCom_3(void);
int WaitTransmitOver_3(void);
int IsTxBufEmpty_3(void);
int IsComOutBufEmpty_3(void);
int SetDataFormat_3(int databit,int parity,int stopbit);
int ReadComn_3(unsigned char *buf,int n);
void SetRtsActive_3(void);
void SetRtsInactive_3(void);
int GetCtsStatus_3(void);
void SendBreak_3(unsigned timems);
void SetBreakMode_3(int mode);
int IsDetectBreak_3(void);
void SetCtsControlMode_3(int mode);
void SetRtsControlMode_3(int mode);

void SetComPortBufferSize_3(int in_size,int out_size);
void InstallComInputData_3(int (*DoInputData)(unsigned char data));

#define bCtsChanged3	bCtsChanged_3
extern int bCtsChanged_3;

#define CurCTS3		CurCTS_3
extern int CurCTS_3;

#define CurRTS3		CurRTS_3
extern int CurRTS_3;

#define fCtsControlMode3	fCtsControlMode_3
extern int fCtsControlMode_3;

#define fRtsControlMode3	fRtsControlMode_3
extern int fRtsControlMode_3;


/*
  For Send command to I-7000/I-87K series.
*/
extern char hex_to_ascii[16];
int ascii_to_hex(char ascii);

int SendCmdTo7000(int iPort, unsigned char *cCmd, int iChksum);
/*
(INPUT)iPort:can be 1~8.
(INPUT)lTimeout: unit is ms.
(INPUT) cCmd: cmd for send to COM port(I-7000/I-87K).
              DON'T add '\r' at the end of cCmd, SendCmdTo7000() will add check sum(if needed) & '\r' after cCmd .
(INPUT) iChksum: 0: disable, 1: enable.
*/

int ReceiveResponseFrom7000(int iPort, unsigned char *cCmd, long lTimeout, int iChksum);
/*
(INPUT)iPort:can be 1~8.
(INPUT)lTimeout: unit is check times.
(OUTPUT) cCmd: response from COM port(I-7000/I-87K).
(INPUT) iChksum: 0: disable, 1: enable.
*/

#define ReceiveResponseFrom7000_ms	ReceiveResponseFrom7000_1
int ReceiveResponseFrom7000_1(int iPort, unsigned char *cCmd, long lTimeout, int iChksum);
/*
(INPUT)iPort:can be 1~8.
(INPUT)lTimeout: unit is ms. (*****)
(OUTPUT) cCmd: response from COM port(I-7000/I-87K).
(INPUT) iChksum: 0: disable, 1: enable.
*/

/* for ALL COM PORT */
int printCom(int port,const char *fmt,...);
int IsDetectBreak(int port);
int SendBreak(int port,unsigned timems);
int SetBreakMode(int port,int mode);
int ClearCom(int port);
int ClearTxBuffer(int port);
int InstallCom(int port, unsigned long baud, int data, int parity,int stop);
int ToComBufn(int port,char *buf,int no);
int RestoreCom(int port);
int ToComStr(int port,char *str);
int DataSizeInCom(int port);
int IsCom(int port);
int ReadComn(int port,unsigned char *buf,int n);
int ReadCom(int port);
int SetBaudrate(int port,unsigned long baud);
int ToCom(int port,int data);
int IsTxBufEmpty(int port);
int GetTxBufferFreeSize(int port);
int WaitTransmitOver(int port);
int SetRtsActive(int port);
int SetRtsInactive(int port);
int GetCtsStatus(int port);
int SetDataFormat(int port,int databit,int parity,int stopbit);
int SetCtsControlMode(int port,int mode);	/*[2006/01/19] add*/
int SetRtsControlMode(int port,int mode);	/*[2006/01/19] add*/
int SetXonXoffControlMode(int port,int mode);	/*[2006/01/19] add*/
int SetMCR(int port,int mcr); /*[2006/06/29] */
int SetMCR_Bit(int port, int mcr_bit); /*[2006/06/29] */
int ClearMCR_Bit(int port,int mcr_bit); /*[2006/06/29] */
int SetComFifoTriggerLevel(int port,int level); /*[2006/06/29] */
void SetComPortBufferSize(int port,int in_size,int out_size); /* [2006/07/12] */
int GetCtsControlMode(int port);/*[2006/11/09]*/
int GetRtsControlMode(int port);/*[2006/11/09]*/
int SetDtrActive(int port); /*[2006/11/09]*/
int SetDtrInactive(int port); /*[2006/11/09]*/
int GetDsrStatus(int port); /*[2006/11/09]*/
int GetMSR(int port);	/*[2006/11/09]*/
int GetCurMsr(int port);	/*[2006/11/09]*/
int GetMsrChanged(int port);	/*[2006/11/09]*/
int ClrMsrChanged(int port);	/*[2006/11/09]*/
int GetComFifoTriggerLevel(int port);	/*[2006/11/09]*/

void InstallComInputData( int port,int (*)(unsigned char)); /* [2007/10/02] */
int IsComOutBufEmpty(int port); /* [2007/10/02] */
int SetComTimeout(int port,unsigned t); /* [2007/10/02] */

/*  function table for up functions except printCom */
/*
  For example if want to call:
   if(IsCom(port)){
   	data=ReadCom(port);
   }
  also can use :
   if(IsCom_[port]()){
   	data=ReadCom_[port]();
   }

  IsCom(port)/ReadCom(port) just for backword compatible, it also will call IsCom_[port]()/ReadCom_[port]()

  and so on.
*/

#define COM_PORT_NO	3

extern int (*IsDetectBreak_[COM_PORT_NO+1])(void);
extern void (*SendBreak_[COM_PORT_NO+1])(unsigned timems);
extern void (*SetBreakMode_[COM_PORT_NO+1])(int mode);
extern int (*ClearCom_[COM_PORT_NO+1])(void);
extern void (*ClearTxBuffer_[COM_PORT_NO+1])(void);
extern int (*InstallCom_[COM_PORT_NO+1])(unsigned long baud, int data, int parity,int stop);
extern int (*ToComBufn_[COM_PORT_NO+1])(char *buf,int no);
extern int (*RestoreCom_[COM_PORT_NO+1])(void);
extern int (*ToComStr_[COM_PORT_NO+1])(char *str);
extern int (*DataSizeInCom_[COM_PORT_NO+1])(void);
extern int (*IsCom_[COM_PORT_NO+1])(void);
extern int (*ReadComn_[COM_PORT_NO+1])(unsigned char *buf,int n);
extern int (*ReadCom_[COM_PORT_NO+1])(void);
extern int (*SetBaudrate_[COM_PORT_NO+1])(unsigned long baud);
extern int (*ToCom_[COM_PORT_NO+1])(int data);
extern int (*IsTxBufEmpty_[COM_PORT_NO+1])(void);
extern int (*GetTxBufferFreeSize_[COM_PORT_NO+1])(void);
extern int (*WaitTransmitOver_[COM_PORT_NO+1])(void);
extern void (*SetRtsActive_[COM_PORT_NO+1])(void);
extern void (*SetRtsInactive_[COM_PORT_NO+1])(void);
extern int (*GetCtsStatus_[COM_PORT_NO+1])(void);
extern int (*SetDataFormat_[COM_PORT_NO+1])(int databit,int parity,int stopbit);
extern void (*SetCtsControlMode_[COM_PORT_NO+1])(int mode);	/*[2006/01/19] add*/
extern void (*SetRtsControlMode_[COM_PORT_NO+1])(int mode);	/*[2006/01/19] add*/
extern void (*SetXonXoffControlMode_[COM_PORT_NO+1])(int mode);	/*[2006/01/19] add*/
extern int (*SetMCR_[COM_PORT_NO+1])(int mcr); /* [2006/06/29] */
extern void (*ClearMCR_Bit_[COM_PORT_NO+1])(int mcr_bit); /*[2006/06/29] */
extern void (*SetMCR_Bit_[COM_PORT_NO+1])(int mcr_bit); /*[2006/06/29] */
extern int (*SetComFifoTriggerLevel_[COM_PORT_NO+1])(int level); /*[2006/06/29] */
extern void (*SetComPortBufferSize_[COM_PORT_NO+1])(int in_size,int out_size); /* [2006/07/12] */

extern int (*GetCtsControlMode_[COM_PORT_NO+1])(void);/*[2006/11/09]*/
extern int (*GetRtsControlMode_[COM_PORT_NO+1])(void);/*[2006/11/09]*/
extern void (*SetDtrActive_[COM_PORT_NO+1])(void); /*[2006/11/09]*/
extern void (*SetDtrInactive_[COM_PORT_NO+1])(void); /*[2006/11/09]*/
extern int (*GetDsrStatus_[COM_PORT_NO+1])(void); /*[2006/11/09]*/
extern int (*GetMSR_[COM_PORT_NO+1])(void);		/*[2006/11/09]*/
extern int (*GetCurMsr_[COM_PORT_NO+1])(void);	/*[2006/11/09]*/
extern int (*GetMsrChanged_[COM_PORT_NO+1])(void);	/*[2006/11/09]*/
extern int (*ClrMsrChanged_[COM_PORT_NO+1])(void);	/*[2006/11/09]*/
extern int (*GetComFifoTriggerLevel_[COM_PORT_NO+1])(void);	/*[2006/11/09]*/

extern void (*InstallComInputData_[COM_PORT_NO+1])(int (*)(unsigned char)); /* [2007/10/02] */
extern int (*IsComOutBufEmpty_[COM_PORT_NO+1])(void); /* [2007/10/02] */
extern void (*SetComTimeout_[COM_PORT_NO+1])(unsigned t); /* [2007/10/02] */

int GetComportNumber(void);
extern int TriggerLevel[COM_PORT_NO];

/* for IP/MASK/GATEWAY/MAC
  Call Setxxx() will store the setting value to EEPROM block 7,
  call Getxxx() will read the setting from EEPROM.
*/
void GetIp(unsigned char *ip);
#define GetMac	GetEid
void GetEid(unsigned char *id);
void GetMask(unsigned char *mask);
void GetGateway(unsigned char *gate);
void SetIp(unsigned char *ip);
#define SetMac	SetEid
void SetEid(unsigned char *id);
void SetMask(unsigned char *mask);
void SetGateway(unsigned char *gate);

extern const unsigned char far * const SystemSerialNumber;

void InitLib(void);
/*
  for I-7186e, User's program MUST CALL InitLib().
*/
void GetLibDate(char *date);
unsigned GetLibVersion(void);
/*
 If current version is 2.03,GetLibVersion() returns 0x0203.
*/

/* [11/06/2003] add Software flow control(Xon/Xoff) for COM1~COM8 except COM2
	COM2 is RS-485, used in half-duplex mode, need not software flow control.
*/
//void SetXonXoffControlMode_1(int mode);
//void SetXonXoffControlMode_3(int mode);
//void SetXonXoffControlMode_4(int mode);
//void SetXonXoffControlMode_5(int mode);
/*
  mode=0 --> disable Xon/Xoff control
  mode=1 --> enable Xon/Xoff control
*/

/* For PIO pins on I/O expansion BUS */
void SetDio4Dir(int dir);
void SetDio4High(void);
void SetDio4Low(void);
int GetDio4(void);

void SetDio9Dir(int dir);
void SetDio9High(void);
void SetDio9Low(void);
int GetDio9(void);

void SetDio14Dir(int dir);
void SetDio14High(void);
void SetDio14Low(void);
int GetDio14(void);

void SetTi0Dir(int dir);
void SetTi0High(void);
void SetTi0Low(void);
int GetTi0(void);

void SetTi1Dir(int dir);
void SetTi1High(void);
void SetTi1Low(void);
int GetTi1(void);

void SetTo0Dir(int dir);
void SetTo0High(void);
void SetTo0Low(void);
int GetTo0(void);

void SetTo1Dir(int dir);
void SetTo1High(void);
void SetTo1Low(void);
int GetTo1(void);

/* general purpose functions for all PIO pins
   Please be carefully for using these 3 functions.
   !!! NOT ALL 32 PIO pins can use used by user. !!!
*/
void SetPioDir(unsigned pin,int dir);
void SetPio(int pin,int mode);
void SetPioHighLow(int pin);
void SetPioLowHigh(int pin);
int GetPio(int pin);

#define _OUTPUT_MODE							0
#define _INPUT_MODE_WITH_PULL_HIGH_OR_LOW		1
#define _INPUT_MODE_WITHOUT_PULL_HIGH_OR_LOW	2
#define _NORMAL_MODE							3
/*
  input:
    pin : 0~31.
    mode: 0 or 1
    dir:
        0: set the PIO pin to output mode
        1: set the PIO pin to input with pull high(for some pin is pull low.)
        2: set the PIO pin to input without pull high/low.
    	3: set the PIO pin to normal mode

  output for GetPio():
	0: for input mode: the input is low.
	   for output mode: current output is low.
	non zero:   for input mode: the input is high.
	   for output mode: current output is high.
*/

/*
 [2003/12/01]
 Add function for debug, using STDIO COM PORT.
 Even after all InstallCom_1() also can use these 3 functions to send message to STDIO COM port.
*/
void pascal _dPutch(int data1);
void _dPuts(char *str);
int _dPrint(const char *fmt,...);

/*
 [2003/12/10]
 Add function for read system timeticks.
*/
long GetTimeTicks(void);
long GetTimeTicks_ISR(void); /* use this one in ISR */
/*
  The return value is *TimeTicks.
*/

int InstallUserTimerFunction_us(unsigned time,void (*fun)(void));
/*
  time unit is 0.1 us.

  for example:
	If want timer generate interrupt for every 0.5ms(500 us=5000*0.1us)
	(That is to say system will call your function once every 0.5 ms)
	just use

	void fun(void)
	{
		...
	}
	...
	InstallUserTimerFunction_us(5000,fun);
*/

int InstallUserTimerFunction_ms(unsigned time,void (*fun)(void));
/*
  time unit is ms.

  for example:
	If want timer generate interrupt for every 1 second(1 sec=1000 ms)
	(That is to say system will call your function once every 1 sec.)
	just use

	void fun(void)
	{
		...
	}
	...
	InstallUserTimerFunction_ms(1000,fun);
*/

void StopUserTimerFun(void);

/* 2004/02/26 add function usr burst mode to read date/time from RTC chip(DS-1302) */
typedef struct {
	int year;
	char month,day,weekday;
	char hour,minute,sec;
}TIME_DATE;

void GetTimeDate(TIME_DATE *timedate);
int SetTimeDate(TIME_DATE *timedate);
/*
  when call SetTimeDate(), need set the right year,month,day and the function
  will auto set the weekday.
*/

/* 2004/06/11 add function for MCR/MSR of COM port */
#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

#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

int GetMSR_3(void);
int SetMCR_3(int mcr);

/*
 [2004/06/14]
 Add function for debug, using CPU UART 2 (COM2 of I-7188E).
 Even after all InstallCom_2() also can use these 3 functions to send message to COM2.
*/
void pascal _d2Putch(int data1);
void _d2Puts(char *str);
int _d2Print(const char *fmt,...);

/*
 [2004/06/29] add
 [2007/11/15] change MAX_CMD_NO from 20 to 50. 
*/
#define MAX_CMD_NO 50
extern int Argc;
extern char *Argv[MAX_CMD_NO];
int CmdToArg(char *cmd);
/*
 use the *Delimiter=" ,\r\t"; to seperate the args.

 for example:

 char *test_str="/A1 test2 3rd /end";

 after call CmdToArg(test_str);
 Argc=4,
 Argv[0]="/A1";
 Argv[1]="test2";
 Argv[2]="3rd";
 Argv[3]="/end";
*/

void far * AllocateTopMemory(unsigned long size);

extern int CurMsr_3;
extern int bMsrChanged_3;

int GetSerialNumber(char *Serial);
/*
[2004/10/13] Add
  GetSerialNumber() is used to read system serial number from hardware.
  on sucess return 0, and the serial number store to Serial.
  on fail return -1: cannot find the hardware IC
	  return -2: CRC error
*/

void SetMCR_Bit_3(int mcr_bit);
void ClearMCR_Bit_3(int mcr_bit);

/*
[2005/05/27] add functions for using timer functions
*/
int SetDelayTimer(int no);
/*
The delay functions(Dealy()/Delay_1()/Delay_2() by default using system's timer 0.
user can call SetDelayTimer(1); set it to use timer 1.

return : 0 for using timer 0, 1 for using timer 1. 
*/
int SetUserTimer(int no);
/*
The user timer functions(InstallUserTimerFunction_us()/InstallUserTimerFunction_ms()
 by default using system's timer 0.
user can call SetUserTimer(1); set it to use timer 1.

return : 0 for using timer 0, 1 for using timer 1. 
*/

/*
  [2006/02/07] Add functions for write/delete file 
  Please refer to the file:"OS7_file.txt"
*/
int OS7_DeleteAllFile(int disk);
long OS7_GetDiskFreeSize(int disk);
int OS7_OpenWriteFile(int disk);
int OS7_WriteFile(int disk,void *buf,int size);
int OS7_CloseWriteFile(int disk,FILE_DATA *f_data);
extern int OS7_FileDateTimeMode;

/*
  [2006/02/10] add functions for CRC16
  OS7_xxxx() will use these function to get the crc16 of the file data.
  Please refer to the file:"OS7_crc16.txt"
*/
int CRC16_Push(void);
int CRC16_Pop(void);
void CRC16_Set(unsigned val);
#define CRC16_Reset()	CRC16_Set(0)
unsigned CRC16_Read(void);
void CRC16_AddData(unsigned char data);
void CRC16_AddDataN_C(unsigned char far *data,unsigned length);
void CRC16_AddDataN(unsigned char far *data,unsigned length);
int CRC16_MakeTable(void);

/*
[2006/10/18] Add functions for CRC-16 used by Modbus/RTU 
*/
extern unsigned Modbus_CRC16; 
void Modbus_CRC16_Set(unsigned val);
#define Modbus_CRC16_Reset()	Modbus_CRC16_Set(0xFFFF)
unsigned Modbus_CRC16_Read(void);
void Modbus_GetCRC16(unsigned char *puchMsg, int DataLen);
int Modbus_CRC16_Push(void);
int Modbus_CRC16_Pop(void);

/*
[2006/10/18] Add functions for swap data byte order. 
*/
int SwapShort(unsigned data);
/*
  swap the high byte and low byte.
  SwapShort(0x1234) will return 0x3412;
*/

unsigned long SwapLong(unsigned long val);
/*
  SwapLong(0x12345678) will return 0x78563412;
*/

#define LowByte(data)	(unsigned char)(data)
#define HighByte(data)	*(((unsigned char *)&data)+1)
/* [2007/08/02] add get high/low word from a long variable */
#define LowWord(data)	(unsigned)(data)
#define HighWord(data)	*(((unsigned *)&(data))+1)

#define LED_OFF     0
#define LED_ON      1
#define LED_TOGGLE  2

/*
 [2007/01/18] Add function to control the high ram(128K FRAM/SRAM or 256K SRAM )
 7186EX can support high ram.(option)
*/
void EnableHighRam(void);
void DisableHighRam(void);
int GetHighRamMode(void);
void SetHighRam(int mode);

/*
[2007/07/06] Add functions for the NAME/VER/DATE of the library. 
*/
unsigned GetOsLibVersion(void); /* the same as GetLibVersion(). */
char *GetOsLibDate(void);
char *GetOsLibName(void);

/*
[2007/09/20] Add variables for COM ports.
lLostDataNo_x: the lost data number that the driver can count for COMx.
*/
extern long lLostDataNo_1;
extern long lLostDataNo_2;
extern long lLostDataNo_3;
extern long lLostDataNo_4;
extern long lLostDataNo_5;

/*
[2007/12/12] add function for system new timer ISR
*/
void InstallNewTimer(void);
void RestoreNewTimer(void);
extern char Int9Flag; 
extern char Int1cFlag;
/*
Int9Flag : default value is 1;
When Int9Flag != 0, the new timer ISR will call INT 9 every time.
If the program does not using INT 9, set Int9Flag to 0 will get better performance.  

Int1cFlag : default value is 1;
When Int1cFlag != 0, the new timer ISR will call INT 0x1c every 55 ms.
If the program does not using INT 0x1C, set Int1cFlag to 0 will get better performance.  

*/

/*----------------------------------------------------------------------------*/
/* 
[2009/03/24] add functions for Keypad, buzzer, LCD backlight
*/
#define KEY_UP			0
#define KEY_LEFT		1
#define KEY_DOWN		2
#define KEY_ENTER		3
#define KEY_RIGHT		4
#define KEY_BACKSPACE	5

#define KEY_3			6
#define KEY_6			7
#define KEY_9			8
#define KEY_0			9
#define KEY_SHIFT		10
#define KEY_POINT		11

#define KEY_1			12
#define KEY_4			13
#define KEY_7			14
#define KEY_8			15
#define KEY_5			16
#define KEY_2			17

#define KEY_F1			18
#define KEY_F2			19
#define KEY_F3			20
#define KEY_F6			21
#define KEY_F5			22
#define KEY_F4			23
/*-----------------------------------*/
#define F1_KEY	0x0F01
#define F2_KEY	0x0F02
#define F3_KEY	0x0F03
#define F4_KEY	0x0F04
#define F5_KEY	0x0F05
#define F6_KEY	0x0F06
#define UP_KEY	0x0F07
#define DOWN_KEY	0x0F08
#define LEFT_KEY	0x0F09
#define RIGHT_KEY	0x0F0A
#define SHIFT_KEY	0x0F0B

int IsKey(void);
int ReadKey(void);
int IsTempKey(void);

unsigned char IsKeyEvent(void);
unsigned char ReadKeyEvent(void);
int ReadKeyStatus(unsigned key);

extern unsigned _SoundFreq[24];
void OutWave1(unsigned long freq,unsigned high,unsigned low);
void StopOutWave1(void);

#define NoSound StopOutWave1

extern char bSoundFlag; /* 1: 按鍵輸入時會有聲音，0：按鍵輸入時不輸出聲音。*/
extern unsigned *SoundPeriod;
void Sound(unsigned freq,unsigned period);

#define LCD_MAX_BACK_LIGHT	20
extern unsigned *LCD_BackLight; /* range: 0~LCD_MAX_BACK_LIGHT */

#ifdef __cplusplus
}
#endif

#endif
