示例#1
0
/*
List connected TCTEC Relays
*/
void
listRelays()
{
	int             ret, i, bRelayStates;
	struct ftdi_device_list *devlist, *curdev;
	char            manufacturer[128], description[128], serial[128],
	                buf[1];
	if ((ret = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0) {
		fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return;
	}
	printf("Number of FTDI devices found: %d\n", ret);
	for (curdev = devlist; curdev != NULL; i++) {
		if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128)) < 0) {
			fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
			return;
		}
		if (strncmp(description, "TCTEC USB RELAY", 15) == 0) {
			printf("Manufacturer: %s, Description: %s, Serial: %s\n\n", manufacturer, description, serial);
		}
		curdev = curdev->next;
	}
	ftdi_list_free(&devlist);
	ftdi_deinit(&ftdic);
	return;
}
static struct ftdi_context *open_ftdi_device(int vid, int pid,
					     int interface, char *serial)
{
	struct ftdi_context *ftdi;
	int ret;

	ftdi = ftdi_new();
	if (!ftdi) {
		fprintf(stderr, "Cannot allocate context memory\n");
		return NULL;
	}

	ret = ftdi_set_interface(ftdi, interface);
	if (ret < 0) {
		fprintf(stderr, "cannot set ftdi interface %d: %s(%d)\n",
			interface, ftdi_get_error_string(ftdi), ret);
		goto open_failed;
	}
	ret = ftdi_usb_open_desc(ftdi, vid, pid, NULL, serial);
	if (ret < 0) {
		fprintf(stderr, "unable to open ftdi device: %s(%d)\n",
			ftdi_get_error_string(ftdi), ret);
		goto open_failed;
	}
	return ftdi;

open_failed:
	ftdi_free(ftdi);
	return NULL;
}
示例#3
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);
}
示例#4
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;
}
示例#5
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));
	}
}
/**
 * Close the USB port and reset the sequencer logic.
 *
 * @param devc The struct containing private per-device-instance data.
 *
 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
 */
