Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

  1. #include <ansi_c.h>
  2. #include <cvirte.h>
  3. #include "ftd2xx.h"
  4. #include <utility.h>
  5.  
  6. int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  7.                        LPSTR lpszCmdLine, int nCmdShow) {
  8.  
  9.   BYTE bufOut[1024],bufIn[1024];
  10.   DWORD bToSend, bSent, bToReceive, bReceived;
  11.   DWORD dwClockDivisor=29;//Value of clock divisor, SCL Frequency = 60/((1+29)*2) (MHz) = 1Mhz
  12.   DWORD numDevs;
  13.   DWORD Flags;
  14.   DWORD ID;
  15.   DWORD Type;
  16.   DWORD LocId;
  17.   char SerialNumber[16];
  18.   char Description[64];
  19.   FT_STATUS ftStatus;
  20.   FT_HANDLE ftHandle,ftHandleTemp;
  21.   FT_DEVICE_LIST_INFO_NODE *devInfo;
  22.  
  23.   if (InitCVIRTE (hInstance, 0, 0) == 0)
  24.     return -1;    /* out of memory */
  25. // create the device information list
  26.   ftStatus = FT_CreateDeviceInfoList(&numDevs);
  27.   if (ftStatus == FT_OK) {
  28.     printf("Number of devices is %d\n",numDevs);
  29.   }
  30.   if (numDevs > 0) {
  31. // allocate storage for list based on numDevs
  32.     devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);
  33. // get the device information list
  34.     ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs);
  35.     if (ftStatus == FT_OK) {
  36.       for (int i = 0; i < numDevs; i++) {
  37.         printf("Dev %d:\n",i);
  38.         printf(" Flags=0x%x\n",devInfo[i].Flags);
  39.         printf(" Type=0x%x\n",devInfo[i].Type);
  40.         printf(" ID=0x%x\n",devInfo[i].ID);
  41.         printf(" LocId=0x%x\n",devInfo[i].LocId);
  42.         printf(" SerialNumber=%s\n",devInfo[i].SerialNumber);
  43.         printf(" Description=%s\n",devInfo[i].Description);
  44.         printf(" ftHandle=0x%x\n",devInfo[i].ftHandle);
  45.       }
  46.     }
  47. // get information for device 0
  48. /*
  49.     ftStatus = FT_GetDeviceInfoDetail(0, &Flags, &Type, &ID, &LocId, SerialNumber, Description, &ftHandleTemp);
  50.     if (ftStatus == FT_OK) {
  51.       printf("Dev 0:\n");
  52.       printf(" Flags=0x%x\n",Flags);
  53.       printf(" Type=0x%x\n",Type);
  54.       printf(" ID=0x%x\n",ID);
  55.       printf(" LocId=0x%x\n",LocId);
  56.       printf(" SerialNumber=%s\n",SerialNumber);
  57.       printf(" Description=%s\n",Description);
  58.       printf(" ftHandle=0x%x\n",ftHandleTemp);
  59.     }
  60. */
  61.   }
  62.   ftStatus = FT_Open(0,&ftHandle);
  63.   if (ftStatus != FT_OK) {
  64.     printf("Can't open FT232H device! \n");
  65.     return 1;
  66.   } else // Port opened successfully
  67.     printf("Successfully open FT2232H device! \n");
  68.  
  69. // reset device
  70.   ftStatus = FT_ResetDevice(ftHandle);
  71. //Purge USB receive buffer first by reading out all old data from FT232H receive buffer
  72.   ftStatus |= FT_GetQueueStatus(ftHandle, &bToReceive); // Get the number of bytes in the FT232H receive buffer
  73.   if ((ftStatus == FT_OK) && (bToReceive > 0))
  74.     ftStatus |= FT_Read(ftHandle, bufIn, bToReceive, &bReceived); //Read out the data from FT232H receive buffer
  75.   ftStatus |= FT_SetUSBParameters(ftHandle, 65535, 65535); //Set USB request transfer size
  76.   ftStatus |= FT_SetChars(ftHandle, FALSE, 0, FALSE, 0); //Disable event and error characters
  77.   ftStatus |= FT_SetTimeouts(ftHandle, 3000, 3000); //Sets the read and write timeouts in 3 sec for the FT232H
  78.   ftStatus |= FT_SetLatencyTimer(ftHandle, 1); //Set the latency timer
  79.   ftStatus |= FT_SetBitMode(ftHandle, 0x0, 0x00); //Reset controller
  80.   ftStatus |= FT_SetBitMode(ftHandle, 0x0, 0x02); //Enable MPSSE mode
  81.   if (ftStatus != FT_OK) {
  82.     printf("fail on initialize FT2232H device ! \n");
  83.     return FALSE;
  84.   }
  85.   Sleep(50); // Wait 50ms for all the USB stuff to complete and work
  86.  
  87. // test communication by sending bad command 0xAA
  88.   bToSend=0;
  89.   bufOut[bToSend++]=0xAA;
  90.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent);
  91.   do{
  92.     ftStatus = FT_GetQueueStatus(ftHandle, &bToReceive); // Get the number of bytes in the device input buffer
  93.   }while ((bToReceive == 0) && (ftStatus == FT_OK)); //or Timeout
  94.   ftStatus = FT_Read(ftHandle, bufIn, bToReceive, &bReceived); //Read out the data from input buffer
  95.   printf("Received byts: %d",bReceived);
  96.   if (bReceived > 1) printf(": 0x%02X, 0x%02X",bufIn[bReceived-2],bufIn[bReceived-1]);
  97.   printf("\n");      
  98.  
  99.   bToSend=0;
  100.   bufOut[bToSend++]=0xAB;
  101.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent);
  102.   do{
  103.     ftStatus = FT_GetQueueStatus(ftHandle, &bToReceive); // Get the number of bytes in the device input buffer
  104.   }while ((bToReceive == 0) && (ftStatus == FT_OK)); //or Timeout
  105.   ftStatus = FT_Read(ftHandle, bufIn, bToReceive, &bReceived); //Read out the data from input buffer
  106.   printf("Received bytes: %d",bReceived);
  107.   if (bReceived > 1) printf(": 0x%02X, 0x%02X",bufIn[bReceived-2],bufIn[bReceived-1]);
  108.   printf("\n");      
  109.  
  110.   bToSend=0;
  111.   bufOut[bToSend++]=0x8A;//Ensure disable clock divide by 5 for 60Mhz master clock
  112.   bufOut[bToSend++]=0x97;//Ensure turn off adaptive clocking
  113.   bufOut[bToSend++]=0x8D;//disable 3 phase data clock
  114.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent);
  115.  
  116.   bToSend = 0;
  117.   bufOut[bToSend++]=0x80; //Command to set directions of lower 8 pins and force value on bits set as output
  118.   bufOut[bToSend++]=0x00; //Set SDA, SCL high, WP disabled by SK, DO at bit &*, GPIOL0 at bit &*
  119.   bufOut[bToSend++]=0x0B; //Set SK,DO,GPIOL0 pins as output with bit **, other pins as input with bit &*
  120. // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off
  121. // SK frequency = 60MHz /((1 + [(1 +0xValueH*256) OR 0xValueL])*2)
  122.   bufOut[bToSend++]=0x86; //Command to set clock divisor
  123.   bufOut[bToSend++]=dwClockDivisor & 0xFF; //Set 0xValueL of clock divisor
  124.   bufOut[bToSend++]=dwClockDivisor >> 8; //Set 0xValueH of clock divisor
  125.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent); // Send out the commands
  126.   Sleep(20); //Delay for a while
  127. //Turn off loop back in case
  128.   bToSend = 0;
  129.   bufOut[bToSend++]=0x85; //Command to turn off loop back of TDI/TDO connection
  130.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent); // Send out the commands
  131.   Sleep(30); //Delay for a while
  132.   printf("SPI initialized successfuly\n");
  133.  
  134.   bToSend=0;
  135.   bufOut[bToSend++]=0x82;// set ACBUS 0-7
  136.   bufOut[bToSend++]=0x00;// value
  137.   bufOut[bToSend++]=0x00;// direction 0-input  
  138.   bufOut[bToSend++]=0x80;// set ADBUS 0-7  
  139.   bufOut[bToSend++]=0xF0;  
  140.   bufOut[bToSend++]=0xFF;  
  141.   bufOut[bToSend++]=0x81;// read ADBUS
  142.   bufOut[bToSend++]=0x83;// read ACBUS  
  143.   bufOut[bToSend++]=0x80;  
  144.   bufOut[bToSend++]=0x00;  
  145.   bufOut[bToSend++]=0xFF;  
  146.   bufOut[bToSend++]=0x81;  
  147.   bufOut[bToSend++]=0x83;  
  148.   ftStatus = FT_Write(ftHandle, bufOut, bToSend, &bSent);
  149.   do{
  150.     ftStatus = FT_GetQueueStatus(ftHandle, &bToReceive); // Get the number of bytes in the device input buffer
  151.   }while ((bToReceive == 0) && (ftStatus == FT_OK)); //or Timeout
  152.   ftStatus = FT_Read(ftHandle, bufIn, bToReceive, &bReceived); //Read out the data from input buffer
  153.   printf("Received bytes: 0x%02X, 0x%02X, 0x%02X, 0x%02X\n",bufIn[0],bufIn[1],bufIn[2],bufIn[3]);
  154.  
  155.   getchar();
  156. //  Delay(10);
  157.   FT_Close(ftHandle);
  158.   return 0;
  159. }
  160.