⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ez811_dk.c

📁 本程序包是USB开发主机系统的例程序。
💻 C
📖 第 1 页 / 共 3 页
字号:
   
   *phDeviceHandle = CreateFile(   completeDeviceName,
      GENERIC_WRITE,
      FILE_SHARE_WRITE,
      NULL,
      OPEN_EXISTING,
      0,
      NULL);
   
   if (*phDeviceHandle == INVALID_HANDLE_VALUE)
      return (FALSE);

   else 
      return (TRUE);
      
}   

//**********************************************************************************
// millisecond delay
//**********************************************************************************
void msDelay(ULONG time)
{
	ULONG StartTime;

	StartTime = (ULONG)GetTickCount();
	while( ((ULONG)GetTickCount()-StartTime) != time);
}

//**********************************************************************************
// Display USB data to Data Windows
//**********************************************************************************
#define dBYTES_PER_LINE 0x08
void DispDataBox(PVOID pvBuffer, ULONG length, HWND hOutputBox1)
{
	int                    nItems    = 0;
	char                   temp[64]  = "";
	char                   temp2[64]  = "";
	ULONG	i;
	ULONG   j;
	PUCHAR	ptr;
   
	ptr = (PUCHAR) pvBuffer;

	if(ptr[0]==XFERSTOP0 && ptr[1]==XFERSTOP1 && length==2)
	{
		bDataXferThread = FALSE;
		EzSendMessage (hOutputBox1, LB_ADDSTRING, 0, (LPARAM)"Repeat data transfer stopped");
	}

	else if(ptr[0]==NONEXIST0 && ptr[1]==NONEXIST1 && length==2)
	{
		bDataXferThread = FALSE;
		EzSendMessage (hOutputBox1, LB_ADDSTRING, 0, (LPARAM)"USB device does not exist");
		EzSendMessage (hOutputBox1, LB_ADDSTRING, 0, (LPARAM)"Repeat data transfer stopped");
	}

	else
	{
		for (i = 0; i < ((length + dBYTES_PER_LINE - 1) / dBYTES_PER_LINE); i++)
		{
			sprintf(temp,"");
			switch(ezTYPE)
			{
				case ISOCHRONOUS:	sprintf(temp,"ISO"); break;
				case BULK:			sprintf(temp,"BULK"); break;
				case INTERRUPT:		sprintf(temp,"INTR"); break;
			}
			if(ezENP&0x80)
				sprintf(temp2,"-IN   : "); 
			else
				sprintf(temp2,"-OUT: "); 
			strcat(temp,temp2);
				
			for (j = 0; j < dBYTES_PER_LINE; j++)
			{
				if (((i * dBYTES_PER_LINE) + j) < length)
				{
					sprintf(temp2,"%02X ",*ptr++);
					strcat(temp,temp2);
				}
			}

			EzSendMessage (hOutputBox1, LB_ADDSTRING, 0, (LPARAM)temp);
		}
	}
	

}

//**********************************************************************************
// Dumping raw descriptor data to Status Windows
//**********************************************************************************
#define sBYTES_PER_LINE 0x08
void DumpStatusBox(PVOID pvBuffer, ULONG length, HWND hOutputBox)
{
	int                    nItems    = 0;
	char                   temp[64]  = "";
	char                   temp2[64]  = "";
	ULONG	i,j;
	PUCHAR	ptr;
   
   ptr = (PUCHAR) pvBuffer;

   for (i = 0; i < ((length + sBYTES_PER_LINE - 1) / sBYTES_PER_LINE); i++)
   {
      wsprintf(temp,"%03X: ",(i*sBYTES_PER_LINE));
      for (j = 0; j < sBYTES_PER_LINE; j++)
      {
         if (((i * sBYTES_PER_LINE) + j) < length)
         {
            wsprintf(temp2,"%02X ",*ptr++);
            strcat(temp,temp2);
         }
      }
      EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
   }
}

