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 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); }
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; }
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; } }
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; }
QString LibFTDIInterface::readLabel(uchar label, int *ESTA_code) { if (ftdi_usb_open_desc(&m_handle, DMXInterface::FTDIVID, DMXInterface::FTDIPID, name().toLatin1().data(), serial().toLatin1().data()) < 0) return QString(); if (ftdi_usb_reset(&m_handle) < 0) return QString(); if (ftdi_set_baudrate(&m_handle, 250000) < 0) return QString(); if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0) return QString(); if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0) return QString(); QByteArray request; request.append(ENTTEC_PRO_START_OF_MSG); request.append(label); request.append(ENTTEC_PRO_DMX_ZERO); // data length LSB request.append(ENTTEC_PRO_DMX_ZERO); // data length MSB request.append(ENTTEC_PRO_END_OF_MSG); if (ftdi_write_data(&m_handle, (uchar*) request.data(), request.size()) < 0) { qDebug() << Q_FUNC_INFO << "Cannot write data to device"; return QString(); } uchar *buffer = (uchar*) malloc(sizeof(uchar) * 40); Q_ASSERT(buffer != NULL); QByteArray array; usleep(300000); // give some time to the device to respond int read = ftdi_read_data(&m_handle, buffer, 40); //qDebug() << Q_FUNC_INFO << "Data read: " << read; array = QByteArray::fromRawData((char*) buffer, read); if (array[0] != ENTTEC_PRO_START_OF_MSG) qDebug() << Q_FUNC_INFO << "Reply message wrong start code: " << QString::number(array[0], 16); *ESTA_code = (array[5] << 8) | array[4]; array.remove(0, 6); // 4 bytes of Enttec protocol + 2 of ESTA ID array.replace(ENTTEC_PRO_END_OF_MSG, '\0'); // replace Enttec termination with string termination //for (int i = 0; i < array.size(); i++) // qDebug() << "-Data: " << array[i]; ftdi_usb_close(&m_handle); return QString(array); }
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; }
bool QLCFTDI::open() { if (ftdi_usb_open_desc(&m_handle, QLCFTDI::VID, QLCFTDI::PID, name().toAscii(), serial().toAscii()) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } }
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] },
void ftdi_bootup() { if(ftdi_init(&ftdi)!=0) printf("ftdi - could not be opened.\nr"); else printf("booting up.\n"); if(ftdi_usb_open_desc(&ftdi,0x0403, 0x6001,NULL/*description*/,usb_serial)!=0) printf("ftdi - error.\n"); else { printf("ftdi opened.\n"); ftdi_set_baudrate(&ftdi,115200); } }
int main ( int argc, char *argv[] ) { unsigned char c = 0; char check = '2'; const char serial[] = "A602HSI6"; const char *descriptor = NULL; struct ftdi_context ftdic; /* Initialize context for subsequent function calls */ ftdi_init(&ftdic); /* Open FTDI device based on FT232R vendor & product IDs */ if(ftdi_usb_open_desc(&ftdic, 0x0403, 0x6001, descriptor, serial) < 0) { //puts("Can't open device cooler USB"); return 1; } if ( argc == 1 ) { //printf( "cooler USB device found, all OK\n"); return 0; } if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv[0] assuming it is the program name */ printf( "usage: %s [0|1]\n", argv[0] ); return 1; } else { check = argv[1][0]; /*printf("Argument: %s C:%c\n",argv[1],check);*/ if (check == '1') { c ^= LED; /*printf("On\n");*/ } /* Enable bitbang mode with a single output line */ ftdi_set_bitmode(&ftdic, LED, BITMODE_BITBANG); ftdi_write_data(&ftdic, &c, 1); return 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; }
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_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_MCU)) < 0) { fprintf(stderr, "ftdi_set_bitmode failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } return 0; }
bool QLCFTDI::open() { if (m_openCount < m_refCount) m_openCount++; if (isOpen() == true) return true; if (ftdi_usb_open_desc(&m_handle, QLCFTDI::VID, QLCFTDI::PID, name().toLatin1(), serial().toLatin1()) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } }
static int dev_open(struct sr_dev_inst *sdi) { struct dev_context *devc; int ret; devc = sdi->priv; /* Make sure it's an ASIX SIGMA. */ if ((ret = ftdi_usb_open_desc(&devc->ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { sr_err("ftdi_usb_open failed: %s", ftdi_get_error_string(&devc->ftdic)); return 0; } sdi->status = SR_ST_ACTIVE; return SR_OK; }
void initBoard(char *serial) { //Init FTDI communication ftdi_init(&ftdic); if (0) { //Open ret = ftdi_usb_open(&ftdic, 0x0403, 0x6001); } else { //Open via serial number, to be implemented later. ret = ftdi_usb_open_desc(&ftdic, 0x0403, 0x6001, NULL, serial); } ret = ftdi_read_pins(&ftdic, bits); //Board has just been plugged in or power lost if (bits[0] == 0xFF) { //Prevents 0xFF from flipping all switches on //ftdi_enable_bitbang(&ftdic, 0xF0); ftdi_set_bitmode(&ftdic, 0xF0, BITMODE_BITBANG); setPins(0); } ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG); //ftdi_enable_bitbang(&ftdic, 0xFF); }
int main() { struct ftdi_context ftdic; ftdi_init(&ftdic); uint8_t data[256]; uint8_t data2[256]; if(ftdi_usb_open_desc(&ftdic, 0x0403, 0x6001, NULL, NULL) < 0) { fprintf(stderr, "ftdi_usb_open_desc failed: %s\n", ftdi_get_error_string(&ftdic)); exit(1); } if(ftdi_set_bitmode(&ftdic, 0xff, BITMODE_SYNCBB) < 0) { fprintf(stderr, "ftdi_set_bitmode failed: %s\n", ftdi_get_error_string(&ftdic)); exit(1); } if(ftdi_set_baudrate(&ftdic, 4096) < 0) { fprintf(stderr, "ftdi_set_baudrate failed: %s\n", ftdi_get_error_string(&ftdic)); exit(1); } for(int i=0; i<sizeof(data); i++) { data[i] = (uint8_t ) i & 0xff; } for(;;) { ftdi_write_data(&ftdic, data, sizeof(data)); ftdi_read_data(&ftdic, data2, sizeof(data2)); } return 0; }
Communication::Communication() { syncPacketSize = 0; if(ftdi_init(&bodyFTDI)<0) { fprintf(stderr, "ftdi_init failed.\n"); return; } if(ftdi_usb_open_desc(&bodyFTDI,0x403,0x6001,NULL, ftdiID)<0) { fprintf(stderr, "Unable to open ftdi device: (%s).\n", ftdi_get_error_string(&bodyFTDI)); printf("Unable to open ftdi.\n"); return; } else { if(ftdi_set_baudrate(&bodyFTDI, BAUD)<0) { printf("Unable to set baudrate.\n"); return; } printf("%s ftdi successfully opened.\n", ftdiID); } }
/* 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; }
static GSList *scan(GSList *options) { struct sr_dev_inst *sdi; struct sr_probe *probe; struct drv_context *drvc; struct dev_context *devc; GSList *devices; unsigned int i; int ret; (void)options; drvc = di->priv; devices = NULL; /* Allocate memory for our private device context. */ if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { sr_err("Device context malloc failed."); goto err_free_nothing; } /* Set some sane defaults. */ devc->ftdic = NULL; devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ devc->limit_msec = 0; devc->limit_samples = 0; devc->cb_data = NULL; memset(devc->mangled_buf, 0, BS); devc->final_buf = NULL; devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ devc->trigger_mask = 0x00; /* All probes are "don't care". */ devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */ devc->trigger_found = 0; devc->done = 0; devc->block_counter = 0; devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */ devc->usb_pid = 0; /* Allocate memory where we'll store the de-mangled data. */ if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { sr_err("final_buf malloc failed."); goto err_free_devc; } /* Allocate memory for the FTDI context (ftdic) and initialize it. */ if (!(devc->ftdic = ftdi_new())) { sr_err("%s: ftdi_new failed.", __func__); goto err_free_final_buf; } /* Check for the device and temporarily open it. */ for (i = 0; i < ARRAY_SIZE(usb_pids); i++) { sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID, usb_pids[i]); ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, usb_pids[i], USB_DESCRIPTION, NULL); if (ret == 0) { sr_dbg("Found LA8 device (%04x:%04x).", USB_VENDOR_ID, usb_pids[i]); devc->usb_pid = usb_pids[i]; } } if (devc->usb_pid == 0) goto err_free_ftdic; /* Register the device with libsigrok. */ sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); if (!sdi) { sr_err("%s: sr_dev_inst_new failed.", __func__); goto err_close_ftdic; } sdi->driver = di; sdi->priv = devc; for (i = 0; chronovu_la8_probe_names[i]; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, chronovu_la8_probe_names[i]))) return NULL; sdi->probes = g_slist_append(sdi->probes, probe); } devices = g_slist_append(devices, sdi); drvc->instances = g_slist_append(drvc->instances, sdi); /* Close device. We'll reopen it again when we need it. */ (void) la8_close(devc); /* Log, but ignore errors. */ return devices; err_close_ftdic: (void) la8_close(devc); /* Log, but ignore errors. */ err_free_ftdic: ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ err_free_final_buf: g_free(devc->final_buf); err_free_devc: g_free(devc); err_free_nothing: return NULL; }
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]); }
static void child_process(int fd_rx2main, int fd_main2tx, int fd_tx2main) { int ret = 0; struct ftdi_context ftdic; alarm(0); signal(SIGTERM, SIG_DFL); signal(SIGPIPE, SIG_DFL); signal(SIGINT, SIG_DFL); signal(SIGHUP, SIG_IGN); signal(SIGALRM, SIG_IGN); ftdi_init(&ftdic); /* indicate we're started: */ ret = write(fd_tx2main, &ret, 1); while (1) { /* Open the USB device */ if (ftdi_usb_open_desc(&ftdic, usb_vendor, usb_product, usb_desc, usb_serial) < 0) { logprintf(LIRC_ERROR, "unable to open FTDI device (%s)", ftdi_get_error_string(&ftdic)); goto retry; } /* Enable bit-bang mode, setting output & input pins * direction */ if (ftdi_set_bitmode(&ftdic, 1 << output_pin, BITMODE_BITBANG) < 0) { logprintf(LIRC_ERROR, "unable to enable bitbang mode (%s)", ftdi_get_error_string(&ftdic)); goto retry; } /* Set baud rate */ if (ftdi_set_baudrate(&ftdic, rx_baud_rate) < 0) { logprintf(LIRC_ERROR, "unable to set required baud rate (%s)", ftdi_get_error_string(&ftdic)); goto retry; } logprintf(LIRC_DEBUG, "opened FTDI device '%s' OK", drv.device); do { unsigned char buf[RXBUFSZ > TXBUFSZ ? RXBUFSZ : TXBUFSZ]; /* transmit IR */ ret = read(fd_main2tx, buf, sizeof(buf)); if (ret > 0) { /* select correct transmit baudrate */ if (ftdi_set_baudrate(&ftdic, tx_baud_rate) < 0) { logprintf(LIRC_ERROR, "unable to set required baud rate for transmission (%s)", ftdi_get_error_string(&ftdic)); goto retry; } if (ftdi_write_data(&ftdic, buf, ret) < 0) logprintf(LIRC_ERROR, "enable to write ftdi buffer (%s)", ftdi_get_error_string(&ftdic)); if (ftdi_usb_purge_tx_buffer(&ftdic) < 0) logprintf(LIRC_ERROR, "unable to purge ftdi buffer (%s)", ftdi_get_error_string(&ftdic)); /* back to rx baudrate: */ if (ftdi_set_baudrate(&ftdic, rx_baud_rate) < 0) { logprintf(LIRC_ERROR, "unable to set restore baudrate for reception (%s)", ftdi_get_error_string(&ftdic)); goto retry; } /* signal transmission ready: */ ret = write(fd_tx2main, &ret, 1); continue; } /* receive IR */ ret = ftdi_read_data(&ftdic, buf, RXBUFSZ); if (ret > 0) parsesamples(buf, ret, fd_rx2main); } while (ret > 0); retry: /* Wait a while and try again */ usleep(500000); } }
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); }
QList <DMXUSBWidget*> QLCFTDI::widgets() { QList <DMXUSBWidget*> widgetList; quint32 input_id = 0; struct ftdi_context ftdi; ftdi_init(&ftdi); #ifdef LIBFTDI1 libusb_device *dev; libusb_device **devs; int i = 0; if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) { qDebug() << "usb_find_devices() failed"; ftdi_error_return(-5, "libusb_get_device_list() failed"); } while ((dev = devs[i++]) != NULL) { #else struct usb_bus *bus; struct usb_device *dev; usb_init(); if (usb_find_busses() < 0) { qDebug() << "usb_find_busses() failed"; return widgetList; } if (usb_find_devices() < 0) { qDebug() << "usb_find_devices() failed"; return widgetList; } for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { #endif Q_ASSERT(dev != NULL); // Skip non wanted devices if (dev->descriptor.idVendor != QLCFTDI::FTDIVID && dev->descriptor.idVendor != QLCFTDI::ATMELVID) continue; if (dev->descriptor.idProduct != QLCFTDI::FTDIPID && dev->descriptor.idProduct != QLCFTDI::DMX4ALLPID && dev->descriptor.idProduct != QLCFTDI::NANODMXPID) continue; char ser[256]; char nme[256]; char vend[256]; ftdi_usb_get_strings(&ftdi, dev, vend, 256, nme, 256, ser, 256); QString serial(ser); QString name(nme); QString vendor(vend); QMap <QString,QVariant> types(typeMap()); qDebug() << Q_FUNC_INFO << "DMX USB VID:" << QString::number(dev->descriptor.idVendor, 16) << "PID:" << QString::number(dev->descriptor.idProduct, 16); qDebug() << Q_FUNC_INFO << "DMX USB serial: " << serial << "name:" << name << "vendor:" << vendor; if (types.contains(serial) == true) { // Force a widget with a specific serial to either type DMXUSBWidget::Type type = (DMXUSBWidget::Type) types[serial].toInt(); switch (type) { case DMXUSBWidget::OpenTX: widgetList << new EnttecDMXUSBOpen(serial, name, vendor); break; case DMXUSBWidget::ProRX: { EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++); widgetList << prorx; break; } case DMXUSBWidget::ProMk2: { EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor, 1); widgetList << protx; widgetList << new EnttecDMXUSBProTX(serial, name, vendor, 2, protx->ftdi()); EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi()); widgetList << prorx; break; } case DMXUSBWidget::UltraProTx: { UltraDMXUSBProTx* protx = new UltraDMXUSBProTx(serial, name, vendor, 1); widgetList << protx; widgetList << new UltraDMXUSBProTx(serial, name, vendor, 2, protx->ftdi()); EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi()); widgetList << prorx; break; } case DMXUSBWidget::VinceTX: widgetList << new VinceUSBDMX512TX(serial, name, vendor); break; default: case DMXUSBWidget::ProTX: widgetList << new EnttecDMXUSBProTX(serial, name, vendor); break; } } else if (name.toUpper().contains("PRO MK2") == true) { EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor, 1); widgetList << protx; widgetList << new EnttecDMXUSBProTX(serial, name, vendor, 2, protx->ftdi()); EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi()); widgetList << prorx; } else if (name.toUpper().contains("DMX USB PRO")) { /** Check if the device responds to label 77 and 78, so it might be a DMXking adapter */ int ESTAID = 0; int DEVID = 0; QString manName = readLabel(&ftdi, name.toLatin1().data(), serial.toLatin1().data(), USB_DEVICE_MANUFACTURER, &ESTAID); qDebug() << "--------> Device Manufacturer: " << manName; QString devName = readLabel(&ftdi, name.toLatin1().data(), serial.toLatin1().data(), USB_DEVICE_NAME, &DEVID); qDebug() << "--------> Device Name: " << devName; qDebug() << "--------> ESTA Code: " << QString::number(ESTAID, 16) << ", Device ID: " << QString::number(DEVID, 16); if (ESTAID == DMXKING_ESTA_ID) { if (DEVID == ULTRADMX_PRO_DEV_ID) { UltraDMXUSBProTx* protxP1 = new UltraDMXUSBProTx(serial, name, vendor, 1); protxP1->setRealName(devName); widgetList << protxP1; UltraDMXUSBProTx* protxP2 = new UltraDMXUSBProTx(serial, name, vendor, 2, protxP1->ftdi()); protxP2->setRealName(devName); widgetList << protxP2; EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protxP1->ftdi()); prorx->setRealName(devName); widgetList << prorx; } else { EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor); protx->setRealName(devName); widgetList << protx; } } else { /* This is probably a Enttec DMX USB Pro widget */ EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor); widgetList << protx; EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi()); widgetList << prorx; } } else if (name.toUpper().contains("USB-DMX512 CONVERTER") == true) { widgetList << new VinceUSBDMX512TX(serial, name, vendor); } else if (dev->descriptor.idVendor == QLCFTDI::FTDIVID && dev->descriptor.idProduct == QLCFTDI::DMX4ALLPID) { widgetList << new Stageprofi(serial, name, vendor); } #if defined(Q_WS_X11) || defined(Q_OS_LINUX) else if (dev->descriptor.idVendor == QLCFTDI::ATMELVID && dev->descriptor.idProduct == QLCFTDI::NANODMXPID) { widgetList << new NanoDMX(serial, name, vendor); } #endif else { /* This is probably an Open DMX USB widget */ widgetList << new EnttecDMXUSBOpen(serial, name, vendor, 0); } #ifndef LIBFTDI1 } #endif } #ifdef LIBFTDI1 libusb_free_device_list(devs, 1); #endif ftdi_deinit(&ftdi); return widgetList; } bool QLCFTDI::open() { if (m_openCount < m_refCount) m_openCount++; if (isOpen() == true) return true; if (ftdi_usb_open_desc(&m_handle, QLCFTDI::FTDIVID, QLCFTDI::FTDIPID, name().toLatin1(), serial().toLatin1()) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::openByPID(const int PID) { if (m_openCount < m_refCount) m_openCount++; if (isOpen() == true) return true; if (ftdi_usb_open(&m_handle, QLCFTDI::FTDIVID, PID) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::close() { if (m_openCount > 1) { m_openCount--; return true; } if (ftdi_usb_close(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::isOpen() const { return (m_handle.usb_dev != NULL) ? true : false; } bool QLCFTDI::reset() { if (ftdi_usb_reset(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::setLineProperties() { if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::setBaudRate() { if (ftdi_set_baudrate(&m_handle, 250000) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::setFlowControl() { if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::clearRts() { if (ftdi_setrts(&m_handle, 0) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::purgeBuffers() { if (ftdi_usb_purge_buffers(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool QLCFTDI::setBreak(bool on) { ftdi_break_type type; if (on == true) type = BREAK_ON; else type = BREAK_OFF; if (ftdi_set_line_property2(&m_handle, BITS_8, STOP_BIT_2, NONE, type) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } }
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 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_MCU)) < 0) { fprintf(stderr, "ftdi_set_bitmode failed: %d (%s)\n", ret, ftdi_get_error_string(&ux400_ftdic)); return EXIT_FAILURE; } return 0; }
int spi_open(int nport) { LOG(DEBUG, "(%d) spi_dev_open=%d", nport, spi_dev_open); if (spi_dev_open > 0) { LOG(WARN, "Superfluos call to spi_open()"); return 0; } if (spi_nports == 0 || nport < spi_nports - 1) { SPI_ERR("No FTDI device found"); goto open_err; } #ifdef SPI_STATS memset(&spi_stats, 0, sizeof(spi_stats)); if (gettimeofday(&spi_stats.tv_open_begin, NULL) < 0) LOG(WARN, "gettimeofday failed: %s", strerror(errno)); #endif /*ftdi_set_interface(&ftdic, INTERFACE_A);*/ /* XXX for multichannel chips */ if (ftdi_usb_open_desc(&ftdic, spi_ports[nport].vid, spi_ports[nport].pid, NULL, spi_ports[nport].serial) < 0) { SPI_ERR("FTDI: ftdi_usb_open_desc() failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } spi_dev_open++; LOG(INFO, "FTDI: using FTDI device: \"%s\"", spi_ports[nport].name); if (ftdi_usb_reset(&ftdic) < 0) { SPI_ERR("FTDI: reset failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } if (ftdi_usb_purge_buffers(&ftdic) < 0) { SPI_ERR("FTDI: purge buffers failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } /* Set 1 ms latency timer, see FTDI AN232B-04 */ if (ftdi_set_latency_timer(&ftdic, 1) < 0) { SPI_ERR("FTDI: setting latency timer failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } if (ftdi_set_bitmode(&ftdic, 0, BITMODE_RESET) < 0) { SPI_ERR("FTDI: reset bitmode failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } if (ftdi_set_bitmode(&ftdic, PINS_OUTPUT, BITMODE_SYNCBB) < 0) { SPI_ERR("FTDI: set synchronous bitbang mode failed: %s", ftdi_get_error_string(&ftdic)); goto open_err; } /* * Note on buffer sizes: * * FT232R has 256 byte receive buffer and 128 byte transmit buffer. It works * like 384 byte buffer. See: * http://developer.intra2net.com/mailarchive/html/libftdi/2011/msg00410.html * http://developer.intra2net.com/mailarchive/html/libftdi/2011/msg00413.html * http://jdelfes.blogspot.ru/2014/03/ft232r-bitbang-spi-part-2.html * * FT2232C has 128 byte TX and 384 byte RX buffers per channel. * FT2232H has 4kB RX and TX buffers per channel. * FT4232H has 2kB RX and TX buffers per channel. * FT232H has 1 kB RX and TX buffers. * FT230X has 512 byte TX and RX buffers. */ switch (ftdic.type) { case TYPE_AM: ftdi_type_str = "FT232AM"; SPI_ERR("This chip type is not supported: %s", ftdi_type_str); goto open_err; break; case TYPE_BM: ftdi_type_str = "FT232BM"; SPI_ERR("This chip type is not supported: %s", ftdi_type_str); goto open_err; break; case TYPE_2232C: ftdi_type_str = "FT2232C/D"; ftdi_buf_size = 512; break; case TYPE_R: ftdi_type_str = "FT232R"; ftdi_buf_size = 384; break; case TYPE_2232H: ftdi_type_str = "FT2232H"; ftdi_buf_size = 8192; break; case TYPE_4232H: ftdi_type_str = "FT4232H"; ftdi_buf_size = 4096; break; case TYPE_232H: ftdi_type_str = "FT232H"; ftdi_buf_size = 2048; break; /* TYPE_230X is supported since libftdi1-1.2 */ /*case TYPE_230X: ftdi_type_str = "FT230X"; ftdi_buf_size = 1024; break; */ default: LOG(WARN, "Unknown FTDI chip type, assuming FT232R"); ftdi_type_str = "Unknown"; ftdi_buf_size = 384; break; } LOG(INFO, "Detected %s type programmer chip, buffer size: %u", ftdi_type_str, ftdi_buf_size); /* Initialize xfer buffer */ ftdi_buf = malloc(ftdi_buf_size); if (ftdi_buf == NULL) { SPI_ERR("Not enough memory"); goto open_err; } ftdi_buf_write_offset = 0; ftdi_pin_state = PINS_INIT; ftdi_buf[ftdi_buf_write_offset++] = ftdi_pin_state; return 0; open_err: if (spi_dev_open > 0) ftdi_usb_close(&ftdic); spi_dev_open = 0; return -1; }
QList<DMXInterface *> LibFTDIInterface::interfaces(QList<DMXInterface *> discoveredList) { QList <DMXInterface*> interfacesList; int id = 0; struct ftdi_context ftdi; ftdi_init(&ftdi); #ifdef LIBFTDI1 libusb_device *dev; libusb_device **devs; struct libusb_device_descriptor dev_descriptor; int i = 0; if (libusb_get_device_list(ftdi.usb_ctx, &devs) < 0) { qDebug() << "usb_find_devices() failed"; return interfacesList; } while ((dev = devs[i++]) != NULL) { libusb_get_device_descriptor(dev, &dev_descriptor); #else struct usb_bus *bus; struct usb_device *dev; struct usb_device_descriptor dev_descriptor; usb_init(); if (usb_find_busses() < 0) { qDebug() << "usb_find_busses() failed"; return interfacesList; } if (usb_find_devices() < 0) { qDebug() << "usb_find_devices() failed"; return interfacesList; } for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { dev_descriptor = dev->descriptor; #endif Q_ASSERT(dev != NULL); // Skip non wanted devices if (validInterface(dev_descriptor.idVendor, dev_descriptor.idProduct) == false) continue; char ser[256]; memset(ser, 0, 256); char nme[256]; char vend[256]; ftdi_usb_get_strings(&ftdi, dev, vend, 256, nme, 256, ser, 256); QString serial(ser); QString name(nme); QString vendor(vend); qDebug() << Q_FUNC_INFO << "DMX USB VID:" << QString::number(dev_descriptor.idVendor, 16) << "PID:" << QString::number(dev_descriptor.idProduct, 16); qDebug() << Q_FUNC_INFO << "DMX USB serial: " << serial << "name:" << name << "vendor:" << vendor; bool found = false; for (int c = 0; c < discoveredList.count(); c++) { if (discoveredList.at(c)->checkInfo(serial, name, vendor) == true) { found = true; break; } } if (found == false) { LibFTDIInterface *iface = new LibFTDIInterface(serial, name, vendor, dev_descriptor.idVendor, dev_descriptor.idProduct, id++); #ifdef LIBFTDI1 iface->setBusLocation(libusb_get_port_number(dev)); #else iface->setBusLocation(dev->bus->location); #endif interfacesList << iface; } #ifndef LIBFTDI1 } #endif } #ifdef LIBFTDI1 libusb_free_device_list(devs, 1); #endif ftdi_deinit(&ftdi); return interfacesList; } bool LibFTDIInterface::open() { if (isOpen() == true) return true; QByteArray sba = serial().toLatin1(); const char *ser = NULL; if (serial().isEmpty() == false) ser = (const char *)sba.data(); if (ftdi_usb_open_desc(&m_handle, vendorID(), productID(), name().toLatin1(), ser) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::openByPID(const int PID) { if (isOpen() == true) return true; if (ftdi_usb_open(&m_handle, DMXInterface::FTDIVID, PID) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::close() { if (ftdi_usb_close(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::isOpen() const { return (m_handle.usb_dev != NULL) ? true : false; } bool LibFTDIInterface::reset() { if (ftdi_usb_reset(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::setLineProperties() { if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::setBaudRate() { if (ftdi_set_baudrate(&m_handle, 250000) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::setFlowControl() { if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::clearRts() { if (ftdi_setrts(&m_handle, 0) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::purgeBuffers() { if (ftdi_usb_purge_buffers(&m_handle) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } } bool LibFTDIInterface::setBreak(bool on) { ftdi_break_type type; if (on == true) type = BREAK_ON; else type = BREAK_OFF; if (ftdi_set_line_property2(&m_handle, BITS_8, STOP_BIT_2, NONE, type) < 0) { qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle); return false; } else { return true; } }
static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_dev_inst *sdi; struct dev_context *devc; unsigned int i; int ret; (void)options; /* Allocate memory for our private device context. */ devc = g_malloc0(sizeof(struct dev_context)); /* Allocate memory for the incoming compressed samples. */ if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) { sr_err("compressed_buf malloc failed."); goto err_free_devc; } /* Allocate memory for the uncompressed samples. */ if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) { sr_err("sample_buf malloc failed."); goto err_free_compressed_buf; } /* Allocate memory for the FTDI context (ftdic) and initialize it. */ if (!(devc->ftdic = ftdi_new())) { sr_err("Failed to initialize libftdi."); goto err_free_sample_buf; } /* Check for the device and temporarily open it. */ ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, USB_IPRODUCT, NULL); if (ret < 0) { /* Log errors, except for -3 ("device not found"). */ if (ret != -3) sr_err("Failed to open device (%d): %s", ret, ftdi_get_error_string(devc->ftdic)); goto err_free_ftdic; } /* Register the device with libsigrok. */ sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup(USB_VENDOR_NAME); sdi->model = g_strdup(USB_MODEL_NAME); sdi->priv = devc; for (i = 0; i < ARRAY_SIZE(channel_names); i++) sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); /* Close device. We'll reopen it again when we need it. */ scanaplus_close(devc); return std_scan_complete(di, g_slist_append(NULL, sdi)); scanaplus_close(devc); err_free_ftdic: ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ err_free_sample_buf: g_free(devc->sample_buf); err_free_compressed_buf: g_free(devc->compressed_buf); err_free_devc: g_free(devc); return NULL; }
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; }