Esempio n. 1
0
/*
 * use libftdi and libusb to send command
 * no kernel driver needed
 */
int usbWriteFtdi(char *cmdstr)
{
    struct ftdi_context ctx;
    int device=0x0c30, vendor=0x1781;

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

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

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

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

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

    return 0;
}
Esempio n. 2
0
/*
List connected TCTEC Relays
*/
void
listRelays()
{
	int             ret, i, bRelayStates;
	struct ftdi_device_list *devlist, *curdev;
	char            manufacturer[128], description[128], serial[128],
	                buf[1];
	if ((ret = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0) {
		fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return;
	}
	printf("Number of FTDI devices found: %d\n", ret);
	for (curdev = devlist; curdev != NULL; i++) {
		if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128)) < 0) {
			fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
			return;
		}
		if (strncmp(description, "TCTEC USB RELAY", 15) == 0) {
			printf("Manufacturer: %s, Description: %s, Serial: %s\n\n", manufacturer, description, serial);
		}
		curdev = curdev->next;
	}
	ftdi_list_free(&devlist);
	ftdi_deinit(&ftdic);
	return;
}
Esempio n. 3
0
/**
 * @param readers Probably should be an empty list.
 */
list *find_all_readers(struct list *readers) {
  int rc;
  struct ftdi_device_list *devlist;
  struct ftdi_context ftdi;

  ftdi_init(&ftdi);
  rc = ftdi_usb_find_all(&ftdi, &devlist, RFID1_VID, RFID1_PID);
  ftdi_deinit(&ftdi);

  if(rc < 0) return NULL;

  struct ftdi_device_list *dp = devlist;
  while(dp){
    struct ftdi_context *c = malloc(sizeof(struct ftdi_context));
    if(c == NULL){
      // Out of memory !!
      // TODO: Clean up
      return NULL;
    }
    ftdi_init(c);

    struct reader *r = reader_create();
    reader_init(r, c, dp->dev);
    list_push(readers, r);

    dp = dp->next;
  }

  ftdi_list_free(&dp);

  return readers;
}
Esempio n. 4
0
void jtagkey_close(int handle) {
	if (handle == 0xff) {
		ftdi_disable_bitbang(&ftdic);
		ftdi_usb_close(&ftdic);
		ftdi_deinit(&ftdic);
	}
}
	FalconCommLibFTDI::~FalconCommLibFTDI()
	{
		LOG_INFO("Destructing object");
		close();
		ftdi_deinit((m_falconDevice));
		delete m_falconDevice;
	}
