Beispiel #1
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;
}
Beispiel #2
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
int main(int argc, char *argv[])
{
    int num_devs;
    struct ftdi_context *ctx;
    struct ftdi_device_list *devs = NULL;

    (void)argc;
    (void)argv;

    ctx = ftdi_new();
    if (!ctx) {
        printf("Initialization error.\n");
        return 1;
    }

    num_devs = ftdi_usb_find_all(ctx, &devs, 0, 0);
    if (num_devs < 0) {
        printf("Device list error: %s.\n", ftdi_get_error_string(ctx));
        ftdi_free(ctx);
        return 2;
    }

    printf("Found %d FTDI devices.\n", (int)num_devs);

    ftdi_list_free(&devs);

    ftdi_free(ctx);

    return 0;
}
Beispiel #5
0
/*
 * Return a list of devices found by libftdi together with a number of fields
 * describing the corresponding USB device.
 * The list is internally cached. To free it, call freeDeviceList().
 *
 * Returns: a vector pointer containing 0 or more devices, or NULL if an error
 * occured while retrieving the list. When NULL is returned, this is guaranteed
 * to be an ftdi error.
 */
const FtdiDevice::vec_deviceInfo* FtdiDevice::getDeviceList( ftdi_context* c )
{
	ftdi_context* context = c ? c : ftdi_new();
	if ( context == 0 ) return 0;
	
	if ( s_deviceList != 0 ) freeDeviceList();
	
	int r = ftdi_usb_find_all( context, &s_ftdiDeviceList, USB_VENDOR_ID, USB_PRODUCT_ID );
	s_deviceList = new vec_deviceInfo();
	if ( r >= 0 ) {
		struct ftdi_device_list* openDev = s_ftdiDeviceList;
		while ( openDev ) {
			struct deviceInfo info;
			info.ftdiDevice = openDev->dev;
			info.usbInfo = fetchUsbInformation( context, info.ftdiDevice );
			s_deviceList->push_back( info );
			
			openDev = openDev->next;
		}
	}
	
	//Only free the context if it has been allocated locally.
	if ( c == 0 ) ftdi_free( context );
	return s_deviceList;
}
Beispiel #6
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));
	}
}
Beispiel #7
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;
}
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);
}
Beispiel #9
0
FtdiWrapper::FtdiWrapper(unsigned int vid, unsigned int pid, string description,
                         string serialNum)
{
    try
    {
        //construct connection context
        ftdiContext = ftdi_new();
        if(ftdiContext == NULL)
            throw runtime_error("unable to initialize ftdi driver");

        //open connection
        const char *desc = ( description.empty() ? NULL : description.c_str() );
        const char *serial = ( serialNum.empty() ? NULL : serialNum.c_str() );
        int rtn = ftdi_usb_open_desc(ftdiContext, vid, pid, desc, serial);
        if(rtn < 0)
        {
            stringstream msg;
            msg << "unable to open ftdi connection: ";
            msg << ftdi_get_error_string(ftdiContext);
//            msg << " (Do you have sufficient privileges to access the usb bus? Is the device connected?)";
            throw runtime_error( msg.str() );
        }
    }
    catch(...)
    {
        ftdi_free(ftdiContext);
        throw;
    }
}
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;
}
Beispiel #11
0
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;
}
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);
}
Beispiel #13
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; 
}
Beispiel #14
0
int main(void)
{
  struct ftdi_context *ftdi;
  int retval = EXIT_SUCCESS;

  if ((ftdi = ftdi_new()) == 0)
  {
    fprintf(stderr, "ftdi_new failed\n");
    return EXIT_FAILURE;
  }
  
  return retval;
}
Beispiel #15
0
int ftdi_context_refresh(ftdi_context_t* context) {
  struct usb_bus* libusb_bus;
  struct usb_device* libusb_device;
  struct ftdi_context* libftdi_context;
  int i = 0;

  error_clear(&context->error);
  
  if (context->num_references) {
    if (context->num_devices) {
      for (i = 0; i < context->num_devices; ++i)
        ftdi_device_destroy(&context->devices[i]);
      
      free(context->devices);
      context->devices = 0;
      context->num_devices = 0;
    }

    usb_init();
    if ((usb_find_busses() > 0) && (usb_find_devices() > 0)) {
      for (libusb_bus = usb_get_busses(); libusb_bus;
          libusb_bus = libusb_bus->next)
        for (libusb_device = libusb_bus->devices; libusb_device;
            libusb_device = libusb_device->next)
          if (libusb_device->descriptor.idVendor == FTDI_VENDOR_ID)
            context->num_devices++;
    }
    
    if (context->num_devices) {
      context->devices = malloc(context->num_devices*sizeof(ftdi_device_t));
      
      i = 0;
      for (libusb_bus = usb_get_busses(); libusb_bus;
          libusb_bus = libusb_bus->next)
        for (libusb_device = libusb_bus->devices; libusb_device;
            libusb_device = libusb_device->next)
          if (libusb_device->descriptor.idVendor == FTDI_VENDOR_ID) {
        libftdi_context = ftdi_new();
        ftdi_init(libftdi_context);
      
        ftdi_device_init(&context->devices[i], libftdi_context, libusb_bus,
          libusb_device);
        i++;
      }
    }
  }
  else
    error_set(&context->error, FTDI_ERROR_INVALID_CONTEXT);
  
  return error_get(&context->error);
}
Beispiel #16
0
int main(int argc, char *argv[])
{
	int ret;
	int i;
	struct ftdi_context *ftdi;
	unsigned int chipid;
	unsigned char buf[256];
	track *tracks;
	int ntracks;


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

	printf("type=%d\n", ftdi->type);
	ftdi_read_chipid(ftdi, &chipid);
	printf("chipid=%x\n", chipid);


	setup(ftdi);

	ftdi_setrts(ftdi, 1);
	while((ret = ftdi_read_data(ftdi, buf, 256)) > 0) {
		for(i = 0; i < ret; ++i)
			printf("%02X ", buf[i]);
	}
	ftdi_setrts(ftdi, 0);

	read_version(ftdi);
	read_product(ftdi);

	read_list(ftdi, &tracks, &ntracks);

	if (argc > 1) {
		i = atoi(argv[1]);
		printf("tracks[%d].no=%d\n", i, tracks[i].no);
		read_track_points(ftdi, &tracks[i], i);
	}
	free(tracks);

	ftdi_usb_close(ftdi);
	ftdi_free(ftdi);

	return 0;
}
Beispiel #17
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;
}
Beispiel #18
0
//Constructor
Dionysus::Dionysus(bool debug){
  printf ("%s(): Entered\n", __func__);
  //Open up Dionysus
  this->debug = debug;
  this->usb_is_open = false;
  this->comm_mode = false;
  this->usb_constructor();
  if (this->debug) printf ("Dionysus: Debug Enabled\n");
  //Open up a context and initialize it
  this->ftdi = ftdi_new();
  int retval = 0;
  retval = ftdi_init(this->ftdi);
  if (retval != 0){
    printf ("Error initializing FTDI Context\n");
  }
}
Beispiel #19
0
int main(void)
{
    int ret;
    struct ftdi_context *ftdi;
    struct ftdi_version_info version;
    if ((ftdi = ftdi_new()) == 0)
   {
        fprintf(stderr, "ftdi_new failed\n");
        return EXIT_FAILURE;
    }
    version = ftdi_get_library_version();
    printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n",
        version.version_str, version.major, version.minor, version.micro,
        version.snapshot_str);
