Esempio n. 1
0
void TellStick::aquireTellStick() {
	char *tempSerial = new char[serial().size()+1];
#ifdef _WINDOWS
	strcpy_s(tempSerial, serial().size()+1, serial().toLocal8Bit());
#else
	strcpy(tempSerial, serial().toLocal8Bit());
	int pid = 0x0C30;
	if (type() == 2) {
		pid = 0x0C31;
	}
	FT_SetVIDPID(0x1781, pid);
#endif
	FT_STATUS ftStatus = FT_OpenEx(tempSerial, FT_OPEN_BY_SERIAL_NUMBER, &d->ftHandle);
	delete tempSerial;
	if (ftStatus != FT_OK) {
		return;
	}
	//open = true;

	if (type() == 2) {
		FT_SetBaudRate(d->ftHandle, 115200);
	} else {
		FT_SetBaudRate(d->ftHandle, 9600);
	}
	FT_SetFlowControl(d->ftHandle, FT_FLOW_NONE, 0, 0);
	FT_SetTimeouts(d->ftHandle,1000,0);

	setUpgradeStep(2);
	QTimer::singleShot(0, this, SLOT(enterBootloader()));
}
Esempio n. 2
0
FT_STATUS OpenDevice(DeviceParams_t* device)
{
	FT_STATUS status;
	FT_SetVIDPID(device->vid, device->pid);
	status = FT_OpenEx((PVOID) device->serial, FT_OPEN_BY_SERIAL_NUMBER, &(device->handle));
	
	if(status == FT_OK)
	{
		status = FT_SetDataCharacteristics(device->handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
	}

	if(status == FT_OK)
	{
		status = FT_SetBaudRate(device->handle, device->baud);
	}

	if(status == FT_OK)
	{
		// pthread_mutex_init(&device->rxEvent.eMutex, NULL);
    // pthread_cond_init(&device->rxEvent.eCondVar, NULL);
    	
    // status = FT_SetEventNotification(device->handle, FT_EVENT_RXCHAR, (PVOID) &(device->rxEvent));
    
		// pthread_create(&(device->thread), NULL, ReadData, (void*)device);
	}

	printf ("Open state [%x]\r\n", status);
	return status;
}
Esempio n. 3
0
FT_STATUS ftdimut_setup() {
  FT_STATUS ftStatus; 
  unsigned char timer;

	ftStatus = FT_SetVIDPID(USB_VID, USB_PID);
  if(ftStatus != FT_OK) return ftStatus;
	ftStatus = FT_Open(0, &ftHandle);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_ResetDevice(ftHandle);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_SetBaudRate(ftHandle, 15625);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_SetTimeouts(ftHandle, 1000, 1000);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_GetLatencyTimer(ftHandle, &timer);
  if(ftStatus != FT_OK) return ftStatus;
  ftStatus = FT_SetLatencyTimer(ftHandle, 1);
  if(ftStatus != FT_OK) return ftStatus;

  return FT_OK;
}
Esempio n. 4
0
static void ft245r_set_bitclock(PROGRAMMER * pgm) {
  FT_STATUS r;
  int rate = 0;
  int i;

  if (pgm->bitclock == 0.0) { // using default
	  rate = 235000.0 /2; 
  } else if (pgm->bitclock >= 0.50 ) {
	  rate = 500000.0 /2;
  } else if (pgm->bitclock <  0.01) {
	  rate =   1000.0 /2;
  } else {
	  rate = pgm->bitclock * 1000000.0 /2;
  }
  for (i= sizeof(valid_rates)/sizeof(valid_rates[0]) -1; i>=0; --i) 
  {
    if (valid_rates[i] <= rate) {
		rate = valid_rates[i];
		break;
    }
  }
  if (i<0) rate = valid_rates[0];

  r = FT_SetBaudRate(handle, rate);
  if ((verbose>=1) || FT245R_DEBUG) {
	fprintf(stderr," ft245r:  bitclk %d -> ft baud %d\n", 
			rate * 2, rate);
  }
}
Esempio n. 5
0
/*
 * Class:     kinetic_Kinetic1090Puck
 * Method:    puckOpen
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_kinetic_Kinetic1090Puck_puckOpen(JNIEnv *env, jobject obj)
{
    // open the device
	FT_STATUS ftStatus = FT_OpenEx("Kinetic 1090 Puck Beavis 3A B", FT_OPEN_BY_DESCRIPTION, &ftHandle);
    if (ftStatus != FT_OK) {
        return -1;
    }

	// set the baud rate
	ftStatus = FT_SetBaudRate(ftHandle, 3000000);
    if (ftStatus != FT_OK) {
        return -1;
    }

	// set the data characteristics
	ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
    if (ftStatus != FT_OK) {
        return -1;
    }

	// set flow control
	ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0x11, 0x13);
	if (ftStatus != FT_OK) {
        return -1;
    }

	// set read and write timeouts
	ftStatus = FT_SetTimeouts(ftHandle, 5000, 1000);
	if (ftStatus != FT_OK) {
        return -1;
    }

	return 0;
}
Esempio n. 6
0
bool FTDXXDevice::connect (int baudrate) throw ()
{
#ifdef FOUND_ftd2xx

  disconnect();

  _status = FT_SetVIDPID(0x0403, 0x6001);
  FXXCHECK("Could not set IDs.");

  _status = FT_Open(0, &_handle);
  FXXCHECK("Could not open handle.");

  _status = FT_SetBaudRate(_handle, baudrate);
  FXXCHECK("Could not set baudrate.");

  _status = FT_SetLatencyTimer(_handle, 0);
  FXXCHECK("Unable to set latency timer.");

  _status = FT_Purge(_handle, FT_PURGE_RX | FT_PURGE_TX);
  FXXCHECK("Unable to purge buffers.");

  _initialized = true;
  FTDLOG("Connection successful.");
  return true;

#else

  _initialized = false;
  FTDERROR("cannot connect FTD2xx device: compiled without required libraries");
  return false;

#endif
}
Esempio n. 7
0
bool FtdiDevices::setSpeed( ULONG baudRate ) {
  FT_STATUS ftStatus;
  ftStatus = FT_SetBaudRate(m_ftHandleA, baudRate);
  if (!FT_SUCCESS(ftStatus)) {
    return false;
  }
  return true;
}
FT_STATUS FTDISerial::Connect(DWORD baudRate) {
	FT_STATUS status;
	status = FT_OpenEx("Nova Power Input Module", FT_OPEN_BY_DESCRIPTION, &m_handle);
	if(status == FT_OK) {
		m_isConnected = true;
		FT_SetBaudRate(m_handle, baudRate);
	}
	return status;
}
Esempio n. 9
0
QString FTD2XXInterface::readLabel(uchar label, int *ESTA_code)
{
    FT_HANDLE ftdi = NULL;

    if (FT_Open(id(), &ftdi) != FT_OK)
        return QString();

    if(FT_ResetDevice(ftdi) != FT_OK)
        return QString();

    if(FT_SetBaudRate(ftdi, 250000) != FT_OK)
        return QString();

    if(FT_SetDataCharacteristics(ftdi, FT_BITS_8, FT_STOP_BITS_2, FT_PARITY_NONE) != FT_OK)
        return QString();

    if(FT_SetFlowControl(ftdi, 0, 0, 0) != FT_OK)
        return QString();

    QByteArray request;
    request.append(ENTTEC_PRO_START_OF_MSG);
    request.append(label);
    request.append(ENTTEC_PRO_DMX_ZERO); // data length LSB
    request.append(ENTTEC_PRO_DMX_ZERO); // data length MSB
    request.append(ENTTEC_PRO_END_OF_MSG);

    DWORD written = 0;
    if (FT_Write(ftdi, (char*) request.data(), request.size(), &written) != FT_OK)
        return QString();

    if (written == 0)
    {
        qDebug() << Q_FUNC_INFO << "Cannot write data to device";
        return QString();
    }

    uchar* buffer = (uchar*) malloc(sizeof(uchar) * 40);
    Q_ASSERT(buffer != NULL);

    int read = 0;
    QByteArray array;
    FT_SetTimeouts(ftdi, 500,0);
    FT_Read(ftdi, buffer, 40, (LPDWORD) &read);
    qDebug() << Q_FUNC_INFO << "----- Read: " << read << " ------";
    for (int i = 0; i < read; i++)
        array.append((char) buffer[i]);

    if (array[0] != ENTTEC_PRO_START_OF_MSG)
        qDebug() << Q_FUNC_INFO << "Reply message wrong start code: " << QString::number(array[0], 16);
    *ESTA_code = (array[5] << 8) | array[4];
    array.remove(0, 6); // 4 bytes of Enttec protocol + 2 of ESTA ID
    array.replace(ENTTEC_PRO_END_OF_MSG, '\0'); // replace Enttec termination with string termination

    FT_Close(ftdi);
    return QString(array);
}
Esempio n. 10
0
bool D2xxSerial::setBaudRate(int baudRate) {
	FT_STATUS err = FT_SetBaudRate(handle, baudRate);
	if(err!=FT_OK) {
		printf("Error setting baud rate: %s\n", getError(err));
		return false;
	}
	//printf("%d baud rate\n", baudRate);
	
	return true;
}
Esempio n. 11
0
int dxl_hal_set_baud( float baudrate )
{
	FT_STATUS ft_status;
	int iBaudrate;

	iBaudrate = (int)baudrate;
	ft_status = FT_SetBaudRate( ghFt_Handle, (DWORD)iBaudrate );
	if( ft_status != FT_OK )
		return 0;

	gfByteTransTime = (float)((1000.0f / baudrate) * 12.0f);
	return 1;
}
Esempio n. 12
0
File: main.c Progetto: 0x6a77/JD2XX
int main(int argc, char *argv[])
{
	char * pcBufRead;
	DWORD dwBytesRead;
	FILE * fh;
	FT_HANDLE ftHandle;
	FT_STATUS ftStatus;
	int iport;
	
	if(argc > 1) {
		sscanf(argv[1], "%d", &iport);
	}
	else {
		iport = 0;
	}
	
	fh = fopen("target.bin", "wb+");
	if(fh == NULL) {
		printf("Cant open source file\n");
		return 1;
	}
		
	ftStatus = FT_Open(iport, &ftHandle);
	if(ftStatus != FT_OK) {
		/* 
			This can fail if the ftdi_sio driver is loaded
		 	use lsmod to check this and rmmod ftdi_sio to remove
			also rmmod usbserial
		 */
		printf("FT_Open(%d) failed\n", iport);
		return 1;
	}

	pcBufRead = (char *)malloc(BUF_SIZE);
	FT_ResetDevice(ftHandle);
	FT_SetBaudRate(ftHandle, 115200);
	FT_SetDtr(ftHandle);
	FT_SetRts(ftHandle);
	FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0);
	FT_SetTimeouts(ftHandle, 0, 0);				// infinite timeouts	
	FT_SetBitMode(ftHandle, 0xFF, 0x01);
	FT_Read(ftHandle, pcBufRead, BUF_SIZE, &dwBytesRead);

	fwrite(pcBufRead, 1, dwBytesRead, fh);
	fclose(fh);
	free(pcBufRead);
	FT_Close(ftHandle);
	
	return 0;
}
Esempio n. 13
0
/**
    Open a power meter probe by a given serial number

    \param pm a pointer to a pm_context
    \param serial the serial number

    \retval  0 - all fine
    \retval -1 - open failed (wrong serial number?)
    \retval -2 - setting baudrate failed
    \retval -3 - setting data characteristics failed
    \retval -4 - setting flow control failed
    \retval -5 - setting timeouts failed
    \retval -6 - purging buffers failed
    \retval -7 - resetting device failed
*/
PM600X_EXPORT int pm_open(struct pm_context *pm, unsigned long serial)
{
	FT_STATUS ftstat;

	// convert the serial into a 8 digit string with leading zeroes
	char serial_string[12];
	snprintf(serial_string, 12, "%06lu", serial);

	// open the device by a given serial number
	ftstat = FT_OpenEx((void *)serial_string, FT_OPEN_BY_SERIAL_NUMBER, &pm->handle);
	if (ftstat != FT_OK)
		pm_error_return(-1, "open failed (wrong serial number?)");

	// get the device info to obtain the power meter type
	unsigned long id;
	ftstat = FT_GetDeviceInfo(pm->handle, NULL, &id, NULL, NULL, NULL);
	if (ftstat != FT_OK)
		pm_error_return(-8, "can not retrieve device info!");

	pm->type = id;

	// set baud rate to 115200
	ftstat = FT_SetBaudRate(pm->handle, FT_BAUD_115200);
	if (ftstat != FT_OK)
		pm_error_return(-2, "setting baudrate failed");

	// set data characteristics to 8n1
	ftstat = FT_SetDataCharacteristics(pm->handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
	if (ftstat != FT_OK)
		pm_error_return(-3, "setting data characteristics failed");

	// set flow control to NONE
	ftstat = FT_SetFlowControl(pm->handle, FT_FLOW_NONE, 0, 0);
	if (ftstat != FT_OK)
		pm_error_return(-4, "setting flow control failed");

	// set timeouts to 1 second for writes and 3.5 seconds for reads. This should be enough when measuring with averaging == 10000
	ftstat = FT_SetTimeouts(pm->handle, 3500, 1000);
	if (ftstat != FT_OK)
		pm_error_return(-5, "setting timeouts failed");

	// purge bufffers just in case
	ftstat = FT_Purge(pm->handle, FT_PURGE_RX | FT_PURGE_TX);
	if (ftstat != FT_OK)
		pm_error_return(-6, "purging buffers failed");

	// reset the device via *RST
	return pm_reset(pm);
}
Esempio n. 14
0
File: main.c Progetto: 0x6a77/JD2XX
int main(int argc, char *argv[])
{
	DWORD dwBytesInQueue = 0;
	FT_STATUS	ftStatus;
	FT_HANDLE	ftHandle;
	unsigned char ucMode = 0x00;
	int iport;
	
	if(argc > 1) {
		sscanf(argv[1], "%d", &iport);
	}
	else {
		iport = 0;
	}
	
	ftStatus = FT_Open(iport, &ftHandle);
	if(ftStatus != FT_OK) {
		/* 
			This can fail if the ftdi_sio driver is loaded
		 	use lsmod to check this and rmmod ftdi_sio to remove
			also rmmod usbserial
		 */
		printf("FT_Open(%d) failed\n", iport);
		return 1;
	}
		
	ftStatus = FT_SetBitMode(ftHandle, 0xFF, 1);
	if(ftStatus != FT_OK) {
		printf("Failed to set bit mode\n");	
	}
	FT_SetBaudRate(ftHandle, 9600);
	
	FT_Write(ftHandle, &ucMode, 1, &dwBytesInQueue);

	ftStatus = FT_GetBitMode(ftHandle, &ucMode);
	if(ftStatus != FT_OK) {
		printf("Failed to get bit mode\n");
	}
	else {
		printf("ucMode = 0x%X\n", ucMode);	
	}

	FT_Close(ftHandle);
	
	return 0;
}
Esempio n. 15
0
BOOL InitializeForBitIO(void) {
	DWORD DeviceCount;
	FT_Status = FT_CreateDeviceInfoList(&DeviceCount);
	if (FT_Status) return printf("FT_CreateDeviceInfoList failed (%d)", FT_Status);
	if (DeviceCount == 0) return printf("No FTDI devices attached\n");

	FT_Status = FT_Open(0, &FT_Handle);
	if (FT_Status) return printf("FT_Open failed (%d)", FT_Status);

	FT_Status = FT_SetBaudRate(FT_Handle, 921600);
	if (FT_Status) return printf("FT_SetBaudRate failed (%d)", FT_Status);

	FT_Status = FT_SetBitMode(FT_Handle, 1, SyncBitBang);
	if (FT_Status) return printf("FT_SetBitMode failed (%d)", FT_Status);

	return 0;
	}
Esempio n. 16
0
bool CUsb3003DF2ETInterface::OpenDevice(void)
{
    FT_STATUS ftStatus;
    int baudrate = 921600;
    
    //sets serial VID/PID for a Standard Device NOTE:  This is for legacy purposes only.  This can be ommitted.
    ftStatus = FT_SetVIDPID(m_uiVid, m_uiPid);
    if (ftStatus != FT_OK) {FTDI_Error((char *)"FT_SetVIDPID", ftStatus ); return false; }
    
    ftStatus = FT_OpenEx((PVOID)m_szDeviceSerial, FT_OPEN_BY_SERIAL_NUMBER, &m_FtdiHandle);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_OpenEx", ftStatus ); return false; }
    
    CTimePoint::TaskSleepFor(50);
    FT_Purge(m_FtdiHandle, FT_PURGE_RX | FT_PURGE_TX );
    CTimePoint::TaskSleepFor(50);
    
    ftStatus = FT_SetDataCharacteristics(m_FtdiHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
    if ( ftStatus != FT_OK ) { FTDI_Error((char *)"FT_SetDataCharacteristics", ftStatus ); return false; }
    
    ftStatus = FT_SetFlowControl(m_FtdiHandle, FT_FLOW_RTS_CTS, 0x11, 0x13);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetFlowControl", ftStatus ); return false; }
    
    ftStatus = FT_SetRts (m_FtdiHandle);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetRts", ftStatus ); return false; }
    
    // for DF2ET-3003 interface pull DTR low to take AMBE3003 out of reset.
    ftStatus = FT_SetDtr( m_FtdiHandle );
    CTimePoint::TaskSleepFor(50);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetDtr", ftStatus); return false; }
    
    ftStatus = FT_SetBaudRate(m_FtdiHandle, baudrate );
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetBaudRate", ftStatus ); return false; }
    
    ftStatus = FT_SetLatencyTimer(m_FtdiHandle, 4);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetLatencyTimer", ftStatus ); return false; }
    
    ftStatus = FT_SetUSBParameters(m_FtdiHandle, USB3XXX_MAXPACKETSIZE, 0);
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetUSBParameters", ftStatus ); return false; }
    
    ftStatus = FT_SetTimeouts(m_FtdiHandle, 200, 200 );
    if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetTimeouts", ftStatus ); return false; }
    
    // done
    return true;
}
bool DYNA_initialize(FT_HANDLE ftHandleDYNA)
{

    FT_STATUS ft_status;

    ft_status = FT_ResetDevice(ftHandleDYNA);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetDataCharacteristics(ftHandleDYNA, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetFlowControl(ftHandleDYNA, FT_FLOW_NONE, (UCHAR)0, (UCHAR)0);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetLatencyTimer(ftHandleDYNA, LATENCY_TIME);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetUSBParameters(ftHandleDYNA, IN_TRASFER_SIZE, 0);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetTimeouts(ftHandleDYNA, 0, 0);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_Purge(ftHandleDYNA, FT_PURGE_RX|FT_PURGE_TX);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    ft_status = FT_SetBaudRate(ftHandleDYNA, 1000000);
    if( ft_status != FT_OK )
        goto DYNA_init_error;

    return true;

DYNA_init_error:
    qDebug() << "\n\nUSB2Dynamixel Initialization Error!";
    FT_Close(ftHandleDYNA);
    return false;
}
Esempio n. 18
0
bool CMmcUsbHndlBase::SetBaudrate(unsigned int ulBaudrate)
{

	if( !AreLibraryFunctionsLoaded() )
    {
		perror("Library not loaded");
        return false;
    }

	FT_STATUS ftStatus = FT_SetBaudRate(m_Handle, ulBaudrate);

	if( ftStatus != FT_OK )
	{
		std::string errormsg = GetFtStatusDescription(ftStatus);
		errormsg += ":FT_GetStatus";
		perror(errormsg.c_str());
	}

	return (FT_OK == ftStatus);
}
Esempio n. 19
0
/* SET PARAMETR USB*/
quint16 ftdiChip::setParametersUSB(quint64 Speed, quint8 DataBit, quint8 StopBit, quint8 Parity)
{
FT_STATUS ftStatus;

ftStatus=FT_SetBaudRate(hdUSB,Speed);
if (ftStatus){
    emit signalStatusError(tr("Error setting parameters <speed FTDI>"),true);
    return retErr;
    }
parameter.setSpeed(Speed);
ftStatus=FT_SetDataCharacteristics(hdUSB,(UCHAR)DataBit,(UCHAR)StopBit,(UCHAR)Parity);
if (ftStatus!=FT_OK){
    emit signalStatusError(tr("Error setting parameters <databit FTDI>"),true);
    return retErr;
    }
parameter.setDataBit(DataBit);
parameter.setStopBit(StopBit);
parameter.setParity(Parity);
emit signalStatusOk(tr("Parameters is successful!"));
return retOk;
}
int main()
{
    int i,n;
    unsigned char data[255 * 256];
    FT_HANDLE handle;
    DWORD bytes;

    /* Generate data for a single PWM 'throb' cycle */
    memset(data, 0, sizeof(data));
    for(i=1; i<128; i++) {
        /* Apply gamma correction to PWM brightness */
        n = (int)(pow((double)i / 127.0, 2.5) * 255.0);
        memset(&data[i * 255], LED1, n);         /* Ramp up */
        memset(&data[(256 - i) * 255], LED1, n); /* Ramp down */
    }   

    /* Copy data from first LED to others, offset as appropriate */
    n = sizeof(data) / 4;
    for(i=0; i<sizeof(data); i++)
    {
        if(data[i] & LED1) {
            data[(i + n    ) % sizeof(data)] |= LED2;
            data[(i + n * 2) % sizeof(data)] |= LED3;
            data[(i + n * 3) % sizeof(data)] |= LED4;
        }
    }   

    /* Initialize, open device, set bitbang mode w/5 outputs */
    if(FT_Open(0, &handle) != FT_OK) {
        puts("Can't open device");
        return 1;
    }
    FT_SetBitMode(handle, LED1 | LED2 | LED3 | LED4, 1);
    FT_SetBaudRate(handle, 9600);  /* Actually 9600 * 16 */

    /* Endless loop: dump precomputed PWM data to the device */
    for(;;) FT_Write(handle, &data, (DWORD)sizeof(data), &bytes);
}
Esempio n. 21
0
void
serial_init (char *port, int baud)
{
  char *chipname;

  switch (board_type)
    {
    case BT_powermeter:
      chipname = "FT232R PowerMeter";
      break;
    case BT_powermeterproto:
      chipname = "FT232R PowerMeter";
      break;
    default:
      chipname = "FT232R - R8C";
      break;
    }

  Fail (FT_OpenEx (chipname, FT_OPEN_BY_DESCRIPTION, &handle));
  if (verbose > 2)
    printf("handle: 0x%x\n", (int)handle);
  atexit (serial_close);

  ProgramMode (1);
  Reset (1);
  /* >3 mSec reset pulse */
  usleep(3*1000);

  Reset (0);

  Fail (FT_SetBaudRate (handle, 9600));
  Fail (FT_SetDataCharacteristics (handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE));
  FT_SetFlowControl (handle, FT_FLOW_NONE, 0, 0);

  /* 3 mSec delay to let board "wake up" after reset */
  usleep(3*1000);
}
Esempio n. 22
0
int main(int argc, char *argv[])
{
	int        retCode = -1; // Assume failure
	int        f = 0;
	FT_STATUS  ftStatus = FT_OK;
	FT_HANDLE  ftHandle = NULL;
	int        portNum = -1; // Deliberately invalid
	DWORD      bytesToWrite = 0;
	DWORD      bytesWritten = 0;
	int        inputRate = -1; // Entered on command line
	int        baudRate = 38400; // Rate to actually use
	int        rates[] = {50, 75, 110, 134, 150, 200, 
	                      300, 600, 1200, 1800, 2400, 4800, 
	                      9600, 19200, 38400, 57600, 115200, 
	                      230400, 460800, 576000, 921600};
	
	if (argc > 1)
	{
		sscanf(argv[1], "%d", &portNum);
	}
	
	if (portNum < 0)
	{
		// Missing, or invalid.  Just use first port.
		portNum = 0;
	}
	
	if (portNum > 16)
	{
		// User probably specified a baud rate without a port number
		printf("Syntax: %s [port number] [baud rate]\n", argv[0]);
		portNum = 0;
	}
	
	if (argc > 2)
	{
		sscanf(argv[2], "%d", &inputRate);

		for (f = 0; f < (int)(ARRAY_SIZE(rates)); f++)
		{
			if (inputRate == rates[f])
			{
				// User entered a rate we support, so we'll use it.
				baudRate = inputRate;
				break;
			}
		}
	}
	
	if (baudRate < 0)
		baudRate = 9600;
		
	printf("Trying FTDI device %d at %d baud.\n", portNum, baudRate);
	
	ftStatus = FT_Open(portNum, &ftHandle);
	if (ftStatus != FT_OK) 
	{
		printf("FT_Open(%d) failed, with error %d.\n", portNum, (int)ftStatus);
		printf("Use lsmod to check if ftdi_sio (and usbserial) are present.\n");
		printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n");
		goto exit;
	}

	assert(ftHandle != NULL);

	ftStatus = FT_ResetDevice(ftHandle);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_ResetDevice returned %d.\n", (int)ftStatus);
		goto exit;
	}
	
	ftStatus = FT_SetBaudRate(ftHandle, (ULONG)baudRate);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetBaudRate(%d) returned %d.\n", 
		       baudRate,
		       (int)ftStatus);
		goto exit;
	}
	
	ftStatus = FT_SetDataCharacteristics(ftHandle, 
	                                     FT_BITS_8,
	                                     FT_STOP_BITS_1,
	                                     FT_PARITY_NONE);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetDataCharacteristics returned %d.\n", (int)ftStatus);
		goto exit;
	}
	                          
	// Indicate our presence to remote computer
	ftStatus = FT_SetDtr(ftHandle);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetDtr returned %d.\n", (int)ftStatus);
		goto exit;
	}

	// Flow control is needed for higher baud rates
	ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetFlowControl returned %d.\n", (int)ftStatus);
		goto exit;
	}

	// Assert Request-To-Send to prepare remote computer
	ftStatus = FT_SetRts(ftHandle);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetRts returned %d.\n", (int)ftStatus);
		goto exit;
	}

	ftStatus = FT_SetTimeouts(ftHandle, 3000, 3000);	// 3 seconds
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_SetTimeouts returned %d\n", (int)ftStatus);
		goto exit;
	}

	bytesToWrite = (DWORD)(sizeof(testPattern) - 1); // Don't write string terminator
	
	ftStatus = FT_Write(ftHandle, 
	                    testPattern,
	                    bytesToWrite, 
	                    &bytesWritten);
	if (ftStatus != FT_OK) 
	{
		printf("Failure.  FT_Write returned %d\n", (int)ftStatus);
		goto exit;
	}
	
	if (bytesWritten != bytesToWrite)
	{
		printf("Failure.  FT_Write wrote %d bytes instead of %d.\n",
		       (int)bytesWritten,
		       (int)bytesToWrite);
		goto exit;
	}

	// Success
	retCode = 0;
	printf("Successfully wrote %d bytes\n", (int)bytesWritten);

