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

📄 bulkloop.c

📁 本函数是EZ_USB的固件函数框架,实现通过端点通信
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

  
    Module Name:
    bulkloop.c
    
                    
--*/

#include <windows.h>
#include <stdio.h>
#include <process.h>

#include "resource.h"

#include <winioctl.h>
#include "C:\Cypress\USB\Drivers\ezusbdrv\ezusbsys.h"

#include "bulkloop.h"

HINSTANCE hGInstance = NULL;
BOOL CALLBACK bMainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
void DumpBuffer(PVOID pvBuffer, ULONG length, HWND hOutputBox);
BOOLEAN bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname);
void RetrievePipeInfo(HWND hDlg);
void DisplayPipeInfo(HWND hDlg);
ULONG FirstPipe(HWND hDlg, BOOL InPipe);

HWND    ghOutputBox                      = NULL;

char *build_time = __TIME__;
char *build_date = __DATE__;

ENDPOINT_SELECT gEndpointSelect;
DATA_PATTERN gDataPattern;
ULONG gTestParms;
BOOLEAN gStopTest;
UCHAR gInterfaceInfo[1024];

/*******************************
*WinMain: Windows Entry point  *
********************************/
int PASCAL WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
   hGInstance=hInstance;
   
   if(DialogBox(hInstance,"BULKLOOP",NULL,(DLGPROC)bMainDlgProc)==-1)
      MessageBox(NULL,"Unable to create root dialog!","DialogBox failure",MB_ICONSTOP);
  
   return 0;
}

void TransferThread(
   PTHREAD_CONTROL threadControl
)
{

   // perform the ISO transfer
   threadControl->status = DeviceIoControl (threadControl->hDevice,
                                             threadControl->Ioctl,
                                             threadControl->InBuffer,
                                             threadControl->InBufferSize,
                                             threadControl->OutBuffer,
                                             threadControl->OutBufferSize,
                                             &threadControl->BytesReturned,
                                             NULL);

   // if an event exists, set it
   if (threadControl->completionEvent)
      SetEvent(threadControl->completionEvent);
   return;
}

