//wdmcan.h - версия 1.03

#define CAN200PC_KRNLDRVR 0x8000
#define MAX_CAN_NUMBER 10

#define V_HardReset 0
//#define chBaseAddress 1
#define V_SetWorkMode 1
#define V_GetWorkMode 2
#define V_SetDriverMode 3
#define V_SetCANSpeed 4
#define V_GetStatus 5
//#define GetInterruptSource 7
#define V_SetInterruptSource 6
#define V_SetTxBuffer 7
#define V_GetRxBuffer 8
#define V_SetCommand 9
#define V_B_SetInputFilter 10
#define V_P_SetRxErrorCounter 11
#define V_P_GetRxErrorCounter 12
#define V_P_SetTxErrorCounter 13
#define V_P_GetTxErrorCounter 14
#define V_P_SetErrorWarningLimit 15
#define V_P_GetErrorWarningLimit 16
#define V_P_GetArbitrationLostCapture 17
#define V_P_GetRxMessageCounter 18
#define V_P_GetErrorCode 19
#define V_P_SetInputFilter 20
#define V_SetCANReg 21
#define V_GetCANReg 22
#define V_ResetRequest 23        
#define V_TestResetRequest 24        
#define V_DefEvent 25
#define V_GetEventData 26
#define V_GetConfig 27


#define CTL_CODE( DeviceType, Function, Method, Access ) (                 \
    ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)

#define METHOD_BUFFERED                 0
#define METHOD_IN_DIRECT                1
#define METHOD_OUT_DIRECT               2
#define METHOD_NEITHER                  3

#define FILE_ANY_ACCESS                 0
#define FILE_READ_ACCESS          ( 0x0001 )    // file & pipe
#define FILE_WRITE_ACCESS         ( 0x0002 )    // file & pipe

        
#define IOCTL_CAN200PC_HardReset                   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_HardReset                  , METHOD_NEITHER, FILE_ANY_ACCESS)
//#define IOCTL_CAN200PC_chBaseAddress               CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_chBaseAddress              , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetWorkMode                 CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetWorkMode                , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetWorkMode                 CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetWorkMode                , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetDriverMode               CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetDriverMode              , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetCANSpeed                 CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetCANSpeed                , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetStatus                   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetStatus                  , METHOD_NEITHER, FILE_ANY_ACCESS)
//#define IOCTL_CAN200PC_GetInterruptSource          CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetInterruptSource         , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetInterruptSource          CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetInterruptSource         , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetTxBuffer                 CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetTxBuffer                , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetRxBuffer                 CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetRxBuffer                , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetCommand                  CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetCommand                 , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_B_SetInputFilter            CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_B_SetInputFilter           , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_SetRxErrorCounter         CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_SetRxErrorCounter        , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetRxErrorCounter         CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetRxErrorCounter        , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_SetTxErrorCounter         CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_SetTxErrorCounter        , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetTxErrorCounter         CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetTxErrorCounter        , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_SetErrorWarningLimit      CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_SetErrorWarningLimit     , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetErrorWarningLimit      CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetErrorWarningLimit     , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetArbitrationLostCapture CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetArbitrationLostCapture, METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetRxMessageCounter       CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetRxMessageCounter      , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_GetErrorCode              CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_GetErrorCode             , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_P_SetInputFilter            CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_P_SetInputFilter           , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_SetCANReg				   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_SetCANReg           , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetCANReg				   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetCANReg           , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_ResetRequest				   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_ResetRequest        , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_TestResetRequest			   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_TestResetRequest    , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_DefEvent					   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_DefEvent    , METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetEventData				   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetEventData, METHOD_NEITHER, FILE_ANY_ACCESS)
#define IOCTL_CAN200PC_GetConfig				   CTL_CODE(CAN200PC_KRNLDRVR, 0x800+V_GetConfig, METHOD_NEITHER, FILE_ANY_ACCESS)



#define ResetMode  0x01
#define NormalMode 0x00
#define BasicCAN   0x00
#define PeliCAN    0x01

/* прототипы функций для режимов BasicCAN и PeliCAN*/
typedef struct
	{
    unsigned short FF;          /* Frame Format - формат кадра        */
                                            /* 0 - стандартный кадр   */
                                            /* 1 - расширенный кадр   */
    unsigned short sID;         /* стандартный идентификатор (11 бит) */
    unsigned long extID;        /* расширенный идентификатор (29 бит) */
    unsigned short RTR;         /* RTR - бит                          */
    unsigned short DLC;         /* DLC - количество байт данных (0-8) */
    unsigned short DataByte[8]; /* Байты данных                       */
    } RX_TX_Buffer,*pRX_TX_Buffer;

typedef struct
	{
	RX_TX_Buffer	rxtxbuf;
	UCHAR			IntrID;
	}TEventData,*pTEventData;

typedef struct
{
char szName[128];
char szSN[10];
USHORT wPorts1;
USHORT wPorts2;
USHORT wIRQ;
}TCAN_VPDData,*pTCAN_VPDData;


unsigned short  CANOpen (int number);
void            CANClose (int number);
int				SelectCAN(int number);
void			chBaseAddress(unsigned int Base);
unsigned short  SetWorkMode(unsigned short Mode);
unsigned short  GetWorkMode(void);
void            HardReset(void);
void            SetDriverMode(unsigned short Mode);
unsigned short  SetCANSpeed(unsigned int Speed);
unsigned short  GetStatus(void);
void            SetInterruptSource(unsigned short Source);
unsigned short  SetTxBuffer(pRX_TX_Buffer Buffer);
unsigned short  GetRxBuffer(pRX_TX_Buffer Buffer);
void            SetCommand(unsigned short Command);

/* прототипы функций для режима BasicCAN */
unsigned short   B_SetInputFilter(unsigned short Filter1,unsigned short Filter2);

/* прототипы функций для режима PeliCAN */
unsigned short  P_SetRxErrorCounter(unsigned short Counter);
int  P_GetRxErrorCounter(void);
unsigned short  P_SetTxErrorCounter(unsigned short Counter);
int  P_GetTxErrorCounter(void);
unsigned short  P_SetErrorWarningLimit(unsigned short Limit);
int  P_GetErrorWarningLimit(void);
int  P_GetArbitrationLostCapture(void);
int  P_GetRxMessageCounter(void);
int  P_GetErrorCode(void);
unsigned short  P_SetInputFilter(unsigned short A1, unsigned short A2, unsigned short A3, unsigned short A4, unsigned short A5, unsigned short A6, unsigned short A7, unsigned short A8, unsigned short A9);
void			SetCANReg(unsigned short A1, unsigned short A2);
unsigned short	GetCANReg(unsigned short A1);
void			ResetRequest(unsigned short A1);
unsigned short	TestResetRequest(void);
void			DefEvent( HANDLE hEvent, BOOL fEventSet );
unsigned short  GetEventData(pTEventData Buffer);
unsigned short  GetConfig(pTCAN_VPDData Buffer);


