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;
	}
示例#2
0
/*
 * use libftdi and libusb to send command
 * no kernel driver needed
 */
int usbWriteFtdi(char *cmdstr)
{
    struct ftdi_context ctx;
    int device=0x0c30, vendor=0x1781;

    if (ftdi_init( &ctx )) {
        fprintf(stderr,  "usb - init error !\n");
        return 1;
    }

    if (ftdi_usb_open(&ctx, vendor, device)) {
        fprintf(stderr,  "usb - open error (cannot find?) !\n");
        ftdi_deinit( &ctx );
        return 2;
    }

    if (ftdi_usb_reset( &ctx )) {
        fprintf(stderr,  "usb - reset error !\n");
        ftdi_usb_close( &ctx );
        ftdi_deinit( &ctx );
        return 3;
    }

    ftdi_disable_bitbang( &ctx );
    ftdi_set_baudrate(&ctx, BAUD);

    ftdi_write_data( &ctx, cmdstr, strlen(cmdstr) );
    sleep(1); /* just for sure */
    ftdi_usb_close( &ctx );
    ftdi_deinit( &ctx );

    return 0;
}
示例#3
0
static int ux400_gps_sys_init()
{
	int ret, i;
	struct ftdi_device_list *devlist, *curdev;
	char manufacturer[128], description[128], serialno[128];

	if (ftdi_init(&ux400_gps_ftdic) < 0)
	{
		fprintf(stderr, "ftdi_init failed\n");
		return EXIT_FAILURE;
	}

	ftdi_set_interface(&ux400_gps_ftdic, UX400_RS232_A);

	if((ret = ftdi_usb_open_desc(&ux400_gps_ftdic, UX400VENDOR, UX400PRODUCT, UX400DES, UX400_RS232_SN)) < 0)
	{
	    fprintf(stderr, "ftdi_usb_open_desc failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_gps_ftdic));
	    return EXIT_FAILURE;
	}

	// Set baudrate
	ret = ftdi_set_baudrate(&ux400_gps_ftdic, GPS_BAUDRATE);
	if (ret < 0)
	{
		fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ux400_gps_ftdic));
		return EXIT_FAILURE;
	}

	return 0;
}
示例#4
0
文件: rs485-test.c 项目: GBert/misc
int init_rs485(struct rs485_data_t *rs485_data) {
    int ret;

    rs485_data->ftdi = ftdi_new();

    if (!rs485_data->ftdi) {
	fprintf(stderr, "ftdi_new failed\n");
	return -1;
    }

    /* check for FT232RL device */
    if ((ret = ftdi_usb_open(rs485_data->ftdi, 0x0403, 0x6001)) < 0) {
	fprintf(stderr, "unable to open FTDI device: %d (%s)\n", ret, ftdi_get_error_string(rs485_data->ftdi));
	ftdi_free(rs485_data->ftdi);
	return EXIT_FAILURE;
    }

    if ((ret = ftdi_set_baudrate(rs485_data->ftdi, rs485_data->speed)) < 0) {
	fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(rs485_data->ftdi));
	ftdi_free(rs485_data->ftdi);
	return EXIT_FAILURE;
    }

    if ((ret = ftdi_setflowctrl(rs485_data->ftdi, SIO_DISABLE_FLOW_CTRL)) < 0) {
        fprintf(stderr, "unable to set flow control: %d (%s)\n", ret, ftdi_get_error_string(rs485_data->ftdi));
        return EXIT_FAILURE;
    }
    return 0;
}
示例#5
0
bool IntanThread::foundInputSource()
{

    //std::cout << "Checking for input source." << std::endl;
    if (simulateDataStream)
    {
        return true;
    }

    if (deviceFound)
    {
        int return_value;
        // try to update the baud rate
        if ((return_value = ftdi_set_baudrate(&ftdic, baudrate)) < 0)
        {
            deviceFound = false;
            return false;
        }
    }
    else
    {
        // try to initialize USB
        if (!initializeUSB(false))
        {
            return false;
        }
        else
        {
            deviceFound = true;
        }
    }

    return true;

}
示例#6
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;
}
示例#7
0
bool GovernorFtdi::open(OpenMode mode)
{
    int ret;

    if(ftdi_init(&ftdic) < 0)
        qDebug("ftdi_init failed");

    ret = ftdi_set_interface(&ftdic, INTERFACE_B);
    if(ret < 0){
        qDebug("Error: unable to set interface: %d (%s)", ret, ftdi_get_error_string(&ftdic));
        return false;
    }

    ret = ftdi_usb_open(&ftdic, 0x0403, 0x6010);
    if(ret < 0){
        qDebug("Error: unable to open ftdi device: %d (%s)", ret, ftdi_get_error_string(&ftdic));
        return false;
    }

    ret = ftdi_set_baudrate(&ftdic, 115200);
    if(ret < 0){
        qDebug("Error: unable to set baudrate: %d (%s)", ret, ftdi_get_error_string(&ftdic));
        return false;
    }

    readThread = new GovernorFtdiReadThread(this, &ftdic);
    connect(readThread, SIGNAL(readyRead()), this, SLOT(on_readThread_readyRead()));
    readThread->start();

    QIODevice::open(mode);

    return true;
}
示例#8
0
/**
 * Will initialize the ftdi connection.
 * @return 0 on success.
 */