//**********************************************************************************
// Dumping String to Status Windows
//**********************************************************************************
void DumpString(PVOID pvBuffer, ULONG length, HWND hOutputBox)
{
	int                    nItems    = 0;
	char                   temp[64]  = "";
	char                   temp2[64]  = "";
	PUCHAR	ptr;
	int		i,len;
   
	ptr = (PUCHAR) pvBuffer;
	len = (int)ptr[0];
	
	if(!ptr[2])				// if 3rd byte is a zero, i.e. error in string
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"No String");   		

	else
	{
		for(i=2;i<len;i+=2)
		{
			sprintf(temp2,"%c",ptr[i]);
			strcat(temp,temp2);
		}
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);   
	}
}

//**********************************************************************************
// Display USB info data (all endpoints) to Info Window
//**********************************************************************************
void DispDevData(PVOID pvBuffer, ULONG length, HWND hOutputBox)
{
	int     nItems		= 0;
	char    temp[256]	= "";
	char    temp2[64]	= "";
	char	vend[256]	= "";
	char	vend2[64]	= "";
	ULONG	i,cnt,vid;
	PUCHAR	ptr;
	PUCHAR	ptrmax;
	ULONG NumDataEp;

	if(length < 2)
		return; // don't print invalid info
   
	ptr = (PUCHAR) pvBuffer;
	ptrmax = (PUCHAR) pvBuffer;
	ptrmax += length;
	for(ptr = (PUCHAR) pvBuffer; (ptr < ptrmax) && ptr[0]; ptr+=cnt) // itterate through addresses
	{
		// USB Device Updates
		sprintf(temp,"USB Address %d, ",ptr[0]);				// USB Address
		if(ptr[1])
		{
			sprintf(temp2,"Port %d ",ptr[1]);					// Hub Port Number
			strcat(temp,temp2);				
		}
		if(ptr[2])
			strcat(temp," (Low Speed - ");						// DeviceSpeed
		else
			strcat(temp," (Full Speed - ");
		switch(ptr[3])
		{
			case 0x01:	strcat(temp,"AUD)"); break;				// Class Type
			case 0x0A:
			case 0x02:	strcat(temp,"COM)"); break;
			case 0x03:	strcat(temp,"HID)"); break;
			case 0x05:	strcat(temp,"PHY)"); break;
			case 0x07:	strcat(temp,"PRN)"); break;
			case 0x08:	strcat(temp,"MSC)"); break;
			case 0x09:	strcat(temp,"HUB)"); break;
			case 0x0B:	strcat(temp,"SMC)"); break;
			case 0x0D:	strcat(temp,"SEC)"); break;
			case 0xFF:	strcat(temp,"VEN)"); break;
			default:strcat(temp,"STD)"); break;
		}		
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);	

		sprintf(vend, "-- Vendor ID  : ");
		sprintf(vend2,"0x%02X%02X",ptr[5],ptr[4]);	// VID hex code
		vid = (ptr[5]*256) + ptr[4];				// VID value		
		switch(vid)		
		{
			case 0x03ee: strcat(vend,vend2); strcat(vend," - Mitsumi"); break;	
			case 0x03f0: strcat(vend,vend2); strcat(vend," - Hewlett-Packard"); break;
		  	case 0x03f3: strcat(vend,vend2); strcat(vend," - Adaptec"); break;
			case 0x040a: strcat(vend,vend2); strcat(vend," - Kodak"); break;
			case 0x041e: strcat(vend,vend2); strcat(vend," - Creative"); break;
			case 0x0423: strcat(vend,vend2); strcat(vend," - CATC"); break;
			case 0x042b: strcat(vend,vend2); strcat(vend," - Intel"); break;
			case 0x0446: strcat(vend,vend2); strcat(vend," - NMB"); break;
		  	case 0x045e: strcat(vend,vend2); strcat(vend," - Microsoft"); break;
			case 0x046d: strcat(vend,vend2); strcat(vend," - Logitech"); break;
			case 0x046e: strcat(vend,vend2); strcat(vend," - BTC"); break;
			case 0x047c: strcat(vend,vend2); strcat(vend," - Dell"); break;
			case 0x047b: strcat(vend,vend2); strcat(vend," - Silitek"); break;
			case 0x049f: strcat(vend,vend2); strcat(vend," - Compaq"); break;
			case 0x04b0: strcat(vend,vend2); strcat(vend," - Nikon"); break;
			case 0x04b3: strcat(vend,vend2); strcat(vend," - IBM"); break;
			case 0x04cb: strcat(vend,vend2); strcat(vend," - Fuji"); break;
			case 0x04d2: strcat(vend,vend2); strcat(vend," - Altec Lansing"); break;
			case 0x0500: strcat(vend,vend2); strcat(vend," - SUH"); break;
			case 0x059b: strcat(vend,vend2); strcat(vend," - Iomega"); break;
			case 0x05ac: strcat(vend,vend2); strcat(vend," - Apple"); break;				
			case 0x06cd: strcat(vend,vend2); strcat(vend," - Keyspan"); break;
			case 0x08ff: strcat(vend,vend2); strcat(vend," - AuthenTec"); break;
			case 0x0a86: strcat(vend,vend2); strcat(vend," - SecuGen"); break;
			case 0x0472: case 0x04f2: strcat(vend,vend2); strcat(vend," - Chicony"); break;
		  	case 0x04c1: case 0x0506: strcat(vend,vend2); strcat(vend," - 3Com"); break;
			case 0x0471: case 0x04cc: strcat(vend,vend2); strcat(vend," - Philips"); break;
			case 0x0445: case 0x047e: strcat(vend,vend2); strcat(vend," - Lucent"); break;
			case 0x0781: case 0x0871: strcat(vend,vend2); strcat(vend," - SanDisk"); break;
			case 0x0547: case 0x04ce: case 0x05ab:
		  	case 0x04b4: strcat(vend,vend2); strcat(vend," - Cypress"); break;
			default:	 strcat(vend,vend2); strcat(vend," - Undefined"); break;
		}
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)vend);

		sprintf(temp,"-- Product ID : 0x%02X%02X",ptr[7],ptr[6]);	// PID
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
		sprintf(temp,"-- EP0 Max.   : %d bytes",ptr[8]);			// bMaxPacketSize0
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
		
		// USB Data Endpoint Updates
		NumDataEp = ptr[9];
		cnt = 10;

		for(i=0;i<NumDataEp;i++)
		{
	      sprintf (temp, "-- EP %d - %d %s %s %3d-byte",  ptr[0],
		  ptr[cnt]&0x0F, PIPE_DIRECTION[ptr[cnt] >> 7],
		  PIPE_TYPE_STRINGS[ptr[cnt+1]&0x03], (ptr[cnt+2] + ptr[cnt+3]*256));
	      EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);// display all strings
		  cnt += 5;												// next next endpoint
		}
		EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)" ");
	}		
}

