#include <Windows.h>

 double GetTicks();

#define SECTOR_MAX_BYTE	512
// (V 1.0.0.1) Duke Yang
typedef struct SECTOR_TxBuf
{
	int Index;
	int LBA;
	BYTE data[SECTOR_MAX_BYTE];
}SECTOR_TxBuf ;


union UData
{
   BYTE bArr[8];
   DWORD lVal;
   DWORD64 hVal;
   WORD iVal;
   BYTE bVal;
};
static DWORD  AndBits[32]={0xfffffffe,0xffffffd,0xfffffffb,0xfffffff7,0xffffffef,0xffffffdf,0xffffffbf,0xffffff7f,
							0xfffffeff,0xfffffdff,0xfffffbff,0xfffff7ff,0xffffefff,0xffffdfff,0xffffbfff,0xffff7fff,
							0xfffeffff,0xfffdffff,0xfffbffff,0xfff7ffff,0xffefffff,0xffdfffff,0xffbfffff,0xff7fffff,
							0xfeffffff,0xfdffffff,0xfbffffff,0xf7ffffff,0xefffffff,0xdfffffff,0xbfffffff,0x7fffffff};//用來關掉某一點輸出

static DWORD OrBits[32] ={0x00000001,0x00000002,0x00000004,0x00000008,0x00000010,0x00000020,0x00000040,0x00000080,
							0x00000100,0x00000200,0x00000400,0x00000800,0x00001000,0x00002000,0x00004000,0x00008000,
							0x00010000,0x00020000,0x00040000,0x00080000,0x00100000,0x00200000,0x00400000,0x00800000,
							0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000};//用來開啟某一點輸出