int IncrementParser::InitRS232() {
	int ret  = 100;

	if ((ftdi_ = ftdi_new()) == 0){
		fprintf(stderr, "ftdi_new failed\n");
		return EXIT_FAILURE;
	}

	if ((ret = ftdi_usb_open(ftdi_, 0x0403, 0x6001)) < 0){
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi_));
		ftdi_free(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_set_baudrate(ftdi_, baudrate_);
	printf("Baudrate set to: %d\n", baudrate_);
	init_done_ = true;
	return EXIT_SUCCESS;
}
示例#9
0
void spi_init(void) {
	int ret;

	ftdi = ftdi_new();
	if (!ftdi) {
		fatal_error("ftdi_new failed!\n");
	}

	ret = ftdi_usb_open(ftdi, 0x0403, 0x6001);

	if (ret < 0 && ret != -5) {
		ftdi_free(ftdi);
		fatal_error("unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}

	ret = ftdi_set_bitmode(ftdi, PINS_OUT, BITMODE_SYNCBB);
	if (ret != 0) {
		ftdi_free(ftdi);
		fatal_error("unable to set bitmode: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}

	ret = ftdi_set_baudrate(ftdi, 57600);
	if (ret != 0) {
		ftdi_disable_bitbang(ftdi);
		ftdi_free(ftdi);
		fatal_error("unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}
}
示例#10
0
文件: ft245r.c 项目: dreimers/avrdude
static int ft245r_set_bitclock(PROGRAMMER * pgm) {
    int r;
    int rate = 0;

    /* bitclock is second. 1us = 0.000001. Max rate for ft232r 750000 */
    if(pgm->bitclock) {
        rate = (uint32_t)(1.0/pgm->bitclock) * 2;
    } else if (pgm->baudrate) {
        rate = pgm->baudrate * 2;
    } else {
        rate = 150000; /* should work for all ftdi chips and the avr default internal clock of 1MHz */
    }

    if (FT245R_DEBUG) {
        avrdude_message(MSG_NOTICE2, " ft245r:  spi bitclk %d -> ft baudrate %d\n",
                rate / 2, rate);
    }
    r = ftdi_set_baudrate(handle, rate);
    if (r) {
        avrdude_message(MSG_INFO, "Set baudrate (%d) failed with error '%s'.\n",
                rate, ftdi_get_error_string (handle));
        return -1;
    }
    return 0;
}
示例#11
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;
}
示例#12
0
void OBDDevice::setupFTDI() {
    if ( !(ftdi = ftdi_new()) ) {
        qDebug() << __FILE__ << __LINE__ << ": Failed to create ftdi context";
        return;
    }

    if ( ftdi_set_interface( ftdi, INTERFACE_ANY ) < 0 ) {
        qDebug() << __FILE__ << __LINE__ << ": Unable to set interface: " << ftdi_get_error_string( ftdi );
        return;
    }

    if ( ftdi_usb_open_desc( ftdi, VID, PID, DESCRIPTION, NULL ) < 0 ) {
        qDebug() << __FILE__ << __LINE__ << ": Unable to open ftdi device: " << ftdi_get_error_string( ftdi );
        return;
    }

    if ( ftdi_set_baudrate( ftdi, BAUDRATE ) < 0 ) {
        qDebug() << __FILE__ << __LINE__ << ": Unable to set baudrate: " << ftdi_get_error_string( ftdi );
        return;
    }

    if ( ftdi_set_line_property( ftdi, BITS_8, STOP_BIT_1, NONE ) < 0 ) {
        qDebug() << __FILE__ << __LINE__ << ": Unable to set line parameters: " << ftdi_get_error_string( ftdi );
        return;
    }
	ftdi_usb_purge_buffers(ftdi);
}
示例#13
0
static int dmx_init(struct ftdi_context* ftdic)
{
    int ret;

    if (ftdi_init(ftdic) < 0)
    {
        fprintf(stderr, "ftdi_init failed\n");
        return EXIT_FAILURE;
    }

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

    if ((ret = ftdi_set_baudrate(ftdic, 250000)) < 0)
	{
        fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(ftdic));
        return EXIT_FAILURE;
	}

	if ((ret = ftdi_set_line_property(ftdic, BITS_8, STOP_BIT_2, NONE)) < 0)
	{
        fprintf(stderr, "unable to set line property: %d (%s)\n", ret, ftdi_get_error_string(ftdic));
        return EXIT_FAILURE;
	}

    if ((ret = ftdi_setflowctrl(ftdic, SIO_DISABLE_FLOW_CTRL)) < 0)
	{
        fprintf(stderr, "unable to set flow control: %d (%s)\n", ret, ftdi_get_error_string(ftdic));
        return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}
示例#14
0
文件: ics.c 项目: amuthelet/kondo
/*-----------------------------------------------------------------------------
 * Iniitialize the ICS interface
 * Here we mainly setup the FTDI USB-to-serial communication
 * 115200 baud, 8 bits, even parity, 1 stop bit.
 * Returns: 0 if successful, < 0 otherwise
 */
int ics_init(ICSData * r)
{
	assert(r);
	r->debug = 1;

	// init usb
	if (ftdi_init(&r->ftdic) < 0)
		ics_ftdi_error(r, "ics_init (init usb)");

	// select first interface
	if (ftdi_set_interface(&r->ftdic, INTERFACE_C) < 0)
		ics_ftdi_error(r, "ics_init (select interface)");

	// open usb device
	if (ftdi_usb_open(&r->ftdic, ICS_USB_VID, ICS_USB_PID) < 0)
		ics_ftdi_error(r, "ics_init (open usb device)");

	// set baud rate
	if (ftdi_set_baudrate(&r->ftdic, ICS_BAUD) < 0)
		ics_ftdi_error(r, "ics_init (set baud rate)");

	// set line parameters (8E1)
	if (ftdi_set_line_property(&r->ftdic, BITS_8, STOP_BIT_1, EVEN) < 0)
		ics_ftdi_error(r, "ics_init (set line params)");

	return 0;
}
示例#15
0
static int jtagkey_init(unsigned short vid, unsigned short pid) {
	int ret = 0;
	unsigned char c;

	if ((ret = ftdi_init(&ftdic)) != 0) {
		fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}
	
	if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) {
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_usb_reset(&ftdic)) != 0) {
		fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) {
		fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE))  != 0) {
		fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE))  != 0) {
		fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0)
		return ret;

	c = 0x00;
	ftdi_write_data(&ftdic, &c, 1);

	if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB))  != 0) {
		fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED))  != 0) {
		fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_usb_purge_buffers(&ftdic))  != 0) {
		fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	return ret;
}
示例#16
0
bool EnttecDMXUSBOpen::open()
{
	if (isOpen() == false)
	{
		if (ftdi_usb_open_desc(&m_context, EnttecDMXUSBWidget::VID,
						   EnttecDMXUSBWidget::PID,
						   name().toAscii(),
						   serial().toAscii()) < 0)
		{
			qWarning() << "Unable to open" << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			return false;
		}

		if (ftdi_usb_reset(&m_context) < 0)
		{
			qWarning() << "Unable to reset" << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			return close();
		}

		if (ftdi_set_line_property(&m_context, BITS_8, STOP_BIT_2, NONE) < 0)
		{
			qWarning() << "Unable to set 8N2 serial properties to"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			return close();
		}

		if (ftdi_set_baudrate(&m_context, 250000) < 0)
		{
			qWarning() << "Unable to set 250kbps baudrate for"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			return close();
		}

		if (ftdi_setrts(&m_context, 0) < 0)
		{
			qWarning() << "Unable to set RTS line to 0 for"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			return close();
		}

		if (isRunning() == false)
			start();

		return true;
	}
	else
	{
		/* Already open */
		return true;
	}
}
示例#17
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 */



}
示例#18
0
QString LibFTDIInterface::readLabel(uchar label, int *ESTA_code)
{
    if (ftdi_usb_open_desc(&m_handle, DMXInterface::FTDIVID, DMXInterface::FTDIPID,
                           name().toLatin1().data(), serial().toLatin1().data()) < 0)

        return QString();

    if (ftdi_usb_reset(&m_handle) < 0)
        return QString();

    if (ftdi_set_baudrate(&m_handle, 250000) < 0)
        return QString();

    if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0)
        return QString();

    if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0)
        return QString();

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

    if (ftdi_write_data(&m_handle, (uchar*) request.data(), request.size()) < 0)
    {
        qDebug() << Q_FUNC_INFO << "Cannot write data to device";
        return QString();
    }

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

    QByteArray array;
    usleep(300000); // give some time to the device to respond
    int read = ftdi_read_data(&m_handle, buffer, 40);
    //qDebug() << Q_FUNC_INFO << "Data read: " << read;
    array = QByteArray::fromRawData((char*) buffer, read);

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

    //for (int i = 0; i < array.size(); i++)
    //    qDebug() << "-Data: " << array[i];
    ftdi_usb_close(&m_handle);

    return QString(array);
}
示例#19
0
bool QLCFTDI::setBaudRate()
{
    if (ftdi_set_baudrate(&m_handle, 250000) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}
示例#20
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; 
}
示例#21
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);
}
示例#22
0
static int
arduiggler_init (urj_cable_t *cable)
{
    params_t *params = cable->params;
    urj_tap_cable_cx_cmd_root_t *cmd_root = &params->cmd_root;

    if (urj_tap_usbconn_open (cable->link.usb) != URJ_STATUS_OK)
        return URJ_STATUS_FAIL;

    /* need to change the default baud rate from libftdi.c
     * to the actual one used by the cable
     */
    ftdi_param_t *fp = cable->link.usb->params;
    int r = ftdi_set_baudrate(fp->fc, BAUD_RATE);

    if (r != 0) {
        urj_warning (_("cannot change baud rate\n"));
        return URJ_STATUS_FAIL;
    }

    urj_tap_cable_cx_cmd_queue (cmd_root, 0);
    urj_tap_cable_cx_cmd_push (cmd_root, CMD_RESET);
    urj_tap_cable_cx_xfer (cmd_root, NULL, cable, URJ_TAP_CABLE_COMPLETELY);

    int ar_status = arduiggler_get_status(cable);

    if (ar_status != STATUS_OK) {
        urj_warning (_("cable not initialized properly\n"));
        return URJ_STATUS_FAIL;
    }

    urj_tap_cable_cx_cmd_queue (cmd_root, 0);
    urj_tap_cable_cx_cmd_push (cmd_root, CMD_GETVER);
    urj_tap_cable_cx_xfer (cmd_root, NULL, cable, URJ_TAP_CABLE_COMPLETELY);

    char ar_swver[] = "    ";

    for (int i = 0; i < strlen(ar_swver); i++) {
      ar_swver[i] = urj_tap_cable_cx_xfer_recv (cable);
    }
    urj_log (URJ_LOG_LEVEL_NORMAL, "Arduiggler firmware: %s\n", ar_swver);

    ar_status = arduiggler_get_status(cable);

    if (ar_status != STATUS_OK) {
        urj_warning (_("cable not initialized properly\n"));
        return URJ_STATUS_FAIL;
    }
    //arduiggler_set_frequency (cable, 0);

    return URJ_STATUS_OK;
}
示例#23
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;
	}