//**********************************************************************************
// ESUSB Host Device Handle
//**********************************************************************************
void Get_EZUSB_Device_Info(HANDLE hDevice, HWND hDlg)
{
	PVOID   pvBuffer                        = 0;
	PVOID   pdBuffer                        = 0;
	HWND    hOutputBox                      = NULL;
	BOOLEAN bResult                         = FALSE;
	int     nBytes                          = 0;
	int     nItems                          = 0;
	HFONT   hFont                           = NULL;
	ULONG   ulLength                         = 0;

	PUSBD_PIPE_INFORMATION pPipe;
	
	pvBuffer = (void*)malloc (sizeof(Usb_String_Descriptor) + 128);
	pdBuffer = (void*)malloc (sizeof(Usb_Device_Descriptor) + 128);
	hOutputBox = GetDlgItem (hDlg, IDC_STATUS_BOX);
	
	////////////////////////////////////////////////////////////////////////////////
	// Get VID/PID from Device Descriptor
	////////////////////////////////////////////////////////////////////////////////
	if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
	{
		MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		return;
	}
	bResult = DeviceIoControl (	hDevice,
								IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
								pdBuffer,
								sizeof (Usb_Device_Descriptor),
								pdBuffer,
								sizeof (Usb_Device_Descriptor),
								(unsigned long *)&nBytes,
								NULL);
	CloseHandle (hDevice);	// Close the handle

	if (bResult!=TRUE)	
		MessageBox(hDlg, "Get VID/PID Failed","Error",MB_ICONSTOP);
						
	////////////////////////////////////////////////////////////////////////////////
	// Get Pipes Info
	////////////////////////////////////////////////////////////////////////////////
	if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
	{
		MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		return;
	}
	bResult = DeviceIoControl (hDevice,
				IOCTL_Ezusb_GET_PIPE_INFO,
				NULL,
				0,
				InterfaceInfo,
				sizeof(InterfaceInfo),
				(unsigned long *)&nBytes,
				NULL);
	CloseHandle (hDevice);	// Close the handle
						
	if (bResult==TRUE)
	{
		pInterface = (PUSBD_INTERFACE_INFORMATION) InterfaceInfo;
		pPipe = pInterface->Pipes;
	}
	else
		MessageBox(hDlg, "Get Pipe Info Failed","Error",MB_ICONSTOP);

	free (pvBuffer);		// Free the memory
}