#define IOCTL_STORAGE_QUERY_PROPERTY   CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)


  ////Genaral/Common Command
        const int MSC_PTCMD_START		        = 0x2000;	//MSC PassThrough Command Start from LBA 0x2000
        //const int MSC_PTCMD_IDENTIFY          = 0x2000;	//Device Identify Info
        const int MSC_PTCMD_WP		            = 0x2001;   //Device WriteProtect LBA 8193(0x2001)
        const int MSC_PTCMD_FRAME_FACTOR        = 0x2002;	//RD/WR FrameRate Factor=SamplingRate/(X+1), Default=3
        const int MSC_PTCMD_RST2DEFAULT         = 0x2003;   //Reset Device to Default Setting

        const int MSC_PTCMD_Description_INFO    = 0x2010;	//RD:Device Info/WR:Device Description
        const int MSC_PTCMD_SW_WDT_CNT          = 0x2011;	//RD/WR:Set S/W WatchDog Counter
        const int MSC_PTCMD_SYNC_SW_WDT         = 0x2012;	//RD:---/WR:Clear S/W WatchDog

        //DI Command
        const int MSC_PTCMD_DI_VALUE            = 0x2020;	//Get DI Value
        const int MSC_PTCMD_DI_DigitalFilter    = 0x2021;	//Get/Set DI Digital Filter Value
        const int MSC_PTCMD_DI_Inverse       = 0x2022;	//Get/Set DI Inverse
        const int MSC_PTCMD_DI_EDGETRIG      = 0x2023;	//Get/Set DI Edge trigger 0/1=Rise/Fall
        const int MSC_PTCMD_DI_COUNTER          = 0x2024;	//Get DI Counter/Clear DI Counter

        //DO Command
        const int MSC_PTCMD_DO_VALUE            = 0x2040;	//Get/Set DO Value
        const int MSC_PTCMD_DO_PWR_ON_VALUE		= 0x2041;	//Get/Set DO Power ON Value
        const int MSC_PTCMD_DO_SAFE_VALUE		= 0x2042;	//Get/Set DO Safe Value
        const int MSC_PTCMD_DO_Inverse		    = 0x2043;	//Get/Set DO Inverse


        //AI Command
        const int MSC_PTCMD_AI_VALUE				= 0x2060;	//Get/Set AO Value
        const int MSC_PTCMD_AI_TYPE					= 0x2061;	//Get/Set AO Inverse
        const int MSC_PTCMD_AI_CHANNEL_ENABLE	    = 0x2062;	//Get/Set AO Power ON Value
        const int MSC_PTCMD_AI_ALM_HI_ENABLE		= 0x2063;	//Get/Set AO Safe Value
        const int MSC_PTCMD_AI_ALM_LOW_ENABLE		= 0x2064;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AI_ALM_HI_MODE			= 0x2065;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AI_ALM_LOW_MODE			= 0x2066;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AI_ALM_HI_VALUE			= 0x2067;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AI_ALM_LOW_VALUE		= 0x2068;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AI_LATCH_HI_VALUE		= 0x2069;	//Get AI 8Ch Latch Hi Value, 2Byte word Value								Set AI Clear ALL Latch Hi Value, 1Byte, 0x55							
		const int MSC_PTCMD_AI_LATCH_LOW_VALUE		= 0x206A;	//Get AI 8Ch Latch Low Value, 2Byte word Value								Set AI Clear ALL Latch Low Value, 1Byte, 0xAA					
		const int MSC_PTCMD_AI_ALM_HI_STATUS		= 0x206B;	//Get AI 8Ch Alarm High Status, 1Byte, 0/1 = Disable/Enable
		const int MSC_PTCMD_AI_ALM_LOW_STATUS		= 0x206C;	//Get AI 8Ch Alarm Low Status, 1Byte, 0/1 = Disable/Enable
		const int MSC_PTCMD_AI_RANGE_FILTER			= 0x2070;	//Get ADC(CH0~7) Range  1: +/-10V, 0: +/-5V, & Filter							
		const int MSC_PTCMD_AI_SCANTIME				= 0x2071;	//Set ADC ScanTimer, CH0~7

		//AO Command
		const int MSC_PTCMD_AO_VALUE            = 0x2080;	//Get/Set AO Value
        const int MSC_PTCMD_AO_TYPE		        = 0x2081;	//Get/Set AO Inverse
        const int MSC_PTCMD_AO_PWR_ON_VALUE	    = 0x2082;	//Get/Set AO Power ON Value
        const int MSC_PTCMD_AO_SAFE_VALUE		= 0x2083;	//Get/Set AO Safe Value
        const int MSC_PTCMD_AO_SLEWRATE		    = 0x2084;	//Get/Set AO SlewRate
		const int MSC_PTCMD_AO_SAFE_ENABLE		= 0x2085;	//Get/Set AO Safe Value Enable
		const int MSC_PTCMD_AO_VALUE_ALL		= 0x2086;   //Get/Set ALL AO Value(16bit/Ch)-12bit AO

        //System Command
        const int MSC_PTCMD_MARK_UD     = 0x20F0;	//Mark update
        const int MSC_PTCMD_EXE_UD      = 0x20F1;	//Exec update
        const int MSC_PTCMD_DEV_RESET   = 0x20FF;	//Forced Device Reset
        //Bulk Command
        const int MSC_PTBULK_0_START    = 0x2100;	//Start from LBA 0x2100	IN/OUT, Frame Reset
        const int MSC_PTBULK_START 	    = 0x2101;	//Start from LBA 0x2101	IN/OUT
		const DWORD MSC_PTCMD_FRAME_OPCODE = 0x2013;	//RD/WR:Frame OP Code, 0/1/2=System/DI/AI


        const int MSC_PTBULK_END		= 0x21FF;   //END (0x21FF) 256 * 512 = 128K

        const int MSC_UDBULK_START      = 0x2200;	//Start from LBA 0x2200	IN/OUT
		const ULONG ICPDAS_2M_Size      = 2097152;
		const DWORD IDENTIFY_HEADER     = 0x5A3577A5;	    //IDENTIFY Header for Command
		

//		const UINT SECTOR_SIZE = 512;
		
//		const UINT MAX_NUMBER_OF_DRIVES = 32;