static int close_usb_reset_sequencer(struct dev_context *devc)
{
	/* Magic sequence of bytes for resetting the sequencer logic. */
	uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
	int ret;

	/* Note: Caller checked that devc and devc->ftdic != NULL. */

	if (devc->ftdic->usb_dev) {
		/* Reset the sequencer logic, then wait 100ms. */
		sr_dbg("Resetting sequencer logic.");
		(void) cv_write(devc, buf, 8); /* Ignore errors. */
		g_usleep(100 * 1000);

		/* Purge FTDI buffers, then reset and close the FTDI device. */
		sr_dbg("Purging buffers, resetting+closing FTDI device.");

		/* Log errors, but ignore them (i.e., don't abort). */
		if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
			sr_err("Failed to purge FTDI buffers (%d): %s.",
			       ret, ftdi_get_error_string(devc->ftdic));
		if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
			sr_err("Failed to reset FTDI device (%d): %s.",
			       ret, ftdi_get_error_string(devc->ftdic));
		if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
			sr_err("Failed to close FTDI device (%d): %s.",
			       ret, ftdi_get_error_string(devc->ftdic));
	}

	/* Close USB device, deinitialize and free the FTDI context. */
	ftdi_free(devc->ftdic);
	devc->ftdic = NULL;

	return SR_OK;
}
示例#7
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;
}
示例#8
0
文件: isp861.c 项目: vasik041/openlab
int tx(int d)
{
uint8_t cmd[] = { 0x37, 7, d };
uint8_t ans = 0;
int i,f;
    f = ftdi_write_data(ftdi, cmd, 3);
    if(f != 3) {
	fprintf(stderr, "unable to write command3 to ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	err = 1;
	return 0;
    }
    
    for(i=0; i < 10; i++) {    
	f = ftdi_read_data(ftdi,&ans,1);
	if(f == 0) {
	    usleep(1);
	    continue;
	}
        if(f == 1) break;
	else {
    	    fprintf(stderr, "unable to read data from ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	    err = 1;
	    return 0;
	}
    }

    return ans & 0xff;
}
SR_PRIV int scanaplus_get_device_id(struct dev_context *devc)
{
	int ret;
	uint16_t val1, val2;

	/* FTDI EEPROM indices 16+17 contain the 3 device ID bytes. */
	if ((ret = ftdi_read_eeprom_location(devc->ftdic, 16, &val1)) < 0) {
		sr_err("Failed to read EEPROM index 16 (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		return SR_ERR;
	}
	if ((ret = ftdi_read_eeprom_location(devc->ftdic, 17, &val2)) < 0) {
		sr_err("Failed to read EEPROM index 17 (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		return SR_ERR;
	}

	/*
	 * Note: Bit 7 of the three bytes must not be used, apparently.
	 *
	 * Even though the three bits can be either 0 or 1 (we've seen both
	 * in actual ScanaPLUS devices), the device ID as sent to the FPGA
	 * has bit 7 of each byte zero'd out.
	 *
	 * It is unknown whether bit 7 of these bytes has any meaning,
	 * whether it's used somewhere, or whether it can be simply ignored.
	 */
	devc->devid[0] = ((val1 >> 0) & 0xff) & ~(1 << 7);
	devc->devid[1] = ((val1 >> 8) & 0xff) & ~(1 << 7);
	devc->devid[2] = ((val2 >> 0) & 0xff) & ~(1 << 7);

	return SR_OK;
}
示例#10
0
int Write_JTAG(struct ftdi_context * handle, unsigned char value, unsigned char direction, unsigned int len)
{
        unsigned char cmd [3] = { 0x00 };
        int ret = 0;
        int i = 0;

        if((ret = ftdi_usb_purge_tx_buffer (handle)) < 0)
        {
            fprintf(stderr, "ftdi_usb_purge_tx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        for(i = 0; i < len; i ++)
        {
                cmd[0] = 0x82;
                cmd[1] = value;
                cmd[2] = direction;

                if((ret = ftdi_write_data (handle, cmd, 3)) < 0)
                {
                    fprintf(stderr, "ftdi_write_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
                    return EXIT_FAILURE;
                }
        }

        return ret;
}
示例#11
0
static int scanaplus_write(struct dev_context *devc, uint8_t *buf, int size)
{
	int i, bytes_written;
	GString *s;

	/* Note: Caller checks devc, devc->ftdic, buf, size. */

	s = g_string_sized_new(100);
	g_string_printf(s, "Writing %d bytes: ", size);
	for (i = 0; i < size; i++)
		g_string_append_printf(s, "0x%02x ", buf[i]);
	sr_spew("%s", s->str);
	g_string_free(s, TRUE);

	bytes_written = ftdi_write_data(devc->ftdic, buf, size);
	if (bytes_written < 0) {
		sr_err("Failed to write FTDI data (%d): %s.",
		       bytes_written, ftdi_get_error_string(devc->ftdic));
	} else if (bytes_written != size) {
		sr_err("FTDI write error, only %d/%d bytes written: %s.",
		       bytes_written, size, ftdi_get_error_string(devc->ftdic));
	}

	return bytes_written;
}
示例#12
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;
}
示例#13
0
int Read_JTAG(struct ftdi_context * handle, unsigned char * buff, unsigned int len)
{
        unsigned char cmd [2] = { 0x00 };
        int ret = 0;

        cmd[0] = 0x83;
        cmd[1] = 0x87;

        if((ret = ftdi_usb_purge_rx_buffer (handle)) < 0)
        {
            fprintf(stderr, "ftdi_usb_purge_rx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_usb_purge_tx_buffer (handle)) < 0)
        {
            fprintf(stderr, "ftdi_usb_purge_tx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_write_data (handle, cmd, 2)) < 0)
        {
            fprintf(stderr, "ftdi_write_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        if((ret = ftdi_read_data (handle, buff, len)) < 0)
        {
            fprintf(stderr, "ftdi_read_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
            return EXIT_FAILURE;
        }

        return ret;
}
示例#14
0
//
// Open the serial port.
// Initialise ftdi_context and use it to open the device
static dc_status_t serial_ftdi_open (void **userdata, const char* name)
{
	// Allocate memory.
	ftdi_serial_t *device = (ftdi_serial_t *) malloc (sizeof (ftdi_serial_t));
	if (device == NULL) {
		SYSERROR (context, errno);
		return DC_STATUS_NOMEMORY;
	}

	struct ftdi_context *ftdi_ctx = ftdi_new();
	if (ftdi_ctx == NULL) {
		free(device);
		SYSERROR (context, errno);
		return DC_STATUS_NOMEMORY;
	}

	// Library context.
	//device->context = context;

	// Default to blocking reads.
	device->timeout = -1;

	// Default to full-duplex.
	device->halfduplex = 0;
	device->baudrate = 0;
	device->nbits = 0;

	// Initialize device ftdi context
	ftdi_init(ftdi_ctx);

	if (ftdi_set_interface(ftdi_ctx,INTERFACE_ANY)) {
		free(device);
		ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
		return DC_STATUS_IO;
	}

	if (serial_ftdi_open_device(ftdi_ctx) < 0) {
		free(device);
		ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
		return DC_STATUS_IO;
	}

	if (ftdi_usb_reset(ftdi_ctx)) {
		free(device);
		ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
		return DC_STATUS_IO;
	}

	if (ftdi_usb_purge_buffers(ftdi_ctx)) {
		free(device);
		ERROR (context, "%s", ftdi_get_error_string(ftdi_ctx));
		return DC_STATUS_IO;
	}

	device->ftdi_ctx = ftdi_ctx;

	*userdata = device;

	return DC_STATUS_SUCCESS;
}
示例#15
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;
}
示例#16
0
int Write_bus(struct ftdi_context * handle, unsigned char haddr, unsigned char laddr, unsigned char * buff, unsigned int len)
{
	unsigned char cmd [4] = { 0x00 };
	int ret = 0;
	int i = 0;

	if((ret = ftdi_usb_purge_tx_buffer (handle)) < 0)
	{
	    fprintf(stderr, "ftdi_usb_purge_tx_buffer failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
	    return EXIT_FAILURE;
	}

	for(i = 0; i < len; i ++)
	{
		cmd[0] = 0x93;
		cmd[1] = haddr;
		cmd[2] = laddr;
		cmd[3] = buff[i];

		if((ret = ftdi_write_data (handle, cmd, 4)) < 0)
		{
		    fprintf(stderr, "ftdi_write_data failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic));
		    return EXIT_FAILURE;
		}
	}

	return ret;
}
示例#17
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;
	}
}
示例#18
0
文件: api.c 项目: anatol/libsigrok
static int dev_open(struct sr_dev_inst *sdi)
{
	struct dev_context *devc;
	int ret;

	if (!(devc = sdi->priv))
		return SR_ERR_BUG;

	/* Allocate memory for the FTDI context and initialize it. */
	if (!(devc->ftdic = ftdi_new())) {
		sr_err("Failed to initialize libftdi.");
		return SR_ERR;
	}

	sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
	       devc->usb_vid, devc->usb_pid);

	/* Open the device. */
	if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
			devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
		sr_err("Failed to open FTDI device (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_ftdi_free;
	}
	sr_dbg("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 buffers (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_ftdi_free;
	}
	sr_dbg("FTDI buffers purged successfully.");

	/* Enable flow control in the FTDI chip. */
	if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
		sr_err("Failed to enable FTDI flow control (%d): %s.",
		       ret, ftdi_get_error_string(devc->ftdic));
		goto err_ftdi_free;
	}
	sr_dbg("FTDI flow control enabled successfully.");

	/* Wait 100ms. */
	g_usleep(100 * 1000);

	sdi->status = SR_ST_ACTIVE;

	if (ret == SR_OK)
		return SR_OK;

err_ftdi_free:
	ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
	devc->ftdic = NULL;
	return ret;
}
示例#19
0
void list_devices() {
	struct ftdi_context *ftdi;
	struct ftdi_device_list *dev_list;
	struct ftdi_device_list *dev_current;
	int num_devices;

	ftdi = ftdi_new();
	if (!ftdi) {
		fprintf(stderr, "failed to initialize ftdi context\n");
		exit(1);
	}

	num_devices = ftdi_usb_find_all(ftdi, &dev_list, VID, PID);
	if (num_devices < 0) {
		fprintf(stderr, "ftdi error: %s\n",
			ftdi_get_error_string(ftdi));
		goto error;
	}

	if (!num_devices) {
		printf("unable to find saturn device!\n");
		goto done;
	}

	dev_current = dev_list;
	num_devices = 0;
	while (dev_current) {
		char manufacturer[255];
		char description[255];
		char serial[255];
		if (ftdi_usb_get_strings(ftdi, dev_current->dev,
					 manufacturer, 255,
					 description, 255,
					 serial, 255) < 0) {
			fprintf(stderr, "ftdi error: %s\n",
				ftdi_get_error_string(ftdi));
			goto done;
		}

		if (strcmp(DESCRIPTION, description) == 0)
			printf("%d: %s (%s, %s)\n", num_devices++, description,
			       manufacturer, serial);

		dev_current = dev_current->next;
	}

	if (!num_devices)
		printf("unable to find saturn device!\n");

done:
	ftdi_list_free(&dev_list);
error:
	ftdi_free(ftdi);
}
示例#20
0
int
main (void)
{
  unsigned char buf[128];
  unsigned char rbuf[128];
  int ret;
  int i;
  struct ftdi_context ftdicon;
  struct ftdi_context *ftdic = &ftdicon;
  unsigned char d;

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

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

  fprintf (stdout, "FTDI Opened. Starting SPI init\n");

  ft2232_spi_init (ftdic);

  // Enable accelerometer
  buf[0] = 0x20;
  buf[1] = 0x67;
  ret = ft2232_spi_send_command (ftdic, 2, 0, buf, rbuf);

  while (1)
    {
      buf[0] = ReadAccelerometerRegister (ftdic, ACC_X_AXIS);
      buf[1] = ReadAccelerometerRegister (ftdic, ACC_Y_AXIS);
      buf[2] = ReadAccelerometerRegister (ftdic, ACC_Z_AXIS);
      printf ("X: 0x%02x\tY: 0x%02x\tZ: 0x%02x\n", buf[0], buf[1], buf[2]);
      usleep (10);
    }

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

  ftdi_deinit (ftdic);

  return EXIT_SUCCESS;
}
示例#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 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;
}
示例#23
0
文件: api.c 项目: adlerweb/libsigrok
static int dev_open(struct sr_dev_inst *sdi)
{
	struct dev_context *devc;
	int ret;

	if (!(devc = sdi->priv)) {
		sr_err("%s: sdi->priv was NULL.", __func__);
		return SR_ERR_BUG;
	}

	sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
	       devc->usb_pid);

	/* Open the device. */
	if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
			devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
		sr_err("%s: ftdi_usb_open_desc: (%d) %s",
		       __func__, ret, ftdi_get_error_string(devc->ftdic));
		(void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
		return SR_ERR;
	}
	sr_dbg("Device opened successfully.");

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

	/* Enable flow control in the FTDI chip. */
	if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
		sr_err("%s: ftdi_setflowcontrol: (%d) %s",
		       __func__, ret, ftdi_get_error_string(devc->ftdic));
		(void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
		goto err_dev_open_close_ftdic;
	}
	sr_dbg("FTDI flow control enabled successfully.");

	/* Wait 100ms. */
	g_usleep(100 * 1000);

	sdi->status = SR_ST_ACTIVE;

	return SR_OK;

err_dev_open_close_ftdic:
	(void) la8_close(devc); /* Log, but ignore errors. */
	return SR_ERR;
}
示例#24
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;

}
示例#25
0
int main(void)
{
    struct ftdi_context ftdic;
    int f;
    unsigned char buf[1];
    unsigned char bitmask;
    unsigned char input[10];

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

    f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
    if (f < 0 && f != -5)
    {
        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
        exit(-1);
    }
    printf("ftdi open succeeded: %d\n",f);

    while (1)
    {
        // Set bitmask from input
        fgets(input, sizeof(input) - 1, stdin);
        if (input[0] == '\n') break;
        bitmask = strtol(input, NULL, 0);
        printf("Using bitmask 0x%02x\n", bitmask);
        f = ftdi_set_bitmode(&ftdic, bitmask, BITMODE_CBUS);
        if (f < 0)
        {
            fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask, f, ftdi_get_error_string(&ftdic));
            exit(-1);
        }

        // read CBUS
        f = ftdi_read_pins(&ftdic, &buf[0]);
        if (f < 0)
        {
            fprintf(stderr, "read_pins failed, error %d (%s)\n", f, ftdi_get_error_string(&ftdic));
            exit(-1);
        }
        printf("Read returned 0x%01x\n", buf[0] & 0x0f);
    }
    printf("disabling bitbang mode\n");
    ftdi_disable_bitbang(&ftdic);

    ftdi_usb_close(&ftdic);
    ftdi_deinit(&ftdic);
}
示例#26
0
int GetFTDIDevicePortNumUSingLibFTDI(UInt32 locationIDFromInterface)
{
    int ret, i;
    struct ftdi_context ftdic;
    struct ftdi_device_list *devlist, *curdev;
    char manufacturer[128], description[128];
    
    if (ftdi_init(&ftdic) < 0)
    {
        fprintf(stderr, "ftdi_init failed\n");
        return -1;
    }
    
    if ((ret = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0)
    {
        fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
        return -1;
    }
    
    printf("Number of FTDI devices found: %d\n", ret);
    
    i = 0;
    for (curdev = devlist; curdev != NULL; i++)
    {
        //printf("Checking device: %d\n", i);
        if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, NULL, 0)) < 0)
        {
            fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
            return -1;
        }
        uint32_t *locationId = (uint32_t *)malloc(sizeof(uint32_t)); 
        
        xlibusb_get_device_location_id((struct libusb_device *)curdev->dev->dev,locationId);
        printf("Manufacturer: %s, Description: %s\n\n", manufacturer, description);
        if ((*locationId) == locationIDFromInterface) {
             free(locationId);
             return i;
        }
        
        free(locationId);
        //printf("0x%x deviceDesc[%d] %s",(unsigned int)&(deviceDesc[i]),i,deviceDesc[i]);
        curdev = curdev->next;
    }
    
    ftdi_list_free(&devlist);
    ftdi_deinit(&ftdic);
    
    return -1;
}
示例#27
0
int main(int argc, char **argv)
{
    struct ftdi_context *ftdi;
    int f;
    unsigned char buf[1];
    int retval = 0;

    if ((ftdi = ftdi_new()) == 0)
    {
        fprintf(stderr, "ftdi_new failed\n");
        return EXIT_FAILURE;
    }
//    fprintf(stderr, "ftdi_new result = %d\n", ftdi);


    f = ftdi_usb_open(ftdi, 0x0403, 0x6001);
//    fprintf(stderr, "ftdi_usb_open result = %d\n", f);


    if (f < 0 && f != -5)
    {
        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
        retval = 1;
        goto done;
    }

//    printf("ftdi open succeeded: %d\n",f);

//    printf("enabling bitbang mode\n");
    ftdi_set_bitmode(ftdi, 0x02, BITMODE_BITBANG);

//    usleep(3 * 1000000);

    buf[0] = 0xff;
//    printf("turning everything on\n");
    f = ftdi_write_data(ftdi, buf, 1);
    if (f < 0)
    {
        fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(ftdi));
    }

//    ftdi_disable_bitbang(ftdi);

    ftdi_usb_close(ftdi);
    ftdi_free(ftdi);
done:
    return retval;
}
示例#28
0
static dc_status_t serial_ftdi_write (void *io, const void *data, size_t size, size_t *actual)
{
	ftdi_serial_t *device = io;

	if (device == NULL)
		return DC_STATUS_INVALIDARGS;

	unsigned int nbytes = 0;
	while (nbytes < size) {

		int n = ftdi_write_data (device->ftdi_ctx, (unsigned char *) data + nbytes, size - nbytes);
		if (n < 0) {
			if (n == LIBUSB_ERROR_INTERRUPTED)
				continue; // Retry.
			ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
			return DC_STATUS_IO; // Error during write call.
		} else if (n == 0) {
			break; // EOF.
		}

		nbytes += n;
	}

	INFO (device->context, "Wrote %d bytes", nbytes);

	if (actual)
		*actual = nbytes;

	return DC_STATUS_SUCCESS;
}
示例#29
0
void ftdi_fatal (struct ftdi_context *ftdi, char *str)
{
    fprintf (stderr, "%s: %s\n",
             str, ftdi_get_error_string (ftdi));
    ftdi_free(ftdi);
    exit (1);
}
示例#30
0
文件: protocol.c 项目: jhol/libsigrok
/**
 * Read a certain amount of bytes from the LA8's FTDI device.
 *
 * @param devc The struct containing private per-device-instance data. Must not
 *            be NULL. devc->ftdic must not be NULL either.
 * @param buf The buffer where the received data will be stored. Must not
 *            be NULL.
 * @param size The number of bytes to read. Must be >= 1.
 * @return The number of bytes read, or a negative value upon errors.
 */
SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size)
{
	int bytes_read;

	/* Note: Caller checked that devc and devc->ftdic != NULL. */

	if (!buf) {
		sr_err("%s: buf was NULL.", __func__);
		return SR_ERR_ARG;
	}

	if (size <= 0) {
		sr_err("%s: size was <= 0.", __func__);
		return SR_ERR_ARG;
	}

	bytes_read = ftdi_read_data(devc->ftdic, buf, size);

	if (bytes_read < 0) {
		sr_err("%s: ftdi_read_data: (%d) %s.", __func__,
		       bytes_read, ftdi_get_error_string(devc->ftdic));
	} else if (bytes_read != size) {
		// sr_err("%s: Bytes to read: %d, bytes read: %d.",
		//        __func__, size, bytes_read);
	}

	return bytes_read;
}