void AbortRefresh(HWND hDlg, HANDLE hDevice)
{
	BOOLEAN bResult = FALSE;
	ULONG pipenum;
	int nBytes= 0;

	if(!Pipe2InPended)
		return;

		pipenum = 2; // Endpoint #2 IN
	    if(!bOpenDriver(&hDevice, pcDriverName))
		{
		  MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		  return; 
		}
					bResult = DeviceIoControl (hDevice,
						IOCTL_Ezusb_ABORTPIPE,
						&pipenum,
						sizeof(ULONG),
						NULL,
						0,
						(unsigned long *)&nBytes,
						NULL);
	    CloseHandle(hDevice);

		if(!bResult)
		{
			MessageBox(hDlg, "EZUSB-IN(2) Failed Abort","Error",MB_ICONSTOP);
		}
}

void EzSendMessage(HWND hLb, int OpType, int OtherOp, LPARAM pStr)
{
	int nItems = 0;
	if(OpType == LB_ADDSTRING)
	{
		nItems = SendMessage (hLb, LB_GETCOUNT, 0, 0);
		if (nItems == MAX_ITEMS_IN_LB)
		{
			SendMessage (hLb, LB_DELETESTRING, 0, 0);
		}
	}
	SendMessage(hLb, OpType, OtherOp, pStr);
	SendMessage(hLb, LB_SETTOPINDEX, nItems, 0);
}

void OnFileTrans(HWND hDlg, char* pFile)
{
	FILE *fp;

	fp = fopen(pFile,"rb");
    if(!fp)
    {
        MessageBox(hDlg, "Error opening data file","Error",MB_ICONSTOP);
		return;
    }
    fileXferBytes = fread(FileXferBuff, sizeof(unsigned char), MAX_FILE_SIZE, fp);
    fclose(fp);

	if(!fileXferBytes)
	{
		return;
	}

}


void OnOutputSave(HWND hDlg, HWND hDataBox, char* pFile)
{
	FILE *fp;
	int fileWriteBytes;
	int nItems = 0;
	int i;
	char strText[256];
	int RetVal;

	fp = fopen(pFile,"w");
    if(!fp)
    {
        MessageBox(hDlg, "Error opening file for output","Error",MB_ICONSTOP);
		return;
    }

	nItems = SendMessage (hDataBox, LB_GETCOUNT, 0, 0);
	for(i=0;i<nItems;i++)
	{
      RetVal = SendMessage(hDataBox, LB_GETTEXT, i, (LPARAM)strText);
	  if(RetVal)
	  {
		strcat(strText, "\n");
        fileWriteBytes = fwrite(strText, sizeof(char), strlen(strText), fp);
        if(!fileWriteBytes)
		{
          MessageBox(hDlg, "Error saving file","Error",MB_ICONSTOP);
		}
	  }
	}

    fclose(fp);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -