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

📄 ez811_dk.c

📁 This is cypress sl811 driver. I think it is very useful. Thankyou.
💻 C
📖 第 1 页 / 共 3 页
字号:
////////////////////////////////////////////////////////////////////////////////
//	File:		EZ811_CP.c
//
// $Header: /USB/ez811/EZ811_DK/EZ811_DK.c 20    5/14/02 2:49p Tpm $
// Copyright (c) 2002 Cypress Semiconductor. May not be reproduced without permission.
// See the license agreement for more details.
// Based on code written by cxn.
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include "resource.h"
#include <time.h>
#include <winioctl.h>
#include "ezusbsys.h"
#include "EZ811_DK.h"

HINSTANCE hGInstance = NULL;
#define DATA_BLOCK_SIZE		256
UCHAR	single_data = 0;	
char	*build_time = __TIME__;
char	*build_date = __DATE__;
ULONG	UsbAddr;
ULONG	EndPointAddr;
ULONG	DataSize;
BYTE	out1Buffer[DATA_BLOCK_SIZE];
BYTE	in1Buffer[DATA_BLOCK_SIZE];
int		in1Bytes = 0;
BYTE	in3Buffer[DATA_BLOCK_SIZE];
int		in3Bytes = 0;
BYTE	out3Buffer[DATA_BLOCK_SIZE];
int		out3Bytes = 0;
int     nOpsPending = 0;

BYTE	refData = 0;

BYTE	VenBuff[64];
BYTE	HostCommand[6];
// HostCommand[0] = type of command
// HostCommand[1] = USB Address
// HostCommand[2] = Endpoint Address/String Index
// HostCommand[3] = USB xfer type
// HostCommand[4] = xfer data length
// HostCommand[5] = loop enable

#define MAX_FILE_SIZE (1024*64)
unsigned char FileXferBuff[MAX_FILE_SIZE];
int fileXferBytes;

BOOL	HUB = FALSE;
BOOL	bRefreshThread = FALSE;
BOOL	bDataXferThread = FALSE;
DWORD   DataLineCount = 0;
BYTE    OutXferCount = 0;
BYTE    Pipe2InPended = 0;
UCHAR   InterfaceInfo[1024];
PUSBD_INTERFACE_INFORMATION pInterface;

#define ISOCHRONOUS			0x01
#define BULK				0x02
#define INTERRUPT			0x03

#define XFERSTOP0			0xDE	// code to indicate stop button pressed
#define XFERSTOP1			0xAF	// "OxDEAF"
#define NONEXIST0			0xDE	// code to indicate device does not exist for data start/stop
#define NONEXIST1			0xAD	// "OxDEAD"
#define NOT_APPL			0xFF	// indicate not applicable, in string & class descp

#define SL_RESET			0xD0
#define SL_DEVICE_DESCP		0xD1
#define SL_CONFIG_DESCP		0xD2
#define SL_CLASS_DESCP		0xD3
#define SL_STRING_DESCP		0xD4
#define SL_REFRESH			0xD5
#define SL_DATA_XFER_START	0xD6
#define SL_DATA_XFER_STOP	0xD7
#define SL_TOGGLE_DS_REFRESH 0xD8
#define SL_SHOW_REGS        0xD9

#define	ezCMD				HostCommand[0]
#define	ezADDR				HostCommand[1]
#define	ezENP				HostCommand[2]
#define	ezIDX				HostCommand[2]
#define	ezTYPE				HostCommand[3]
#define	ezLEN				HostCommand[4]
#define	ezREPEAT			HostCommand[5]

#define IS_IN_EP(EpIdx) (EpIdx & 0x80)

BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL GetUsbAddress(HWND hDlg);
BOOL GetEndpointNum(HWND hDlg);
BOOL GetDataSize(HWND hDlg);
void AbortRefresh(HWND hDlg, HANDLE hDevice);
void FillBuffer(PUCHAR buffer, PUCHAR pattern, ULONG length);
void DumpStatusBox(PVOID pvBuffer, ULONG length, HWND hOutputBox);
void DumpString(PVOID pvBuffer, ULONG length, HWND hOutputBox);
void DispDevData(PVOID pvBuffer, ULONG length, HWND hOutputBox);
void Get_EZUSB_Device_Info(HANDLE hDevice, HWND hDlg);
void RefreshThread(LPVOID lpParameter);
void inDataXferThread(LPVOID lpParameter);
void msDelay(ULONG time);
char pcDriverName[MAX_DRIVER_NAME] = "Ezusb-0";
void OnFileTrans(HWND hDlg, char* pFile);
void OnOutputSave(HWND hDlg, HWND hDataBox, char* pFile);
void Vend_CmdXfer(LPVOID lpParameter);

char PIPE_TYPE_STRINGS[4][4] = { "CTL", "ISO", "BLK", "INT" };

char PIPE_DIRECTION[2][4] = { "OUT", "IN " };

void DispDataBox(PVOID pvBuffer, ULONG length, HWND hOutputBox1);
void EzSendMessage(HWND hLb, int OpType, int OtherOp, LPARAM pStr);

#define ABOUT_BOX_STR_0 "  EZ-811 Dev Kit Version 4.2"
#define ABOUT_BOX_STR_1 "  Cypress Semiconductor"

typedef struct CThreadInfo
{ // ThreadInfo class
    VENDOR_OR_CLASS_REQUEST_CONTROL	VenReq;
	HWND hOutputBox;
	HWND hStatusBox;
	HWND hInfoBox;
	HWND hDlg;
	char* strpcDriverName;
	WORD length;
	int pipeNum;
	DWORD request;
	DWORD value;
	DWORD index;
	int direction;
    BYTE VenBuff[VEN_BUFF_SIZE];
	int m_hOpPended;
	BOOL fileXfer;
} CThreadInfo;

CThreadInfo* pTh = NULL;


//**********************************************************************************
// WinMain
//**********************************************************************************
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   hGInstance=hInstance;
   
   if(DialogBox(hInstance,"MAIN_DIALOG",NULL,(DLGPROC)bMainDlgProc)==-1)
      MessageBox(NULL,"Unable to create root dialog!","DialogBox failure",MB_ICONSTOP);

	return 0;
}