exit:
	if (ftHandle != NULL)
		FT_Close(ftHandle);

	return retCode;
}
Esempio n. 23
0
void
serial_change_baud (int baud)
{
  Fail (FT_SetBaudRate (handle, baud));
}
	void main(void)
	{
		void Delay(void);                       // Delay function
		setvbuf(stdout,NULL,_IONBF,0);     		// Used to flush the IO buffers otherwise-
		setvbuf(stderr,NULL,_IONBF,0);     		//-eclipse terminal will behave strangely


		FT_HANDLE ft_handle;               		// Declaring the handle to the chip
		FT_STATUS ft_status;               		// Variable for storing status of the operation

		DWORD     BaudRate;                		// Variable to hold the baud rate
		char      TxByte;                       // Variable to hold the byte to be tx'ed
		DWORD     NoOfBytesWritten = 0;         // No of Bytes written to the port

		UCHAR     Mask;                         // for selecting which pins are input/output
		UCHAR     Mode;                         // Selects the mode of operation for the chip

		int i = 1;

		//-------------------------------  Opening the Connection to the chip -----------------------------------//

		ft_status = FT_Open(0,&ft_handle); 		// Opening a connection to the connected FT232RL chip

		if(ft_status == FT_OK)             		// Error checking for FT_Open()
			printf("Connection to the chip opened successfully\n");
		else
			printf("error in opening connection,Chip not connected or loose cable\n");

		//--------------------------------------------------------------------------------------------------------//




		//-------------------------------  Setting the baud rate of the chip -------------------------------------//

		BaudRate = 9600;                   		// Set BaudRate = 9600
		FT_SetBaudRate(ft_handle,BaudRate);		// Setting the baudrate for the chip for 9600bps

		if(ft_status == FT_OK)             		//Error checking for FT_SetBaudRate()
			printf("Baud rate set to %d\n",BaudRate);
		else
			printf("Error in setting baud rate\n");

		//--------------------------------------------------------------------------------------------------------//



		//--------------------------- Setting Data bits,Stop bits,Parity Bits ------------------------------------//

				ft_status = FT_SetDataCharacteristics( ft_handle, 		// Handle of the chip(FT232)
											           FT_BITS_8,       // No of Data bits = 8
											           FT_STOP_BITS_1,  // No of Stop Bits = 1
						                               FT_PARITY_NONE	// Parity = NONE
						                              );
				if(ft_status == FT_OK)
					printf("Format-> 8 DataBits,No Parity,1 Stop Bit (8N1)\n");
				else
					printf("Error in setting Data Format \n");


				//---------------- Setting Flow Control bits ------------------------------//

				ft_status = FT_SetFlowControl( ft_handle,		// Handle of the chip(FT232)
						                       FT_FLOW_NONE,    // No Flow control
						                       NULL,            // XON  = Null since no Flow control
						                       NULL             // XOFF = Null since no Flow control
						                      );

				if(ft_status == FT_OK)
				    printf("Flow Control = None \n");
				else
					printf("Error in setting Flow Control \n");






		//------------------------ Putting the Chip in CBUS bit Bang Mode ---------------------------------------//

		Mode = 0x20;                                     // Select Chip mode as CBUS Bit Bang

		//--------------------------------------------------------------------------------------------------------------------------------//
		//  Calculating the Mask Value in CBUS Bit Bang Mode                                                                              //
		//													                                                                              //
		//  +------------------------------------------------------+                                                                      //
		//  |<------------------- Mask(8bit) --------------------->|  Mask is an 8 bit Variable                                           //
		//  +-----------------------+------+-----------------------+                                                                      //
		//  |    Direction(4bits)   |      |   Data (4bits)        |  Upper Nibble controls the Direction,LowerNibble controls data bits  //
		//  +-----------------------+------+-----------------------+                                                                      //
		//  |  7  |  6  |  5  |  4  |      |  3  |  2  |  1  |  0  |                                                                      //
		//  +-----+-----+-----+-----+------+-----+-----+-----+-----+                                                                      //
		//  |CBUS3|CBUS2|CBUS1|CBUS0|      |CBUS3|CBUS2|CBUS1|CBUS0|  CBUS1 Connected to ~RE, CBUS2 Connected to DE                       //
		//  +-----+-----+-----+-----+------+-----+-----+-----+-----+                                                                      //
		//  |     |     |  __ |     |      |     |     | __  |     |                         __                                           //
		//  |  x  | DE  |  RE |  x  |      |  x  | DE  | RE  |  x  |  Data For TX,{ CBUS1 -> RE = High } and { CBUS2 -> DE = HIGH }       //
		//  +-----+-----+-----+-----+------+-----+-----+-----+-----+                                                                      //
		//  |  0  |  1  |  1  |  0  |      |  0  |  1  |  1  |  0  |  Direction For TX,{ CBUS1->RE = Output } and { CBUS2->DE = Output }  //
		//  +-----------------------+------+-----------------------+                                                                      //
		//  |                         0x66                         |                                                                      //
		//  +------------------------------------------------------+                                                                      //
		//                                                                                                                                //
        //--------------------------------------------------------------------------------------------------------------------------------//
 
 		Mask =  0x66;  //To activate the RE and DE signals for MAX485,
					   // ~RE -> High 
					   //  DE -> High
					   
		ft_status = FT_SetBitMode(ft_handle,Mask,Mode);  // Opening  Bit Bang Mode to put the chip in Transmit Mode

		if(ft_status == FT_OK)
		   	printf("Opening CBUS Bit Bang Mode successful\n");
		else
		   	printf("ERROR in Opening CBUS Bit Bang Mode\n");
		//--------------------------------------------------------------------------------------------------------//
	
		while(1)
		{
			//-------------------------------  Writing a byte to serial port -----------------------------------------//

			TxByte = 'A';
			ft_status = FT_Write( ft_handle,         // Handle to the chip
								  &TxByte,           // Variable Containing the byte to be Txed
							      sizeof(TxByte),    // sizeof(TxByte) = char
							      &NoOfBytesWritten  // No of Bytes written to the port
							     );

		    if(ft_status == FT_OK)             		 // Error checking for FT_Writee()
		       printf("%c written to the serial port at %d bps - [%d]\n",TxByte,BaudRate,i);
		    else
			   printf("Error in writing to port\n");
			 
			 i++;

		     //--------------------------------------------------------------------------------------------------------//
			 Delay();
		}


		FT_Close(ft_handle);                      // Closing the handle to the chip
		getch();

	}