Esempio n. 6
0
bool IntanThread::closeUSB()
{
    ftdi_usb_close(&ftdic);
    ftdi_deinit(&ftdic);
    std::cout << "FTDI interface destroyed." << std::endl;
    return true;
}
Esempio n. 7
0
int main(int argc, char *argv[] )
{
	unsigned char fan = 0;
	char temp = 0;
	int ret = 0;

	if(argc != 2){
		printf("usage: ux400opm 1/0\n");
		exit(0);
	}

	if((ret = sys_init())<0)
	{
		printf("LIBFTDI init failed, exit\n");
		exit(1);
	}

	temp = cpldver();
	if(temp < 0 )
	{
		printf("Read CPLD version failed, exit\n");
		exit(1);
	}

	if(atoi(argv[1]) == 1){
		opm_pwr(OPM_ON);
	}else{
		opm_pwr(OPM_OFF);
	}

	ftdi_usb_close(&ux400_ftdic);
	ftdi_deinit(&ux400_ftdic);
}
Esempio n. 8
0
int main(void) {


	snd_rawmidi_open(&midi_in, &midi_out, "virtual", SND_RAWMIDI_NONBLOCK);
	//snd_rawmidi_open(&midi_in, &midi_out, "virtual", 0);


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

	if (ftdi_usb_open(&ftdi, 0x0403, 0x6001)) {
		fprintf(stderr,  "usb - open error (cannot find?) !\n");
		fprintf(stderr, "ftdi_usb_open failed, error (%s)\n", ftdi_get_error_string(&ftdi));
		ftdi_deinit( &ftdi );
		return 2;
	}

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

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

	unsigned char buf[BUFFER_SIZE];
	int ret;
	while(1) {
		//FTDI2MIDI
		ret = ftdi_read_data(&ftdi, buf, BUFFER_SIZE);
		if(ret < 0) break;
		if(ret > 0) snd_rawmidi_write(midi_out, buf, BUFFER_SIZE);

		//MIDI2FTDI
		/*
		ret = snd_rawmidi_read(midi_in, buf,BUFFER_SIZE);
		if(ret < 0 && ret != -EAGAIN) break;
		if(ret > 0) ftdi_write_data(&ftdi, buf,BUFFER_SIZE);
		*/
		usleep(LATENCY);
	}
	exit(0);
}
Esempio n. 9
0
int spectrig_connect(struct ftdi_context *ftdi)
{
	int ret;

	ftdi_init(ftdi);

	/* TODO: Be a bit more selective and support multiple devices properly */
	ret = open_by_desc_prefix(ftdi, 0x0403, 0x6010, spectrig_descs);
	if (ret < 0) {
		perror("Failed to open the device");
		return ret;
	}
	ftdi_usb_purge_buffers(ftdi);

	/* Initialize synchronous communication */
	ret = ftdi_set_latency_timer(ftdi, 2);
	if (ret < 0) {
		perror("Failed to set the latency timer");
		goto close;
	}
	ret = ftdi_read_data_set_chunksize(ftdi, 0x10000);
	if (ret < 0) {
		perror("Failed to set read data chunksize");
		goto close;
	}
	ret = ftdi_write_data_set_chunksize(ftdi, 0x10000);
	if (ret < 0) {
		perror("Failed to write read data chunksize");
		goto close;
	}
	ret = ftdi_setflowctrl(ftdi, SIO_RTS_CTS_HS);
	if (ret < 0) {
		perror("Failed to set flow control");
		goto close;
	}
	msleep(20);
	ret = ftdi_set_bitmode(ftdi, 0xff, 0x00);
	if (ret < 0) {
		perror("Failed to set bitmode 0x00");
		goto close;
	}
	msleep(20);
	ftdi_set_bitmode(ftdi, 0xff, 0x40);
	if (ret < 0) {
		perror("Failed to set bitmode 0x40");
		goto close;
	}

	msleep(300);
	ftdi_usb_purge_buffers(ftdi);

	return 0;

close:
	ftdi_usb_close(ftdi);
	ftdi_deinit(ftdi);

	return ret;
}
Esempio n. 10
0
void ftdi_close()
{
  if(ftdi_usb_close(&ftdi)!=0)
    printf("\nusb could not be closed.\n");
  else
    printf("\nusb closed.\n");
  ftdi_deinit(&ftdi);
}
Esempio n. 11
0
GovernorFtdi::~GovernorFtdi()
{
    if(readThread)
        readThread->shutdown();
    ftdi_usb_close(&ftdic);
    ftdi_deinit(&ftdic);
    ftdic.usb_dev = NULL;
}
Esempio n. 12
0
static int ublast_ftdi_quit(struct ublast_lowlevel *low)
{
	struct ftdi_context *ftdic = ublast_getftdic(low);

	ftdi_usb_close(ftdic);
	ftdi_deinit(ftdic);
	return ERROR_OK;
};
Esempio n. 13
0
List* List::find_all(int vendor, int product)
{
    struct ftdi_device_list* dlist = 0;
    struct ftdi_context ftdi;
    ftdi_init(&ftdi);
    ftdi_usb_find_all(&ftdi, &dlist, vendor, product);
    ftdi_deinit(&ftdi);
    return new List(dlist);
}
Esempio n. 14
0
int main(int argc, char *argv[] )
{
	unsigned char fan = 0;
	char temp = 0;
	int ret = 0;
	sem_t * sem_id;

	if(argc != 2){
		printf("usage: ux400opm 1/0\n");
		exit(0);
	}

	if((ret = sys_init())<0)
	{
		printf("LIBFTDI init failed, exit\n");
	}

	temp = cpldver();
	if(temp < 0 )
	{
		printf("Read CPLD version failed, exit\n");
		exit(1);
	}

	printf("before wait\n");
	sem_id = sem_open(UX400_SEM_CPLD, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
	if(sem_id == SEM_FAILED) {
		perror("UX400 OPM sem_open");
		exit(1);
	}
	printf("before wait\n");

	if(sem_wait(sem_id) < 0) {
		perror("UX400 OPM sem_wait");
		exit(1);
	}

	printf("argv[1] = %d\n",atoi(argv[1]));
	if(atoi(argv[1]) == 1){
		printf("argv[1] = 1\n");
		opm_pwr(OPM_ON);
	}else{
		printf("argv[1] = else\n");
		opm_pwr(OPM_OFF);
	}

	if(sem_post(sem_id) < 0) {
		perror("UX400 OPM sem_post");
	}

	ftdi_usb_close(&ux400_ftdic);
	ftdi_deinit(&ux400_ftdic);
}
Esempio n. 15
0
int
main (void)
{
  unsigned char buf[128];
  unsigned char rbuf[128];
  int ret;
  int i;
  struct ftdi_context ftdicon;
  struct ftdi_context *ftdic = &ftdicon;
  unsigned char d;

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

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

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

  ft2232_spi_init (ftdic);

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

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

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

  ftdi_deinit (ftdic);

  return EXIT_SUCCESS;
}
Esempio n. 16
0
static void ft245r_close(PROGRAMMER * pgm) {
    if (handle) {
        // I think the switch to BB mode and back flushes the buffer.
        ftdi_set_bitmode(handle, 0, BITMODE_SYNCBB); // set Synchronous BitBang, all in puts
        ftdi_set_bitmode(handle, 0, BITMODE_RESET); // disable Synchronous BitBang
        ftdi_usb_close(handle);
        ftdi_deinit (handle);
        pthread_cancel(readerthread);
        pthread_join(readerthread, NULL);
        free(handle);
        handle = NULL;
    }
}
Esempio n. 17
0
/*-----------------------------------------------------------------------------
 * Close / Deinitialize the ICS Interface.
 * Mainly closes the USB device and deletes the allocated data.
 * Returns 0 if successful, < 0 otherwise
 */
int ics_close(ICSData * r)
{
	assert(r);

	// close usb device
	if (ftdi_usb_close(&r->ftdic) < 0)
		ics_ftdi_error(r, "ics_close");

	// deinit
	ftdi_deinit(&r->ftdic);

	return 0;
}
Esempio n. 18
0
static int presto_close(void)
{

	int result = ERROR_OK;

#if BUILD_PRESTO_FTD2XX == 1
	DWORD ftbytes;

	if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
		return result;

	presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Write(presto->handle,
			&presto_init_seq,
			sizeof(presto_init_seq),
			&ftbytes);
	if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_SetLatencyTimer(presto->handle, 16);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->status = FT_Close(presto->handle);
	if (presto->status != FT_OK)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;

#elif BUILD_PRESTO_LIBFTDI == 1

	presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
	if (presto->retval != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_usb_close(&presto->ftdic);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		ftdi_deinit(&presto->ftdic);
#endif

	return result;
}
Esempio n. 19
0
int main(void)
{
    struct ftdi_context ftdic;
    int f;
    unsigned char buf[1];
    unsigned char bitmask;
    unsigned char input[10];

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

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

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

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

    ftdi_usb_close(&ftdic);
    ftdi_deinit(&ftdic);
}
Esempio n. 20
0
int GetFTDIDevicePortNumUSingLibFTDI(UInt32 locationIDFromInterface)
{
    int ret, i;
    struct ftdi_context ftdic;
    struct ftdi_device_list *devlist, *curdev;
    char manufacturer[128], description[128];
    
    if (ftdi_init(&ftdic) < 0)
    {
        fprintf(stderr, "ftdi_init failed\n");
        return -1;
    }
    
    if ((ret = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0)
    {
        fprintf(stderr, "ftdi_usb_find_all failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
        return -1;
    }
    
    printf("Number of FTDI devices found: %d\n", ret);
    
    i = 0;
    for (curdev = devlist; curdev != NULL; i++)
    {
        //printf("Checking device: %d\n", i);
        if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, NULL, 0)) < 0)
        {
            fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
            return -1;
        }
        uint32_t *locationId = (uint32_t *)malloc(sizeof(uint32_t)); 
        
        xlibusb_get_device_location_id((struct libusb_device *)curdev->dev->dev,locationId);
        printf("Manufacturer: %s, Description: %s\n\n", manufacturer, description);
        if ((*locationId) == locationIDFromInterface) {
             free(locationId);
             return i;
        }
        
        free(locationId);
        //printf("0x%x deviceDesc[%d] %s",(unsigned int)&(deviceDesc[i]),i,deviceDesc[i]);
        curdev = curdev->next;
    }
    
    ftdi_list_free(&devlist);
    ftdi_deinit(&ftdic);
    
    return -1;
}
Esempio n. 21
0
static gboolean plugin_unload(PurplePlugin *plugin) {
    purple_signal_disconnect(purple_conversations_get_handle(),
                             "conversation-updated", plugin,
                             PURPLE_CALLBACK(hwnotify_conversation_updated));

    if (ftdi_ok) {
        ftdi_usb_close (&ctx);
        ftdi_deinit (&ctx);
    }

    if (important_list)
        g_list_free_full (important_list, free);

    return TRUE;
}
Esempio n. 22
0
int main(int argc, char *argv[] )
{
	unsigned char fan = 0;
	char temp = 0;
	int ret = 0;
	sem_t * sem_id;

	sem_id = sem_open(UX400_SEM_CPLD, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
	if(sem_id == SEM_FAILED) {
		perror("UX400 sem_open");
		exit(1);
	}

	if(sem_wait(sem_id) < 0) {
		perror("UX400 sem_wait");
		exit(1);
	}

	if((ret = sys_init())<0)
	{
		printf("LIBFTDI init failed, exit\n");
		if(sem_post(sem_id) < 0) {
			perror("UX400 sem_post");
		}
		exit(1);
	}

	temp = cpldver();
	if(temp < 0 )
	{
		printf("Read CPLD version failed, exit\n");
		if(sem_post(sem_id) < 0) {
			perror("UX400 sem_post");
		}
		exit(1);
	}

	if(sem_post(sem_id) < 0) {
		perror("UX400 sem_post");
	}

	ftdi_usb_close(&ux400_ftdic);
	ftdi_deinit(&ux400_ftdic);
	
	exit(temp);
}
Esempio n. 23
0
int main(void)
{
    int ret;
    struct ftdi_context ftdic;
    if (ftdi_init(&ftdic) < 0)
    {
        fprintf(stderr, "ftdi_init failed\n");
        return EXIT_FAILURE;
    }

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

    // Read out FTDIChip-ID of R type chips
    if (ftdic.type == TYPE_R)
    {
        unsigned int chipid;
        printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(&ftdic, &chipid));
        printf("FTDI chipid: %X\n", chipid);
    }

    ftdi_set_bitmode(&ftdic, 0x0b, BITMODE_MPSSE); /* ctx, JTAG I/O mask */

    write_ftdi_command(&ftdic, SET_BITS_LOW, 0x00, 0x0b);
    write_ftdi_command(&ftdic, SET_BITS_HIGH, OLIMEX_CBUS_nSRST, OLIMEX_CBUS_nSRST|OLIMEX_CBUS_LED);
    usleep(100);
    write_ftdi_command(&ftdic, SET_BITS_HIGH, OLIMEX_CBUS_LED, OLIMEX_CBUS_nSRST|OLIMEX_CBUS_LED);
    usleep(1500000);
    write_ftdi_command(&ftdic, SET_BITS_LOW, 0x00, 0x00);

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

    ftdi_deinit(&ftdic);

    return EXIT_SUCCESS;
}
Esempio n. 24
0
int main(int argc, char **argv)
{

  int ret, i, no_devices;
  struct ftdi_context ftdic;
  struct ftdi_device_list *devlist, *curdev;
  char manufacturer[128], description[128];
  char serial[128];

  if (ftdi_init(&ftdic) < 0)
  {
    std::cerr << "ftdi_init failed" << std::endl;
    return EXIT_FAILURE;
  }

  if ((no_devices = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0)
  {
    std::cerr << "ftdi_usb_find_all failed: " << ftdi_get_error_string(&ftdic) << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Number of FTDI devices found: " << no_devices << std::endl;

  i = 0;
  for (curdev = devlist; curdev != NULL; i++)
  {
    std::cout << "Device #" << i << std::endl;
    if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128)) < 0)
    {
      std::cerr << "ftdi_usb_get_strings failed: " << ftdi_get_error_string(&ftdic) << std::endl;
      return EXIT_FAILURE;
    }
    std::cout << "  Manufacturer: " << manufacturer << std::endl;
    std::cout << "  Description : " << description << std::endl;
    std::cout << "  Serial Id   : " << serial << std::endl;
    curdev = curdev->next;
  }

  ftdi_list_free(&devlist);
  ftdi_deinit(&ftdic);

  return 0;
}
Esempio n. 25
0
static int presto_close(void)
{

	int result = ERROR_OK;

	presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
	if (presto->retval != sizeof(presto_init_seq))
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;

	presto->retval = ftdi_usb_close(&presto->ftdic);
	if (presto->retval < 0)
		result = ERROR_JTAG_DEVICE_ERROR;
	else
		ftdi_deinit(&presto->ftdic);

	return result;
}
Esempio n. 26
0
int main()
{
    unsigned char c = 0;
    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(&ftdic, 0x0403, 0x6001) < 0) {
        puts("Can't open device");
        return 1;
    }

    /* Enable bitbang mode with a single output line */
