/*----------------------------------------------------------------------------- * 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; }
// // 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; }
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; }
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); }
void hftdi::init(bool isquick) { m_ftdi = (ftdi_context*)malloc(sizeof(ftdi_context)); m_usb = new husb(); m_usb->init(); m_ftdi->usb_ctx = NULL; m_ftdi->usb_dev = NULL; m_ftdi->usb_read_timeout = 5000; m_ftdi->usb_write_timeout = 5000; m_ftdi->type = TYPE_BM; /* chip type */ m_ftdi->baudrate = -1; m_ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */ m_ftdi->readbuffer = NULL; m_ftdi->readbuffer_offset = 0; m_ftdi->readbuffer_remaining = 0; m_ftdi->writebuffer_chunksize = 4096; m_ftdi->max_packet_size = 0; m_ftdi->error_str = NULL; m_ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE; m_ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */ m_ftdi->eeprom = 0; pthread_cond_init(&m_cond,NULL); pthread_cond_init(&m_cond_all_finish,NULL); if(!m_usb->is_running()){ LOGE("current usb_is not running so we call ftdi_set_interface()\n\n"); ftdi_set_interface(INTERFACE_ANY); } }
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; }
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; }
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; }
int ftdi_device_open(ftdi_device_t* dev, ftdi_interface_t interface) { error_clear(&dev->error); if (ftdi_set_interface(dev->libftdi_context, interface)) error_set(&dev->error, FTDI_ERROR_INVALID_INTERFACE); else if (ftdi_usb_open_dev(dev->libftdi_context, dev->libusb_device)) error_setf(&dev->error, FTDI_ERROR_OPEN, "%03:%03", dev->bus, dev->address); return error_get(&dev->error); }
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; }
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); }
int main(int argc, char *argv[]) { int ret; struct ftdi_context ftdic; struct adapter *adap; struct adapter_ops *ops; ops = &v2_ops; if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) print_usage(argv[0]); if (ftdi_init(&ftdic) < 0) { fprintf(stderr, "ftdi_init failed\n"); return EXIT_FAILURE; } ftdi_set_interface(&ftdic, ops->interface); if (argc > 1 && strchr(argv[1],':')) { ret = ftdi_usb_open_string(&ftdic, argv[1]); argv++; argc--; } else ret = ftdi_usb_open_desc(&ftdic, 0x0403, 0x6010, ops->desc, NULL); if (ret < 0) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic)); return EXIT_FAILURE; } adap = adapter_attach(&ftdic, ops, 0); if (!adap) { perror("attach"); return EXIT_FAILURE; } if (argc < 2) { short shunt_v; int shunt_v_sh; unsigned int bus_v; double shunt; double bus; double r = 0.050; double current; double power; unsigned char cmdbuf[] = {1, 2}; unsigned char msgbuf[2][2]; struct i2c_msg msgs[] = { { .addr = ADDR, .len = 1, .buf = cmdbuf }, { .addr = ADDR, .flags = I2C_M_RD, .len = 2, .buf = msgbuf[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; }
// // 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; }
int main(int argc, char **argv) { struct ftdi_context ftdic, ftdic2; char buf[1]; int f,i; // Init 1. channel if (ftdi_init(&ftdic) < 0) { fprintf(stderr, "ftdi_init failed\n"); return EXIT_FAILURE; } ftdi_set_interface(&ftdic, INTERFACE_A); 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(channel 1): %d\n",f); printf("enabling bitbang mode(channel 1)\n"); ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG); // Init 2. channel if (ftdi_init(&ftdic2) < 0) { fprintf(stderr, "ftdi_init failed\n"); return EXIT_FAILURE; } ftdi_set_interface(&ftdic2, INTERFACE_B); f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001); if (f < 0 && f != -5) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2)); exit(-1); } printf("ftdi open succeeded(channel 2): %d\n",f); printf("enabling bitbang mode (channel 2)\n"); ftdi_set_bitmode(&ftdic2, 0xFF, BITMODE_BITBANG); // Write data printf("startloop\n"); for (i = 0; i < 23; i++) { buf[0] = 0x1; printf("porta: %02i: 0x%02x \n",i,buf[0]); f = ftdi_write_data(&ftdic, buf, 1); if (f < 0) fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); sleep(1); buf[0] = 0x2; printf("porta: %02i: 0x%02x \n",i,buf[0]); f = ftdi_write_data(&ftdic, buf, 1); if (f < 0) fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); sleep(1); buf[0] = 0x1; printf("portb: %02i: 0x%02x \n",i,buf[0]); f = ftdi_write_data(&ftdic2, buf, 1); if (f < 0) fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); sleep(1); buf[0] = 0x2; printf("portb: %02i: 0x%02x \n",i,buf[0]); f = ftdi_write_data(&ftdic2, buf, 1); if (f < 0) fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); sleep(1); } printf("\n"); printf("disabling bitbang mode(channel 1)\n"); ftdi_disable_bitbang(&ftdic); ftdi_usb_close(&ftdic); ftdi_deinit(&ftdic); printf("disabling bitbang mode(channel 2)\n"); ftdi_disable_bitbang(&ftdic2); ftdi_usb_close(&ftdic2); ftdi_deinit(&ftdic2); }
Ft245sync::Ft245sync(unsigned int chunkSizeRead, unsigned int chunkSizeWrite, uint8_t gpio, struct ftdi_context * vftdic, struct ftdi_context * vftdic2) { if(vftdic == NULL) { this->ftdic = static_cast<struct ftdi_context*>(malloc(sizeof(struct ftdi_context))); } else { this->ftdic = vftdic; } if(vftdic2 == NULL) { this->ftdic2 = static_cast<struct ftdi_context*>(malloc(sizeof(struct ftdi_context))); } else { this->ftdic2 = vftdic2; } int f; // Init 1. channel if (ftdi_init(ftdic) < 0) { throw Exception("ftdi_init failure\n"); } ftdi_set_interface(ftdic, INTERFACE_A); f = ftdi_usb_open(ftdic, 0x0403, PID); if (f < 0 && f != -5) { throw Exception("Unable to open FTDI device, channel A\n"); } // Init 2. channel if (ftdi_init(ftdic2) < 0) { throw Exception("ftdi_init failure\n"); } ftdi_usb_reset(ftdic); ftdi_usb_reset(ftdic2); ftdi_set_interface(ftdic2, INTERFACE_B); f = ftdi_usb_open(ftdic2, 0x0403, PID); if (f < 0 && f != -5) { throw Exception("Unable to open FTDI device, channel B\n"); } ftdi_write_data_set_chunksize(ftdic2, 512); ftdi_set_interface(ftdic2, INTERFACE_B); ftdi_usb_reset(ftdic2); ftdi_set_latency_timer(ftdic2, 2); ftdi_setflowctrl(ftdic2, SIO_RTS_CTS_HS); ftdi_set_bitmode(ftdic2, 0, BBMODE_SPI); uint8_t buf[3]; buf[0] = SET_BITS_LOW; buf[1] = 8; buf[2] = BIT_DIR; //holding programming of FPGA*/ ftdi_write_data(ftdic2, buf, 3); buf[0] = SET_BITS_HIGH; buf[1] = 0xFF; //lighting leds buf[2] = BIT_DIR; ftdi_write_data(ftdic2, buf, 3); buf[0] = SET_BITS_HIGH; buf[1] = 0x00; //lighting leds buf[2] = BIT_DIR; ftdi_write_data(ftdic2, buf, 3); buf[0] = SET_BITS_LOW; buf[1] = gpio; buf[2] = BIT_DIR; //releasing programming of FPGA ftdi_write_data(ftdic2, buf, 3); sleep(1); buf[0] = SET_BITS_LOW; buf[1] = 0xFF; //reseting design in FPGA buf[2] = BIT_DIR; ftdi_write_data(ftdic2, buf, 3); sleep(1); buf[0] = SET_BITS_LOW; buf[1] = 0xDD; //releasing reset buf[2] = BIT_DIR; ftdi_write_data(ftdic2, buf, 3); sleep(1); buf[0] = SET_BITS_HIGH; buf[1] = 0xFF; //lighting leds buf[2] = BIT_DIR; ftdi_write_data(ftdic2, buf, 3); if (ftdi_usb_purge_buffers(ftdic2)) { throw Exception("Purging buffers failed\n"); } ftdi_usb_close(ftdic2); // close channel 2 ftdi_deinit(ftdic2); // close channel 2 ftdic->usb_read_timeout = READ_TIMEOUT; ftdic->usb_write_timeout = WRITE_TIMEOUT; ftdi_read_data_set_chunksize(ftdic, chunkSizeRead); ftdi_write_data_set_chunksize(ftdic, chunkSizeWrite); if (ftdi_usb_reset(ftdic)) { throw Exception("Reset failed\n"); } usleep(1000); if(ftdi_usb_purge_buffers(ftdic) < 0) { throw Exception("Setting FT2232 synchronous bitmode failed\n"); } if(ftdi_set_bitmode(ftdic, 0xFF, 0x00) < 0) { throw Exception("Setting FT2232 synchronous bitmode failed\n"); } if(ftdi_set_bitmode(ftdic, 0xFF, 0x40) < 0) { throw Exception("Setting FT2232 synchronous bitmode failed\n"); } if (ftdi_set_latency_timer(ftdic, 2)) /* AN_130 */ { throw Exception("Set latency failed failed\n"); } //SetUSBParameters(ftHandle,0x10000, 0x10000); if (ftdi_setflowctrl(ftdic, SIO_RTS_CTS_HS)) // AN_130 { throw Exception("Set RTS_CTS failed\n"); } /*if(ftdi_usb_purge_buffers(ftdic) < 0) { throw Exception("Setting FT2232 synchronous bitmode failed\n"); }*/ //fixes unalignment of first read (should be fixed in cleaner manner) usleep(400); unsigned char cleanup[10] = { 0xBB, 0xBB, 0xBB, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; ftdi_write_data(ftdic, cleanup, 10); unsigned char recvbuf[4000]; read(recvbuf); }
int Context::set_interface(enum ftdi_interface interface) { return ftdi_set_interface(d->ftdi, interface); }
static int dev_open(struct sr_dev_inst *sdi) { struct dev_context *devc; int ret; devc = sdi->priv; /* Select interface A, otherwise communication will fail. */ ret = ftdi_set_interface(devc->ftdic, INTERFACE_A); if (ret < 0) { sr_err("Failed to set FTDI interface A (%d): %s", ret, ftdi_get_error_string(devc->ftdic)); return SR_ERR; } sr_dbg("FTDI chip interface A set successfully."); /* Open the device. */ ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, USB_IPRODUCT, NULL); if (ret < 0) { sr_err("Failed to open device (%d): %s", ret, ftdi_get_error_string(devc->ftdic)); return SR_ERR; } sr_dbg("FTDI device opened successfully."); /* Purge RX/TX buffers in the FTDI chip. */ if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); goto err_dev_open_close_ftdic; } sr_dbg("FTDI chip buffers purged successfully."); /* Reset the FTDI bitmode. */ ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET); if (ret < 0) { sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); goto err_dev_open_close_ftdic; } sr_dbg("FTDI chip bitmode reset successfully."); /* Set FTDI bitmode to "sync FIFO". */ ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF); if (ret < 0) { sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); goto err_dev_open_close_ftdic; } sr_dbg("FTDI chip sync FIFO mode entered successfully."); /* Set the FTDI latency timer to 2. */ ret = ftdi_set_latency_timer(devc->ftdic, 2); if (ret < 0) { sr_err("Failed to set FTDI latency timer (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); goto err_dev_open_close_ftdic; } sr_dbg("FTDI chip latency timer set successfully."); /* Set the FTDI read data chunk size to 64kB. */ ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024); if (ret < 0) { sr_err("Failed to set FTDI read data chunk size (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); goto err_dev_open_close_ftdic; } sr_dbg("FTDI chip read data chunk size set successfully."); /* Get the ScanaPLUS device ID from the FTDI EEPROM. */ if ((ret = scanaplus_get_device_id(devc)) < 0) { sr_err("Failed to get ScanaPLUS device ID: %d.", ret); goto err_dev_open_close_ftdic; } sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.", devc->devid[0], devc->devid[1], devc->devid[2]); sdi->status = SR_ST_ACTIVE; return SR_OK; err_dev_open_close_ftdic: scanaplus_close(devc); return SR_ERR; }
int main(int argc, char **argv) { struct ftdi_context *ftdi; unsigned char buf[1024]; int f = 0, i; int vid = 0x403; int pid = 0; int baudrate = 115200; int interface = INTERFACE_ANY; int do_write = 0; unsigned int pattern = 0xffff; int retval = EXIT_FAILURE; while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1) { switch (i) { case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D interface = strtoul(optarg, NULL, 0); break; case 'v': vid = strtoul(optarg, NULL, 0); break; case 'p': pid = strtoul(optarg, NULL, 0); break; case 'b': baudrate = strtoul(optarg, NULL, 0); break; case 'w': do_write = 1; if (optarg) pattern = strtoul(optarg, NULL, 0); if (pattern > 0xff) { fprintf(stderr, "Please provide a 8 bit pattern\n"); exit(-1); } break; default: fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv); exit(-1); } } // Init if ((ftdi = ftdi_new()) == 0) { fprintf(stderr, "ftdi_new failed\n"); return EXIT_FAILURE; } if (!vid && !pid && (interface == INTERFACE_ANY)) { ftdi_set_interface(ftdi, INTERFACE_ANY); struct ftdi_device_list *devlist; int res; if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0) { fprintf(stderr, "No FTDI with default VID/PID found\n"); goto do_deinit; } if (res == 1) { f = ftdi_usb_open_dev(ftdi, devlist[0].dev); if (f<0) { fprintf(stderr, "Unable to open device %d: (%s)", i, ftdi_get_error_string(ftdi)); } } ftdi_list_free(&devlist); if (res > 1) { fprintf(stderr, "%d Devices found, please select Device with VID/PID\n", res); /* TODO: List Devices*/ goto do_deinit; } if (res == 0) { fprintf(stderr, "No Devices found with default VID/PID\n"); goto do_deinit; } } else { // Select interface ftdi_set_interface(ftdi, interface); // Open device 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)); exit(-1); } // Set baudrate f = ftdi_set_baudrate(ftdi, baudrate); if (f < 0) { fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi)); exit(-1); } /* Set line parameters * * TODO: Make these parameters settable from the command line * * Parameters are choosen that sending a continous stream of 0x55 * should give a square wave * */ f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE); if (f < 0) { fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi)); exit(-1); } if (do_write) for(i=0; i<1024; i++) buf[i] = pattern; signal(SIGINT, sigintHandler); while (!exitRequested) { if (do_write) f = ftdi_write_data(ftdi, buf, (baudrate/512 >sizeof(buf))?sizeof(buf): (baudrate/512)?baudrate/512:1); else f = ftdi_read_data(ftdi, buf, sizeof(buf)); if (f<0) sleep(1); else if(f> 0 && !do_write) { fprintf(stderr, "read %d bytes\n", f); fwrite(buf, f, 1, stdout); fflush(stderr); fflush(stdout); } } signal(SIGINT, SIG_DFL); retval = EXIT_SUCCESS; ftdi_usb_close(ftdi); do_deinit: ftdi_free(ftdi); return retval; }
int main(int argc, char **argv) { struct ftdi_context ftdic; char buf[16]; int r,i; // Initialize FTDI Driver if (ftdi_init(&ftdic) < 0) { fprintf(stderr, "ftdi_init failed\n"); return EXIT_FAILURE; } // Initialize Channel A on Device with corresponding VID, PID ftdi_set_interface(&ftdic, INTERFACE_A); r = ftdi_usb_open(&ftdic, DEVICE_VID, DEVICE_PID); if (r < 0 && r != -5) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", r, ftdi_get_error_string(&ftdic)); exit(-1); } printf("FTDI Open Succeeded on Channel A: %d\n",r); printf("Enabling MPSSE Mode on Channel A\n\n"); ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_MPSSE); // Enable the Div-By-5 Clock Prescaler buf[0] = EN_DIV_5; send_buf(&ftdic, buf, 1); recv_buf(&ftdic, buf, 1); // Configure the clock divisor for SPI buf[0] = TCK_DIVISOR; buf[1] = 0x00; // set to 0x0000 for 6MHz SPI Clock buf[2] = 0x00; send_buf(&ftdic, buf, 3); // Configure and check Pin State buf[0] = SET_BITS_LOW; buf[1] = 0x08; // 0x08 = 0b00001000 ==> CS=High, Start SK low buf[2] = 0x0B; // 0x0B = 0b00001011 ==> SK,DO,CS=Output send_buf(&ftdic, buf, 3); buf[0] = GET_BITS_LOW; send_buf(&ftdic, buf, 1); recv_buf(&ftdic, buf, 1); printf("Current State of IO Pins = 0x%02X\n\n", (uint8_t)buf[0]); SPI_SRAM_23K256_Init(&ftdic); SPI_SRAM_23K256_Read_Byte(&ftdic, 0x5555); SPI_SRAM_23K256_Write_Byte(&ftdic, 0x5555, ((uint8_t)(&buf)) ); // a random value SPI_SRAM_23K256_Read_Byte(&ftdic, 0x5555); SPI_SRAM_23K256_Read_Byte(&ftdic, 0x5556); printf("\nDisabling and Exiting FTDI Communication\n"); ftdi_disable_bitbang(&ftdic); ftdi_usb_close(&ftdic); ftdi_deinit(&ftdic); }
int main(int argc, char **argv) { struct ftdi_context *ftdi; int err, c; FILE *of = NULL; char const *outfile = 0; outputFile =0; exitRequested = 0; char *descstring = NULL; int option_index; static struct option long_options[] = {{NULL},}; while ((c = getopt_long(argc, argv, "P:n", long_options, &option_index)) !=- 1) switch (c) { case -1: break; case 'P': descstring = optarg; break; case 'n': check = 0; break; default: usage(argv[0]); } if (optind == argc - 1) { // Exactly one extra argument- a dump file outfile = argv[optind]; } else if (optind < argc) { // Too many extra args usage(argv[0]); } if ((ftdi = ftdi_new()) == 0) { fprintf(stderr, "ftdi_new failed\n"); return EXIT_FAILURE; } if (ftdi_set_interface(ftdi, INTERFACE_A) < 0) { fprintf(stderr, "ftdi_set_interface failed\n"); ftdi_free(ftdi); return EXIT_FAILURE; } if (ftdi_usb_open_desc(ftdi, 0x0403, 0x6014, descstring, NULL) < 0) { fprintf(stderr,"Can't open ftdi device: %s\n",ftdi_get_error_string(ftdi)); ftdi_free(ftdi); return EXIT_FAILURE; } /* A timeout value of 1 results in may skipped blocks */ if(ftdi_set_latency_timer(ftdi, 2)) { fprintf(stderr,"Can't set latency, Error %s\n",ftdi_get_error_string(ftdi)); ftdi_usb_close(ftdi); ftdi_free(ftdi); return EXIT_FAILURE; } /* if(ftdi_usb_purge_rx_buffer(ftdi) < 0) { fprintf(stderr,"Can't rx purge\n",ftdi_get_error_string(ftdi)); return EXIT_FAILURE; }*/ if (outfile) if ((of = fopen(outfile,"w+")) == 0) fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); if (of) if (setvbuf(of, NULL, _IOFBF , 1<<16) == 0) outputFile = of; signal(SIGINT, sigintHandler); err = ftdi_readstream(ftdi, readCallback, NULL, 8, 256); if (err < 0 && !exitRequested) exit(1); if (outputFile) { fclose(outputFile); outputFile = NULL; } fprintf(stderr, "Capture ended.\n"); if (ftdi_set_bitmode(ftdi, 0xff, BITMODE_RESET) < 0) { fprintf(stderr,"Can't set synchronous fifo mode, Error %s\n",ftdi_get_error_string(ftdi)); ftdi_usb_close(ftdi); ftdi_free(ftdi); return EXIT_FAILURE; } ftdi_usb_close(ftdi); ftdi_free(ftdi); signal(SIGINT, SIG_DFL); if (check && outfile) { if ((outputFile = fopen(outfile,"r")) == 0) { fprintf(stderr,"Can't open logfile %s, Error %s\n", outfile, strerror(errno)); ftdi_usb_close(ftdi); ftdi_free(ftdi); return EXIT_FAILURE; } check_outfile(descstring); fclose(outputFile); } else if (check) fprintf(stderr,"%d errors of %llu blocks (%Le), %d (%Le) blocks skipped\n", n_err, (unsigned long long) blocks, (long double)n_err/(long double) blocks, skips, (long double)skips/(long double) blocks); exit (0); }
int main(int argc, char **argv) { struct ftdi_context ftdi; uint8_t buf[4]; uint8_t conf_buf[] = {SET_BITS_LOW, 0x08, 0x0b, SET_BITS_HIGH, 0x00, 0x00, TCK_DIVISOR, 0x00, 0x00, LOOPBACK_END}; if (argc < 2) { usage(argv[0]); return 1; } if (strcmp (argv[1], "idcode") && strcmp (argv[1], "reset") && strcmp (argv[1], "load") && strcmp (argv[1], "readreg") && strcmp (argv[1], "read") && strcmp (argv[1], "write") ) { usage(argv[0]); return 1; } /* Init */ ftdi_init(&ftdi); if (ftdi_usb_open_desc(&ftdi, VENDOR, PRODUCT, 0, 0) < 0) { fprintf(stderr, "Can't open device %04x:%04x\n", VENDOR, PRODUCT); return 1; } ftdi_usb_reset(&ftdi); ftdi_set_interface(&ftdi, INTERFACE_A); ftdi_set_latency_timer(&ftdi, 1); ftdi_set_bitmode(&ftdi, 0xfb, BITMODE_MPSSE); if (ftdi_write_data(&ftdi, conf_buf, 10) != 10) { fprintf(stderr, "Can't configure device %04x:%04x\n", VENDOR, PRODUCT); return 1; } buf[0] = GET_BITS_LOW; buf[1] = SEND_IMMEDIATE; if (ftdi_write_data(&ftdi, buf, 2) != 2) { fprintf(stderr, "Can't send command to device\n"); return 1; } ftdi_read_data(&ftdi, &buf[2], 1); if (!(buf[2] & 0x10)) { fprintf(stderr, "Vref not detected. Please power on target board\n"); return 1; } if (!strcmp(argv[1], "idcode")) { uint8_t out[4]; tap_reset_rti(&ftdi); tap_shift_dr_bits(&ftdi, NULL, 32, out); rev_dump(out, 4); printf("\n"); } if (!strcmp (argv[1], "reset")) brd_reset(&ftdi); if (!strcmp (argv[1], "load")) { int i; struct load_bits *bs; FILE *fp; uint8_t *dr_data; uint32_t u; if(argc < 3) { usage(argv[0]); goto exit; } if (!strcmp(argv[2], "-")) fp = stdin; else { fp = fopen(argv[2], "r"); if (!fp) { perror("Unable to open file"); goto exit; } } bs = calloc(1, sizeof(*bs)); if (!bs) { perror("memory allocation failed"); goto exit; } if (load_bits(fp, bs) != 0) { fprintf(stderr, "%s not supported\n", argv[2]); goto free_bs; } printf("Bitstream information:\n"); printf("\tDesign: %s\n", bs->design); printf("\tPart name: %s\n", bs->part_name); printf("\tDate: %s\n", bs->date); printf("\tTime: %s\n", bs->time); printf("\tBitstream length: %d\n", bs->length); /* copy data into shift register */ dr_data = calloc(1, bs->length); if (!dr_data) { perror("memory allocation failed"); goto free_bs; } for (u = 0; u < bs->length; u++) dr_data[u] = rev8(bs->data[u]); brd_reset(&ftdi); tap_shift_ir(&ftdi, CFG_IN); tap_shift_dr_bits(&ftdi, dr_data, bs->length * 8, NULL); /* ug380.pdf * P161: a minimum of 16 clock cycles to the TCK */ tap_shift_ir(&ftdi, JSTART); for (i = 0; i < 32; i++) tap_tms(&ftdi, 0, 0); tap_reset_rti(&ftdi); free(dr_data); free_bs: bits_free(bs); fclose(fp); } if (!strcmp(argv[1], "readreg") && argc == 3) { int i; char *err; uint8_t reg; uint8_t out[2]; uint8_t dr_in[14]; uint8_t in[14] = { 0xaa, 0x99, 0x55, 0x66, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00 }; uint16_t cmd = 0x2801; /* type 1 packet (word count = 1) */ reg = strtol(argv[2], &err, 0); if((*err != 0x00) || (reg < 0) || (reg > 0x22)) { fprintf(stderr, "Invalid register, use a decimal or hexadecimal(0x...) number between 0x0 and 0x22\n"); goto exit; } cmd |= ((reg & 0x3f) << 5); in[4] = (cmd & 0xff00) >> 8; in[5] = cmd & 0xff; tap_reset_rti(&ftdi); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 0, 0); tap_tms(&ftdi, 0, 0); /* Goto shift IR */ tap_shift_ir_only(&ftdi, CFG_IN); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 0, 0); tap_tms(&ftdi, 0, 0); /* Goto SHIFT-DR */ for (i = 0; i < 14; i++) dr_in[i] = rev8(in[i]); tap_shift_dr_bits_only(&ftdi, dr_in, 14 * 8, NULL); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); /* Goto SELECT-IR */ tap_tms(&ftdi, 0, 0); tap_tms(&ftdi, 0, 0); /* Goto SHIFT-IR */ tap_shift_ir_only(&ftdi, CFG_OUT); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 0, 0); tap_tms(&ftdi, 0, 0); /* Goto SHIFT-IR */ tap_shift_dr_bits_only(&ftdi, NULL, 2 * 8, out); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); tap_tms(&ftdi, 1, 0); /* Goto SELECT-IR */ tap_tms(&ftdi, 0, 0); tap_tms(&ftdi, 0, 0); /* Goto SHIFT-IR */ tap_reset_rti(&ftdi); out[0] = rev8(out[0]); out[1] = rev8(out[1]); printf("REG[%d]: 0x%02x%02x\n", reg, out[0], out[1]); }
int main(void) { struct ftdi_context* ftdic; int result; // open ftdi a device ftdic = ftdi_new(); ftdi_set_interface(ftdic, TARGET_INTERFACE); result = ftdi_usb_open(ftdic, TARGET_VENDOR_ID, TARGET_PRODUCT_ID); // result < 0 : opened // result == -5 : ftdi_iso is running if ( result < 0 && result != -5 ) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", result, ftdi_get_error_string(ftdic)); exit(-1); } printf("ftdi open succeeded: %d\n", result); //ftdi_set_line_property(ftdic, // BITS_8, // bit count // STOP_BIT_1, // stop bit type // NONE); // parity type printf("type: %d\n", ftdic->type); printf("TYPE_2232H: %d\n", TYPE_2232H); // To set boardrate as 12Mbit/s, // libftdi version (maybe) 1 or more is needed // (This program is tested with using version 1.3) result = ftdi_set_baudrate(ftdic, TARGET_BAUDRATE); if ( result != 0 ) { fprintf(stderr, "failed to set baudrate: %d (%s)\n", result, ftdi_get_error_string(ftdic)); exit(-1); } printf("baudrate: %d\n", ftdic->baudrate); const char* command_chars = "abcd"; unsigned char sending_data[1]; unsigned char received_data[255]; int received_data_length = 0; for ( int i=0; i<4; i++ ) { // data send sending_data[0] = command_chars[i]; ftdi_write_data(ftdic, sending_data, 1); printf("Send: %c\n", sending_data[0]); // data receive received_data_length = ftdi_read_data(ftdic, received_data, 254); received_data[received_data_length] = 0; printf("Received: %s\n", received_data); sleep(1); } ftdi_usb_close(ftdic); ftdi_free(ftdic); return 0; }
void sample(uint32_t baudrate) { int vid = 0x403; int pid = 0x6010; struct ftdi_context *ftdi; if ((ftdi = ftdi_new()) == 0) { fprintf(stderr, "ftdi_new failed\n"); exit(EXIT_FAILURE); } ftdi_set_interface(ftdi, 1); ftdi_read_data_set_chunksize(ftdi, 1<<14); uint32_t chunksize = 0; ftdi_read_data_get_chunksize(ftdi,&chunksize); printf("%i\n\r",chunksize); int f = 0; 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)); exit(-1); } f = ftdi_set_baudrate(ftdi, baudrate); if (f < 0) { fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi)); exit(-1); } f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE); if (f < 0) { fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi)); exit(-1); } FILE * raw = fopen("raw.bin","w"); if (raw == NULL) { fprintf(stderr, "Value of errno: %d\n", errno); perror("Error printed by perror"); fprintf(stderr, "Error opening file: %s\n", strerror(errno)); exit(EXIT_FAILURE); } int total_bytes = 0; #define BUFFERLENGTH (uint16_t)2000 uint8_t buffer[BUFFERLENGTH]; uint16_t count = 0; while (!exit_requested) { count = ftdi_read_data(ftdi, buffer, BUFFERLENGTH); fwrite(buffer, sizeof(char), count, raw); total_bytes += count; printf("\rTotal bytes: %d",total_bytes); fflush(stdout); } printf("\n\rCleaning up...\n\r"); fflush(raw); fclose(raw); ftdi_usb_close(ftdi); ftdi_free(ftdi); }
int sys_init() { int ret, i; struct ftdi_device_list *devlist, *curdev; char manufacturer[128], description[128], serialno[128]; if (ftdi_init(&ux400_ftdic) < 0) { fprintf(stderr, "ftdi_init failed\n"); return EXIT_FAILURE; } if((ret = ftdi_set_interface(&ux400_ftdic, 2)) < 0) { fprintf(stderr, "ftdi_set_interface failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } #if 0 if ((ret = ftdi_usb_find_all(&ux400_ftdic, &devlist, UX400VENDOR, UX400PRODUCT)) < 0) { fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } 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(&ux400_ftdic, curdev->dev, manufacturer, 128, description, 128, serialno, 128)) < 0) { fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } printf("Manufacturer: %s, Description: %s, Serial number: %s\n\n", manufacturer, description, serialno); curdev = curdev->next; } ftdi_list_free(&devlist); #endif if((ret = ftdi_usb_open_desc(&ux400_ftdic, UX400VENDOR, UX400PRODUCT, UX400DES, UX400_LOCAL_SN)) < 0) { fprintf(stderr, "ftdi_usb_open_desc failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } if((ret = ftdi_usb_reset(&ux400_ftdic)) < 0) { fprintf(stderr, "ftdi_usb_reset failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } if((ret = ftdi_set_baudrate(&ux400_ftdic, 9600)) < 0) { fprintf(stderr, "ftdi_set_baudrate failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } if((ret = ftdi_set_bitmode(&ux400_ftdic, 0x00, BITMODE_RESET)) < 0) { fprintf(stderr, "ftdi_set_bitmode failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } usleep(10000); if((ret = ftdi_set_bitmode(&ux400_ftdic, 0x00, BITMODE_MPSSE)) < 0) { fprintf(stderr, "ftdi_set_bitmode failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } return 0; }
JNIEXPORT jint JNICALL Java_com_jamesots_libftdi_LibFtdi_setInterface (JNIEnv *env, jclass cls, jlong ctx, jint aOrB) { return ftdi_set_interface((struct ftdi_context*)ctx, aOrB); }
void platform_init(int argc, char **argv) { int err; int c; unsigned index = 0; char *serial = NULL; char * cablename = "ftdi"; while((c = getopt(argc, argv, "c:s:")) != -1) { switch(c) { case 'c': cablename = optarg; break; case 's': serial = optarg; break; } } for(index = 0; index < sizeof(cable_desc)/sizeof(cable_desc[0]); index++) if (strcmp(cable_desc[index].name, cablename) == 0) break; if (index == sizeof(cable_desc)/sizeof(cable_desc[0])){ fprintf(stderr, "No cable matching %s found\n",cablename); exit(-1); } active_cable = &cable_desc[index]; printf("\nBlack Magic Probe (" FIRMWARE_VERSION ")\n"); printf("Copyright (C) 2015 Black Sphere Technologies Ltd.\n"); printf("License GPLv3+: GNU GPL version 3 or later " "<http://gnu.org/licenses/gpl.html>\n\n"); if(ftdic) { ftdi_usb_close(ftdic); ftdi_free(ftdic); ftdic = NULL; } if((ftdic = ftdi_new()) == NULL) { fprintf(stderr, "ftdi_new: %s\n", ftdi_get_error_string(ftdic)); abort(); } if((err = ftdi_set_interface(ftdic, active_cable->interface)) != 0) { fprintf(stderr, "ftdi_set_interface: %d: %s\n", err, ftdi_get_error_string(ftdic)); abort(); } if((err = ftdi_usb_open_desc( ftdic, active_cable->vendor, active_cable->product, active_cable->description, serial)) != 0) { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", err, ftdi_get_error_string(ftdic)); abort(); } if((err = ftdi_set_latency_timer(ftdic, 1)) != 0) { fprintf(stderr, "ftdi_set_latency_timer: %d: %s\n", err, ftdi_get_error_string(ftdic)); abort(); } if((err = ftdi_set_baudrate(ftdic, 1000000)) != 0) { fprintf(stderr, "ftdi_set_baudrate: %d: %s\n", err, ftdi_get_error_string(ftdic)); abort(); } if((err = ftdi_write_data_set_chunksize(ftdic, BUF_SIZE)) != 0) { fprintf(stderr, "ftdi_write_data_set_chunksize: %d: %s\n", err, ftdi_get_error_string(ftdic)); abort(); } assert(gdb_if_init() == 0); }