Esempio n. 25
0
/* This internal function handles the actual FTDI init and memory alloc
   for the library, with graceful cleanup in all error cases.  Keeps
   subsequent TCopen() function simpler with regards to error handling. */
static TCstatusCode openAlloc(
  unsigned char s, /* Number of strands                  */
  int           p) /* Number of pixels in longest strand */
{
	/* Parameter validation was already done in TCopen(). */

	/* Function works from a presumed error condition progressing
	   toward success.  This makes the cleanup cases easier. */
	TCstatusCode status = TC_ERR_MALLOC;

	/* Size of pixelOutBuffer depends whether the serial clock is
	   provided by one of the CBUS pins or must be bit-banged via
	   software.  If using 8 strands, MUST use CBUS clock. */
	if(s >= TC_CBUS_CLOCK)
	{
		bytesPerPixel = 32;
		if(s > TC_CBUS_CLOCK) s -= TC_CBUS_CLOCK;
	} else
	{
		bytesPerPixel = 64;
	}

	/* All library memory use is handled in one big malloc.
	   The data types are sorted to avoid alignment issues.
	   pixelOutBuffer includes latch data at end. */
	if((pixelCurrent = (double *)malloc(
	    (s * p * sizeof(double)) +      /* pixelCurrent array + */
	    ((p+((p+63)/64)) * bytesPerPixel))))  /* pixelOutBuffer */
	{
		pixelOutBuffer = (unsigned char *)&pixelCurrent[s * p];

		/* Alloc successful.  Next phase... */
		status = TC_ERR_OPEN;

		/* Currently rigged for a single FTDI device,
		   and always index 0.  Might address this in
		   a future update if it's an issue. */
		if(FT_OK == FT_Open(0,&ftdiHandle))
		{
			status = TC_ERR_MODE;
			/* Currently hogs all pins as outputs,
			   whether they're used by strands or not. */
			if(FT_OK == FT_SetBitMode(ftdiHandle,255,1))
			{
				status = TC_OK; /* Tentative success */

				/* Try to set baud rate & divisor to non-
				   default values.  3090000 seems to be the
				   absolute max baud rate; even +1 more, and
				   it fails.  Failure of either of these
				   steps returns a warning but does not
				   abort; program can continue with default
				   baud rate setting.  FTDI docs suggest max
				   of 3000000; this may be pushing it. */
				if(FT_OK != FT_SetDivisor(ftdiHandle,1))
					status = TC_ERR_DIVISOR;
				if(FT_OK != FT_SetBaudRate(ftdiHandle,3090000))
					status = TC_ERR_BAUDRATE;

				/* Clear any lingering data in queue. */
				(void)FT_Purge(ftdiHandle,
				  FT_PURGE_RX | FT_PURGE_TX);

				return status; /* Success */
			}
			/* Else fatal error of some sort.
			   Clean up any interim results. */
			FT_Close(ftdiHandle);
		}
		ftdiHandle = NULL;
		free(pixelCurrent);
		pixelCurrent   = NULL;
		pixelOutBuffer = NULL;
	}
	return status; /* Fail */
}
Esempio n. 26
0
int main()
{
	char 	cBufWrite[BUF_SIZE];
	char * 	pcBufRead = NULL;
	char * 	pcBufLD[MAX_DEVICES + 1];
	char 	cBufLD[MAX_DEVICES][64];
	DWORD	dwRxSize = 0;
	DWORD 	dwBytesWritten, dwBytesRead;
	FT_STATUS	ftStatus;
	FT_HANDLE	ftHandle[MAX_DEVICES];
	int	iNumDevs = 0;
	int	i, j;
	int	iDevicesOpen;
	
	FT_DEVICE_LIST_INFO_NODE *devInfo;

	//
	// create the device information list
	//
	ftStatus = FT_CreateDeviceInfoList(&iNumDevs);
	if (ftStatus == FT_OK) {
	   printf("Number of devices is %d\n",iNumDevs);
	}

	//
	// allocate storage for list based on numDevs
	//
	devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*iNumDevs);

	//
	// get the device information list
	//
	ftStatus = FT_GetDeviceInfoList(devInfo,&iNumDevs);
	if (ftStatus == FT_OK) {
		for (i = 0; i < iNumDevs; 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",devInfo[i].ftHandle);
	   }
	} 	
	
	for(i = 0; i < MAX_DEVICES; i++) {
		pcBufLD[i] = cBufLD[i];
	}
	pcBufLD[MAX_DEVICES] = NULL;

	ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER);