//     ftdi_enable_bitbang(&ftdic, LED);
    if (ftdi_set_bitmode(&ftdic, LED, BITMODE_BITBANG) < 0)
        puts( "Can't enable bitbang");

    /* Endless loop: invert LED state, write output, pause 1 second */
//     for(;;) {
    ftdi_write_data(&ftdic, &c, 1); //low
    sleep(1);
    c ^= LED;
    ftdi_write_data(&ftdic, &c, 1);//high
    sleep(1);
    c ^= LED;
    ftdi_write_data(&ftdic, &c, 1);//low
//     }
//     ftdi_set_bitmode(&ftdic, LED, 0x00);
    if(ftdi_disable_bitbang(&ftdic) < 0)
        puts("Can't disable bitbang");

//     if(ftdi_usb_reset(&ftdic) < 0)
//       puts("Can't reset device");
    if(ftdi_usb_close(&ftdic)<0)
        puts("Can't close device");
    ftdi_deinit(&ftdic);
}
Esempio n. 27
0
int main(int argc, char **argv)
{
  int ret;
  struct ftdi_context ftdic;
  ftdi_init(&ftdic);

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

  // Read out FTDIChip-ID of R type chips
  if (ftdic.type == TYPE_R) {
    unsigned int chipid;
    printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(&ftdic, &chipid));
    printf("FTDI chipid: %X\n", chipid);
  }

  ftdi_usb_close(&ftdic);
  ftdi_deinit(&ftdic);

  return EXIT_SUCCESS;
}
Esempio n. 28
0
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 main(int argc, char **argv)
{

    struct ftdi_context *ftdi;
    struct ftdi_device_list *devlist;
    int ret;

    if (!(argc==1 || argc==3))
    {
       fprintf(stderr, "Usage: opal-init [<bus> <device>]\n");
       return EXIT_FAILURE;
    }



    // Initialize FTDI layer
    if ((ftdi = ftdi_new()) == 0)
    {
        fprintf(stderr, "Initializing FTDI chip failed\n");
        return EXIT_FAILURE;
    }

    if (argc==1)
    {
        // get first matching device
        if ((ret = debug_get_devices(ftdi, &devlist))<=0)
        {
            fprintf(stderr, "Error: No opal debug board found.\n");
            return EXIT_FAILURE;
        }

    } else {

        // get device with matching bus and device identifier
        if ((ret = debug_get_device(ftdi, &devlist, argv[1], argv[2]))<=0)
        {
            fprintf(stderr, "Error: No opal debug board found.\n");
            return EXIT_FAILURE;
        }

    }


    // open device
    printf("Opal: Debug board found (Bus: %s Device: %s).\n", devlist->dev->bus->dirname, devlist->dev->filename);
    if (debug_open_device(ftdi, devlist)==EXIT_FAILURE)
    {
        ftdi_deinit(ftdi);
        return 0;
    }

    // set all pins to input    
    debug_set_pins(ftdi, SET_BITS_LOW, 0, 0);
    debug_set_pins(ftdi, SET_BITS_HIGH, 0, 0);

    debug_close_device(ftdi);

    printf("Opal: Debug board initialized.\n");

    return 0;
}
Esempio n. 30
0
int main(int argc, char *argv[])
{
    /*
    configuration options
    */
    cfg_opt_t opts[] =
    {
        CFG_INT("vendor_id", 0, 0),
        CFG_INT("product_id", 0, 0),
        CFG_BOOL("self_powered", cfg_true, 0),
        CFG_BOOL("remote_wakeup", cfg_true, 0),
        CFG_BOOL("in_is_isochronous", cfg_false, 0),
        CFG_BOOL("out_is_isochronous", cfg_false, 0),
        CFG_BOOL("suspend_pull_downs", cfg_false, 0),
        CFG_BOOL("use_serial", cfg_false, 0),
        CFG_BOOL("change_usb_version", cfg_false, 0),
        CFG_INT("usb_version", 0, 0),
        CFG_INT("default_pid", 0x6001, 0),
        CFG_INT("max_power", 0, 0),
        CFG_STR("manufacturer", "Acme Inc.", 0),
        CFG_STR("product", "USB Serial Converter", 0),
        CFG_STR("serial", "08-15", 0),
        CFG_INT("eeprom_type", 0x00, 0),
        CFG_STR("filename", "", 0),
        CFG_BOOL("flash_raw", cfg_false, 0),
        CFG_BOOL("high_current", cfg_false, 0),
        CFG_STR_LIST("cbus0", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0),
        CFG_STR_LIST("cbus1", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0),
        CFG_STR_LIST("cbus2", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0),
        CFG_STR_LIST("cbus3", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0),
        CFG_STR_LIST("cbus4", "{TXDEN,PWRON,RXLED,TXLED,TX_RX_LED,SLEEP,CLK48,CLK24,CLK12,CLK6}", 0),
        CFG_BOOL("invert_txd", cfg_false, 0),
        CFG_BOOL("invert_rxd", cfg_false, 0),
        CFG_BOOL("invert_rts", cfg_false, 0),
        CFG_BOOL("invert_cts", cfg_false, 0),
        CFG_BOOL("invert_dtr", cfg_false, 0),
        CFG_BOOL("invert_dsr", cfg_false, 0),
        CFG_BOOL("invert_dcd", cfg_false, 0),
        CFG_BOOL("invert_ri", cfg_false, 0),
        CFG_END()
    };
    cfg_t *cfg;

    /*
    normal variables
    */
    int _read = 0, _erase = 0, _flash = 0;

    const int max_eeprom_size = 256;
    int my_eeprom_size = 0;
    unsigned char *eeprom_buf = NULL;
    char *filename;
    int size_check;
    int i, argc_filename;
    FILE *fp;

    struct ftdi_context *ftdi = NULL;

    printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
    printf ("(c) Intra2net AG and the libftdi developers <*****@*****.**>\n");

    if (argc != 2 && argc != 3)
    {
        printf("Syntax: %s [commands] config-file\n", argv[0]);
        printf("Valid commands:\n");
        printf("--read-eeprom  Read eeprom and write to -filename- from config-file\n");
        printf("--erase-eeprom  Erase eeprom\n");
        printf("--flash-eeprom  Flash eeprom\n");
        exit (-1);
    }

    if (argc == 3)
    {
        if (strcmp(argv[1], "--read-eeprom") == 0)
            _read = 1;
        else if (strcmp(argv[1], "--erase-eeprom") == 0)
            _erase = 1;
        else if (strcmp(argv[1], "--flash-eeprom") == 0)
            _flash = 1;
        else
        {
            printf ("Can't open configuration file\n");
            exit (-1);
        }
        argc_filename = 2;
    }
    else
    {
        argc_filename = 1;
    }

    if ((fp = fopen(argv[argc_filename], "r")) == NULL)
    {
        printf ("Can't open configuration file\n");
        exit (-1);
    }
    fclose (fp);

    cfg = cfg_init(opts, 0);
    cfg_parse(cfg, argv[argc_filename]);
    filename = cfg_getstr(cfg, "filename");

    if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
        printf("Hint: Self powered devices should have a max_power setting of 0.\n");

    if ((ftdi = ftdi_new()) == 0)
    {
        fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
                ftdi_get_error_string(ftdi));
        return EXIT_FAILURE;
    }

    if (_read > 0 || _erase > 0 || _flash > 0)
    {
        int vendor_id = cfg_getint(cfg, "vendor_id");
        int product_id = cfg_getint(cfg, "product_id");

        i = ftdi_usb_open(ftdi, vendor_id, product_id);

        if (i != 0)
        {
            int default_pid = cfg_getint(cfg, "default_pid");
            printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
            printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
            printf("Retrying with default FTDI pid=%#04x.\n", default_pid);

            i = ftdi_usb_open(ftdi, 0x0403, default_pid);
            if (i != 0)
            {
                printf("Error: %s\n", ftdi->error_str);
                exit (-1);
            }
        }
    }
    ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), 
                              cfg_getstr(cfg, "product"), 
                              cfg_getstr(cfg, "serial"));

    printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
    eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
    printf("EEPROM size: %d\n", my_eeprom_size);

    if (_read > 0)
    {
        ftdi_eeprom_decode(ftdi, 1);

        eeprom_buf = malloc(my_eeprom_size);
        ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);

        if (eeprom_buf == NULL)
        {
            fprintf(stderr, "Malloc failed, aborting\n");
            goto cleanup;
        }
        if (filename != NULL && strlen(filename) > 0)
        {

            FILE *fp = fopen (filename, "wb");
            fwrite (eeprom_buf, 1, my_eeprom_size, fp);
            fclose (fp);
        }
        else
        {
            printf("Warning: Not writing eeprom, you must supply a valid filename\n");
        }

        goto cleanup;
    }

    eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
    eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));

    eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
    eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
    eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));

    eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
    eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
    eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));

    eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
    eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
    eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
    eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));

    eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
    eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13));
    eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13));
    eeprom_set_value(ftdi, CBUS_FUNCTION_2, str_to_cbus(cfg_getstr(cfg, "cbus2"), 13));
    eeprom_set_value(ftdi, CBUS_FUNCTION_3, str_to_cbus(cfg_getstr(cfg, "cbus3"), 13));
    eeprom_set_value(ftdi, CBUS_FUNCTION_4, str_to_cbus(cfg_getstr(cfg, "cbus4"), 9));
    int invert = 0;
    if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
    if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
    if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
    if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
    if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
    if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
    if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
    if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
    eeprom_set_value(ftdi, INVERT, invert);

    eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
    eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
    eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP);
    eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP);
    eeprom_set_value(ftdi, CHANNEL_A_RS485, 0);
    eeprom_set_value(ftdi, CHANNEL_B_RS485, 0);
    eeprom_set_value(ftdi, CHANNEL_C_RS485, 0);
    eeprom_set_value(ftdi, CHANNEL_D_RS485, 0);

    if (_erase > 0)
    {
        printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
    }

    size_check = ftdi_eeprom_build(ftdi);
    eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);

    if (size_check == -1)
    {
        printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
        printf ("You need to short your string by: %d bytes\n", size_check);
        goto cleanup;
    } else if (size_check < 0) {
        printf ("ftdi_eeprom_build(): error: %d\n", size_check);
    }
    else
    {
        printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
    }

    if (_flash > 0)
    {
        if (cfg_getbool(cfg, "flash_raw"))
        {
            if (filename != NULL && strlen(filename) > 0)
            {
                eeprom_buf = malloc(max_eeprom_size);
                FILE *fp = fopen(filename, "rb");
                if (fp == NULL)
                {
                    printf ("Can't open eeprom file %s.\n", filename);
                    exit (-1);
                }
                my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
                fclose(fp);
                if (my_eeprom_size < 128)
                {
                    printf ("Can't read eeprom file %s.\n", filename);
                    exit (-1);
                }

                ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
            }
        }
        printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
        libusb_reset_device(ftdi->usb_dev);
    }

    // Write to file?
    if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
    {
        fp = fopen(filename, "w");
        if (fp == NULL)
        {
            printf ("Can't write eeprom file.\n");
            exit (-1);
        }
        else
            printf ("Writing to file: %s\n", filename);

        if (eeprom_buf == NULL)
            eeprom_buf = malloc(my_eeprom_size);
        ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);

        fwrite(eeprom_buf, my_eeprom_size, 1, fp);
        fclose(fp);
    }

cleanup:
    if (eeprom_buf)
        free(eeprom_buf);
    if (_read > 0 || _erase > 0 || _flash > 0)
    {
        printf("FTDI close: %d\n", ftdi_usb_close(ftdi));
    }

    ftdi_deinit (ftdi);
    ftdi_free (ftdi);

    cfg_free(cfg);

    printf("\n");
    return 0;
}