//    // own stuff
//    struct ftdi_device_list *devList;
//    if ((ret = ftdi_usb_find_all(ftdi,devList, 0, 0)) < 0)
//    {
//    	fprintf(stderr, "unable to find devices: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
//		ftdi_free(ftdi);
//        return EXIT_FAILURE;
//    }


    if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x6014)) < 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_232H)
    {
        unsigned int chipid;
        printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
        printf("FTDI chipid: %X\n", chipid);
    }

    if ((ret = ftdi_usb_close(ftdi)) < 0)
    {
        fprintf(stderr, "unable to close ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        ftdi_free(ftdi);
        return EXIT_FAILURE;
    }
    ftdi_free(ftdi);
    return EXIT_SUCCESS;
}
Beispiel #20
0
static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
	int ret;
	unsigned int i;
	GSList *devices;
	struct ftdi_context *ftdic;

	(void)di;
	(void)options;

	devices = NULL;

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

	/* Check for LA8 and/or LA16 devices with various VID/PIDs. */
	for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
		ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
			vid_pid[i].pid, vid_pid[i].iproduct, NULL);
		/* Show errors other than "device not found". */
		if (ret < 0 && ret != -3)
			sr_dbg("Error finding/opening device (%d): %s.",
			       ret, ftdi_get_error_string(ftdic));
		if (ret < 0)
			continue; /* No device found, or not usable. */

		sr_dbg("Found %s device (%04x:%04x).",
		       vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid);

		if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0)
			sr_dbg("Failed to add device: %d.", ret);

		if ((ret = ftdi_usb_close(ftdic)) < 0)
			sr_dbg("Failed to close FTDI device (%d): %s.",
			       ret, ftdi_get_error_string(ftdic));
	}

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

	return devices;
}
Beispiel #21
0
void ftdi_reader_create() {
    if ((ftdi_c = ftdi_new()) == 0)
    {
        Throw(FTDI_NEW_FAILED);
    }

    if (ftdi_usb_open(ftdi_c, FTDI_VID, FTDI_PID)!=0) {
        ftdi_free(ftdi_c);
        Throw(FTDI_USB_OPEN_FAILED);
    }
    
    if (ftdi_set_baudrate(ftdi_c, 3000000)!=0) {
        ftdi_usb_close(ftdi_c);
        ftdi_free(ftdi_c);
        Throw(FTDI_SET_BAUDRATE_FAILED);
    }
}
Beispiel #22
0
int main(int argc, char **argv)
{
    struct ftdi_context *ftdi;
    int i, t;
    unsigned char data;
    int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */

    while ((t = getopt (argc, argv, "d:")) != -1)
    {
        switch (t)
        {
            case 'd':
                delay = atoi (optarg);
                break;
        }
    }

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

    if (ftdi_usb_open(ftdi, 0x0403, 0x6010) < 0)
        ftdi_fatal (ftdi, "Can't open ftdi device");

    if (ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG) < 0)
        ftdi_fatal (ftdi, "Can't enable bitbang");

    for (i=optind; i < argc ; i++)
    {
        sscanf (argv[i], "%x", &t);
        data = t;
        if (ftdi_write_data(ftdi, &data, 1) < 0)
        {
            fprintf(stderr,"write failed for 0x%x: %s\n",
                    data, ftdi_get_error_string(ftdi));
        }
        usleep(delay);
    }

    ftdi_usb_close(ftdi);
    ftdi_free(ftdi);
    exit (0);
}
void relay_setup (void) {
	int f;

	if ((ftdi = ftdi_new()) == 0) {
        printf("ftdi_new failed\nFatal error, exiting\n");
		exit (1);
    }

    f = ftdi_usb_open(ftdi, 0x0403, 0x6001);
    if (f < 0 && f != -5) {
        printf("Unable to open ftdi device %s\nFatal error, exiting\n", ftdi_get_error_string(ftdi));
        ftdi_free(ftdi);
		exit (1);
    }

    ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
    return;
}
Beispiel #24
0
int init()
{
int f;
    if ((ftdi = ftdi_new()) == 0) {
        fprintf(stderr, "ftdi_new failed\n");
        return -1;
    }

    f = ftdi_usb_open(ftdi, 0x0403, 0x6010);
    if (f < 0 && f != -5) {
        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	ftdi_free(ftdi);
	ftdi = NULL;
        return -1;
    }

    f = ftdi_set_bitmode(ftdi, 0xff, BITMODE_MPSSE);
    if(f != 0) {
	fprintf(stderr, "unable to set bit mode: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	release();
	return -1;
    }

    uint8_t p[] = { 0x80, 0, 3 };
    f = ftdi_write_data(ftdi, p, 3);
    if(f != 3) {
	fprintf(stderr, "unable to write command to ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	release();
	return -1;
    }

    //Set TCK/SK Divisor 0x86, 0xValueL, 0xValueH
    uint8_t p1[] = { 0x86, 0, 1 };	//250KHz
//  uint8_t p1[] = { 0x86, 0, 10 };	//25KHz
    
    f = ftdi_write_data(ftdi, p1, 3);
    if(f != 3) {
	fprintf(stderr, "unable to write command2 to ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
	release();
	return -1;
    }

    return 0;
}
Beispiel #25
0
int main(int argc, char **argv)
{
    struct ftdi_context *ftdi;
    int f = 0;
    int vid = 0x403;  /* USB vendor ID  -- use 'lsusb' to see */
    int pid = 0x6001; /* USB product ID */
    tpl_node *tn;
    tn = tpl_map("cccc",&R,&G,&B,&D);

    if ((ftdi = ftdi_new()) == 0) {
        fprintf(stderr, "ftdi_new failed\n");
        return -1;
    }

    ftdi_set_interface(ftdi, INTERFACE_ANY);
    f = ftdi_usb_open(ftdi, vid, pid);
    if (f < 0)
    {
        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
        fprintf(stderr, "you may need to run as root\n");
        return -1;
    }

    f = ftdi_write_data(ftdi, enttec_msg, sizeof(enttec_msg));
    while(tpl_load(tn, TPL_FD, 0) == 0) {
      tpl_unpack(tn,0);
      fprintf(stderr,"writing %d %d %d %d\n", R,G,B,D);
      enttec_msg[6] = R;
      enttec_msg[7] = G;
      enttec_msg[8] = B;
      enttec_msg[9] = 0;
      enttec_msg[10] = D;
      ftdi_write_data(ftdi, enttec_msg, sizeof(enttec_msg));
    }


    ftdi_usb_close(ftdi);
    ftdi_free(ftdi);
    tpl_free(tn);
    return 0;
}
Beispiel #26
0
CKMotionIO::CKMotionIO()
{
	ConsoleHandler=NULL;

	Mutex = new CMutex(FALSE,"KMotionIO",0);

	m_Connected=false;
	SendAbortOnConnect=true;
	FailMessageAlreadyShown=false;
	Token=0;
	NonRespondingCount=0;
	m_FirmwareVersion=2;
	USB_Loc_ID=-1;
  BoardIDAssigned=false;

  if ((ftdi = ftdi_new()) == 0)
  {
    log_info("ftdi_new failed.");
    exit(EXIT_FAILURE);
  }
}
Beispiel #27
0
/* prepare the FTDI USB device */
int do_init(struct ftdi2s88_t *fs88) {
    fs88->ftdic = ftdi_new();

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

    if (ftdi_usb_open_desc(fs88->ftdic, 0x0403, 0x6001, NULL, NULL) < 0) {
    /* if (ftdi_usb_open_desc(ftdic, 0x0403, 0x6015, NULL, NULL) < 0) { */
	fprintf(stderr, "ftdi_usb_open_desc failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_set_bitmode(fs88->ftdic, BITBANG_MASK, BITMODE_SYNCBB) < 0) {
	fprintf(stderr, "ftdi_set_bitmode failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_set_baudrate(fs88->ftdic, fs88->baudrate) < 0) {
	fprintf(stderr, "ftdi_set_baudrate failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_usb_purge_tx_buffer(fs88->ftdic)) {
	fprintf(stderr, "tx buffer flushing failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    if (ftdi_usb_purge_rx_buffer(fs88->ftdic)) {
	fprintf(stderr, "rx buffer flushing failed: %s\n", ftdi_get_error_string(fs88->ftdic));
	return -1;
    }

    return 0;
}
Beispiel #28
0
//
// Open the serial port.
// Initialise ftdi_context and use it to open the device
static dc_status_t serial_ftdi_open (void **io, dc_context_t *context)
{
	INFO(0, "serial_ftdi_open called");
	// Allocate memory.
	ftdi_serial_t *device = (ftdi_serial_t *) malloc (sizeof (ftdi_serial_t));
	if (device == NULL) {
		INFO(0, "couldn't allocate memory");
		SYSERROR (context, errno);
		return DC_STATUS_NOMEMORY;
	}
	INFO(0, "setting up ftdi_ctx");
	struct ftdi_context *ftdi_ctx = ftdi_new();
	if (ftdi_ctx == NULL) {
		INFO(0, "failed ftdi_new()");
		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->baudrate = 0;
	device->nbits = 0;
	device->databits = 0;
	device->stopbits = 0;
	device->parity = 0;

	// Initialize device ftdi context
	INFO(0, "initialize ftdi_ctx");
	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;
	}

	INFO(0, "call serial_ftdi_open_device");
	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;

	*io = device;

	return DC_STATUS_SUCCESS;
}
Beispiel #29
0
int main(void)
{
		//set interrupt handler for CTRL+C
    signal(SIGINT,sigintHandler);
    int ret;
    struct ftdi_context *ftdi;
    struct ftdi_version_info version;
    if ((ftdi = ftdi_new()) == 0)
   {
        fprintf(stderr, "ftdi_new failed\n");
        return EXIT_FAILURE;
    }

    version = ftdi_get_library_version();
    printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n",
        version.version_str, version.major, version.minor, version.micro,
        version.snapshot_str);

    if ((ret = ftdi_usb_open(ftdi, 0x0403, 0x6014)) < 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_232H)
    {
        unsigned int chipid;
        printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(ftdi, &chipid));
        printf("FTDI chipid: %X\n", chipid);
    }
    printf("Resetting...");
    if ((ret = ftdi_set_bitmode(ftdi, 0x00, BITMODE_RESET)) != 0)
    {
        fprintf(stderr, "unable to reset: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        return EXIT_FAILURE;
    }
    printf(" success!\n");

    printf("Setting mode 245...");
    if ((ret = ftdi_set_bitmode(ftdi, 0x00, BITMODE_SYNCFF)) != 0)
    {
        fprintf(stderr, "unable to set to mode 245: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        return EXIT_FAILURE;
    }
    printf(" success!\n");


    //ACBUS5 already outputs the 30MHz clock

    //Is this the bit that caused the error? Try commenting out for now and see what it does.
    /*
    printf("Setting ACBUS[8] to 30 MHz...");
    if ((ret = ftdi_set_eeprom_value(ftdi,CBUS_FUNCTION_8,CBUSH_CLK30)) != 0)
    {

        fprintf(stderr, " unable to set: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        return EXIT_FAILURE;

    }
    else
    {
	printf(" success!\n");
    }
    

       //lets make sure that the eeprom value was set....
    int val;
    ret = ftdi_get_eeprom_value(ftdi,CBUS_FUNCTION_8,&val);
    printf("ret: %d  val: %d\n",ret,val);
    printf("does val match %d?\n",CBUSH_CLK30);
    */
    unsigned char buf[1024];
    int its = 10000;
    return;
    while(quit == 0)
    {

        //ret = ftdi_read_data(&ftdic,&buf,1);
        if ((ret = ftdi_read_data(ftdi,buf,3)) < 0)
        {
            fprintf(stderr, "unable to read ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
            //return EXIT_FAILURE;
        }
	else
	{
	    printf("Read in %x %x %x\n", buf[0],buf[1],buf[2]);
	}
    }
    if ((ret = ftdi_usb_close(ftdi)) < 0)
    {
        fprintf(stderr, "unable to close ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
        ftdi_free(ftdi);
        return EXIT_FAILURE;
    }

    ftdi_free(ftdi);

    return EXIT_SUCCESS;
}
Beispiel #30
0
/*
 * Open an FTDI device. The device is selected with regard to the given restrictions;
 * any of those may be NULL and index is counted in the subset selected by the
 * description and/or serial if given.
 *
 * Returns: true if successfully opened, false otherwise.
 */
bool FtdiDevice::open( const char* description, const char* serial, int index )
{
	hasFtdiError_ = false;
	
	if ( isOpen() ) {
		hasFtdiError_ = false;
		return false;
	}
	
	context_ = ftdi_new();
	if ( context_ == 0 ) {
		hasFtdiError_ = true;
		return false;
	}
	
	bool success = false;
	vec_deviceInfo* devs = const_cast<vec_deviceInfo*>( getDeviceList( context_ ) );
	
	vec_deviceInfo::iterator it;
	
	if ( devs != 0 ) {
		for ( it = devs->begin(); it != devs->end(); ++it ) {
			const deviceInfo& di = *it;
			
			if ( description != 0 || serial != 0 ) {
				if ( di.usbInfo == 0 )
					break;
				
				if ( description != 0 && std::strncmp( di.usbInfo->description, description,
																							std::strlen( description ) ) != 0 )
					continue;
				
				if ( serial != 0 && std::strncmp( di.usbInfo->serial, serial,
																				 std::strlen( serial ) ) != 0 )
					continue;
			}
			
			if ( index-- != 0 )
				continue;
			
			//If control gets here, we have found a suitable device.
			if ( ftdi_usb_open_dev( context_, di.ftdiDevice ) < 0 ) {
				//do not free context since that would also free the error message
				hasFtdiError_ = true;
			}
			usbInfo_ = new usbInformation( *( di.usbInfo ) );
			success = true;
		}
	} else {
		hasFtdiError_ = ( devs == 0 );
	}
	
	if ( ! success && ! hasFtdiError_ ) {
		delete context_; context_ = 0;
	}
	
	if ( success ) {
		purgeBuffers();
		reset();
	}
	
	return success;
}