示例#24
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
}
示例#25
0
文件: spi.c 项目: z80/bt-board
int spi_set_clock(unsigned long spi_clk) {
    unsigned long ftdi_clk;

    LOG(DEBUG, "(%lu)", spi_clk);

    if (!spi_isopen()) {
        SPI_ERR("FTDI: setting SPI clock failed: SPI device is not open");
        return -1;
    }

    if (spi_clk > spi_max_clock)
        spi_clk = spi_max_clock;

    spi_clock = spi_clk;
    /* FTDI clock in Hz is 2 * SPI clock, FTDI clock in Hz */
    ftdi_clk = spi_clock * 2000;

    /* Flush the buffer before setting clock */
    if (ftdi_buf_write_offset) {
        if (spi_ftdi_xfer(ftdi_buf, ftdi_buf_write_offset) < 0)
            return -1;
        /* The data in the buffer is useless, discard it */
        ftdi_buf_write_offset = 0;
    }

    /*
     * See FT232R datasheet, section "Baud Rate Generator" and AppNote
     * AN_232R-01, section "Synchronous Bit Bang Mode". Also see this thread on
     * bitbang baud rate hardware bug in FTDI chips (XXX is this related to
     * syncbb mode?):
     * http://developer.intra2net.com/mailarchive/html/libftdi/2010/msg00240.html
     */
    LOG(INFO, "FTDI: setting SPI clock to %lu (FTDI baudrate %lu)", spi_clk, ftdi_clk / 16);
    if (ftdi_set_baudrate(&ftdic, ftdi_clk / 16) < 0) {
        SPI_ERR("FTDI: set baudrate %lu failed: %s",
                ftdi_clk / 16, ftdi_get_error_string(&ftdic));
        return -1;
    }

#ifdef SPI_STATS
    if (spi_stats.spi_clock_max == 0)
        spi_stats.spi_clock_max = spi_max_clock;
    if (spi_stats.spi_clock_min == 0)
        spi_stats.spi_clock_min = spi_max_clock;
    /* Don't account for slow cmds, that are executing at 20 kHz,
     * they are short and not representative */
    if (spi_clock > 20 && spi_clock < spi_stats.spi_clock_min)
            spi_stats.spi_clock_min = spi_clock;
#endif
    return 0;
}
示例#26
0
bool IntanThread::initializeUSB(bool verbose)
{
    int return_value;

    // Step 1: initialise the ftdi_context:
    if (ftdi_init(&ftdic) < 0)  // -1 = couldn't allocate read buffer
    {
        // -2 = couldn't allocate struct buffer
        if (verbose)
            fprintf(stderr, "ftdi_init failed\n");
        return false;
    }
    else
    {
        if (verbose)
            std::cout << "FTDI context initialized." << std::endl;
    }

    // Step 2: open USB device
    // -3 = device not found
    // -8 = wrong permissions
    if ((return_value = ftdi_usb_open(&ftdic, vendorID, productID)) < 0)
    {
        if (verbose)
            fprintf(stderr, "unable to open FTDI device: %d (%s)\n",
                    return_value,
                    ftdi_get_error_string(&ftdic));
        return false;
    }
    else
    {
        std::cout << "USB connection opened." << std::endl;
    }

    // Step 3: set the baud rate
    if ((return_value = ftdi_set_baudrate(&ftdic, baudrate)) < 0)
    {
        if (verbose)
            fprintf(stderr, "unable to set baud rate: %d (%s)\n",
                    return_value,
                    ftdi_get_error_string(&ftdic));
        return false;
    }
    else
    {
        std::cout << "Baud rate set to 115200" << std::endl;
    }

    return true;

}
示例#27
0
ReturnCode reader_connect(struct reader *reader,
			  int beep) {
  ReturnCode rc;

  if(reader->ftdic == NULL || reader->dev == NULL) {
    printf("reader entry null\n");
    return RC_NULL_ERROR;
  }

  if(ftdi_usb_open_dev(reader->ftdic, reader->dev) < 0){
    printf("ftdi usb open dev failed\n");
    return RC_IO_ERROR;
  }

  if((rc = reader_reset(reader)) != RC_SUCCESS){
    printf("reset failed\n");
    ftdi_usb_close(reader->ftdic);
    return rc;
  }

  if((rc = reader_purge(reader)) != RC_SUCCESS){
    printf("purge failed\n");
    ftdi_usb_close(reader->ftdic);
    return rc;
  }

  if(ftdi_set_baudrate(reader->ftdic, RFID1_BAUDRATE) < 0){
    printf("setbaudrate failed\n");
    ftdi_usb_close(reader->ftdic);
    return RC_IO_ERROR;
  }

  usleep(10000);


  if((rc = reader_ping(reader)) != RC_SUCCESS){
    ftdi_usb_close(reader->ftdic);
    return rc;
  }

  reader->connected = 1;

  if(beep) {
    reader_pass_beep(reader);
  }


  return RC_SUCCESS;
}
示例#28
0
static int ublast_ftdi_speed(struct ublast_lowlevel *low, int speed)
{
	struct ftdi_context *ftdic = ublast_getftdic(low);

	LOG_DEBUG("TODO: ublast_speed() isn't optimally implemented!");
	/* TODO: libftdi's ftdi_set_baudrate chokes on high rates, use lowlevel
	 * usb function instead! And additionally allow user to throttle.
	 */
	if (ftdi_set_baudrate(ftdic, 3000000 / 4) < 0) {
		LOG_ERROR("Can't set baud rate to max: %s",
			  ftdi_get_error_string(ftdic));
		return ERROR_JTAG_DEVICE_ERROR;
	}
	return ERROR_OK;
}
示例#29
0
int hftdi::ftdi_usb_open()
{
//    struct

    if(m_usb->claim_interface(m_ftdi->interface) < 0)
        return  -1;
    if(ftdi_usb_reset() != 0)
        return  -6;
    m_ftdi->type = TYPE_232H;

    if(ftdi_set_baudrate(9600) != 0)
        return  -7;

    return  0;
}
示例#30
0
void ftdi_bootup()
{
  if(ftdi_init(&ftdi)!=0)
    printf("ftdi - could not be opened.\nr");
  else
    printf("booting up.\n");

  if(ftdi_usb_open_desc(&ftdi,0x0403, 0x6001,NULL/*description*/,usb_serial)!=0)
    printf("ftdi - error.\n");
  else
  {
    printf("ftdi opened.\n");
    ftdi_set_baudrate(&ftdi,115200);
  }
}