BOOL CDriverInterface::GetVIDPID(
    PUSHORT a_puwVID, 
    PUSHORT a_puwPID
    )
{
    return FT_GetVIDPID(
        m_FTHandle, 
        a_puwVID, 
        a_puwPID
        ) == FT_OK ? TRUE : FALSE;
}
int main(int argc, char *argv[])
{

  static const struct option long_opts[] = {
    {"prompt",     no_argument,       NULL, 'p'},
    {"verbose",    no_argument,       NULL, 'v'},
    {"help",       no_argument,       NULL, 'h'},
    {NULL,         no_argument,       NULL, 0}
  };

  opterr = 0;
  int c;
  int option_index = 0;
  while ((c = getopt_long(argc, argv, "vph", long_opts, &option_index)) != -1)
    switch (c) {
      case 'v':
        verbose++;
        break;
      case 'p':
        dont_prompt++;
        break;
      case 'h':
        print_usage();
        return EXIT_SUCCESS;
      case '?':
        fprintf(stderr, "Unknown option `-%c'.\n", optopt);
        return EXIT_FAILURE;
      default:
        abort();
     }
  
  DWORD num_devs;
  DWORD VID, PID;
  int iport = 0;

  /* See how many devices are plugged in, fail if greater than 1 */
  if (verbose)
    printf("Creating device info list\n");
  ft_status = FT_CreateDeviceInfoList(&num_devs);
  if (ft_status != FT_OK){
    fprintf(stderr,"ERROR : Failed to create device info list, ft_status = %d\n",ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("Making sure only one FTDI device is plugged in\n");
  /* Check that there aren't more than 1 device plugged in */
  if (num_devs > 1){
    fprintf(stderr,"ERROR : More than one FTDI device plugged in\n");
    return EXIT_FAILURE;
  }

  /* Get VID/PID from FTDI device */
  if (verbose)
    printf("Getting VID/PID from device\n");
  ft_status = FT_GetVIDPID(&VID,&PID);
  if (ft_status != FT_OK){
    fprintf(stderr,"ERROR : Failed to get VID and PID from FTDI device, ft_status = %d\n",ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("    VID = %04x, PID = %04x\n",VID,PID);
  /* Set the VID/PID found */
  if (verbose)
    printf("Setting VID/PID\n");
  ft_status = FT_SetVIDPID(VID,PID);
  if (ft_status != FT_OK){
    fprintf(stderr,"ERROR : Failed to set VID and PID, ft_status = %d\n",ft_status);
    return EXIT_FAILURE;
  }

  /* Get info about the device and print to user */
  //FT_GetDeviceInfoDetail seems to not work very reliably
  //DWORD lpdw_flags;
  //DWORD lpdw_type;
  //DWORD lpdw_id;
  //char pc_serial_number[16];
  //char pc_description[64];
  //ft_status = FT_GetDeviceInfoDetail(0,&lpdw_flags,&lpdw_type,&lpdw_id,NULL,pc_serial_number,pc_description,&ft_handle);
  //if (ft_status != FT_OK){
  //  fprintf(stderr,"ERROR : Failed to get device info, ft_status = %d\n",ft_status);
  //  return EXIT_FAILURE;
  //}
  //if (verbose || !dont_prompt){
  //  printf("Device Information : \n");
  //  printf("     Description   : %s\n", pc_description);
  //  printf("     Serial Number : %s\n", pc_serial_number);
  //  printf("     Flags         : 0x%x\n", lpdw_flags);
  //  printf("     Type          : 0x%x\n", lpdw_type);
  //  printf("     ID            : 0x%x\n", lpdw_id);
  //}

  /* Ask user if this is the correct device */
  if (!dont_prompt) {
    char correct_device;
    printf("Is this the correct device? (y/n) : ");
    scanf("%c",&correct_device);
    while ((correct_device != 'y') && (correct_device != 'n')) {
      printf("\rPlease enter y or n");
      scanf("%c",&correct_device);
    }
    if (correct_device == 'n'){
      printf("Exiting, since this is not the device we want to program\n");
      return EXIT_SUCCESS;
    }
  }

  /* Open the device */
  if (verbose)
    printf("Attempting to open device using read VID/PID...");
  ft_status = FT_Open(iport, &ft_handle);
  /* If that didn't work, try some other likely VID/PID combos */
  /* Try 0403:6014 */
  if (ft_status != FT_OK){
    if (verbose)
      printf("FAILED\nTrying VID=0x0403, PID=0x6014...");
    ft_status = FT_SetVIDPID(0x0403,0x6014);
    if (ft_status != FT_OK){
      fprintf(stderr,"ERROR : Failed to set VID and PID, ft_status = %d\n",ft_status);
      return EXIT_FAILURE;
    }
    ft_status = FT_Open(iport, &ft_handle);
  }
  /* Try 0403:8398 */
  if (ft_status != FT_OK){
    if (verbose)
      printf("FAILED\nTrying VID=0x0403, PID=0x8398...");
    ft_status = FT_SetVIDPID(0x0403,0x8398);
    if (ft_status != FT_OK){
      fprintf(stderr,"ERROR : Failed to set VID and PID, ft_status = %d\n",ft_status);
      return EXIT_FAILURE;
    }
    ft_status = FT_Open(iport, &ft_handle);
  }
  /* Exit program if we still haven't opened the device */
  if (ft_status != FT_OK){
    if (verbose)
      printf("FAILED\n");
    fprintf(stderr,"ERROR : Failed to open device : ft_status = %d\nHave you tried (sudo rmmod ftdi_sio)?\n",ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("SUCCESS\n");

  /* Erase the EEPROM */
  ft_status = FT_EraseEE(ft_handle);
  if(ft_status != FT_OK) {
    fprintf(stderr, "ERROR: Device EEPROM could not be erased : ft_status = %d\n", ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("Erased device's EEPROM\n");

  /* Assign appropriate values to eeprom data */
  eeprom_data.Signature1 = 0x00000000;
  eeprom_data.Signature2 = 0xffffffff;
  eeprom_data.Version = 5; /* 5=FT232H */
  eeprom_data.VendorId = USB_CUSTOM_VID;        
  eeprom_data.ProductId = USB_CUSTOM_PID;
  eeprom_data.Manufacturer = "FTDI";
  eeprom_data.ManufacturerId = "FT";
  eeprom_data.Description = "Piksi Passthrough";
  eeprom_data.IsFifoH = 1; /* needed for FIFO samples passthrough */

  /* Program device EEPROM */
  ft_status = FT_EE_Program(ft_handle, &eeprom_data);
  if (ft_status != FT_OK) {
    fprintf(stderr,"ERROR : Failed to program device EEPROM : ft_status = %d\n",ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("Programmed device's EEPROM\n");

  /* Reset the device */
  ft_status = FT_ResetDevice(ft_handle);
  if (ft_status != FT_OK) {
    fprintf(stderr, "ERROR: Device could not be reset : ft_status = %d\n", ft_status);
    return EXIT_FAILURE;
  }
  if (verbose)
    printf("Reset device\n");

  /* Close the device */
  if (verbose)
    printf("Closing device\n");
  FT_Close(ft_handle);
  if(ft_status != FT_OK) {
    fprintf(stderr,"ERROR : Failed to close device : ft_status = %d\n",ft_status);
    return EXIT_FAILURE;
  }

  printf("Re-configuring for FIFO mode successful, please unplug and replug your device now\n");

  return EXIT_SUCCESS;
}
BOOL CDriverInterface::Initialize(
    EOPEN_BY a_eTypeOpenBy, 
    PVOID a_pvOpenBy
    )
{
    BOOL bResult = FALSE;
    FT_STATUS ftStatus = FT_OK;
    int iDeviceNumber = 0;
    int iNumTries = 0;
	DWORD dwNumDevices = 0;
	DWORD dwDeviceIndex = 0;
	FT_DEVICE_LIST_INFO_NODE *ptDevicesInfo = NULL;


	std::string strOpenBy((CHAR*)a_pvOpenBy);
	std::wstring wstrOpenBy(strOpenBy.begin(), strOpenBy.end());

	dwNumDevices = GetNumberOfDevicesConnected();
	if (dwNumDevices == 0)
	{
		//GUI_LOG(_T("No device is connected!"));
		return FALSE;
	}

	if (GetDevicesInfoList(&ptDevicesInfo) == 0)
	{
		//GUI_LOG(_T("No device is connected!"));
		return FALSE;
	}

	//GUI_LOG(_T("\nList of Connected Devices!\n"));
	for (DWORD i = 0; i < dwNumDevices; i++)
	{
		std::string strSerialNumber(ptDevicesInfo[i].SerialNumber);
		std::wstring wstrSerialNumber(strSerialNumber.begin(), strSerialNumber.end());
		std::string strDescription(ptDevicesInfo[i].Description);
		std::wstring wstrDescription(strDescription.begin(), strDescription.end());

		/*GUI_LOG(_T("Device[%d]"),			i);
		GUI_LOG(_T("\tFlags: 0x%x %s  |  Type: %d %s  |  ID: 0x%08X  |  ftHandle: 0x%x %s"),	
											ptDevicesInfo[i].Flags,
											ptDevicesInfo[i].Flags & FT_FLAGS_SUPERSPEED ? _T("[USB 3]") :
											ptDevicesInfo[i].Flags & FT_FLAGS_HISPEED ? _T("[USB 2]") : 
											ptDevicesInfo[i].Flags & FT_FLAGS_OPENED ? _T("[OPENED]") : _T(""),
											ptDevicesInfo[i].Type,
											ptDevicesInfo[i].Type == FT_DEVICE_600 || ptDevicesInfo[i].Type == FT_DEVICE_601 ? _T("[FT60X]") : _T(""),
											ptDevicesInfo[i].ID,
											ptDevicesInfo[i].ftHandle,
											ptDevicesInfo[i].ftHandle ? _T("[Already open]") : _T("")
											);
		GUI_LOG(_T("\tDescription: %s"),	wstrDescription.c_str());
		GUI_LOG(_T("\tSerialNumber: %s"),	wstrSerialNumber.c_str());*/
	}
	//GUI_LOG(_T(""));

	dwDeviceIndex = GetDeviceIndex(ptDevicesInfo, dwNumDevices, a_eTypeOpenBy, a_pvOpenBy);
	if (dwDeviceIndex == 0xFFFFFFFF)
	{
		/*GUI_LOG(_T("Device with %s [%s] not found!"),
			a_eTypeOpenBy == EOPEN_BY_DESC ? _T("Description") : 
			a_eTypeOpenBy == EOPEN_BY_SERIAL ? _T("Serial Number"):
			a_eTypeOpenBy == EOPEN_BY_INDEX ? _T("Index") : _T(""),
			wstrOpenBy.c_str());*/

		ReleaseDevicesInfoList(ptDevicesInfo);
		return FALSE;
	}

	ReleaseDevicesInfoList(ptDevicesInfo);

    //CMD_LOG(_T("FT_Open"));
    m_FTHandle = NULL;

    do
    {
        switch (a_eTypeOpenBy)
        {
            case EOPEN_BY_GUID:
            {
                ftStatus = FT_Create(a_pvOpenBy, FT_OPEN_BY_GUID, &m_FTHandle);
                break;
            }
            case EOPEN_BY_DESC:
            {
                ftStatus = FT_Create(a_pvOpenBy, FT_OPEN_BY_DESCRIPTION, &m_FTHandle);
                break;
            }
			case EOPEN_BY_SERIAL:
			{
				ftStatus = FT_Create(a_pvOpenBy, FT_OPEN_BY_SERIAL_NUMBER, &m_FTHandle);
				break;
			}
			case EOPEN_BY_INDEX:
			{
				ULONG ulIndex = atoi((CHAR*)a_pvOpenBy);
				ftStatus = FT_Create((PVOID)ulIndex, FT_OPEN_BY_INDEX, &m_FTHandle);
				break;
			}
		}

        if (FT_FAILED(ftStatus))
        { 
            //CMD_LOG(_T("\t%s FAILED! FT_Open"), _T(__FUNCTION__));
            Sleep(1000);
            continue;
        }

        // Get device VID and PID 
        // to verify if this is FT60X
        USHORT uwVID = 0;
        USHORT uwPID = 0;
        ftStatus = FT_GetVIDPID(m_FTHandle, &uwVID, &uwPID);
        if (FT_FAILED(ftStatus))
        { 
            //CMD_LOG(_T("\t%s FAILED! FT_GetVIDPID"), _T(__FUNCTION__));
            break;
        }
        if ((uwVID != FT600_VID) || 
            (uwPID != FT600_PID && uwPID != FT601_PID) )
        {
            //CMD_LOG(_T("\t%s FAILED! FT_GetVIDPID VID=0x%04x PID=0x%04x"), _T(__FUNCTION__), uwVID, uwPID);
            Cleanup();
            break;
        }

        break;
    }
    while (iNumTries++ < 1); //while (iNumTries++ < 3);


    if (ftStatus != FT_OK || m_FTHandle == NULL) 
    {
        if (ftStatus == FT_DEVICE_NOT_FOUND) 
        {
            //GUI_LOG(_T("Device not connected or driver not installed!"));
        } 
        else 
        {
            //GUI_LOG(_T("Failed looking for device, FT_STATUS 0x%x!"), ftStatus);
        }

        Cleanup();
        return FALSE;
    }

	/*GUI_LOG(_T("Device with %s [%s] opened! Device[%d]!"), 
		a_eTypeOpenBy == EOPEN_BY_DESC ? _T("Description") : 
		a_eTypeOpenBy == EOPEN_BY_SERIAL ? _T("Serial Number") :
		a_eTypeOpenBy == EOPEN_BY_INDEX ? _T("Index") : _T(""),
		wstrOpenBy.c_str(), dwDeviceIndex);*/

    return TRUE;
}
示例#4
0
main (int argc, char *argv[])
{
    DWORD num;
    FT_CreateDeviceInfoList(&num);

    std::cout << num << " devices found" << std::endl;

    DWORD version;
    FT_GetLibraryVersion (&version);
    std::cout << "Version " << version << std::endl;

    FT_STATUS ftStatus;

    DWORD vid,pid;    
    ftStatus = FT_GetVIDPID(&vid,&pid);
    if (ftStatus!= FT_OK)
    {
        std::cout << "Error " << ftStatus << std::endl;
    }

    std::cout << std::hex << "vid("<<vid<<") pid("<<pid<<")" << std::endl;
    std::cout << std::dec << "vid("<<vid<<") pid("<<pid<<")" << std::endl;

    FT_DEVICE_LIST_INFO_NODE devInfo[16];
    ftStatus = FT_GetDeviceInfoList(devInfo, &num);
    if (ftStatus != FT_OK)
    {
        std::cout << "Error " << ftStatus << std::endl;
    }

    for (int i = 0; i < num; i++) {
        printf("Dev %d:\n",i);
        printf("  Flags=0x%x\n",devInfo[i].Flags);
        printf("  Type=0x%x\n",devInfo[i].Type);
        printf("  ID=0x%x\n",devInfo[i].ID);
        printf("  LocId=0x%x\n",devInfo[i].LocId);
        printf("  SerialNumber=%s\n",devInfo[i].SerialNumber);
        printf("  Description=%s\n",devInfo[i].Description);
        //printf("  ftHandle=0x%x\n",(unsigned int)devInfo[i].ftHandle);
    }

    FT_HANDLE h;

    ftStatus = FT_Open(0, &h);
    if (ftStatus != FT_OK) {
        std::cout << "Failed to open device - " << ftStatus << std::endl;
    }

/*
    std::cout << "start" << std::endl;
    for(size_t i(0); i < 10; ++i)
    {
        FT_SetRts(h);
        FT_ClrDtr(h);
        sleep(1);
        FT_ClrRts(h);
        FT_SetDtr(h);
        sleep(1);
    }
*/

    FT_Close(0);
    std::cout << "done" << std::endl;
}