#ifdef __cplusplus
extern "C" {
#endif

#ifndef DWORD
#define DWORD unsigned long
#endif

#ifndef WORD
#define WORD unsigned short
#endif

#ifndef BYTE
#define BYTE unsigned char
#endif

///////////////////////////////////////////
//                                       //
//    Common Parameters and Functions    //
//                                       //
///////////////////////////////////////////
//Error Code Definitions
#define I8120_NoError                    0
#define I8120_SlotNumberError            3
#define I8120_InitError                  7
#define I8120_SoftBufferIsEmpty          21
#define I8120_SoftBufferIsFull           22
#define I8120_TimeOut                    23
#define I8120_SetCyclicMsgFailure        24
#define I8120_DpramOverRange             25
#define I8120_NoDpramCmd                 26
#define I8120_ModeError                  27
#define I8120_NoFileInside               30
#define I8120_DownloadFailure            31
#define I8120_EEPROMDamage               32
#define I8120_NotEnoughSpace             33
#define I8120_StillDownloading           34
#define I8120_BoardModeError             35
#define I8120_SetDateTimeFailure         36
#define I8120_SlotNotConfig              40
#define I8120_SlotNotInit                41
#define I8120_ReplyError                 42
#define I8120_WaitForReply               43

//API Definitions
WORD I8120_GetDllVersion(void);
int I8120_AdjustDateTime(BYTE SlotNo);
int I8120_Reset(BYTE SlotNo);
int I8120_Init(BYTE SlotNo);
int I8120_HardwareReset(BYTE SlotNo);
int I8120_Check186Mode(BYTE SlotNo, BYTE *Mode);
int I8120_Status(BYTE SlotNo, BYTE *bStatus);
int I8120_AddCyclicTxMsg(BYTE SlotNo, BYTE Mode, DWORD MsgID, BYTE RTR, 
								  BYTE DataLen, BYTE *Data, DWORD TimePeriod, DWORD TransmitTimes,
								  BYTE *Handle);
int I8120_DeleteCyclicTxMsg(BYTE SlotNo, BYTE Handle);
int I8120_EnableCyclicTxMsg(BYTE SlotNo, BYTE Handle);
int I8120_DisableCyclicTxMsg(BYTE SlotNo, BYTE Handle);
int I8120_OutputByte(BYTE SlotNo, WORD wOffset,BYTE bValue);
int I8120_InputByte(BYTE SlotNo, WORD wOffset, BYTE *GetData);
int I8120_IsTxTimeout(BYTE SlotNo, BYTE *Status);
int I8120_SetSystemMsg(BYTE SlotNo, BYTE Mode);
int I8120_EnableSJA1000(BYTE SlotNo);
int I8120_DisableSJA1000(BYTE SlotNo);
////////////////////////////////////////////////////////////////
//                                                            //
//  Parameters and Functions Only For I8120 default Firmware  //
//                                                            //
////////////////////////////////////////////////////////////////

//structure Definitions
typedef struct
{
	BYTE AccCode[4];
	BYTE AccMask[4];  //bit 1 for pass, bit 0 for check
	BYTE BaudRate; //BaudRate 0:user define, 1=10K, 2=20K, 3=50K, 4=125K, 5=250K, 6=500K, 7=800K, 8=1Mbps
	BYTE BT0,BT1;
} ConfigStruct;

typedef struct packet
{	
	DWORD UpperTimeStamp;
	DWORD LowerTimeStamp;  //TimeStamps=(MsgUpperTimeStamps<<32)+MsgLowerTimeStamps , unit:100us
	BYTE  Mode;   //mode 0 -> 11 bits ID    1 -> 29 bits ID
	DWORD Id;
	BYTE  Rtr;
	BYTE  Len;
	BYTE  Data[8];
} PacketStruct;

//API Definitions
int I8120_ClearSoftBuffer(BYTE SlotNo);
int I8120_ClearBufferStatus(BYTE SlotNo);
int I8120_ClearDataOverrun(BYTE SlotNo);
int I8120_Config(BYTE SlotNo, ConfigStruct *CanConfig);
int I8120_ConfigWithoutStruct(BYTE SlotNo, DWORD AccCode, DWORD AccMask, BYTE BaudRate, BYTE BT0, BYTE BT1);
int I8120_RxMsgCount(BYTE SlotNo, WORD *RxMsgCnt);
int I8120_ReceiveMsg(BYTE SlotNo, PacketStruct *CanPacket);
int I8120_ReceiveWithoutStruct(BYTE SlotNo, BYTE *Mode, DWORD *MsgID,
										        BYTE *RTR, BYTE *DataLen, BYTE *Data,
										        DWORD *UpperTime, DWORD *LowerTime);
int I8120_SendMsg(BYTE SlotNo, PacketStruct *CanPacket);
int I8120_SendWithoutStruct(BYTE SlotNo, BYTE Mode, DWORD MsgID, 
											 BYTE RTR, BYTE DataLen, BYTE *Data);

///////////////////////////////////////////////////////////////
//                                                           //
//  Parameters and Functions Only For user-defined Firmware  //
//                                                           //
///////////////////////////////////////////////////////////////

//Interrupt To User ISR
#define CAN_COMM_CMD_FROM_I8120  0xd1

//API Definitions
int I8120_SJA1000Config(BYTE SlotNo, DWORD AccCode, DWORD AccMask, BYTE BaudRate, BYTE BT0, BYTE BT1);
int I8120_DPRAMInttToI8120(BYTE SlotNo, BYTE Data);
int I8120_DPRAMWriteByte(BYTE SlotNo, WORD Address, BYTE Data);
int I8120_DPRAMWriteWord(BYTE SlotNo, WORD Address, WORD Data);
int I8120_DPRAMWriteDword(BYTE SlotNo, WORD Address, DWORD Data);
int I8120_DPRAMWriteMultiByte(BYTE SlotNo, WORD Address, BYTE *Data, WORD DataNum);
int I8120_DPRAMReadByte(BYTE SlotNo,WORD Address, BYTE *Data);
int I8120_DPRAMReadWord(BYTE SlotNo, WORD Address, WORD *Data);
int I8120_DPRAMReadDword(BYTE SlotNo, WORD Address, DWORD *Data);
int I8120_DPRAMReadMultiByte(BYTE SlotNo, WORD Address, BYTE *Data, WORD DataNum);
int I8120_DPRAMMemset(BYTE SlotNo, WORD Address, BYTE Data, WORD DataNum);
int I8120_ReceiveCmd(BYTE SlotNo, BYTE *Data, WORD *DataNum);
int I8120_ClearReceiveCmd(BYTE SlotNo);
int I8120_SendCmd(BYTE SlotNo, BYTE *Data, WORD DataNum);
int I8120_InstallUserISR(BYTE SlotNo, void (*UserISR)(BYTE SlotNo, BYTE InttValue));
int I8120_RemoveUserISR(BYTE SlotNo);
int I8120_RestoreI8120(BYTE SlotNo);
#ifdef __cplusplus
}
#endif
