예제 #1
0
static int ublast_ftdi_init(struct ublast_lowlevel *low)
{
	uint8_t latency_timer;
	struct ftdi_context *ftdic = ublast_getftdic(low);

	LOG_INFO("usb blaster interface using libftdi");
	if (ftdi_init(ftdic) < 0)
		return ERROR_JTAG_INIT_FAILED;

	/* context, vendor id, product id */
	if (ftdi_usb_open(ftdic, low->ublast_vid, low->ublast_pid) < 0)	{
		LOG_ERROR("unable to open ftdi device: %s", ftdic->error_str);
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_usb_reset(ftdic) < 0) {
		LOG_ERROR("unable to reset ftdi device");
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_set_latency_timer(ftdic, 2) < 0) {
		LOG_ERROR("unable to set latency timer");
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_get_latency_timer(ftdic, &latency_timer) < 0) {
		LOG_ERROR("unable to get latency timer");
		return ERROR_JTAG_INIT_FAILED;
	}
	LOG_DEBUG("current latency timer: %u", latency_timer);

	ftdi_disable_bitbang(ftdic);
	return ERROR_OK;
}
예제 #2
0
cell ft_open_serial (cell devidx, cell pid)
{
	ft_handle fh = ftdi_new();

	ft_errno = ftdi_usb_open_desc_index(fh, 0x0403, pid, NULL, NULL, devidx);
	if (ft_errno) {
		return (cell)NULL;
	}

	ftdi_set_baudrate(fh, 115200);
	ftdi_set_line_property(fh, BITS_8, STOP_BIT_1, NONE);
	ftdi_setflowctrl(fh, SIO_DISABLE_FLOW_CTRL);
	ftdi_set_latency_timer(fh, 1);

	com_ops_t *ops = malloc(sizeof(com_ops_t));
	ops->handle = (cell)fh;
	ops->close = ft_close;
	ops->get_modem_control = ft_get_modem_control;
	ops->set_modem_control = ft_set_modem_control;
	ops->set_baud = ft_set_baud;
	ops->set_parity = ft_set_parity;
	ops->write = ft_write;
	ops->timed_read = ft_timed_read;

	return (cell)ops;
}
예제 #3
0
int nifalcon_open(falcon_device* dev, unsigned int device_index)
{
	unsigned int count, i, status;
	struct ftdi_device_list *dev_list, *current;
	if(!dev->is_initialized) nifalcon_error_return(NIFALCON_DEVICE_NOT_VALID_ERROR, "tried to open an uninitialized device");
	if(dev->is_open) nifalcon_close(dev);

	count = ftdi_usb_find_all(&(dev->falcon), &dev_list, NIFALCON_VENDOR_ID, NIFALCON_PRODUCT_ID);
	if(count <= 0 || device_index > count)
	{
		ftdi_list_free(&dev_list);
		if(count == 0) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "no devices connected to system");
		nifalcon_error_return(NIFALCON_DEVICE_INDEX_OUT_OF_RANGE_ERROR, "device index out of range");
	}
	for(i = 0, current = dev_list; current != NULL && i < device_index; current = dev_list->next, ++i);
	if((dev->falcon_status_code = ftdi_usb_open_dev(&(dev->falcon), current->dev)) < 0) return dev->falcon_status_code;
	ftdi_list_free(&dev_list);
	//VERY IMPORTANT
	//If we do not reset latency to 1ms, then we either have to fill the FTDI butter (64bytes) or wait 16ms
	//to get any data back. This is what was causing massive slowness in pre-1.0 releases
	if((dev->falcon_status_code = ftdi_set_latency_timer(&(dev->falcon), 1)) < 0) return dev->falcon_status_code;

	//Shift to full speed
	if((dev->falcon_status_code = ftdi_set_baudrate(&(dev->falcon), 1456312)) < 0) return dev->falcon_status_code;

	dev->is_open = 1;
	return 0;
}
static int config_i2c(struct ftdi_context *ftdi)
{
	int ret;
	uint8_t buf[5];
	uint16_t divisor;

	ret = ftdi_set_latency_timer(ftdi, 16 /* ms */);
	if (ret < 0)
		fprintf(stderr, "Cannot set latency\n");

	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
	if (ret < 0) {
		fprintf(stderr, "Cannot reset MPSSE\n");
		return -EIO;
	}
	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE);
	if (ret < 0) {
		fprintf(stderr, "Cannot enable MPSSE\n");
		return -EIO;
	}

	ret = ftdi_usb_purge_buffers(ftdi);
	if (ret < 0)
		fprintf(stderr, "Cannot purge buffers\n");

	/* configure the clock */
	divisor = (60000000 / (2 * I2C_FREQ * 3 / 2 /* 3-phase CLK */) - 1);
	buf[0] = EN_3_PHASE;
	buf[1] = DIS_DIV_5;
	buf[2] = TCK_DIVISOR;
	buf[3] = divisor & 0xff;
	buf[4] = divisor >> 8;
	ret = ftdi_write_data(ftdi, buf, sizeof(buf));
	return ret;
}
예제 #5
0
int spectrig_connect(struct ftdi_context *ftdi)
{
	int ret;

	ftdi_init(ftdi);

	/* TODO: Be a bit more selective and support multiple devices properly */
	ret = open_by_desc_prefix(ftdi, 0x0403, 0x6010, spectrig_descs);
	if (ret < 0) {
		perror("Failed to open the device");
		return ret;
	}
	ftdi_usb_purge_buffers(ftdi);

	/* Initialize synchronous communication */
	ret = ftdi_set_latency_timer(ftdi, 2);
	if (ret < 0) {
		perror("Failed to set the latency timer");
		goto close;
	}
	ret = ftdi_read_data_set_chunksize(ftdi, 0x10000);
	if (ret < 0) {
		perror("Failed to set read data chunksize");
		goto close;
	}
	ret = ftdi_write_data_set_chunksize(ftdi, 0x10000);
	if (ret < 0) {
		perror("Failed to write read data chunksize");
		goto close;
	}
	ret = ftdi_setflowctrl(ftdi, SIO_RTS_CTS_HS);
	if (ret < 0) {
		perror("Failed to set flow control");
		goto close;
	}
	msleep(20);
	ret = ftdi_set_bitmode(ftdi, 0xff, 0x00);
	if (ret < 0) {
		perror("Failed to set bitmode 0x00");
		goto close;
	}
	msleep(20);
	ftdi_set_bitmode(ftdi, 0xff, 0x40);
	if (ret < 0) {
		perror("Failed to set bitmode 0x40");
		goto close;
	}

	msleep(300);
	ftdi_usb_purge_buffers(ftdi);

	return 0;

close:
	ftdi_usb_close(ftdi);
	ftdi_deinit(ftdi);

	return ret;
}
예제 #6
0
	bool FalconCommLibFTDI::setFirmwareMode()
	{
		unsigned int bytes_written, bytes_read;
		unsigned char check_msg_1_send[3] = {0x0a, 0x43, 0x0d};
		unsigned char check_msg_1_recv[4] = {0x0a, 0x44, 0x2c, 0x0d};
		unsigned char check_msg_2[1] = {0x41};
		unsigned char send_buf[128], receive_buf[128];
		int k;
	
		if(!m_isCommOpen)
		{
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}

		//Save ourselves having to reset this on every error
		m_errorCode = FALCON_COMM_DEVICE_ERROR;
		
		//Clear out current buffers to make sure we have a fresh start
		if((m_deviceErrorCode = ftdi_usb_purge_buffers((m_falconDevice))) < 0) return false;

		//Reset the device
		if((m_deviceErrorCode = ftdi_usb_reset((m_falconDevice))) < 0) return false;

		//Make sure our latency timer is at 16ms, otherwise firmware checks tend to always fail
		if((m_deviceErrorCode = ftdi_set_latency_timer((m_falconDevice), 16)) < 0) return false;
	
		//Set to:
		// 9600 baud
		// 8n1
		// No Flow Control
		// RTS Low
		// DTR High	
		if((m_deviceErrorCode = ftdi_set_baudrate((m_falconDevice), 9600)) < 0) return false;
		if((m_deviceErrorCode = ftdi_set_line_property((m_falconDevice), BITS_8, STOP_BIT_1, NONE)) < 0) return false;
		if((m_deviceErrorCode = ftdi_setflowctrl((m_falconDevice), SIO_DISABLE_FLOW_CTRL)) < 0) return false;
		if((m_deviceErrorCode = ftdi_setrts((m_falconDevice), 0)) < 0) return false;
		if((m_deviceErrorCode = ftdi_setdtr((m_falconDevice), 0)) < 0) return false;
		if((m_deviceErrorCode = ftdi_setdtr((m_falconDevice), 1)) < 0) return false;

		//Send 3 bytes: 0x0a 0x43 0x0d
		if(!write(check_msg_1_send, 3)) return false;
		if(!read(receive_buf, 4)) return false;
	
		//Set to:
		// DTR Low
		// 140000 baud (0x15 clock ticks per signal)
		if((m_deviceErrorCode = ftdi_setdtr((m_falconDevice),0)) < 0) return false;
		if((m_deviceErrorCode = ftdi_set_baudrate((m_falconDevice), 140000)) < 0) return false;

		//Send "A" character
		if(!write(check_msg_2, 1)) return false;

		//Expect back 2 bytes:
		// 0x13 0x41
		if(!read(receive_buf, 2)) return false;
		m_errorCode = 0;
		return true;
	}
