示例#1
0
BOOL openChannel( FT_HANDLE ftHandle, int nSpeed )
{
  char buf[80];
  unsigned long size;
  unsigned long retLen;
  
  // Set baudrate
  FT_Purge( ftHandle, FT_PURGE_RX );

  sprintf( buf, "S%d\r", nSpeed );
  size = 3;
  if ( !( FT_OK == FT_Write( ftHandle, buf, size, &retLen ) ) ) { 
    printf("Write failed\n");
    return FALSE;
  }
      
  // Open device
  FT_Purge( ftHandle, FT_PURGE_RX );
  strcpy( buf, "O\r" );
  size = 2;
  if ( !( FT_OK == FT_Write( ftHandle, buf, size, &retLen ) ) ) {
    printf("Write failed\n");
    return FALSE;
  }

  return TRUE;
}
示例#2
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;
}
示例#3
0
// USB 初期化
int InitUSB()
{
	FT_STATUS ftStatus;

	// USB をオープン
	ftStatus = FT_OpenEx("USBCONEMU", FT_OPEN_BY_DESCRIPTION, &ftHandle);
	if (ftStatus != FT_OK) return 1;

	// Output mode 設定
	ftStatus = FT_SetBitMode(ftHandle, 0x00, 0x40);
	if (ftStatus != FT_OK) return 1;

	// バッファサイズ設定
	ftStatus = FT_SetUSBParameters(ftHandle, MAX_BUFSIZE, 0);
	if (ftStatus != FT_OK) return 1;

	// デバイスリセット
	ftStatus = FT_ResetDevice(ftHandle);
	if (ftStatus != FT_OK) return 1;

	// バッファクリア
	ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);
	if (ftStatus != FT_OK) return 1;

	// Time Out 設定
	ftStatus = FT_SetTimeouts(ftHandle, NULL, NULL);
	if (ftStatus != FT_OK) return 1;

	Sleep(150);
	return 0;
}
示例#4
0
文件: d2xx.c 项目: GBert/openwrt-misc
void
serial_drain ()
{
  FT_Purge (handle, FT_PURGE_RX);
  if (verbose > 2)
    printf("\033[31m(D)\033[0m");
}
示例#5
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
}
示例#6
0
文件: libgecko.c 项目: 111X/radare
int gecko_opendevice()
{
	// Open by Serial Number
	status = FT_OpenEx("GECKUSB0", FT_OPEN_BY_SERIAL_NUMBER, &fthandle);
	if(status != FT_OK){
		eprintf("Error: Couldn't connect to USB Gecko. Please check Installation\n");
		return 0;
	}
	// Reset the Device
	status = FT_ResetDevice(fthandle);
	if(status != FT_OK){
		eprintf("Error: Couldnt Reset Device %d\n",status);
		status = FT_Close(fthandle);
		return 0;
	}
	// Set a 3 second timeout for this example
	status = FT_SetTimeouts(fthandle,3000,3000);
	if(status != FT_OK){
		eprintf("Error: Timeouts failed to set %d\n",status);
		status = FT_Close(fthandle);
		return 0;
	}
	// Purge RX buffer
	status = FT_Purge(fthandle,FT_PURGE_RX);
	if(status != FT_OK){
		eprintf("Error: Problem clearing buffers %d\n",status);
		status = FT_Close(fthandle);
		return 0;
	}
	// Purge TX buffer
	status = FT_Purge(fthandle,FT_PURGE_TX);
	if(status != FT_OK){
		eprintf("Error: Problem clearing buffers %d\n",status);
		status = FT_Close(fthandle);
		return 0;
	}
	// Set packet size in bytes - 65536 packet is maximum packet size (USB 2.0)
	status = FT_SetUSBParameters(fthandle,65536,0);
	if(status != FT_OK){
		eprintf("Error: Couldnt Set USB Parameters %d\n",status);
		status = FT_Close(fthandle);
		return 0;
	}
	// take breath
	sleep(1);
	return 1;
}
示例#7
0
void getSerialNumber( FT_HANDLE ftHandle )
{
  FT_STATUS status;
  char buf[80];
  char c;
  char *p;
  unsigned long nBytesWritten;
  unsigned long eventStatus;
  unsigned long nRxCnt;// Number of characters in receive queue
  unsigned long nTxCnt;// Number of characters in transmit queue

  memset( buf, 0, sizeof( buf ) );
  printf("Get serial number.\n");
  printf("==================\n");

  FT_Purge( ftHandle, FT_PURGE_RX | FT_PURGE_TX );

  sprintf( buf, "N\r" );
  if ( FT_OK != ( status = FT_Write( ftHandle, buf, strlen( buf ), &nBytesWritten ) ) ) {
    printf("Error: Failed to write command. return code = %d\n", status );
    return;
  }
  
  // Check if there is something to receive
  while ( 1 ){

    if ( FT_OK == FT_GetStatus( ftHandle, &nRxCnt, &nTxCnt, &eventStatus ) ) {
      
      // If there are characters to receive
      if ( nRxCnt ) {
	
	if ( FT_OK != ( status = FT_Read( ftHandle, buf, nRxCnt, &nBytesWritten ) ) ) {
	  printf("Error: Failed to read data. return code = %d\n", status );
	  return;
	}
	
	p = buf;
	while ( *p ) {
	  if ( 0x0d == *p ) {
	    *p = 0;
	    break;
	  }
	  p++;
	}
      
	printf( "Serial = %s \n", buf  );
	break;
	
      }
      
    }
    else {
      printf("Error: Failed to get status. return code = %d\n", status );
      return;
    }

  }
  
}
示例#8
0
extern "C" void	InitPort (void)
{
	if (ParPort == -1)
	{
		DWORD modemWord = 0;
        usb_timeout_error = FALSE;
		ftStatus = FT_GetModemStatus (ftHandleB, &modemWord);
		if (ftStatus != FT_OK)
		{
			MessageBox(topHWnd, "USB Error: Get Power Status Failed!", "InitPort", MB_OK | MB_ICONERROR);
			return;
		}

		if (modemWord & 0x80)
			MessageBox(topHWnd, "USB CopyNES is OFF - turn on then hit OK", "InitPort", MB_OK | MB_ICONERROR);

		// flush receive/transmit buffers
		ftStatus = FT_Purge (ftHandleA, FT_PURGE_RX | FT_PURGE_TX);
		if (ftStatus != FT_OK)
		{
			MessageBox(topHWnd, "USB Error: Purge A Failed!", "InitPort", MB_OK | MB_ICONERROR);
			return;
		}

		ftStatus = FT_Purge (ftHandleB, FT_PURGE_RX | FT_PURGE_TX);
		if (ftStatus != FT_OK)
		{
			MessageBox(topHWnd, "USB Error: Purge B Failed!", "InitPort", MB_OK | MB_ICONERROR);
			return;
		}
		connection_initialized = TRUE;
	}
	else
	{
		if ((prECP() & 0x03) == 0x01)
		{
			pwECP(0x34);
			if (prECP() == 0x35)
				pwECP((prECP() & 0x1F) | 0x20);	// try to set ECP port to bidirectional mode
		}
		shadow |= 0x40;	// pull ACK line high - this way, printers won't listen to us
		pwControl(shadow);
		c = prStatus();
		connection_initialized = TRUE;
	}
}
示例#9
0
文件: usb.cpp 项目: meierb/psi46test
void CUSB::Clear()
{
	if (!isUSB_open) return;

	ftStatus = FT_Purge(ftHandle, FT_PURGE_RX|FT_PURGE_TX);
	m_posR = m_sizeR = 0;
	m_posW = 0;
}
示例#10
0
static int presto_close(void)
{

	int result = ERROR_OK;

#if BUILD_PRESTO_FTD2XX == 1
	DWORD ftbytes;

	if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
		return result;

	presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Write(presto->handle,
			&presto_init_seq,
			sizeof(presto_init_seq),
			&ftbytes);
	if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_SetLatencyTimer(presto->handle, 16);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Close(presto->handle);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;

#elif BUILD_PRESTO_LIBFTDI == 1

	presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
	if (presto->retval != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_usb_close(&presto->ftdic);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		ftdi_deinit(&presto->ftdic);
#endif

	return result;
}
示例#11
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);
}
示例#12
0
BOOL closeChannel( FT_HANDLE ftHandle )
{
  char buf[80];
  unsigned long size;
  unsigned long retLen;

  // Close device
  FT_Purge( ftHandle, FT_PURGE_RX | FT_PURGE_TX );
  strcpy( buf, "C\r" );
  size = 2;
  if ( !( FT_OK == FT_Write( ftHandle, buf, size, &retLen ) ) ) { 
    return FALSE;
  }

  
  return TRUE;
}
示例#13
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;
}
示例#15
0
int dxl_hal_open()
{
	FT_STATUS ft_status;

	dxl_hal_close();

	ft_status = FT_Open( 1, &ghFt_Handle );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_ResetDevice( ghFt_Handle );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_SetDataCharacteristics( ghFt_Handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_SetFlowControl( ghFt_Handle, FT_FLOW_NONE, (UCHAR)0, (UCHAR)0 );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_SetLatencyTimer( ghFt_Handle, LATENCY_TIME );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_SetUSBParameters( ghFt_Handle, IN_TRASFER_SIZE, 0 );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_SetTimeouts( ghFt_Handle, 0, 0 );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	ft_status = FT_Purge( ghFt_Handle, FT_PURGE_RX|FT_PURGE_TX );
	if( ft_status != FT_OK )
		goto DXL_HAL_OPEN_ERROR;

	return 1;

DXL_HAL_OPEN_ERROR:
	dxl_hal_close();
	return 0;
}
示例#16
0
BOOL SendTestFrames( FT_HANDLE ftHandle )
{
  CANMsg msg;

  if ( !openChannel( ftHandle, 6 ) ) {
    printf("Failed to open channel\n");
    return FALSE;
  }

  // Send extended frame
  msg.flags = CANMSG_EXTENDED;
  msg.id = 0x123456;
  msg.len = 2;
  msg.data[0] = 1;
  msg.data[1] = 2;

  if ( !sendFrame( ftHandle, &msg ) ) {
    printf("Failed to send frame\n");
    return FALSE;
  }

  // Send standard frame
  msg.flags = 0;
  msg.id = 0x123;
  msg.len = 2;
  msg.data[0] = 1;
  msg.data[1] = 2;

  if ( !sendFrame( ftHandle, &msg ) ) {
    printf("Failed to send frame\n");
    return FALSE;
  }
  

  FT_Purge( ftHandle, FT_PURGE_TX );

  if ( !closeChannel( ftHandle ) ) {
    printf("Failed to close channel\n");
    return FALSE;
  }
  
  return TRUE;
}
示例#17
0
void TellStick::enterBootloader() {
	FT_Purge(d->ftHandle, FT_PURGE_RX | FT_PURGE_TX);

	FT_STATUS ftStatus = FT_SetBitMode(d->ftHandle, 0xff, 0x20);
	for (int i = 0; i < 100; ++i) {
		msleep(1);
		QApplication::processEvents();
	}
	ftStatus = FT_SetBitMode(d->ftHandle, 0xf0, 0x20);

	char buf = 0;
	while(1) { //TODO: Make it possible to bail!
		QApplication::processEvents();
		buf = getCh();
		if (buf == 'g') {
			break;
		}
	}
	send('r');
	setUpgradeStep(3);
	QTimer::singleShot(1000, this, SLOT(downloadFirmware()));
}
示例#18
0
文件: p9813.c 项目: TELE-TWIN/p9813
/* 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 */
}
示例#19
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);
}
示例#20
0
int CKMotionIO::FlushInputBuffer()
{
	FT_STATUS ftStatus;
	DWORD RxBytes;
	DWORD TxBytes;
	DWORD BytesReceived;
	DWORD BytesWritten;
	DWORD EventDWord;
	char s[10];
	char RxBuffer[500];

	ftStatus = FT_Purge(ftHandle,FT_PURGE_RX|FT_PURGE_TX);
	if (ftStatus != FT_OK) 	return 1;


	// discard any data in the read queue in the driver

	DWORD t0=timeGetTime();
	do
	{
		ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
		if (ftStatus != FT_OK) 
			return 1;

		if (RxBytes > 0)
		{
			if (RxBytes > 400) RxBytes=400;
			ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
		}
	}
	while (RxBytes > 0 && timeGetTime()-t0 < CONNECT_TIMEOUT);



	if (SendAbortOnConnect)
	{
		// send flush command to DSP

		s[0]=ABORT_CHAR;
		s[1]=0;


		ftStatus = FT_Write(ftHandle, s, 1, &BytesWritten);
		if (ftStatus != FT_OK) 
			return 1;
		if (BytesWritten != 1) 
			return 1;


		// wait and be sure chars are transmitted

		t0=timeGetTime();
		do
		{
			ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
			if (ftStatus != FT_OK) 
				return 1;
		}
		while (TxBytes != 0 && timeGetTime()-t0 < CONNECT_TIMEOUT);

		if (TxBytes != 0) return 1;


		// wait for a fixed time for the abort acknowledge
		// to come back which is exactly 3 characters ESC C \r

		t0=timeGetTime();

		do
		{
			ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
			if (ftStatus != FT_OK) 
				return 1;
		}
		while (RxBytes < 3 && timeGetTime()-t0 < CONNECT_TIMEOUT);


		if (RxBytes == 0)
		{
			// KMotion seems to be present but not responding
			// after two attemps flag as non responsive and
			// stop trying

			NonRespondingCount++;

			if (NonRespondingCount == 2)
			{
				ErrorMessageBox("KMotion present but not responding\r\r"
								"Correct problem and restart application");
			}
			return 1;
		}
		
		if (RxBytes != 3) return 1;

		NonRespondingCount=0;

		// read the 3 bytes

		ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
		
		if (ftStatus != FT_OK) return 1; 
		if (BytesReceived != 3) return 1; 

		if (RxBuffer[0] != '\x1b') return 1; 
		if (RxBuffer[1] != 'C') return 1; 
		if (RxBuffer[2] != '\r') return 1; 
	}

	// verify there are no transmit or receive characters

	ftStatus=FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
	if (ftStatus != FT_OK) return 1;
	if (RxBytes != 0) return 1; 
	if (TxBytes != 0) return 1; 
	
	// OK looks like we are in sync

	return 0;
}
示例#21
0
static int presto_open_ftd2xx(char *req_serial)
{
	uint32_t i;
	DWORD numdevs;
	DWORD vidpid;
	char devname[FT_DEVICE_NAME_LEN];
	FT_DEVICE device;

	BYTE presto_data;
	DWORD ftbytes;

	presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;

#if IS_WIN32 == 0
	/* Add non-standard Vid/Pid to the linux driver */
	presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID);
	if (presto->status != FT_OK) {
		LOG_ERROR("couldn't add PRESTO VID/PID");
		exit(-1);
	}