//**********************************************************************************
// Vendor Command Transfer
//**********************************************************************************
void Vend_CmdXfer(LPVOID lpParameter)
{
  HANDLE	hDevice = NULL;
  BOOLEAN	bResult = TRUE;
  BOOLEAN	bRepeatXfer;
  int		nBytes = 0;	
  int i;
  CThreadInfo* pTh = (CThreadInfo*)lpParameter;
  int sentBytes = 0;
  int sendBytes;
						
  pTh->VenReq.requestType=2; // vendor specific request type (2)
  pTh->VenReq.recepient=0; // recepient is device (0)

  bRepeatXfer = TRUE;
  while(bRepeatXfer && bResult) // do transfer once, then repeat if Repeat is selected
  {
	if(!bOpenDriver (&hDevice, pcDriverName))
	{
		EzSendMessage (pTh->hOutputBox, LB_ADDSTRING, 0, (LPARAM)"EZ-USB Transfer Failed - EZ-USB not present");
		bResult = FALSE; 
	}
	if( (pTh->VenReq.request == SL_DATA_XFER_START) && (!IS_IN_EP(pTh->VenReq.index)) && (pTh->fileXfer))
	{
		sendBytes = (fileXferBytes < MAX_EP0_PAYLOAD) ? fileXferBytes : MAX_EP0_PAYLOAD;
		memcpy(pTh->VenBuff, FileXferBuff+sentBytes, sendBytes);
		pTh->length = sendBytes;
	}
	if( (pTh->VenReq.request == SL_DATA_XFER_START) && (!IS_IN_EP(pTh->VenReq.index)) && (!pTh->fileXfer))
	{
		OutXferCount++;
		for(i=0; i<pTh->length; i++)
		  pTh->VenBuff[i] = OutXferCount;
	}

	//if (hDevice != NULL) 
	if((!nOpsPending) && (hDevice != NULL))
	{
	  nOpsPending++;
	  bResult = DeviceIoControl (hDevice,
								IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
								&(pTh->VenReq),
								sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
								pTh->VenBuff,
								pTh->length,
								(unsigned long *)&nBytes,
								NULL);
	  nOpsPending--;
	}
	CloseHandle(hDevice);


	if(bResult)
	{
	  if(pTh->VenReq.request == SL_DATA_XFER_START)
		DispDataBox(pTh->VenBuff,nBytes,pTh->hOutputBox);
	  else if((pTh->VenReq.request == SL_DEVICE_DESCP)
		   || (pTh->VenReq.request == SL_CONFIG_DESCP))
	  {
		DumpStatusBox(pTh->VenBuff,nBytes,pTh->hStatusBox);
	  }
	  else if(pTh->VenReq.request == SL_CLASS_DESCP)
	  {
		if(nBytes)
		  DumpStatusBox(pTh->VenBuff,nBytes,pTh->hStatusBox);
		else
		  EzSendMessage (pTh->hStatusBox, LB_ADDSTRING, 0, (LPARAM)"Non-HID/HUB class USB device");
	  }
	  else if(pTh->VenReq.request == SL_STRING_DESCP)
	  {
		if(nBytes)
		  DumpString(pTh->VenBuff,nBytes,pTh->hStatusBox);
		else
		  EzSendMessage (pTh->hStatusBox, LB_ADDSTRING, 0, (LPARAM)"No String Descriptor");
	  }
	  else if(pTh->VenReq.request == SL_REFRESH)
	  {
		if(nBytes)
		{
	  	  DispDevData(pTh->VenBuff,nBytes,pTh->hInfoBox);
		}
	  }
	  else if(pTh->VenReq.request == SL_SHOW_REGS)
	  {
		DumpStatusBox(pTh->VenBuff,nBytes,pTh->hStatusBox);
	  }

	}
	else
	{
	  CheckDlgButton(pTh->hDlg,IDC_REPEAT,BST_UNCHECKED);
	  EzSendMessage (pTh->hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Vendor Request to EZ-USB Failed");
	}

	bRepeatXfer = (IsDlgButtonChecked(pTh->hDlg,IDC_REPEAT)==BST_CHECKED);

	if( (pTh->VenReq.request == SL_DATA_XFER_START) && (!IS_IN_EP(pTh->VenReq.index)) && (pTh->fileXfer))
	{
		sentBytes += sendBytes;
		if(sentBytes < fileXferBytes)
			bRepeatXfer = 1;
		else
			bRepeatXfer = 0;
	}

  }
  free(pTh); // this makes the thread evaporate
}

//**********************************************************************************
// BULK OUT Transfer - OUT Command Request on EP1, Pipe 0
//**********************************************************************************
BOOL BulkOut_CmdXfer(HWND hDlg, ULONG OutPktSize)
{
	BULK_TRANSFER_CONTROL bulkControl;
	HANDLE	hDevice = NULL;
	BOOLEAN	bResult = FALSE;
	int		i,nBytes = 0;	
	
	bulkControl.pipeNum = 0; // Endpoint #1 OUT
	for(i=0;i<(int)OutPktSize;i++)
		out1Buffer[i] = HostCommand[i];
						
	if(!bOpenDriver (&hDevice, pcDriverName))
	{
		MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		return FALSE; 
	}
	bResult = DeviceIoControl (	hDevice,
								IOCTL_EZUSB_BULK_WRITE,
								&bulkControl,
								sizeof(BULK_TRANSFER_CONTROL),
								&out1Buffer,
								OutPktSize,
								(unsigned long *)&nBytes,
								NULL); 	
	CloseHandle(hDevice);

	if(!bResult)
	{
		return FALSE;
	}

	return TRUE;					;
}

//**********************************************************************************
// BULK IN Transfer - IN Descriptor data on EP1, Pipe 1
//**********************************************************************************
BOOL BulkIn_CmdXfer(HWND hDlg)
{
	BULK_TRANSFER_CONTROL bulkControl;
	HANDLE	hDevice = NULL;
	BOOLEAN	bResult = FALSE;
	
	// Endpoint #1 IN
	bulkControl.pipeNum = 1;
	if(!bOpenDriver(&hDevice, pcDriverName))
	{
		MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		return FALSE; 
	}
	bResult = DeviceIoControl (	hDevice,
								IOCTL_EZUSB_BULK_READ,
								&bulkControl,
								sizeof(BULK_TRANSFER_CONTROL),
								&in1Buffer,
								DATA_BLOCK_SIZE,		// max IN size allowed
								(unsigned long *)&in1Bytes,	// actual bytes returned
								NULL);			
	CloseHandle(hDevice);

	if(!bResult)
	{
		return FALSE;
	}

	return TRUE;					
}

//******************************************************************************************
// Refresh - For sensing new device attachment - IN Data (state change to 1) on EP2, Pipe 2
//******************************************************************************************
void RefreshThread(LPVOID lpParameter)   // thread data
{
	HWND	hDlg = (HWND) lpParameter;
	BULK_TRANSFER_CONTROL bulkControl;
	HWND    hInfoBox	= NULL;
	HANDLE	hDevice		= NULL;
	BOOLEAN	bResult		= FALSE;
	int		refByte = 0;
	FLOAT	delaystart;
	
	hInfoBox = GetDlgItem (hDlg, IDC_INFO_BOX);
	
	bRefreshThread = TRUE;
	refData = 1; // do the initial refresh
	while(bRefreshThread)
	{
		// Ensure the refresh is updated when 1st launched, in case the SL811HS demo board is running already
		if(refData == 1)				
		{
		    EzSendMessage (hInfoBox, LB_RESETCONTENT, 0, 0);
			ezCMD = SL_REFRESH;
				// Endpoint #1 OUT
				if(!BulkOut_CmdXfer(hDlg, 2)) 
				{
					MessageBox(hDlg, "EZUSB-OUT(1) Failed (Refresh)","Error",MB_ICONSTOP);
					break;
				}

				delaystart = (FLOAT)GetTickCount();
				while(GetTickCount()-delaystart < 5);

				// Endpoint #1 IN 
				if(BulkIn_CmdXfer(hDlg)) 
					DispDevData(in1Buffer,in1Bytes,hInfoBox);
				else
				{
					MessageBox(hDlg, "EZUSB-IN(1) Failed (Refresh)","Error",MB_ICONSTOP);
					break;
				}		
			refData = 0;			// wait for next valid device attached/detached

		}

		bulkControl.pipeNum = 2; // Endpoint #2 IN
	    if(!bOpenDriver(&hDevice, pcDriverName))
		{
		  MessageBox(hDlg, "EZ-USB Development Kit Not Found !","Error",MB_ICONSTOP);
		  return; 
		}
		Pipe2InPended = 1;
		bResult = DeviceIoControl (	hDevice,
									IOCTL_EZUSB_BULK_READ,
									&bulkControl,
									sizeof(BULK_TRANSFER_CONTROL),
									&refData,				// valid data received from device
									1,						// max IN size allowed
									(unsigned long *)&refByte,	// actual bytes returned
									NULL);			
	    CloseHandle(hDevice);
		Pipe2InPended = 0;

	}

	return;					
}

//**********************************************************************************
// Main Dialog proc                              
//**********************************************************************************
BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND    hDataBox						= NULL;
	HWND    hStatusBox                      = NULL;
	HWND    hInfoBox	                    = NULL;
	HANDLE  hDevice                         = NULL;
	BOOLEAN bResult                         = FALSE;
	int     nBytes                          = 0;
	HFONT   hFont                           = NULL;
	ULONG   ulLength                        = 0;
	char    temp1[256]= "";
	char    temp2[256]= "";
	char    strFileOut[256];
	//int		i;

⌨️ 快捷键说明

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