//	ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
	
	if(ftStatus != FT_OK) {
		printf("Error: FT_ListDevices(%d)\n", ftStatus);
		return 1;
	}
	for(j = 0; j < BUF_SIZE; j++) {
		cBufWrite[j] = j;
	}
	
	for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ); i++) {
		printf("Device %d Serial Number - %s\n", i, cBufLD[i]);
	}
		
	for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) {
		/* Setup */
		if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK){
//		if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_DESCRIPTION, &ftHandle[i])) != FT_OK){
			/* 
				This can fail if the ftdi_sio driver is loaded
		 		use lsmod to check this and rmmod ftdi_sio to remove
				also rmmod usbserial
		 	*/
			printf("Error FT_OpenEx(%d), device\n", ftStatus, i);
			return 1;
		}
		
//		FT_SetDeadmanTimeout(ftHandle[i], 10000);
	
		iDevicesOpen++;
		if((ftStatus = FT_SetBaudRate(ftHandle[i], 9600)) != FT_OK) {
			printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", ftStatus, cBufLD[i]);
			break;
		}
		for(j = 0; j < BUF_SIZE; j++) {
			printf("cBufWrite[%d] = 0x%02X\n", j, cBufWrite[j]);
		}
		
		/* Write */
		if((ftStatus = FT_Write(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten)) != FT_OK) {
			printf("Error FT_Write(%d)\n", ftStatus);
			break;
		}
		sleep(1);
		
		/* Read */
		dwRxSize = 0;			
		while ((dwRxSize < BUF_SIZE) && (ftStatus == FT_OK)) {
			ftStatus = FT_GetQueueStatus(ftHandle[i], &dwRxSize);
		}
		if(ftStatus == FT_OK) {
			pcBufRead = (char*)realloc(pcBufRead, dwRxSize);
			memset(pcBufRead, 0xFF, dwRxSize);
			for(j = 0; j < dwRxSize; j++) {
				printf("b4 pcBufRead[%d] = 0x%02X\n", j, pcBufRead[j]);
			}
			if((ftStatus = FT_Read(ftHandle[i], pcBufRead, dwRxSize, &dwBytesRead)) != FT_OK) {
				printf("Error FT_Read(%d)\n", ftStatus);
			}
			else {
				printf("FT_Read = %d\n", dwBytesRead);
				for(j = 0; j < dwBytesRead; j++) {
					printf("aftr pcBufRead[%d] = 0x%02X\n", j, pcBufRead[j]);
				}
			}
		}
		else {
			printf("Error FT_GetQueueStatus(%d)\n", ftStatus);	
		}
	}

	iDevicesOpen = i;
	/* Cleanup */
	for(i = 0; i < iDevicesOpen; i++) {
		FT_Close(ftHandle[i]);
		printf("Closed device %s\n", cBufLD[i]);
	}

	if(pcBufRead)
		free(pcBufRead);
	return 0;
}
	void main(void)
	{
		FT_HANDLE ft_handle;               		// Declaring the handle to the chip
		FT_STATUS ft_status;               		// Variable for storing status of the operation
		DWORD     BaudRate;                		// Variable to hold the baud rate
		char      TxByte;                       // Variable to hold the byte to be tx'ed
		DWORD     NoOfBytesWritten = 0;         // No of Bytes written to the port



		//-------------------------------  Opening the Connection to the chip -----------------------------------//

		ft_status = FT_Open(0,&ft_handle); 		// Opening a connection to the connected FT232RL chip

		if(ft_status == FT_OK)             		// Error checking for FT_Open()
			printf("\n\n\tConnection to the chip opened successfully\n");
		else
			printf("\terror in opening connection,Chip not connected or loose cable\n");

		//--------------------------------------------------------------------------------------------------------//


		
		//-------------------------------  Setting the baud rate of the chip -------------------------------------//

		BaudRate = 9600;                   		// Set BaudRate = 9600
		FT_SetBaudRate(ft_handle,BaudRate);		// Setting the baudrate for the chip for 9600bps

		if(ft_status == FT_OK)             		//Error checking for FT_SetBaudRate()
			printf("\tBaud rate set to %d\n",BaudRate);
		else
			printf("\tError in setting baud rate\n");

		
		
		//--------------------------- Setting Data bits,Stop bits,Parity Bits ------------------------------------//

		ft_status = FT_SetDataCharacteristics( ft_handle, 		// Handle of the chip(FT232)
											   FT_BITS_8,       // No of Data bits = 8
											   FT_STOP_BITS_1,  // No of Stop Bits = 1
						                       FT_PARITY_NONE	// Parity = NONE
						                      );
		if(ft_status == FT_OK)
			printf("\tFormat-> 8 DataBits,No Parity,1 Stop Bit (8N1)\n");
		else
			printf("\tError in setting Data Format \n");


		//--------------------------------- Setting Flow Control bits -------------------------------------------//

		ft_status = FT_SetFlowControl( ft_handle,		// Handle of the chip(FT232)
				                       FT_FLOW_NONE,    // No Flow control
				                       NULL,            // XON  = Null since no Flow control
				                       NULL             // XOFF = Null since no Flow control
				                      );

		if(ft_status == FT_OK)
			printf("\tFlow Control = None \n");
		else
			printf("\tError in setting Flow Control \n");


		//-------------------------------  Writing a byte to serial port -----------------------------------------//

		TxByte = 'A';
		ft_status = FT_Write( ft_handle,         // Handle to the chip
							  &TxByte,           // Variable Containing the byte to be Txed
							  sizeof(TxByte),    // sizeof(TxByte) = char
							  &NoOfBytesWritten  // No of Bytes written to the port
							 );

		if(ft_status == FT_OK)             		 // Error checking for FT_Writee()
		    printf("\t'%c' written to the serial port at %d bps\n",TxByte,BaudRate);
		else
			printf("\tError in writing to port\n");

		//--------------------------------------------------------------------------------------------------------//


		FT_Close(ft_handle);                      // Closing the handle to the chip
		
		getch();
	}