예제 #7
0
파일: test.c 프로젝트: gammy/JeePointer
struct ftdi_context *
bub_init(unsigned int baud_rate,
	 unsigned char latency,
	 unsigned int tx_buf_size,
	 unsigned int rx_buf_size) {

	int ret = 0;

	struct ftdi_context *ftdic;
	
	ftdic = malloc(sizeof(struct ftdi_context));
   
	if(ftdic == NULL) {
		perror("malloc");
		return(NULL);
	}

	ret = ftdi_init(ftdic);
	if (ret < 0) {
		fprintf(stderr, "ftdi_init failed: %d.\n", ret);
		return(NULL);
	}

	ftdi_set_interface(ftdic, INTERFACE_ANY);

	ret = ftdi_usb_open(ftdic, 0x0403, 0x6001); // FIXME make nice defines
	if(ret < 0) {
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", 
			ret, ftdi_get_error_string(ftdic));
		return(NULL);
	}

	if(ftdi_usb_reset(ftdic) != 0)
		fprintf(stderr, "WARN: ftdi_usb_reset failed!\n");

	ftdi_disable_bitbang(ftdic);

	if (ftdi_set_baudrate(ftdic, baud_rate) < 0) {
		fprintf(stderr, "Unable to set baudrate: (%s)\n", 
			ftdi_get_error_string(ftdic));
		return(NULL);
	} 

	ftdi_set_latency_timer(ftdic, latency);

	if(tx_buf_size > 0)
		ftdi_write_data_set_chunksize(ftdic, tx_buf_size);
	if(rx_buf_size > 0)
		ftdi_read_data_set_chunksize(ftdic, rx_buf_size);

	return(ftdic);
}
예제 #8
0
int platform_init(void) 
{ 
	int err;

	if(ftdic) {
		ftdi_usb_close(ftdic);
		ftdi_free(ftdic);
		ftdic = NULL;
	}
	if((ftdic = ftdi_new()) == NULL) {
		fprintf(stderr, "ftdi_new: %s\n", 
			ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_set_interface(ftdic, INTERFACE_A)) != 0) {
		fprintf(stderr, "ftdi_set_interface: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_usb_open(ftdic, FT2232_VID, FT2232_PID)) != 0) {
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	if((err = ftdi_set_latency_timer(ftdic, 1)) != 0) {
		fprintf(stderr, "ftdi_set_latency_timer: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_set_baudrate(ftdic, 1000000)) != 0) {
		fprintf(stderr, "ftdi_set_baudrate: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_usb_purge_buffers(ftdic)) != 0) {
		fprintf(stderr, "ftdi_set_baudrate: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_write_data_set_chunksize(ftdic, BUF_SIZE)) != 0) {
		fprintf(stderr, "ftdi_write_data_set_chunksize: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	assert(gdb_if_init() == 0);

	jtag_scan(NULL);
	
	return 0; 
}
예제 #9
0
	bool FalconCommLibFTDI::setNormalMode()
	{
		if(!m_isCommOpen)
		{
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}
		m_errorCode = FALCON_COMM_DEVICE_ERROR;
		if((m_deviceErrorCode = ftdi_set_latency_timer((m_falconDevice), 1)) < 0) return false;
		if((m_deviceErrorCode = ftdi_set_baudrate((m_falconDevice), 1456312)) < 0) return false;
		m_errorCode = 0;
		return true;
	}
예제 #10
0
bool FTDIDevice::connect (int baudrate) throw ()
{
#ifdef FOUND_ftdi

  int ret, i;
  disconnect();

  ftdi_init (&_ftdic);
  while ((ret = ftdi_usb_open(&_ftdic, 0x0403, 0x6001)) < 0)
    FTDERROR("unable to open ftdi device: " << ret << " (" << ftdi_get_error_string(&_ftdic) << ")");

  for (i=0, ret=-1; i<10 && ret!=0; i++)
    ret = ftdi_usb_reset(&_ftdic);
  if (ret != 0) {
    FTDERROR("unable to reset ftdi device.");
    return false;
  }

  for (i=0, ret=-1; i<10 && ret!=0; i++)
    ret = ftdi_set_baudrate(&_ftdic, baudrate);
  if (ret != 0) {
    FTDERROR("unable to set baud rate.");
    return false;
  }

  if (_latency > 0) for (i=0, ret=-1; i<10 && ret!=0; i++)
      ret = ftdi_set_latency_timer(&_ftdic, _latency);
  if (ret != 0) {
    FTDERROR("unable to set latency timer.");
    return false;
  }

  for (i=0, ret=-1; i<10 && ret!=0; i++)
    ret = ftdi_usb_purge_buffers(&_ftdic);
  if (ret != 0) {
    FTDERROR("unable to purge buffers.");
    return false;
  }

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

#else

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

#endif
}
예제 #11
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;
}
예제 #12
0
static int jtagkey_latency(int latency) {
	static int current = 0;
	int ret;

	if (current != latency) {
		DPRINTF("switching latency\n");
		if ((ret = ftdi_set_latency_timer(&ftdic, latency))  != 0) {
			fprintf(stderr, "unable to set latency timer: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
			return ret;
		}
		
		current = latency;
	}

	return ret;
}
예제 #13
0
//Try to find the ftdi chip and open it.
int FtdiNand::open(int vid, int pid, bool doslow) {
	unsigned char slow=DIS_DIV_5;
	if (doslow) slow=EN_DIV_5;
	m_slowAccess=doslow;
	//If vid/pid is zero, use default FT2232H vid/pid.
	if (vid==0) vid=0x0403;
	if (pid==0) pid=0x6010;
	//Open FTDI communications
	if (ftdi_init(&m_ftdi)<0) return error("init");
	if (ftdi_usb_open(&m_ftdi, vid, pid)<0) return error("open");
	if (ftdi_set_bitmode(&m_ftdi, 0, BITMODE_MCU)<0) error("bitmode");
	if (ftdi_write_data(&m_ftdi, &slow, 1)<0) return error("writing div5 cmd");
	if (ftdi_set_latency_timer(&m_ftdi, 1)<0) return error("setting latency");
	ftdi_usb_purge_buffers(&m_ftdi);
	return 1;
}
예제 #14
0
int CKMotionIO::SetLatency(UCHAR LatencyTimer)
{
	int ftStatus;
		
	unsigned char c;

	
	Mutex->Lock();

	ftStatus = ftdi_get_latency_timer(ftdi,&c );
	if(c != LatencyTimer){
		log_info("ftdi_get_latency_timer old value %d", c);
		ftStatus = ftdi_set_latency_timer(ftdi,LatencyTimer );

		if (ftStatus < FT_OK)
		{
			// FT_SetLatencyTimer FAILED!
			ErrorMessageBox("Unable to set USB Latency timer");
			Mutex->Unlock();
			return 1;
		}
	}
	// LatencyTimer set


	ftStatus = ftdi_set_event_char(ftdi,'\n',1);

	if (ftStatus == FT_OK)
	{
		// Event set

		Mutex->Unlock();
		return 0;
	}
	else 
	{ 
		// FT_SetLatencyTimer FAILED!
		ErrorMessageBox("Unable to set USB Event Character");
		Mutex->Unlock();
		return 1;
	}

}
예제 #15
0
bool
USBPort::open_port (std::string /*port_name*/)
{
  if (ftdi_usb_open (&ftdic, 0x0403, 0x6001) < 0)
    return false;
  if (ftdic.type == TYPE_R) {
        unsigned int chipid;
        //printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(&ftdic, &chipid));
        //printf("FTDI chipid: %X\n", chipid);
  }

/* choose speed */
  if (Settings::speed == STANDARD)
    {
      if (ftdi_set_baudrate (&ftdic, 185000) < 0)
	return false;
    }
  else if (Settings::speed == LOW)
    {
      if (ftdi_set_baudrate (&ftdic, 125000) < 0)
	return false;
    }
  else if (Settings::speed == HIGH)
    {
      if (ftdi_set_baudrate (&ftdic, 375000) < 0)
	return false;
    }

  if (ftdi_set_latency_timer (&ftdic, 2) < 0)
    return false;
  if (ftdi_set_line_property (&ftdic, BITS_8, STOP_BIT_1, NONE) < 0)
    return false;
  //if(FT_SetTimeouts(ftHandle,5000,0) != FT_OK)
  //      return false;
  //if(ftdi_enable_bitbang(&ftdic,0xFF) < 0)
  //      return false;

  return true;			/* all ok */



}
예제 #16
0
cell ft_timed_read(cell handle, cell ms, cell len, cell buffer)
{
	int this_ms;
	int blocking = (ms == 0);
	while (blocking || ms > 0) {
		this_ms = 1;
		ftdi_set_latency_timer((ft_handle)handle, this_ms);
		ft_errno = ftdi_read_data((ft_handle)handle, (unsigned char *)buffer, len);
                // ft_errno is:
                //   positive - not an error - if bytes were read
                //   negative if an error occurred
                //   0 if no data is currently available
                // looping will continue only in the 0 case
		if (ft_errno)
			return ft_errno;
		if (!blocking)
			ms -= this_ms;
	}
	return -1;
}
예제 #17
0
static int presto_close(void)
{

	int result = ERROR_OK;

	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);

	return result;
}
예제 #18
0
	bool FalconCommLibFTDI::setNormalMode()
	{
		LOG_INFO("Setting normal communications mode");
		if(!m_isCommOpen)
		{
			LOG_ERROR("Device not open");
			m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR;
			return false;
		}
		m_errorCode = FALCON_COMM_DEVICE_ERROR;
		if((m_deviceErrorCode = ftdi_set_latency_timer((m_falconDevice), 1)) < 0)
		{
			LOG_ERROR("Cannot set latency timers - Device error " << m_deviceErrorCode);
			return false;
		}
		if((m_deviceErrorCode = ftdi_set_baudrate((m_falconDevice), 1456312)) < 0)
		{
			LOG_ERROR("Cannot set baud rate - Device error " << m_deviceErrorCode);
			return false;
		}
		m_errorCode = 0;
		return true;
	}
예제 #19
0
static int presto_open_libftdi(char *req_serial)
{
	uint8_t presto_data;

	LOG_DEBUG("searching for PRESTO using libftdi");

	/* initialize FTDI context structure */
	if (ftdi_init(&presto->ftdic) < 0) {
		LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
		return ERROR_JTAG_DEVICE_ERROR;
	}

	/* context, vendor id, product id */
	if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) {
		LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
		return ERROR_JTAG_DEVICE_ERROR;
	}

	if (ftdi_usb_reset(&presto->ftdic) < 0) {
		LOG_ERROR("unable to reset PRESTO device");
		return ERROR_JTAG_DEVICE_ERROR;
	}

	if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) {
		LOG_ERROR("unable to set latency timer");
		return ERROR_JTAG_DEVICE_ERROR;
	}

	if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) {
		LOG_ERROR("unable to purge PRESTO buffers");
		return ERROR_JTAG_DEVICE_ERROR;
	}

	presto_data = 0xD0;
	if (presto_write(&presto_data, 1) != ERROR_OK) {
		LOG_ERROR("error writing to PRESTO");
		return ERROR_JTAG_DEVICE_ERROR;
	}

	if (presto_read(&presto_data, 1) != ERROR_OK) {
		LOG_DEBUG("no response from PRESTO, retrying");

		if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
			return ERROR_JTAG_DEVICE_ERROR;

		presto_data = 0xD0;
		if (presto_write(&presto_data, 1) != ERROR_OK)
			return ERROR_JTAG_DEVICE_ERROR;

		if (presto_read(&presto_data, 1) != ERROR_OK) {
			LOG_ERROR("no response from PRESTO, giving up");
			return ERROR_JTAG_DEVICE_ERROR;
		}
	}

	if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) {
		LOG_ERROR("error writing PRESTO init sequence");
		return ERROR_JTAG_DEVICE_ERROR;
	}

	return ERROR_OK;
}
예제 #20
0
void platform_init(int argc, char **argv)
{
	int err;
	int c;
	unsigned index = 0;
	char *serial = NULL;
	char * cablename =  "ftdi";
	while((c = getopt(argc, argv, "c:s:")) != -1) {
		switch(c) {
		case 'c':
			cablename =  optarg;
			break;
		case 's':
			serial = optarg;
			break;
		}
	}

	for(index = 0; index < sizeof(cable_desc)/sizeof(cable_desc[0]);
		index++)
		 if (strcmp(cable_desc[index].name, cablename) == 0)
		 break;

	if (index == sizeof(cable_desc)/sizeof(cable_desc[0])){
		fprintf(stderr, "No cable matching %s found\n",cablename);
		exit(-1);
	}

	active_cable = &cable_desc[index];

	printf("\nBlack Magic Probe (" FIRMWARE_VERSION ")\n");
	printf("Copyright (C) 2015  Black Sphere Technologies Ltd.\n");
	printf("License GPLv3+: GNU GPL version 3 or later "
	       "<http://gnu.org/licenses/gpl.html>\n\n");

	if(ftdic) {
		ftdi_usb_close(ftdic);
		ftdi_free(ftdic);
		ftdic = NULL;
	}
	if((ftdic = ftdi_new()) == NULL) {
		fprintf(stderr, "ftdi_new: %s\n",
			ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_set_interface(ftdic, active_cable->interface)) != 0) {
		fprintf(stderr, "ftdi_set_interface: %d: %s\n",
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_usb_open_desc(
		ftdic, active_cable->vendor, active_cable->product,
		active_cable->description, serial)) != 0) {
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	if((err = ftdi_set_latency_timer(ftdic, 1)) != 0) {
		fprintf(stderr, "ftdi_set_latency_timer: %d: %s\n",
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_set_baudrate(ftdic, 1000000)) != 0) {
		fprintf(stderr, "ftdi_set_baudrate: %d: %s\n",
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	if((err = ftdi_write_data_set_chunksize(ftdic, BUF_SIZE)) != 0) {
		fprintf(stderr, "ftdi_write_data_set_chunksize: %d: %s\n",
			err, ftdi_get_error_string(ftdic));
		abort();
	}
	assert(gdb_if_init() == 0);
}
예제 #21
0
int main(int argc, char **argv)
{
	struct ftdi_context ftdi;
	uint8_t buf[4];
	uint8_t conf_buf[] = {SET_BITS_LOW,  0x08, 0x0b,
			      SET_BITS_HIGH, 0x00, 0x00,
			      TCK_DIVISOR,   0x00, 0x00,
			      LOOPBACK_END};

	if (argc < 2) {
		usage(argv[0]);
		return 1;
	}

	if (strcmp (argv[1], "idcode") && strcmp (argv[1], "reset") &&
	    strcmp (argv[1], "load")  && strcmp (argv[1], "readreg") &&
	    strcmp (argv[1], "read") && strcmp (argv[1], "write")
		) {
		usage(argv[0]);
		return 1;
	}

	/* Init */
	ftdi_init(&ftdi);
	if (ftdi_usb_open_desc(&ftdi, VENDOR, PRODUCT, 0, 0) < 0) {
		fprintf(stderr,
			"Can't open device %04x:%04x\n", VENDOR, PRODUCT);
		return 1;
	}
	ftdi_usb_reset(&ftdi);
	ftdi_set_interface(&ftdi, INTERFACE_A);
	ftdi_set_latency_timer(&ftdi, 1);
	ftdi_set_bitmode(&ftdi, 0xfb, BITMODE_MPSSE);
	if (ftdi_write_data(&ftdi, conf_buf, 10) != 10) {
		fprintf(stderr,
			"Can't configure device %04x:%04x\n", VENDOR, PRODUCT);
		return 1;
	}

	buf[0] = GET_BITS_LOW;
	buf[1] = SEND_IMMEDIATE;

	if (ftdi_write_data(&ftdi, buf, 2) != 2) {
		fprintf(stderr,
			"Can't send command to device\n");
		return 1;
	}
	ftdi_read_data(&ftdi, &buf[2], 1);
	if (!(buf[2] & 0x10)) {
		fprintf(stderr,
			"Vref not detected. Please power on target board\n");
		return 1;
	}

	if (!strcmp(argv[1], "idcode")) {
		uint8_t out[4];
		tap_reset_rti(&ftdi);
		tap_shift_dr_bits(&ftdi, NULL, 32, out);
		rev_dump(out, 4);
		printf("\n");
	}

	if (!strcmp (argv[1], "reset"))
		brd_reset(&ftdi);

	if (!strcmp (argv[1], "load")) {
		int i;
		struct load_bits *bs;
		FILE *fp;
		uint8_t *dr_data;
		uint32_t u;

		if(argc < 3) {
			usage(argv[0]);
			goto exit;
		}

		if (!strcmp(argv[2], "-"))
			fp = stdin;
		else {
			fp = fopen(argv[2], "r");
			if (!fp) {
				perror("Unable to open file");
				goto exit;
			}
		}

		bs = calloc(1, sizeof(*bs));
		if (!bs) {
			perror("memory allocation failed");
			goto exit;
		}

		if (load_bits(fp, bs) != 0) {
			fprintf(stderr, "%s not supported\n", argv[2]);
			goto free_bs;
		}

		printf("Bitstream information:\n");
		printf("\tDesign: %s\n", bs->design);
		printf("\tPart name: %s\n", bs->part_name);
		printf("\tDate: %s\n", bs->date);
		printf("\tTime: %s\n", bs->time);
		printf("\tBitstream length: %d\n", bs->length);

		/* copy data into shift register */
		dr_data = calloc(1, bs->length);
		if (!dr_data) {
			perror("memory allocation failed");
			goto free_bs;
		}

		for (u = 0; u < bs->length; u++)
			dr_data[u] = rev8(bs->data[u]);

		brd_reset(&ftdi);

		tap_shift_ir(&ftdi, CFG_IN);
		tap_shift_dr_bits(&ftdi, dr_data, bs->length * 8, NULL);

		/* ug380.pdf
		 * P161: a minimum of 16 clock cycles to the TCK */
		tap_shift_ir(&ftdi, JSTART);
		for (i = 0; i < 32; i++)
			tap_tms(&ftdi, 0, 0);

		tap_reset_rti(&ftdi);

		free(dr_data);
	free_bs:
		bits_free(bs);
		fclose(fp);
	}

	if (!strcmp(argv[1], "readreg") && argc == 3) {
		int i;
		char *err;
		uint8_t reg;
		uint8_t out[2];
		uint8_t dr_in[14];

		uint8_t in[14] = {
			0xaa, 0x99, 0x55, 0x66,
			0x00, 0x00, 0x20, 0x00,
			0x20, 0x00, 0x20, 0x00,
			0x20, 0x00
		};

		uint16_t cmd = 0x2801;	/* type 1 packet (word count = 1) */

		reg = strtol(argv[2], &err, 0);
		if((*err != 0x00) || (reg < 0) || (reg > 0x22)) {
			fprintf(stderr,
				"Invalid register, use a decimal or hexadecimal(0x...) number between 0x0 and 0x22\n");
			goto exit;
		}

		cmd |= ((reg & 0x3f) << 5);
		in[4] = (cmd & 0xff00) >> 8;
		in[5] = cmd & 0xff;

		tap_reset_rti(&ftdi);

		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 0, 0);
		tap_tms(&ftdi, 0, 0);	/* Goto shift IR */

		tap_shift_ir_only(&ftdi, CFG_IN);

		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 0, 0);
		tap_tms(&ftdi, 0, 0);	/* Goto SHIFT-DR */

		for (i = 0; i < 14; i++)
			dr_in[i] = rev8(in[i]);

		tap_shift_dr_bits_only(&ftdi, dr_in, 14 * 8, NULL);

		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);	/* Goto SELECT-IR */
		tap_tms(&ftdi, 0, 0);
		tap_tms(&ftdi, 0, 0);	/* Goto SHIFT-IR */

		tap_shift_ir_only(&ftdi, CFG_OUT);

		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 0, 0);
		tap_tms(&ftdi, 0, 0);	/* Goto SHIFT-IR */

		tap_shift_dr_bits_only(&ftdi, NULL, 2 * 8, out);

		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);
		tap_tms(&ftdi, 1, 0);	/* Goto SELECT-IR */
		tap_tms(&ftdi, 0, 0);
		tap_tms(&ftdi, 0, 0);	/* Goto SHIFT-IR */

		tap_reset_rti(&ftdi);

		out[0] = rev8(out[0]);
		out[1] = rev8(out[1]);

		printf("REG[%d]: 0x%02x%02x\n", reg, out[0], out[1]);
	}
예제 #22
0
int nifalcon_load_firmware(falcon_device* dev, const char* firmware_filename)
{
	unsigned int bytes_written, bytes_read;
	unsigned char check_msg_1_send[3] = {0x0a, 0x43, 0x0d};
	unsigned char check_msg_1_recv[4] = {0x0a, 0x44, 0x2c, 0x0d};
	unsigned char check_msg_2[1] = {0x41};
	unsigned char send_buf[128], receive_buf[128];
	FILE* firmware_file;
	int k;
	if(!dev->is_initialized) nifalcon_error_return(NIFALCON_DEVICE_NOT_VALID_ERROR, "tried to load firmware on an uninitialized device");
	if(!dev->is_open) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "tried to load firmware on an unopened device");
  
	//Clear out current buffers to make sure we have a fresh start
	if((dev->falcon_status_code = ftdi_usb_purge_buffers(&(dev->falcon))) < 0) return dev->falcon_status_code;

	//Reset the device
	if((dev->falcon_status_code = ftdi_usb_reset(&(dev->falcon))) < 0) return dev->falcon_status_code;

	//Make sure our latency timer is at 16ms, otherwise firmware checks tend to always fail
	if((dev->falcon_status_code = ftdi_set_latency_timer(&(dev->falcon), 16)) < 0) return dev->falcon_status_code;
	
	//Set to:
	// 9600 baud
	// 8n1
	// No Flow Control
	// RTS Low
	// DTR High	
	if((dev->falcon_status_code = ftdi_set_baudrate(&(dev->falcon), 9600)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_set_line_property(&(dev->falcon), BITS_8, STOP_BIT_1, NONE)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_setflowctrl(&(dev->falcon), SIO_DISABLE_FLOW_CTRL)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_setrts(&(dev->falcon), 0)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_setdtr(&(dev->falcon), 0)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_setdtr(&(dev->falcon), 1)) < 0) return dev->falcon_status_code;
	//Send 3 bytes: 0x0a 0x43 0x0d

	if((dev->falcon_status_code = nifalcon_write(dev, check_msg_1_send, 3)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, 4, 1000)) < 0) return dev->falcon_status_code;
	
	//Set to:
	// DTR Low
	// 140000 baud (0x15 clock ticks per signal)
	if((dev->falcon_status_code = ftdi_setdtr(&(dev->falcon),0)) < 0) return dev->falcon_status_code;
	if((dev->falcon_status_code = ftdi_set_baudrate(&(dev->falcon), 140000)) < 0) return dev->falcon_status_code;

	//Send "A" character
	if((dev->falcon_status_code = nifalcon_write(dev, check_msg_2, 1)) < 0) return dev->falcon_status_code;

	//Expect back 2 bytes:
	// 0x13 0x41
	if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, 2, 1000)) < 0) return dev->falcon_status_code;
	
	firmware_file = fopen(firmware_filename, "rb");

	if(!firmware_file)
	{
		nifalcon_error_return(NIFALCON_FIRMWARE_NOT_FOUND_ERROR, "cannot find falcon firmware file");
	}

	while(!feof(firmware_file))
	{
		int firmware_bytes_read;
		int i;
		firmware_bytes_read = fread(send_buf, 1, 128, firmware_file);
		if((dev->falcon_status_code = nifalcon_write(dev, send_buf, firmware_bytes_read)) < 0) return dev->falcon_status_code;
		if((dev->falcon_status_code = nifalcon_read(dev, receive_buf, firmware_bytes_read, 1000)) < firmware_bytes_read)
		{			
			nifalcon_error_return(NIFALCON_FIRMWARE_CHECKSUM_ERROR, "error sending firmware (firmware send step, 128 byte reply not received)");
		}
		for(i = 0; i < firmware_bytes_read; ++i)
		{
			if(send_buf[i] != receive_buf[i])
			{
				nifalcon_error_return(NIFALCON_FIRMWARE_CHECKSUM_ERROR, "error sending firmware (firmware send step, checksum does not match)");
			}
		}
		if(firmware_bytes_read < 128) break;
	}
	fclose(firmware_file);

	//VERY IMPORTANT
	//If we do not reset latency to 1ms, then we either have to fill the FTDI butter (64bytes) or wait 16ms
	//to get any data back. This is what was causing massive slowness in pre-1.0 releases
	if((dev->falcon_status_code = ftdi_set_latency_timer(&(dev->falcon), 1)) < 0) return dev->falcon_status_code;

	//Shift to full speed
	if((dev->falcon_status_code = ftdi_set_baudrate(&(dev->falcon), 1456312)) < 0) return dev->falcon_status_code;

	return 0;
}
예제 #23
0
// init serial communication device
bool CPlatform::initSerial(char*cPort, unsigned int baudrate)
{
// Windows
#ifdef WIN32
    if(hSerialPort != INVALID_HANDLE_VALUE) {
        CloseHandle(hSerialPort);
    }

    if(cPort == NULL) {
        cPort = STDSERIALWINDOWS;
    }

    hSerialPort = ::CreateFile(cPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

    if(hSerialPort == INVALID_HANDLE_VALUE) {
        CUtil::cout("initSerial: CreateFile() failed.\n", TEXT_ERROR);
        return false;
    }
    else {
        CUtil::cout("initSerial: Connected!\n", TEXT_ERROR);
    }

    COMMTIMEOUTS timeouts;
    GetCommTimeouts(hSerialPort, &timeouts);

    int a = 40; //40
    timeouts.ReadIntervalTimeout = a;
    timeouts.ReadTotalTimeoutMultiplier = 20;
    timeouts.ReadTotalTimeoutConstant = a;
    timeouts.WriteTotalTimeoutMultiplier = 20;
    timeouts.WriteTotalTimeoutConstant = a;

    SetCommTimeouts(hSerialPort, &timeouts);

    DCB dcb = {0};
    dcb.DCBlength = sizeof(DCB);

    if(!::GetCommState(hSerialPort, &dcb) ) {
        CUtil::cout("initSerial: GetCommState() failed.\n", TEXT_ERROR);
    }
    else {
        // 8bit, no parity, one stopbit
        dcb.BaudRate  = baudrate;
        dcb.ByteSize = 8;
        dcb.Parity = 0;
        dcb.StopBits = 0;

        if(!::SetCommState(hSerialPort, &dcb) ) {
            CUtil::cout("initSerial: SetCommState() failed.\n", TEXT_ERROR);
        }
    }

    return true;

// LINUX
#else
    if(cPort == NULL) {
        cPort = (char *)STDSERIALLINUX;
    }

#ifdef USE_FTDI

    /* Still testing */

    unsigned char  buf [1000];

    int a;
    int rsize;
    if (ftdi_init(&ftdi) < 0 )
        return false;
    int ret;

    if((ret = ftdi_usb_open(&ftdi, 0x0403, 0x6001)) < 0) {
        printf( "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdi));
        return EXIT_FAILURE;
    }

    // Read out FTDIChip-ID of R type chips
    if (ftdi.type == TYPE_R) {
        unsigned int chipid;
        printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(&ftdi, &chipid));
        printf("FTDI chipid: %X\n", chipid);
    }
    ftdi_read_data_set_chunksize(&ftdi, 4096);
    ftdi_write_data_set_chunksize(&ftdi, 4096);
    ftdi_set_line_property2(&ftdi, BITS_8, STOP_BIT_1, NONE, BREAK_OFF);
    ftdi_usb_reset (&ftdi);
    printf("baudrate: %d\n", ftdi_set_baudrate (&ftdi, baudrate));
    ftdi_setflowctrl(&ftdi, SIO_DISABLE_FLOW_CTRL);
    ftdi_set_latency_timer(&ftdi, 1);
    ftdi_usb_purge_buffers (&ftdi);

    printf (" Err : %s \n", ftdi.error_str);

    unsigned char nix[150];
    int j = 0;
    int cr;
    for (j = 0; j < 10; j++) {
        cr = ftdi_read_data (&ftdi, &nix[0], 100);
        printf (" %i \n", cr);
    }


    printf ("Typ: %d\n", ftdi.type);
    printf ("usb_read_timeout: %d\n", ftdi.usb_read_timeout);
    printf ("usb_write_timeout: %d\n",
            ftdi.usb_write_timeout);
    printf ("baudrate: %d\n",  ftdi.baudrate);
    printf ("bitbang_enabled: %x\n", ftdi.bitbang_enabled);
    printf ("bitbang_mode: %x\n", ftdi.bitbang_mode);

    return true;

    /* close handles */
    ftdi_usb_close(&ftdi);
    ftdi_deinit(&ftdi);
    return false;
#endif
    //system("stty -F /dev/ttyS0 speed 115200 raw cs8");

    fdSerialPort = open(cPort, O_RDWR | O_NOCTTY | O_NDELAY);// | O_NONBLOCK | O_NDELAY);
    if(fdSerialPort == -1) {
        CUtil::cout("initSerial: open() failed.\n", TEXT_ERROR);
        return false;
    }
    else {
        CUtil::cout("initSerial: Connected.\n", TEXT_ERROR);
    }

    clearLine();


#ifndef SERIALDONTINIT


    //fcntl(fdSerialPort, F_SETFL, FNDELAY);

    struct termios options;

    if(tcgetattr(fdSerialPort, &options) == -1) {
        CUtil::cout("initSerial: tvgetattr() failed.\n", TEXT_ERROR);
    }
    else {
        options.c_iflag = 0;
        options.c_oflag = 0;
        options.c_cflag = 0;
        options.c_lflag = 0;

        cfsetispeed(&options, getSpeedConstant(baudrate) );
        cfsetospeed(&options, getSpeedConstant(baudrate) );
        cfmakeraw(&options);

        options.c_cflag |= (CLOCAL | CREAD);

        // 8 bit, no parity, 1 stopbit
        options.c_cflag |= CS8;

        options.c_cflag &= ~CRTSCTS;
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);


        // wait for 1 characters
        options.c_cc[VMIN]  = 1;
        // timeout 1 seconds
        options.c_cc[VTIME] = 1;

        /*
           cfsetispeed(&options, getSpeedConstant(baudrate));
           cfsetospeed(&options, getSpeedConstant(baudrate));

           //options.c_cflag = 0;
           options.c_cflag |= (getSpeedConstant(baudrate) | CLOCAL | CREAD);

           // 8 bit, no parity, 1 stopbit
           options.c_cflag |= CS8;

           options.c_cflag &= ~CRTSCTS;

           //options.c_lflag = 0;
           options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);*/
        // testing -->

        // <-- testing

        /*
           // baudrate >> 1152000

           struct serial_struct nuts;
           int arby;
           if(ioctl(fdSerialPort, TIOCGSERIAL, &nuts) == -1)
           CUtil::cout( "Serial: ioctl(*,TIOCGSERIAL,*) failed.\n");

           nuts.custom_divisor = nuts.baud_base / 500000;

           if (!(nuts.custom_divisor))
           nuts.custom_divisor = 1;

           arby = nuts.baud_base / nuts.custom_divisor;
           nuts.flags &= ~ASYNC_SPD_MASK;
           nuts.flags |= ASYNC_SPD_CUST;

           if (ioctl(fdSerialPort, TIOCSSERIAL, &nuts) == -1)
           CUtil::cout("Serial: ioctl(*, TIOCSSERIAL, *) failed.\n");
           options.c_cflag |= B38400;

           // wait for 1 characters
           options.c_cc[VMIN]  = 1;
           // timeout 1 seconds
           options.c_cc[VTIME] = 1;
         */


        if(tcsetattr(fdSerialPort, TCSANOW, &options) != 0) {
            CUtil::cout("initSerial: tcsetattr() failed.\n", TEXT_ERROR);
        }
    }
#endif
    return true;

#endif
}
예제 #24
0
파일: spi.c 프로젝트: z80/bt-board
int spi_open(int nport)
{
    LOG(DEBUG, "(%d) spi_dev_open=%d", nport, spi_dev_open);

    if (spi_dev_open > 0) {
        LOG(WARN, "Superfluos call to spi_open()");
        return 0;
    }

    if (spi_nports == 0 || nport < spi_nports - 1) {
        SPI_ERR("No FTDI device found");
        goto open_err;
    }

#ifdef SPI_STATS
    memset(&spi_stats, 0, sizeof(spi_stats));
    if (gettimeofday(&spi_stats.tv_open_begin, NULL) < 0)
        LOG(WARN, "gettimeofday failed: %s", strerror(errno));
#endif

    /*ftdi_set_interface(&ftdic, INTERFACE_A);*/ /* XXX for multichannel chips */

    if (ftdi_usb_open_desc(&ftdic, spi_ports[nport].vid, spi_ports[nport].pid,
                NULL, spi_ports[nport].serial) < 0)
    {
        SPI_ERR("FTDI: ftdi_usb_open_desc() failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    spi_dev_open++;

    LOG(INFO, "FTDI: using FTDI device: \"%s\"", spi_ports[nport].name);

    if (ftdi_usb_reset(&ftdic) < 0) {
        SPI_ERR("FTDI: reset failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    if (ftdi_usb_purge_buffers(&ftdic) < 0) {
        SPI_ERR("FTDI: purge buffers failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    /* Set 1 ms latency timer, see FTDI AN232B-04 */
    if (ftdi_set_latency_timer(&ftdic, 1) < 0) {
        SPI_ERR("FTDI: setting latency timer failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    if (ftdi_set_bitmode(&ftdic, 0, BITMODE_RESET) < 0) {
        SPI_ERR("FTDI: reset bitmode failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    if (ftdi_set_bitmode(&ftdic, PINS_OUTPUT, BITMODE_SYNCBB) < 0) {
        SPI_ERR("FTDI: set synchronous bitbang mode failed: %s", ftdi_get_error_string(&ftdic));
        goto open_err;
    }

    /*
     * Note on buffer sizes:
     *
     * FT232R has 256 byte receive buffer and 128 byte transmit buffer. It works
     * like 384 byte buffer. See:
     * http://developer.intra2net.com/mailarchive/html/libftdi/2011/msg00410.html
     * http://developer.intra2net.com/mailarchive/html/libftdi/2011/msg00413.html
     * http://jdelfes.blogspot.ru/2014/03/ft232r-bitbang-spi-part-2.html
     *
     * FT2232C has 128 byte TX and 384 byte RX buffers per channel.
     * FT2232H has 4kB RX and TX buffers per channel.
     * FT4232H has 2kB RX and TX buffers per channel.
     * FT232H has 1 kB RX and TX buffers.
     * FT230X has 512 byte TX and RX buffers.
     */
    switch (ftdic.type) {
        case TYPE_AM:
            ftdi_type_str = "FT232AM";
            SPI_ERR("This chip type is not supported: %s", ftdi_type_str);
            goto open_err;
            break;
        case TYPE_BM:
            ftdi_type_str = "FT232BM";
            SPI_ERR("This chip type is not supported: %s", ftdi_type_str);
            goto open_err;
            break;
        case TYPE_2232C:
            ftdi_type_str = "FT2232C/D";
            ftdi_buf_size = 512;
            break;
        case TYPE_R:
            ftdi_type_str = "FT232R";
            ftdi_buf_size = 384;
            break;
        case TYPE_2232H:
            ftdi_type_str = "FT2232H";
            ftdi_buf_size = 8192;
            break;
        case TYPE_4232H:
            ftdi_type_str = "FT4232H";
            ftdi_buf_size = 4096;
            break;
        case TYPE_232H:
            ftdi_type_str = "FT232H";
            ftdi_buf_size = 2048;
            break;
        /* TYPE_230X is supported since libftdi1-1.2 */
        /*case TYPE_230X:
            ftdi_type_str = "FT230X";
            ftdi_buf_size = 1024;
            break;
        */
        default:
            LOG(WARN, "Unknown FTDI chip type, assuming FT232R");
            ftdi_type_str = "Unknown";
            ftdi_buf_size = 384;
            break;
    }

    LOG(INFO, "Detected %s type programmer chip, buffer size: %u",
            ftdi_type_str, ftdi_buf_size);

    /* Initialize xfer buffer */
    ftdi_buf = malloc(ftdi_buf_size);
    if (ftdi_buf == NULL) {
        SPI_ERR("Not enough memory");
        goto open_err;
    }
    ftdi_buf_write_offset = 0;

    ftdi_pin_state = PINS_INIT;
    ftdi_buf[ftdi_buf_write_offset++] = ftdi_pin_state;

    return 0;

open_err:
    if (spi_dev_open > 0)
        ftdi_usb_close(&ftdic);
    spi_dev_open = 0;

    return -1;
}
예제 #25
0
Ft245sync::Ft245sync(unsigned int chunkSizeRead,
                     unsigned int chunkSizeWrite,
                     uint8_t gpio,
                     struct ftdi_context * vftdic, 
                     struct ftdi_context * vftdic2)
{
    if(vftdic == NULL)
    {
        this->ftdic = static_cast<struct ftdi_context*>(malloc(sizeof(struct ftdi_context)));
    }
    else
    {
        this->ftdic = vftdic;
    }
    if(vftdic2 == NULL)
    {
        this->ftdic2 = static_cast<struct ftdi_context*>(malloc(sizeof(struct ftdi_context)));
    }
    else
    {
        this->ftdic2 = vftdic2;
    }
    int f;
    // Init 1. channel
    if (ftdi_init(ftdic) < 0)
    {
        throw Exception("ftdi_init failure\n");
    }
    ftdi_set_interface(ftdic, INTERFACE_A);
    f = ftdi_usb_open(ftdic, 0x0403, PID);
    if (f < 0 && f != -5)
    {
        throw Exception("Unable to open FTDI device, channel A\n");
    }
    
    // Init 2. channel
    if (ftdi_init(ftdic2) < 0)
    {
        throw Exception("ftdi_init failure\n");
    }
    ftdi_usb_reset(ftdic);
    ftdi_usb_reset(ftdic2);
    ftdi_set_interface(ftdic2, INTERFACE_B);
    f = ftdi_usb_open(ftdic2, 0x0403, PID);
    if (f < 0 && f != -5)
    {
        throw Exception("Unable to open FTDI device, channel B\n");
    }
    
    ftdi_write_data_set_chunksize(ftdic2, 512);
    ftdi_set_interface(ftdic2, INTERFACE_B);
    ftdi_usb_reset(ftdic2);
    ftdi_set_latency_timer(ftdic2, 2);
    ftdi_setflowctrl(ftdic2, SIO_RTS_CTS_HS);
    ftdi_set_bitmode(ftdic2, 0, BBMODE_SPI);
    
    uint8_t buf[3];
    buf[0] = SET_BITS_LOW;
    buf[1] = 8;
    buf[2] = BIT_DIR; //holding programming of FPGA*/
    ftdi_write_data(ftdic2, buf, 3);
    buf[0] = SET_BITS_HIGH;
    buf[1] = 0xFF; //lighting leds
    buf[2] = BIT_DIR;
    ftdi_write_data(ftdic2, buf, 3);
    buf[0] = SET_BITS_HIGH;
    buf[1] = 0x00; //lighting leds
    buf[2] = BIT_DIR;
    ftdi_write_data(ftdic2, buf, 3);
    buf[0] = SET_BITS_LOW;
    buf[1] = gpio;
    buf[2] = BIT_DIR; //releasing programming of FPGA
    ftdi_write_data(ftdic2, buf, 3);
    sleep(1);
    buf[0] = SET_BITS_LOW;
    buf[1] = 0xFF; //reseting design in FPGA
    buf[2] = BIT_DIR;
    ftdi_write_data(ftdic2, buf, 3);
    sleep(1);
    buf[0] = SET_BITS_LOW;
    buf[1] = 0xDD; //releasing reset
    buf[2] = BIT_DIR;
    ftdi_write_data(ftdic2, buf, 3);
    sleep(1);
    buf[0] = SET_BITS_HIGH;
    buf[1] = 0xFF; //lighting leds
    buf[2] = BIT_DIR;
    ftdi_write_data(ftdic2, buf, 3);
    
    if (ftdi_usb_purge_buffers(ftdic2))
    {
        throw Exception("Purging buffers failed\n");
    }
    ftdi_usb_close(ftdic2); // close channel 2
    ftdi_deinit(ftdic2); // close channel 2
    ftdic->usb_read_timeout = READ_TIMEOUT;
    ftdic->usb_write_timeout = WRITE_TIMEOUT;
    ftdi_read_data_set_chunksize(ftdic, chunkSizeRead);
    ftdi_write_data_set_chunksize(ftdic, chunkSizeWrite);
    
    if (ftdi_usb_reset(ftdic))
    {
        throw Exception("Reset failed\n");
    }
    usleep(1000);
    
    if(ftdi_usb_purge_buffers(ftdic) < 0)
    {
       throw Exception("Setting FT2232 synchronous bitmode failed\n");
    }
    if(ftdi_set_bitmode(ftdic, 0xFF, 0x00) < 0)
    {
       throw Exception("Setting FT2232 synchronous bitmode failed\n");
    }
    if(ftdi_set_bitmode(ftdic, 0xFF, 0x40) < 0) 
    {
        throw Exception("Setting FT2232 synchronous bitmode failed\n");
    }
    if (ftdi_set_latency_timer(ftdic, 2)) /* AN_130 */
    {
        throw Exception("Set latency failed failed\n");
    }
    //SetUSBParameters(ftHandle,0x10000, 0x10000);
    if (ftdi_setflowctrl(ftdic, SIO_RTS_CTS_HS)) // AN_130 
    {
        throw Exception("Set RTS_CTS failed\n");
    }
    /*if(ftdi_usb_purge_buffers(ftdic) < 0)
    {
        throw Exception("Setting FT2232 synchronous bitmode failed\n");
    }*/
    //fixes unalignment of first read (should be fixed in cleaner manner)
    usleep(400);
    unsigned char cleanup[10] = { 0xBB, 0xBB, 0xBB, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
    ftdi_write_data(ftdic, cleanup, 10);
    unsigned char recvbuf[4000];
    read(recvbuf);
}
예제 #26
0
파일: ftdi.cpp 프로젝트: amuthelet/kondo
int Context::set_latency(unsigned char latency)
{
    return ftdi_set_latency_timer(d->ftdi, latency);
}
예제 #27
0
파일: api.c 프로젝트: BayLibre/libsigrok
static int dev_open(struct sr_dev_inst *sdi)
{
	struct dev_context *devc;
	int ret;

	devc = sdi->priv;

	/* Select interface A, otherwise communication will fail. */
	ret = ftdi_set_interface(devc->ftdic, INTERFACE_A);
	if (ret < 0) {
		sr_err("Failed to set FTDI interface A (%d): %s", ret,
		       ftdi_get_error_string(devc->ftdic));
		return SR_ERR;
	}
	sr_dbg("FTDI chip interface A set successfully.");

	/* Open the device. */
	ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
				 USB_IPRODUCT, NULL);
	if (ret < 0) {
		sr_err("Failed to open device (%d): %s", ret,
		       ftdi_get_error_string(devc->ftdic));
		return SR_ERR;
	}
	sr_dbg("FTDI device opened successfully.");

	/* Purge RX/TX buffers in the FTDI chip. */
	if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
		sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI chip buffers purged successfully.");

	/* Reset the FTDI bitmode. */
	ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
	if (ret < 0) {
		sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI chip bitmode reset successfully.");

	/* Set FTDI bitmode to "sync FIFO". */
	ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF);
	if (ret < 0) {
		sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI chip sync FIFO mode entered successfully.");

	/* Set the FTDI latency timer to 2. */
	ret = ftdi_set_latency_timer(devc->ftdic, 2);
	if (ret < 0) {
		sr_err("Failed to set FTDI latency timer (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI chip latency timer set successfully.");

	/* Set the FTDI read data chunk size to 64kB. */
	ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
	if (ret < 0) {
		sr_err("Failed to set FTDI read data chunk size (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI chip read data chunk size set successfully.");

	/* Get the ScanaPLUS device ID from the FTDI EEPROM. */
	if ((ret = scanaplus_get_device_id(devc)) < 0) {
		sr_err("Failed to get ScanaPLUS device ID: %d.", ret);
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
	       devc->devid[0], devc->devid[1], devc->devid[2]);

	sdi->status = SR_ST_ACTIVE;

	return SR_OK;

err_dev_open_close_ftdic:
	scanaplus_close(devc);
	return SR_ERR;
}
예제 #28
0
ULONG DapiOpenModuleGetFT_HANDLE(ULONG moduleID, ULONG moduleNr, ULONG BusType, ULONG retry_max, ULONG subdevice, FT_HANDLE* handle)
{
	unsigned long NumDevices = 0;
	ULONG LocID = 0, ChipID = 0;
	FT_STATUS dStatus;
//	FT_HANDLE ftHandle;
	FT_STATUS ftStatus;
	DWORD iNumDevs;
	long dwLoc;
	DWORD EEUA_Size;
	ULONG sn;

	char text_dev_nr[10];
	unsigned char buffer[64];
	DWORD BytesRead;
	ULONG i;

	FT_DEVICE_LIST_INFO_NODE *devInfo;
	ULONG check_device;
	char msg[200];

	DWORD dwDriverVer;

	FT_STATUS status;

	ULONG ModuleLocation;


	ULONG baudrate;

	ULONG ok;


	if(subdevice==0)
	{
		sprintf(text_dev_nr , "DT00%4dA", (int) moduleID);
	}
	else if(subdevice==1)
	{
		sprintf(text_dev_nr , "DT00%4dB", (int) moduleID);
	}
	else
	{
		// Ohne A oder B
		sprintf(text_dev_nr , "DT00%4d", (int) moduleID);
	}

	// Linux KEIN A oder B
	//sprintf(text_dev_nr , "DT00%4d", (int) moduleID);


	sprintf(msg,"|DELIB|---------------OpenModuleGetFT_HANDLE(0x%x, 0x%x, 0x%x, 0x%x) Normal\n", (unsigned int) moduleID, (unsigned int) moduleNr, (unsigned int) retry_max, (unsigned int) subdevice);
	debug_print(msg);

	for(i=4;i!=8;++i)
	{
		if(text_dev_nr[i]<'0' || text_dev_nr[i]>'9') text_dev_nr[i]='0';
	}
	text_dev_nr[9]=0;

	sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - Searching for the following S/N = %s\n", text_dev_nr);
	debug_print(msg);
	
	// ----------------------------------------------------

	// 6.12.2011 Geändert auf -1
	ModuleLocation = -1;
//	ModuleLocation = 0;



	ftStatus = FT_OpenEx(text_dev_nr,FT_OPEN_BY_SERIAL_NUMBER, handle);
	if (ftStatus == FT_OK)
	{

		sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - FTDI-Handle = %x\n", (unsigned int) *handle);
		debug_print(msg);

	}
	else
	{
		sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - FTDI OPEN ERROR \n");
		debug_print(msg);

		*handle = (FT_HANDLE) -1;
	}




	if(*handle != ((FT_HANDLE) -1))
	{
		if((BusType == bus_SeriellFTDI)  || (BusType == bus_FTDI_SINGLE))
		{

		
		
		
//		dStatus = FT_SetTimeouts(ftHandle, 100*5, 100*5);

		sprintf(msg,"|DELIB|OpenModuleGetFT_HANDLE - Timeout ok\n");
		debug_print(msg);

	
		if((moduleID != USB_MINI_STICK) && (moduleID != USB_LOGI_18) && (moduleID != USB_SPI_MON) && (moduleID != USB_WATCHDOG) && (moduleID != USB_OPTOIN_8))
		{
			baudrate = 115200;
		}
		else
		{
			baudrate = (unsigned long) (250000/1.666);					// 150 KBaud beim R8C1A


		}


//		printf("write ftdic = 0x%x\n", handle);



//		ftStatus=FT_SetBaudRate(ftHandle, baudrate);
		if(ftdi_set_baudrate (*handle, baudrate) != 0)
//		if (ftStatus != FT_OK)
		{
			sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetBaudRate");
			debug_print(msg);
			//return -1;
		}
		else
		{
			sprintf(msg,"|DELIB|DapiHandle * Serial Baudrate = %d", (unsigned int) baudrate);
			debug_print(msg);
		}

//printf("XX\n");
		// ----------------------------
//		ftStatus=FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
		if (ftStatus != FT_OK)
		{
			sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetDataCharacteristics");
			debug_print(msg);
			//return -1;
		}

		// ----------------------------
//		ftStatus=FT_SetTimeouts(ftHandle,1,1); 
		if (ftStatus != FT_OK)
		{
			sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetTimeouts");
			debug_print(msg);
			//return -1;
		}
		// ----------------------------
//		ftStatus=FT_SetLatencyTimer(ftHandle,2); 
		if(ftdi_set_latency_timer(*handle, 2) != 0)
		//if (ftStatus != FT_OK)
		{
			sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetLatencyTimer");
			debug_print(msg);
			//return -1;
		}
		// ----------------------------
	
	}
	}
	

	return (ULONG) *handle;
}
예제 #29
0
int ftdi_device_setup(ftdi_device_t* dev, int baud_rate, int data_bits, int
    stop_bits, ftdi_parity_t parity, ftdi_flow_ctrl_t flow_ctrl, ftdi_break_t
    break_type, double timeout, double latency) {
  struct ftdi_context* libftdi_context = dev->libftdi_context;
  enum ftdi_bits_type libftdi_data_bits;
  enum ftdi_stopbits_type libftdi_stop_bits;
  enum ftdi_parity_type libftdi_parity;
  int libftdi_flow_ctrl;
  enum ftdi_break_type libftdi_break;
  int error;
  
  error_clear(&dev->error);
  
  switch (data_bits) {
    case 7:
      libftdi_data_bits = BITS_7;
      break;
    case 8:
      libftdi_data_bits = BITS_8;
      break;
    default:
      error_setf(&dev->error, FTDI_ERROR_INVALID_DATA_BITS, "%d", data_bits);
      return error_get(&dev->error);
  }
  dev->data_bits = data_bits;

  switch (stop_bits) {
    case 1:
      libftdi_stop_bits = STOP_BIT_1;
      break;
    case 2 :
      libftdi_stop_bits = STOP_BIT_2;
      break;
    case 15:
      libftdi_stop_bits = STOP_BIT_15;
      break;
    default:
      error_setf(&dev->error, FTDI_ERROR_INVALID_STOP_BITS, "%d", stop_bits);
      return error_get(&dev->error);
  }
  dev->stop_bits = stop_bits;

  switch (parity) {
    case ftdi_parity_none:
      libftdi_parity = NONE;
      break;
    case ftdi_parity_odd:
      libftdi_parity = ODD;
      break;
    case ftdi_parity_even:
      libftdi_parity = EVEN;
      break;
    case ftdi_parity_mark:
      libftdi_parity = MARK;
      break;
    case ftdi_parity_space:
      libftdi_parity = SPACE;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_PARITY);
      return error_get(&dev->error);
  }
  dev->parity = parity;

  switch (flow_ctrl) {
    case ftdi_flow_ctrl_off:
      libftdi_flow_ctrl = SIO_DISABLE_FLOW_CTRL;
      break;
    case ftdi_flow_ctrl_xon_xoff:
      libftdi_flow_ctrl = SIO_XON_XOFF_HS;
      break;
    case ftdi_flow_ctrl_rts_cts:
      libftdi_flow_ctrl = SIO_RTS_CTS_HS;
      break;
    case ftdi_flow_ctrl_dtr_dsr:
      libftdi_flow_ctrl = SIO_DTR_DSR_HS;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_FLOW_CTRL);
      return error_get(&dev->error);
  }
  dev->flow_ctrl = flow_ctrl;
  
  switch (break_type) {
    case ftdi_break_off:
      libftdi_break = BREAK_OFF;
      break;
    case ftdi_break_on:
      libftdi_break = BREAK_ON;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_BREAK);
      return error_get(&dev->error);
  }
  dev->break_type = break_type;

  if (ftdi_set_line_property2(libftdi_context, libftdi_data_bits,
      libftdi_stop_bits, libftdi_parity, libftdi_break)) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  if (ftdi_setflowctrl(libftdi_context, libftdi_flow_ctrl)) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  error = ftdi_set_baudrate(libftdi_context, baud_rate);
  if (error == -1) {
    error_setf(&dev->error, FTDI_ERROR_INVALID_BAUD_RATE, "%d", baud_rate);
    return error_get(&dev->error);
  }
  dev->baud_rate = baud_rate;
  if (error) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }

  libftdi_context->usb_read_timeout = timeout*1e6;
  libftdi_context->usb_write_timeout = timeout*1e6;
  dev->timeout = timeout;
  
  error = ftdi_set_latency_timer(libftdi_context, latency*1e3);
  if (error == -1) {
    error_setf(&dev->error, FTDI_ERROR_INVALID_LATENCY, "%f", latency);
    return error_get(&dev->error);
  }
  dev->latency = latency;
  if (error) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  return error_get(&dev->error);
}
int main(int argc, char **argv)
{
   struct ftdi_context *ftdi;
   int err, c;
   FILE *of = NULL;
   char const *outfile  = 0;
   outputFile =0;
   exitRequested = 0;
   char *descstring = NULL;
   int option_index;
   static struct option long_options[] = {{NULL},};

   while ((c = getopt_long(argc, argv, "P:n", long_options, &option_index)) !=- 1)
       switch (c) 
       {
       case -1:
           break;
       case 'P':
           descstring = optarg;
           break;
       case 'n':
           check = 0;
           break;
       default:
           usage(argv[0]);
       }
   
   if (optind == argc - 1)
   {
       // Exactly one extra argument- a dump file
       outfile = argv[optind];
   }
   else if (optind < argc)
   {
       // Too many extra args
       usage(argv[0]);
   }
   
   if ((ftdi = ftdi_new()) == 0)
   {
       fprintf(stderr, "ftdi_new failed\n");
       return EXIT_FAILURE;
   }
   
   if (ftdi_set_interface(ftdi, INTERFACE_A) < 0)
   {
       fprintf(stderr, "ftdi_set_interface failed\n");
       ftdi_free(ftdi);
       return EXIT_FAILURE;
   }
   
   if (ftdi_usb_open_desc(ftdi, 0x0403, 0x6014, descstring, NULL) < 0)
   {
       fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(ftdi));
       ftdi_free(ftdi);
       return EXIT_FAILURE;
   }
   
   /* A timeout value of 1 results in may skipped blocks */
   if(ftdi_set_latency_timer(ftdi, 2))
   {
       fprintf(stderr,"Can't set latency, Error %s\n",ftdi_get_error_string(ftdi));
       ftdi_usb_close(ftdi);
       ftdi_free(ftdi);
       return EXIT_FAILURE;
   }
   
/*   if(ftdi_usb_purge_rx_buffer(ftdi) < 0)
   {
       fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(ftdi));
       return EXIT_FAILURE;
       }*/
   if (outfile)
       if ((of = fopen(outfile,"w+")) == 0)
           fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno));
   if (of)
       if (setvbuf(of, NULL, _IOFBF , 1<<16) == 0)
           outputFile = of;
   signal(SIGINT, sigintHandler);
   
   err = ftdi_readstream(ftdi, readCallback, NULL, 8, 256);
   if (err < 0 && !exitRequested)
       exit(1);
   
   if (outputFile) {
       fclose(outputFile);
       outputFile = NULL;
   }
   fprintf(stderr, "Capture ended.\n");
   
   if (ftdi_set_bitmode(ftdi,  0xff, BITMODE_RESET) < 0)
   {
       fprintf(stderr,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_string(ftdi));
       ftdi_usb_close(ftdi);
       ftdi_free(ftdi);
       return EXIT_FAILURE;
   }
   ftdi_usb_close(ftdi);
   ftdi_free(ftdi);
   signal(SIGINT, SIG_DFL);
   if (check && outfile)
   {
       if ((outputFile = fopen(outfile,"r")) == 0)
       {
           fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno));
           ftdi_usb_close(ftdi);
           ftdi_free(ftdi);
           return EXIT_FAILURE;
       }
       check_outfile(descstring);
       fclose(outputFile);
   }
   else if (check)
       fprintf(stderr,"%d errors of %llu blocks (%Le), %d (%Le) blocks skipped\n",
               n_err, (unsigned long long) blocks, (long double)n_err/(long double) blocks,
               skips, (long double)skips/(long double) blocks);
   exit (0);
}