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

📄 testin.cpp

📁 USB开发的一些代码!适用于EX-USBFX2平台!这些源码还是蛮不错的!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestIn.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>

#include <stdio.h>
#include <math.h>
#include <winioctl.h>
#include "C:\Cypress\USB\Drivers\ezusbdrv\ezusbsys.h" /*..\..\drivers\ezusbdrv\*/
#include "Complex.h"
#include "FFT.h"
#include "PRBS.h"
#include "Constellation.h"



bool bEndTest;

#define PI 3.1415927

BOOLEAN
bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
   char completeDeviceName[64] = "";
   char pcMsg[64] = "";
   
   strcat (completeDeviceName,
      "\\\\.\\"
      );
   
   strcat (completeDeviceName,
		    devname
          );
   
   *phDeviceHandle = CreateFile(   completeDeviceName, ///point to the name of file
      GENERIC_WRITE,          ///access to the file
      FILE_SHARE_WRITE,       ///share write mode
      NULL,                   ///NULL means the default safe description
      OPEN_EXISTING,          ///Open the file, If the file is not exist, the calling fail
      0,//FILE_FLAG_OVERLAPPED,   ///file attribute
      NULL);				  ///handle to file with attributes to copy
   
   if (*phDeviceHandle == INVALID_HANDLE_VALUE) {
      return (FALSE);
   } else {
      return (TRUE);
   } /*else*/
     
}//OpenDevice


int VendorRequest(HANDLE hDevice, BYTE request, WORD value)
{
    VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest;

	char buffer[16];
	int bufferSize = 16;
	int nBytes;

    myRequest.request = request;
    myRequest.value = value;

    myRequest.index = 0;
    myRequest.direction = 1;
    myRequest.requestType = 2; // vendor specific request type (2)
    myRequest.recepient = 0; // recepient is device (0)

	int bResult = DeviceIoControl (hDevice,
		IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
		&myRequest,
		sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
		buffer,
		bufferSize,
		(unsigned long *)&nBytes,
		NULL);

	return bResult;
}



int nTone = 0;