static void FTClassicPort_setBaudRate(FTClassicPort *self,ULONG BaudRate){
	self->status = FT_SetBaudRate(self->handle, BaudRate);
}
Esempio n. 29
0
int openSerialPorts(int baud) {
  char * 	pcBufRead = NULL;
  char * 	pcBufLD[MAX_DEVICES + 1];
  DWORD	dwRxSize = 0;
  DWORD 	dwBytesWritten, dwBytesRead;
  FT_STATUS	ftStatus;
  int	iNumDevs = 0;
  int	i, j;
  int	iDevicesOpen;	
  unsigned char ucMode = 0x00;

  printf( "warning: opening up to %d ports and assuming all are Safecast devices.\n", MAX_DEVICES );
  printf( "todo: make this more selective and safer.\n" );

  for(i = 0; i < MAX_DEVICES; i++) {
    pcBufLD[i] = cBufLD[i];
  }
  pcBufLD[MAX_DEVICES] = NULL;
  
  ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER);
  
  if(ftStatus != FT_OK) {
    printf("Error: FT_ListDevices(%d)\n", ftStatus);
    return -1;
  }
  
  for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ); i++) {
    printf("Device %d Serial Number - %s\n", i, cBufLD[i]);
  }
  
  for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) {
    /* Setup */
    if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK){
      /* 
	 This can fail if the ftdi_sio driver is loaded
	 use lsmod to check this and rmmod ftdi_sio to remove
	 also rmmod usbserial
      */
      printf("Error FT_OpenEx(%d), device\n", ftStatus);
      return -1;
    }
    
    printf("Opened device %s\n", cBufLD[i]);
    