#endif

	presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY);
	if (presto->status != FT_OK) {
		LOG_ERROR("FT_ListDevices failed: %s", ftd2xx_status_string(presto->status));
		return ERROR_JTAG_DEVICE_ERROR;
	}

	LOG_DEBUG("FTDI devices available: %" PRIu32, (uint32_t)numdevs);
	for (i = 0; i < numdevs; i++) {
		presto->status = FT_Open(i, &(presto->handle));
		if (presto->status != FT_OK) {
			/* this is not fatal, the device may be legitimately open by other process,
			 *hence debug message only */
			LOG_DEBUG("FT_Open failed: %s", ftd2xx_status_string(presto->status));
			continue;
		}
		LOG_DEBUG("FTDI device %i open", (int)i);

		presto->status = FT_GetDeviceInfo(presto->handle, &device,
				&vidpid, presto->serial, devname, NULL);
		if (presto->status == FT_OK) {
			if (vidpid == PRESTO_VID_PID && (req_serial == NULL ||
					!strcmp(presto->serial, req_serial)))
				break;
		} else
			LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string(
					presto->status));

		LOG_DEBUG("FTDI device %i does not match, closing", (int)i);
		FT_Close(presto->handle);
		presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
	}

	if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
		return ERROR_JTAG_DEVICE_ERROR;	/* presto not open, return */

	presto->status = FT_SetLatencyTimer(presto->handle, 1);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_SetTimeouts(presto->handle, 100, 0);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	presto_data = 0xD0;
	presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	/* delay between first write/read turnaround (after purge?) necessary
	 * under Linux for unknown reason,
	 * probably a bug in library threading */
	usleep(100000);
	presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	if (ftbytes != 1) {
		LOG_DEBUG("PRESTO reset");

		presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;
		presto->status = FT_SetBitMode(presto->handle, 0x80, 1);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;
		presto->status = FT_SetBaudRate(presto->handle, 9600);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		presto_data = 0;
		for (i = 0; i < 4 * 62; i++) {
			presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
			if (presto->status != FT_OK)
				return ERROR_JTAG_DEVICE_ERROR;
		}
		usleep(100000);

		presto->status = FT_SetBitMode(presto->handle, 0x00, 0);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		presto_data = 0xD0;
		presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		/* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
		   probably a bug in library threading */
		usleep(100000);
		presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
		if (presto->status != FT_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		if (ftbytes != 1) {
			LOG_DEBUG("PRESTO not responding");
			return ERROR_JTAG_DEVICE_ERROR;
		}
	}

	presto->status = FT_SetTimeouts(presto->handle, 0, 0);
	if (presto->status != FT_OK)
		return ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Write(presto->handle, &presto_init_seq,
			sizeof(presto_init_seq), &ftbytes);

	if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
		return ERROR_JTAG_DEVICE_ERROR;

	return ERROR_OK;
}
示例#22
0
bool FTDIDMXDevice::open()
{
	// Change QString to char* (not const char* note)

	char *serial;
	QByteArray a = m_path.toLatin1();

	serial = (char*)malloc(sizeof(char) * (a.count() + 1));
	memcpy(serial, a.constData(), a.count());
	serial[a.count()] = 0;

#ifndef WIN32
	// Windows users cannot dynamiccaly set VID/PID of harward
	if (FT_SetVIDPID(m_vid, m_pid) == FT_OK && 
	    FT_OpenEx(serial, FT_OPEN_BY_SERIAL_NUMBER, &m_handle) == FT_OK)
	{
#else
	if (FT_OpenEx(serial, FT_OPEN_BY_SERIAL_NUMBER, &m_handle) == FT_OK)
	{
#endif
		free(serial);
		if (!FT_SUCCESS(FT_ResetDevice(m_handle)))
		{
			qWarning() << "Unable to reset FTDI device" << m_path;
			return false;
		}

		// Set the baud rate 12 will give us 250Kbits
		if (!FT_SUCCESS(FT_SetDivisor(m_handle, 12)))
		{
			qWarning() << "Unable to set divisor on FTDI device"
				   << m_path;
			return false;
		}

		// Set the data characteristics
		if (!FT_SUCCESS(FT_SetDataCharacteristics(m_handle,
							  FT_BITS_8,
							  FT_STOP_BITS_2,
							  FT_PARITY_NONE)))
		{
			qWarning() << "Unable to set data characteristics on"
				   << "FTDI device" << m_path;
			return false;
		}

		// Set flow control
	 	if (!FT_SUCCESS(FT_SetFlowControl(m_handle, FT_FLOW_NONE, 0, 0)))
	 	{
			qWarning() << "Unable to set flow control on"
				   << "FTDI device" << m_path;
			return false;
		}

		// set RS485 for sendin
		FT_ClrRts(m_handle);

		// Clear TX RX buffers
		FT_Purge(m_handle,FT_PURGE_TX | FT_PURGE_RX);

		m_threadRunning = true;
		start(QThread::TimeCriticalPriority);

		return true;
	}
	else
	{
		qWarning() << "Unable to open FTDIDMX"
			   << m_output << ":" << serial;
		free(serial);
		return false;
	}
}

bool FTDIDMXDevice::close()
{
	// Kill thread
	m_threadRunning = false;
	wait(500);

	FT_Close(m_handle);
	return true;
}
示例#23
0
void FtdiDevices::purge() {
  FT_Purge(m_ftHandleA, FT_PURGE_TX | FT_PURGE_RX);
}
示例#24
0
文件: fld_ctrl.c 项目: avasilje/btcar
int dev_open_uart (int n_dev_indx, FT_HANDLE *ph_device)
{

    FT_STATUS ft_status;
    DWORD   dw_num_devs;
    LONG    devLocation;


    ft_status = FT_ListDevices(&dw_num_devs, NULL, FT_LIST_NUMBER_ONLY);
    if (ft_status != FT_OK) return FALSE;

    if (dw_num_devs == 0){
        // No devices were found
        return FALSE;
    }

    ft_status = FT_ListDevices((void*)n_dev_indx, &devLocation, FT_LIST_BY_INDEX | FT_OPEN_BY_LOCATION);
    if (ft_status != FT_OK) {
        return FALSE;
    }

    ft_status |= FT_ListDevices((void*)n_dev_indx, &devDescriptor, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
    ft_status |= FT_ListDevices((void*)n_dev_indx, &devSerial, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER);

    if (ft_status != FT_OK){
        return FALSE;
    }
#define FT_Classic  0

#if FT_Classic
    ft_status |= FT_OpenEx((void*)devLocation, FT_OPEN_BY_LOCATION, ph_device);

    ft_status |= FT_SetTimeouts(*ph_device, 500, 500);
    ft_status |= FT_SetLatencyTimer(*ph_device, 2);

    // Divisor selection
    // BAUD = 3000000 / Divisor
    // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875)
    // Divisor = 24 ==> Baud 125000 
    ft_status |= FT_SetDivisor(*ph_device, 3000000 / 125000);

    // Set UART format 8N1
    ft_status |= FT_SetDataCharacteristics(*ph_device, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);

    if (ft_status != FT_OK){
        return FALSE;
    }


    // Just in case
    FT_Purge(*ph_device, FT_PURGE_TX | FT_PURGE_RX);
#else


    // Open a device for overlapped I/O using its serial number
    *ph_device = FT_W32_CreateFile(
        (LPCTSTR)devLocation,
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_LOCATION,
        0);

    if (*ph_device == INVALID_HANDLE_VALUE)
    {
        // FT_W32_CreateDevice failed
        return FALSE;
    }

    // ----------------------------------------
    // --- Set comm parameters
    // ----------------------------------------

    FTDCB ftDCB;
    FTTIMEOUTS    ftTimeouts;
    FTCOMSTAT    ftPortStatus;
    DWORD   dw_port_error;

    if (!FT_W32_GetCommState(*ph_device, &ftDCB))
    {
        return FALSE;
    }

    // Divisor selection
    // BAUD = 3000000 / Divisor
    // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875)
    // Divisor = 24 ==> Baud 125000 

    ftDCB.BaudRate = 38400;
    ftDCB.fBinary = TRUE;                       /* Binary Mode (skip EOF check)    */
    ftDCB.fParity = FALSE;                      /* Enable parity checking          */
    ftDCB.fOutxCtsFlow = FALSE;                 /* CTS handshaking on output       */
    ftDCB.fOutxDsrFlow = FALSE;                 /* DSR handshaking on output       */
    ftDCB.fDtrControl = DTR_CONTROL_DISABLE;    /* DTR Flow control                */
    ftDCB.fTXContinueOnXoff = FALSE;

    ftDCB.fErrorChar = FALSE;            // enable error replacement 
    ftDCB.fNull = FALSE;                // enable null stripping 
    ftDCB.fRtsControl = RTS_CONTROL_DISABLE;       // RTS flow control 
    ftDCB.fAbortOnError = TRUE;            // abort reads/writes on error 

    ftDCB.fOutX = FALSE;                        /* Enable output X-ON/X-OFF        */
    ftDCB.fInX = FALSE;                         /* Enable input X-ON/X-OFF         */
    ftDCB.fNull = FALSE;                        /* Enable Null stripping           */
    ftDCB.fRtsControl = RTS_CONTROL_DISABLE;    /* Rts Flow control                */
    ftDCB.fAbortOnError = TRUE;                 /* Abort all reads and writes on Error */

    // 8N1
    ftDCB.ByteSize = 8;                 /* Number of bits/byte, 4-8        */
    ftDCB.Parity = NOPARITY;            /* 0-4=None,Odd,Even,Mark,Space    */
    ftDCB.StopBits = ONESTOPBIT;        /* 0,1,2 = 1, 1.5, 2               */

    if (!FT_W32_SetCommState(*ph_device, &ftDCB))
    {
        return FALSE;
    }

    FT_W32_GetCommState(*ph_device, &ftDCB);

    // Set serial port Timeout values
    FT_W32_GetCommTimeouts(*ph_device, &ftTimeouts);

    ftTimeouts.ReadIntervalTimeout = 0;
    ftTimeouts.ReadTotalTimeoutMultiplier = 0;
    ftTimeouts.ReadTotalTimeoutConstant = 200;
    ftTimeouts.WriteTotalTimeoutConstant = 0;
    ftTimeouts.WriteTotalTimeoutMultiplier = 0;

    FT_W32_SetCommTimeouts(*ph_device, &ftTimeouts);

    FT_W32_ClearCommError(*ph_device, &dw_port_error, &ftPortStatus);
    FT_W32_PurgeComm(*ph_device, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

#endif  // End of W32 device init

    return TRUE;
}
示例#25
0
void dxl_hal_clear(void)
{
	FT_Purge( ghFt_Handle, FT_PURGE_RX );
}
示例#26
0
void image_transfer(FILE *fp, ftdi_context_t *c, u8 dump, u8 type, u32 addr, u32 size)
{	
	u32		ram_addr = addr;
	int		bytes_left = size;
	int		bytes_done = 0;
	int		bytes_do;
	int		trunc_flag = 0;
	int		i; 
	int		chunk = 0;
        struct timespec time_start;
        struct timespec time_stop;
        double          time_duration;
        time_t          time_diff_seconds;
        long            time_diff_nanoseconds;
	dev_cmd_resp_t	r;
  
	// make sure handle is valid
	if(!c->handle) die(err[DEV_ERR_NULL_HANDLE], __FUNCTION__);

	// decide a better, more optimized chunk size
	if(size > 16 * 1024 * 1024) 
		chunk = 32;
	else if( size > 2 * 1024 * 1024)
		chunk = 16;
	else 
		chunk = 4;

	// convert to megabytes
	chunk *= 128 * 1024;
	if(c->verbose) _printf(info[INFO_CHUNK], CHUNK_SIZE);
	if(c->verbose) _printf(info[INFO_OPT_CHUNK], chunk);

	// get initial time count
// 	QueryPerformanceFrequency(&time_freq);
// 	QueryPerformanceCounter(&time_start);
        clock_gettime(CLOCK_MONOTONIC, &time_start);

	while(1){
		if(bytes_left >= chunk) 
			bytes_do = chunk;
		else
			bytes_do = bytes_left;
		if(bytes_do % 512 != 0) {
			trunc_flag = 1;
			bytes_do -= (bytes_do % 512);
		}
		if(bytes_do <= 0) break;
		
		for(i = 0; i < 2; i++){
			if(i == 1) {
				printf("\n");
				_printf("Retrying\n");

				FT_ResetPort(c->handle);
				FT_ResetDevice(c->handle);
				_printf("Retrying FT_ResetDevice() success\n");
				// set synchronous FIFO mode
				//FT_SetBitMode(c->handle, 0xff, 0x40);
				_printf("Retrying FT_SetBitMode() success\n");
				FT_Purge(c->handle, FT_PURGE_RX | FT_PURGE_TX);
				_printf("Retrying FT_Purge() success\n");

			}
			device_sendcmd(c, &r, dump ? DEV_CMD_DUMPRAM : DEV_CMD_LOADRAM, 2, 0, 1, 
				ram_addr, (bytes_do & 0xffffff) | type << 24);

			if(dump){
				c->status = FT_Read(c->handle, buffer, bytes_do, &c->bytes_written);
				fwrite(buffer, bytes_do, 1, fp);
			}else{
				fread(buffer, bytes_do, 1, fp);
				c->status = FT_Write(c->handle, buffer, bytes_do, &c->bytes_written);
			}
			if(c->bytes_written) break;
		}
		// check for a timeout
		if(c->bytes_written == 0) die(err[DEV_ERR_TIMED_OUT], __FUNCTION__);
		// dump success response
		c->status = FT_Read(c->handle, buffer, 4, &c->bytes_read);

		bytes_left -= bytes_do;
		bytes_done += bytes_do;
		ram_addr += bytes_do;

		// progress bar
		prog_draw(bytes_done, size);

		c->status = FT_GetStatus(c->handle, &c->bytes_read, &c->bytes_written, &c->event_status);
	}
	// stop the timer
// 	QueryPerformanceCounter(&time_stop);
        clock_gettime(CLOCK_MONOTONIC, &time_stop);
        // get the difference of the timer
        time_diff_seconds = time_stop.tv_sec - time_start.tv_sec;
        time_diff_nanoseconds = time_stop.tv_nsec - time_start.tv_nsec;
	time_duration = (double)time_diff_seconds + (double)(time_diff_nanoseconds/1000000000.0f);
        
	// erase progress bar
	prog_erase();
	if(c->verbose && trunc_flag) 
		_printf(info[INFO_TRUNCATED]);
	if(c->verbose) 
		_printf(info[INFO_COMPLETED_TIME],	time_duration, (float)size/1048576.0f/(float)time_duration);
}
void FTDI_PurgeBuffer( NxDeviceDmxUtil * mDevice )
{
	FT_Purge (mDevice->device_handle,FT_PURGE_TX);
	FT_Purge (mDevice->device_handle,FT_PURGE_RX);
}
示例#28
0
bool CApoxObj::open( const char *szFileName, unsigned long flags )
{
	const char *p;
	unsigned long busspeed = 125;
	m_emergencyInfo = 0;
	char szDrvParams[ MAX_PATH ];
	m_initFlag = flags;

	m_RxMsgState = USB_IDLE;

	// save parameter string and conbert to upper case
	strncpy( szDrvParams, szFileName, MAX_PATH );
	_strupr( szDrvParams );

	// Initiate statistics
	m_stat.cntReceiveData = 0;
	m_stat.cntReceiveFrames = 0;
	m_stat.cntTransmitData = 0;
	m_stat.cntTransmitFrames = 0;

	m_stat.cntBusOff = 0;
	m_stat.cntBusWarnings = 0;
	m_stat.cntOverruns = 0;

	// if open we have noting to do
	if ( m_bRun ) return true;
		
	// serial
	p = strtok( szDrvParams, ";" );
	if ( NULL != p ) {		
		strcpy( m_SerialNumber, p );	
	}

	// Bus-Speed
	p = strtok( NULL, ";" );
	if ( NULL != p ) {		
		if ( ( NULL != strstr( p, "0x" ) ) || ( NULL != strstr( p, "0X" ) )  ) {
			sscanf( p + 2, "%x", &busspeed );
		}
		else {
			busspeed = atol( p );
		}
	}

	// Handle busspeed
	uint8_t nSpeed = CAN_BAUD_1000;
	switch ( busspeed ) {

		case 125:
			nSpeed = CAN_BAUD_125;
			break;

		case 250:
			nSpeed = CAN_BAUD_250;
			break;

		case 500:
			nSpeed = CAN_BAUD_500;
			break;

		case 1000:
			nSpeed = CAN_BAUD_1000;
			break;

		default:
			nSpeed = CAN_BAUD_125;
			break;

	}


	FT_STATUS ftStatus;
	ftStatus = FT_OpenEx( (void *)m_SerialNumber, 
							FT_OPEN_BY_SERIAL_NUMBER, 
							&m_ftHandle );
	if ( !FT_SUCCESS( ftStatus ) ) return false;
		
	ftStatus = FT_ResetDevice( m_ftHandle );
	ftStatus = FT_Purge( m_ftHandle, FT_PURGE_RX | FT_PURGE_TX );
	ftStatus = FT_SetTimeouts(m_ftHandle, 378, 128);
	ftStatus = FT_SetUSBParameters ( m_ftHandle, 2048, 2048 );
	ftStatus = FT_SetLatencyTimer( m_ftHandle, 3 );
    
	
	// Run run run ..... 

	m_bRun = true;

#ifdef WIN32
	
	// Start write thread 
	DWORD threadId;
	if ( NULL == 
			( m_hTreadTransmit = CreateThread(	NULL,
										0,
										(LPTHREAD_START_ROUTINE) workThreadTransmit,
										this,
										0,
										&threadId ) ) ) { 
		// Failure
		close();
		return false;
	}

	// Start read thread 
	if ( NULL == 
			( m_hTreadReceive = CreateThread(	NULL,
										0,
										(LPTHREAD_START_ROUTINE) workThreadReceive,
										this,
										0,
										&threadId ) ) ) { 
		// Failure
		close();
		return  false;
	}
	
	// Release the mutex
	UNLOCK_MUTEX( m_apoxMutex );
	UNLOCK_MUTEX( m_receiveMutex );
	UNLOCK_MUTEX( m_transmitMutex );
	UNLOCK_MUTEX( m_responseMutex );

#else // LINUX


	pthread_attr_t thread_attr;
	pthread_attr_init( &thread_attr );
	
	
	// Create the log write thread.
	if ( pthread_create( 	&m_threadId,
								&thread_attr,
								workThreadTransmit,
								this ) ) {	
							
		syslog( LOG_CRIT, "canallogger: Unable to create apoxdrv write thread.");
		rv = false;
		fclose( m_flog );
	}


	// Create the log write thread.
	if ( pthread_create( 	&m_threadId,
								&thread_attr,
								workThreadReceive,
								this ) ) {	
							
		syslog( LOG_CRIT, "canallogger: Unable to create apoxdrv receive thread.");
		rv = false;
		fclose( m_flog );
	}
		
    // We are open
	m_bOpen = true;

	// Release the mutex
	pthread_mutex_unlock( &m_apoxMutex );

#endif	

	// Switch to main mode if in boot mode
	if ( RUNMODE_BOOT == getAdapterRunMode() ) {
		setAdapterRunMode( RUNMODE_MAIN );
	}

	// Set baudrate
	setBaudrate( nSpeed );

	// Set initial filter/mask	
	switch( m_initFlag & 0x03 ) {

		case 0:
		case 3:
			m_filtermask.rx_buff1_ext = 0;
			m_filtermask.rx_buff2_ext = 1;
			break;

		case 1:
			m_filtermask.rx_buff1_ext = 0;
			m_filtermask.rx_buff2_ext = 0;
			break;

		case 2:
			m_filtermask.rx_buff1_ext = 1;
			m_filtermask.rx_buff2_ext = 1;
			break;
	}

	m_filtermask.RXM0 = 0;
	m_filtermask.RXM1 = 0;
	m_filtermask.RXF0 = 0;	
	m_filtermask.RXF1 = 0;
	m_filtermask.RXF2 = 0;
	m_filtermask.RXF3 = 0;
	m_filtermask.RXF4 = 0;
	m_filtermask.RXF5 = 0;

	setAdapterFilterMask( &m_filtermask );

	// Set transmission mode
	short trmode = ( (short)( m_initFlag & 0x0c ) >> 2 );
	if ( 0 == trmode ) trmode = TRMODE_NORMAL;
	setTransmissionMode( trmode );

	return true;
}