DWORD  __stdcall WriteData(LPVOID lpParameter)
{
   HANDLE  hInDevice = NULL;
   PUCHAR outBuffer = NULL;    ///outBuffer points to the Unsigned CHAR
   ULONG MaxTransferSize;      
   ULONG OutPipeNum;
   char    DeviceName[255]   = "";
   BULK_TRANSFER_CONTROL   outBulkControl;     //defined by EZUSB
   ULONG BytesTransmitted;
   int status;
   int loopcount = 1;
   OVERLAPPED  ovl[5];
   //
   // get the device handle
   //

   
   
  
   
   if (bOpenDriver (&hInDevice, "EZUSB-0") != TRUE)
   {
      printf("Failed to Open Device\n");
      return 0;
   }

   OutPipeNum = 0;

   //
   // get the transfer size and allocate the transfer buffers
   //

   MaxTransferSize = 2048;
   outBuffer = (unsigned char *)VirtualAlloc(NULL, ///addr of region to reserve or commit
	   MaxTransferSize,                      ///size of region
	   MEM_RESERVE | MEM_COMMIT,             ///type of region
	   PAGE_READWRITE);                      ///type of access protection


   //for (unsigned int k = 0 ; k < MaxTransferSize / 2 ; k ++)
	 //((SHORT *)outBuffer)[k] = k;

   if (1)
   {   
	   CPRBS prbs;
	   prbs.Initialize(1, 512);
	   int bits;


	   Complex Cbits;
	   CConstellation cons;
	   //cons.Map(bits, 0, Cbits);

	   
	   
	   Complex f[2048];
	   for (int i = 1; i < 1024; i ++)
	   {
		   if (i == nTone)
		   //if (i < 256)
		   //if (i > 26 && i < 100 && (i % 2 == 0))
		   {
			   bits = prbs.GetReverbPRBSBits(i);
			   cons.Map(bits, 0, Cbits);
			   f[i].r = Cbits.r * 20;
			   f[i].i = Cbits.i * 20;
		   }
		   else
		   {
			   f[i].r = 0;
			   f[i].i = 0;
		   }

		   f[2048 - i].r = f[i].r;
		   f[2048 - i].i = -f[i].i;
	   }

	   f[0].r = f[0].i = 0;
	   f[1024].r = f[1024].i = 0;
 
	   float tout[2048];
	   
	   IFFT(2048, f, tout);


	   for (unsigned int k = 0 ; k < 2048; k ++)
	   {
		   //double v = tout[k] * 90 + 128;
		   //double v = tout[k] * 190 + 128;
		   double v = tout[k] * 2000 + 128;
		   //double v = tout[k] * 1800 + 128;

		   if (v >= 255 || v <= 0)
			   v = v;

		   tout[k] = v;
	   }
		   
	   for (k = 0 ; k < MaxTransferSize; k ++)
	   {
		   double v = tout[k % 2048];

		   unsigned char a = (unsigned char)((v > 0) ? ( v + 0.5) : (v - 0.5));

		   ((unsigned char *)outBuffer)[k] = a;
		   //((unsigned char *)outBuffer)[k] = 255;
		   //((unsigned char *)outBuffer)[k] = k % 256;
		   //((unsigned char *)outBuffer)[k] = (k % 2048) / 8;
		   //((unsigned char *)outBuffer)[k] = 0;
	   }
	}	   
/*	   
	FILE * fp = fopen("D:\\test_data.dat", "wb");

	fwrite(outBuffer, sizeof(char), MaxTransferSize, fp);

	fclose(fp);
*/
   	//get speed
	VendorRequest(hInDevice, 0xB7, 0x0);

	
   //set relay (MAX4820)
   VendorRequest(hInDevice, 0xBE, 0x0);
   //VendorRequest(hInDevice, 0xBF, 0x000A); //transmit on, ANNEX A
   //VendorRequest(hInDevice, 0xBF, 0x0006); //transmit off, ANNEX A
   VendorRequest(hInDevice, 0xBF, 0x0009); //transmit on, ANNEX B
   //VendorRequest(hInDevice, 0xBF, 0x0005); //transmit off, ANNEX B
   Sleep(1000);
   VendorRequest(hInDevice, 0xBF, 0x0000);



   //set DAC0800 reference voltage (MAX548)
   VendorRequest(hInDevice, 0xBE, 0x01);
   VendorRequest(hInDevice, 0xBF, 0x0AFF);

   //set PAA (MAX307)
   VendorRequest(hInDevice, 0xBE, 0x0A);
   VendorRequest(hInDevice, 0xBF, 0x0775);

   //set PGA (AD8370)
   VendorRequest(hInDevice, 0xBE, 0x02);
   //VendorRequest(hInDevice, 0xBF, 0xFF);
   VendorRequest(hInDevice, 0xBF, 0x14);
   //VendorRequest(hInDevice, 0xBF, 0x00);

   VendorRequest(hInDevice, 0xBE, 0x03);
   //VendorRequest(hInDevice, 0xBF, 0xFF);
   VendorRequest(hInDevice, 0xBF, 0x14);
   //VendorRequest(hInDevice, 0xBF, 0x00);


   
   
   //set FPGA FIFO to normal mode
   VendorRequest(hInDevice, 0xBB, 1);

   //disable FPGA FIFO output
   VendorRequest(hInDevice, 0xBA, 0);

   //clear FPGA FIFO
   VendorRequest(hInDevice, 0xB2, 0);

   //enable FPGA FIFO output
   //VendorRequest(hInDevice, 0xBA, 1);
	
	
   outBulkControl.pipeNum = OutPipeNum;   ///defined by EZUSB

   //for (int m = 0; m < 1000000; m ++)
   status = DeviceIoControl (hInDevice,
		 IOCTL_EZUSB_BULK_WRITE,
         (PVOID)&outBulkControl,
         sizeof(BULK_TRANSFER_CONTROL),
         outBuffer,
         MaxTransferSize,
         &BytesTransmitted,
         NULL);


   //set FPGA FIFO to loopback mode
   VendorRequest(hInDevice, 0xBB, 0);

   //enable FPGA FIFO output
   VendorRequest(hInDevice, 0xBA, 1);


   VirtualFree(outBuffer, 0, MEM_RELEASE);
   CloseHandle(hInDevice);

   return 0;
}