//    if(getandcheckCBUS(ftHandle[i]) ) {
 //     printf( "getandcheckCBUS failed, exiting.\n" );
 //     return -1;
 //   }
    
    iDevicesOpen++;
    if((ftStatus = FT_SetBaudRate(ftHandle[i], baud)) != FT_OK) {
      printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", ftStatus, cBufLD[i]);
      break;
    }

    if((ftStatus = FT_SetDataCharacteristics(ftHandle[i], FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_EVEN)) != FT_OK) {
      printf("Error FT_SetDataCharacteristics(%d) = %s\n", ftStatus, cBufLD[i]);
      break;
    }
    
    FT_SetTimeouts(ftHandle[i], 500, 500);	// 500 ms read/write timeout

  }
  
  iDevicesOpen = i;
  foundDevices = i; // record this in a global

  if(pcBufRead)
    free(pcBufRead);

  return 0; // we always use the 0th device for now
}
Esempio n. 30
0
int main(int argc, char **argv) {
	FILE *fd;
	FT_STATUS ftStatus = FT_OK;
	FT_HANDLE ftHandle;
	DWORD dwNumberOfDevices = 0;
//	int vid = 0x0403, pid = 0x6001;
//	int vid = 0x1781, pid = 0x0C31;
	int vid = 0x1781, pid = 0x0C30;

	if (argc < 2) {
		printf("Usage: %s filename\n", argv[0]);
		return 1;
	}

	FT_SetVIDPID(vid, pid);

	fd = fopen(argv[1], "r");
	if (!fd) {
		fclose(fd);
		printf("Could not open file\n");
		return 1;
	}

	ftStatus = FT_CreateDeviceInfoList(&dwNumberOfDevices);
	if (ftStatus == FT_OK) {
		bool found = false;
		for (int i = 0; i < (int)dwNumberOfDevices; i++) {

			FT_PROGRAM_DATA pData;
			char ManufacturerBuf[32];
			char ManufacturerIdBuf[16];
			char DescriptionBuf[64];
			char SerialNumberBuf[16];

			pData.Signature1 = 0x00000000;
			pData.Signature2 = 0xffffffff;
			pData.Version = 0x00000002;      // EEPROM structure with FT232R extensions
			pData.Manufacturer = ManufacturerBuf;
			pData.ManufacturerId = ManufacturerIdBuf;
			pData.Description = DescriptionBuf;
			pData.SerialNumber = SerialNumberBuf;

			ftStatus = FT_Open(i, &ftHandle);
			ftStatus = FT_EE_Read(ftHandle, &pData);
			if(ftStatus == FT_OK){
				if(pData.VendorId == vid && pData.ProductId == pid){
					found = true;
					break;
				}
			}
			FT_Close(ftHandle);
		}
		if (!found) {
			printf("Could not find TellStick (Duo)\n");
			return 1;
		}
	}

	if (pid == 0x0C31) {
		FT_SetBaudRate(ftHandle, 115200);
	} else {
		FT_SetBaudRate(ftHandle, 9600);
	}
	FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);

	std::string data = readHex(fd);

	FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);

	printf("Reboot TellStick...");
	ftStatus = FT_SetBitMode(ftHandle, 0xff, 0x20);
	sleep(1);
	printf("Done\n");
	ftStatus = FT_SetBitMode(ftHandle, 0xf0, 0x20);

	printf("Waiting for TellStick Duo Bootloader.\n");
	waitFor(ftHandle, 'g');
	send(ftHandle, 'r');


	printf("Uploading hex-file\n");
	uploadHex(ftHandle, data);

	printf("Rebooting TellStick\n");
	waitFor(ftHandle, 'b');
 	send(ftHandle, 0x00);

	printf("Firmware updated!\n");

	FT_Close(ftHandle);
	fclose(fd);
}