void TestThread(
  LPVOID lpParameter   // thread data
)
{
   HWND hDlg = (HWND) lpParameter;
   HWND hOutputBox;
   HANDLE  hInDevice = NULL;
   HANDLE  hOutDevice = NULL;
   PUCHAR inBuffer = NULL;
   PUCHAR outBuffer = NULL;
   ULONG MaxTransferSize, CurrentTransferSize;
   ULONG InPipeNum;
   ULONG OutPipeNum;
   ULONG TestPass;
   ULONG Errors;
   HANDLE ReadCompleteEvent;
   HANDLE WriteCompleteEvent;
   char    DeviceName[MAX_DRIVER_NAME]   = "";
   ULONG  pattern;
   ULONG  i;
   BULK_TRANSFER_CONTROL   inBulkControl,outBulkControl;
   THREAD_CONTROL inThreadControl,outThreadControl;
   char tempbuff[256];
   FILE *dumpfile;
   FILE *binfile;

   hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);

   //
   // get the device handle
   //
   GetDlgItemText (hDlg, IDC_DEVICENAME, DeviceName, MAX_DRIVER_NAME);
   if (bOpenDriver (&hInDevice, DeviceName) != TRUE)
   {
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Device");
      return;
   }
   if (bOpenDriver (&hOutDevice, DeviceName) != TRUE)
   {
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Device");
      return;
   }

   //
   // get the pipe numbers for the transfer
   //
   switch (gEndpointSelect)
   {
      case FIRSTPAIR:
         InPipeNum = FirstPipe(hDlg,TRUE);
         OutPipeNum = FirstPipe(hDlg,FALSE);
         break;
      case SELECTPAIR:
      case OUTONLY:
      case INONLY:
         InPipeNum = GetDlgItemInt(hDlg,IDC_INPIPE,NULL,FALSE);
         OutPipeNum = GetDlgItemInt(hDlg,IDC_OUTPIPE,NULL,FALSE);
         break;
   }
   sprintf(tempbuff,"IN PIPE = %d OUT PIPE = %d",InPipeNum,OutPipeNum);
   SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);

   //
   // get the transfer size and allocate the transfer buffers
   //
   MaxTransferSize = GetDlgItemInt(hDlg,IDC_TRANSFERSIZE,NULL,FALSE);
   CurrentTransferSize = MaxTransferSize;
   outBuffer = malloc(MaxTransferSize);
   inBuffer = malloc(MaxTransferSize);

   //
   // seed the random number generator, and set our starting pattern
   //
   srand(GetDlgItemInt(hDlg,IDC_STARTVALUE,NULL,FALSE));
   pattern = GetDlgItemInt(hDlg,IDC_STARTVALUE,NULL,FALSE);

   //
   // initialize the pass and error counters
   //
   TestPass = 0;
   SetDlgItemInt(hDlg,IDC_TESTPASS,TestPass,FALSE);
   Errors = 0;
   SetDlgItemInt(hDlg,IDC_ERRORS,Errors,FALSE);

   //
   // Set up our event objects
   //
   ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
   WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);

   // if requested, set up for an incrementing packet size.  Start the packet size at 1
   if (gTestParms & bmINCPACKET)
   {
      CurrentTransferSize = 1;
   }

   //
   // main test loop
   //
   while (!gStopTest)
   {

      // initialize the out buffer
      switch(gDataPattern)
      {
         case INCREMENTINGBYTE:
            for (i=0;i<CurrentTransferSize;i++)
               outBuffer[i] = (UCHAR) pattern++;
            break;
         case INCREMENTINGDWORD:
            for (i=0;i<CurrentTransferSize/4;i++)
               ((PULONG) outBuffer)[i] = pattern+i;
            pattern++;
            break;
         case CONSTANTBYTE:
            for (i=0;i<CurrentTransferSize;i++)
               outBuffer[i] = (UCHAR) pattern;
            break;
         case RANDOMBYTE:
            for (i=0;i<CurrentTransferSize;i++)
               outBuffer[i] = rand();
            break;
      }

      // initialize the in buffer
      memset(inBuffer, 0, MaxTransferSize);

      // initialize data structures for the read thread
      if (gEndpointSelect != OUTONLY)
      {
         inBulkControl.pipeNum = InPipeNum;
         inThreadControl.hDevice = hInDevice;

         inThreadControl.Ioctl = IOCTL_EZUSB_BULK_READ;
         inThreadControl.InBuffer = (PVOID)&inBulkControl;
         inThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
         inThreadControl.OutBuffer = inBuffer;
         inThreadControl.OutBufferSize = CurrentTransferSize;

         inThreadControl.completionEvent = ReadCompleteEvent;
         inThreadControl.status = FALSE;
         inThreadControl.BytesReturned = 0;
      }

      // initialize data structures for the write thread
      if (gEndpointSelect != INONLY)
      {
         outBulkControl.pipeNum = OutPipeNum;
         outThreadControl.hDevice = hOutDevice;

         outThreadControl.Ioctl = IOCTL_EZUSB_BULK_WRITE;
         outThreadControl.InBuffer = (PVOID)&outBulkControl;
         outThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
         outThreadControl.OutBuffer = outBuffer;
         outThreadControl.OutBufferSize = CurrentTransferSize;

         outThreadControl.completionEvent = WriteCompleteEvent;
         outThreadControl.status = FALSE;
         outThreadControl.BytesReturned = 0;
      }

      // start and wait for transfer threads
      if (gEndpointSelect != OUTONLY)
         _beginthread(TransferThread,0,&inThreadControl);
      if (gEndpointSelect != INONLY)
         _beginthread(TransferThread,0,&outThreadControl);
      if (gEndpointSelect != OUTONLY)
         WaitForSingleObject(ReadCompleteEvent,INFINITE);
      if (gEndpointSelect != INONLY)
         WaitForSingleObject(WriteCompleteEvent,INFINITE);

      // if either the read or write failed, we want to stop the test
      if (gEndpointSelect != OUTONLY && !inThreadControl.status)
      {
         SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Error: Read failed");

         SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Dumping OUT packet to file");
         dumpfile = fopen("c:\\dumpfile.txt","w");
         for (i=0;i<CurrentTransferSize;i++)
            fprintf(dumpfile,"%X\n",outBuffer[i]);
         fclose(dumpfile);

         binfile = fopen("c:\\binfile.bix","wb");
         fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
         fclose(binfile);

         gStopTest = TRUE;
      }
      if (gEndpointSelect != INONLY && !outThreadControl.status)
      {
         SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Error: Write failed");
         gStopTest = TRUE;
      }

      // verify the data, unless the user has disabled verify data
      // or this was a write only test.
      if ((gTestParms & bmVERIFYDATA) && (gEndpointSelect != OUTONLY))
      {
         // make sure the correct amount of data was returned
         if (inThreadControl.BytesReturned != CurrentTransferSize)
         {
            sprintf(tempbuff,
                     "Error: Length expected = %d actual = %d",
                     CurrentTransferSize,
                     inThreadControl.BytesReturned);
            SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
            Errors++;
            if (gTestParms & bmSTOPONERROR)
               gStopTest = TRUE;
         }

         // copmpare the buffers
         for (i=0;i<CurrentTransferSize;i++)
         {
            if (inBuffer[i] != outBuffer[i])
            {
               sprintf(tempbuff,
                        "Error: Data offset 0x%x expected = 0x%x actual = 0x%x",
                        i,
                        outBuffer[i],
                        inBuffer[i]);
               SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
               Errors++;
               if (gTestParms & bmSTOPONERROR)
                  gStopTest = TRUE;
            }
         }

         if (Errors && (gStopTest == TRUE))
         {
            SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Dumping Error packet to file");
            dumpfile = fopen("c:\\dumpfile.txt","w");
            for (i=0;i<CurrentTransferSize;i++)
               fprintf(dumpfile,"%X\n",outBuffer[i]);
            fclose(dumpfile);

            binfile = fopen("c:\\binfile.bix","wb");
            fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
            fclose(binfile);
         }
      }

      //  if requested, increment the packet size
      if (gTestParms & bmINCPACKET)
      {
         CurrentTransferSize++;

         if (CurrentTransferSize > MaxTransferSize)
            CurrentTransferSize = MaxTransferSize;
      }

      // update pass counter
      TestPass++;
      SetDlgItemInt(hDlg,IDC_TESTPASS,TestPass,FALSE);
      SetDlgItemInt(hDlg,IDC_ERRORS,Errors,FALSE);
   }

   SetDlgItemInt(hDlg,IDC_TESTPASS,TestPass,FALSE);
   SetDlgItemInt(hDlg,IDC_ERRORS,Errors,FALSE);

   free(inBuffer);
   free(outBuffer);
   CloseHandle(hInDevice);

⌨️ 快捷键说明

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