double  __stdcall ReadData(LPVOID lpParameter)
{
   HANDLE  hInDevice = NULL;
   PUCHAR inBuffer = NULL, txBuffer = NULL;
   ULONG MaxTransferSize;
   ULONG InPipeNum;
   char    DeviceName[255]   = "";
   BULK_TRANSFER_CONTROL   inBulkControl;
   ULONG BytesReturned;
   int status;
   int loopcount = 20;
   OVERLAPPED  ovl[5];
   //
   // get the device handle
   //
   if (bOpenDriver (&hInDevice, "EZUSB-0") != TRUE)
   {
      printf("Failed to Open Device\n");
      return 0;
   }

   //enable input
   VendorRequest(hInDevice, 0xB3, 0x02);

   
   InPipeNum = 1;

   //
   // get the transfer size and allocate the transfer buffers
   //
   MaxTransferSize = 2048 * 16;
   //MaxTransferSize = 16384;
   //MaxTransferSize = 512;
   //inBuffer = (unsigned char *)VirtualAlloc(NULL, MaxTransferSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
   inBuffer = (unsigned char *)VirtualAlloc(NULL, MaxTransferSize * loopcount, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
   txBuffer = (unsigned char *)VirtualAlloc(NULL, MaxTransferSize * loopcount / 2, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);


   memset(inBuffer, 0, MaxTransferSize * loopcount);

 

   SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
   SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
   //
   // main test loop
   //
   
   
   for (int v = 0; v < 5; v ++)
	   ovl[v].hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

   inBulkControl.pipeNum = InPipeNum;

   DWORD t = GetTickCount();

   
/*
   for (int c = 0; c < loopcount; c ++)
   {
	 status = DeviceIoControl (hInDevice,
			 IOCTL_EZUSB_BULK_READ,
                                             (PVOID)&inBulkControl,
                                             sizeof(BULK_TRANSFER_CONTROL),
                                             inBuffer + c * MaxTransferSize,
                                             //inBuffer,
                                             MaxTransferSize,
                                             &BytesReturned,
                                             NULL);
	 //Sleep(1);

	 if (!status)
	 {
		 int a;

		 a = GetLastError();

		 a = 0;
	 }
   }

*/
   
   for (int c = 0; c < 5; c ++)
   {
	 status = DeviceIoControl (hInDevice,
			 IOCTL_EZUSB_BULK_READ,
                                             (PVOID)&inBulkControl,
                                             sizeof(BULK_TRANSFER_CONTROL),
                                             inBuffer + c * MaxTransferSize,
                                             //inBuffer,
                                             MaxTransferSize,
                                             &BytesReturned,
                                             ovl + c);
   }
   
   
   

   for (c; c < loopcount; c ++)
   {
	   WaitForSingleObject(ovl[c % 5].hEvent, INFINITE);
	   ResetEvent(ovl[c % 5].hEvent);
		 
	   status = DeviceIoControl (hInDevice,
			 IOCTL_EZUSB_BULK_READ,
                                             (PVOID)&inBulkControl,
                                             sizeof(BULK_TRANSFER_CONTROL),
                                             inBuffer + c * MaxTransferSize,
                                             //inBuffer,
                                             MaxTransferSize,
                                             &BytesReturned,
                                             ovl + (c % 5));
   }

   for (v = 0; v < 5; v ++)
   {

⌨️ 快捷键说明

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