static int config_i2c(struct ftdi_context *ftdi)
{
	int ret;
	uint8_t buf[5];
	uint16_t divisor;

	ret = ftdi_set_latency_timer(ftdi, 16 /* ms */);
	if (ret < 0)
		fprintf(stderr, "Cannot set latency\n");

	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
	if (ret < 0) {
		fprintf(stderr, "Cannot reset MPSSE\n");
		return -EIO;
	}
	ret = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE);
	if (ret < 0) {
		fprintf(stderr, "Cannot enable MPSSE\n");
		return -EIO;
	}

	ret = ftdi_usb_purge_buffers(ftdi);
	if (ret < 0)
		fprintf(stderr, "Cannot purge buffers\n");

	/* configure the clock */
	divisor = (60000000 / (2 * I2C_FREQ * 3 / 2 /* 3-phase CLK */) - 1);
	buf[0] = EN_3_PHASE;
	buf[1] = DIS_DIV_5;
	buf[2] = TCK_DIVISOR;
	buf[3] = divisor & 0xff;
	buf[4] = divisor >> 8;
	ret = ftdi_write_data(ftdi, buf, sizeof(buf));
	return ret;
}
Example #2
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;
}
Example #3
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;
    }
}
Example #4
0
File: lm60.c Project: jatocode/ftdi
int main(int argc, char *argv[])
{
    struct ftdi_context ftdic;
    int portrelay = MOTOR_RELAY;  
    unsigned char relay = 0;

    /* 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");
        fprintf(stderr, "unable to open ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        return EXIT_FAILURE;
    }
    ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG);

    /* Reading existing state */
    relay = 0;     
    ftdi_read_data(&ftdic, &relay, 1);

//    printf("Activating relay %d\n", MOTOR_RELAY);
    relay |= (1 << portrelay);
    ftdi_write_data(&ftdic, &relay, 1);

//    printf("Sleeping for %dms\n", SLEEP_TIME);
    usleep(SLEEP_TIME * 1000); // usleep want microsecs 

//    printf("Dectivating relay %d\n", MOTOR_RELAY);
    relay &= ~(1 << portrelay);
    ftdi_write_data(&ftdic, &relay, 1);

    return 0;
}
Example #5
0
static int ft245r_drain(PROGRAMMER * pgm, int display) {
    int r;
    unsigned char t;

    // flush the buffer in the chip by changing the mode.....
    r = ftdi_set_bitmode(handle, 0, BITMODE_RESET); 	// reset
    if (r) return -1;
    r = ftdi_set_bitmode(handle, ft245r_ddr, BITMODE_SYNCBB); // set Synchronuse BitBang
    if (r) return -1;

    // drain our buffer.
    while (head != tail) {
        ft245r_recv (pgm, &t, 1);
    }
    return 0;
}
Example #6
0
void spi_init(void) {
	int ret;

	ftdi = ftdi_new();
	if (!ftdi) {
		fatal_error("ftdi_new failed!\n");
	}

	ret = ftdi_usb_open(ftdi, 0x0403, 0x6001);

	if (ret < 0 && ret != -5) {
		ftdi_free(ftdi);
		fatal_error("unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}

	ret = ftdi_set_bitmode(ftdi, PINS_OUT, BITMODE_SYNCBB);
	if (ret != 0) {
		ftdi_free(ftdi);
		fatal_error("unable to set bitmode: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}

	ret = ftdi_set_baudrate(ftdi, 57600);
	if (ret != 0) {
		ftdi_disable_bitbang(ftdi);
		ftdi_free(ftdi);
		fatal_error("unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(ftdi));
	}
}
Example #7
0
int main()
{
    unsigned char d = 0;
    unsigned char i;
    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");
        fprintf(stderr, "unable to open ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        return EXIT_FAILURE;
    }
    ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG);

    ftdi_write_data(&ftdic, &d, 1);
    sleep(1);
    for(i=0;i<8;i++) {
        printf("%d\n",d); 
        d |= d<<1;
    	ftdi_write_data(&ftdic, &d, 1);
	usleep(200 * 1000);
    }
    sleep(2);
    d = 0;
    ftdi_write_data(&ftdic, &d, 1);
   
    return 0;
}
Example #8
0
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;
}
Example #9
0
static int jtagkey_init(unsigned short vid, unsigned short pid) {
	int ret = 0;
	unsigned char c;

	if ((ret = ftdi_init(&ftdic)) != 0) {
		fprintf(stderr, "unable to initialise libftdi: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}
	
	if ((ret = ftdi_usb_open(&ftdic, vid, pid)) != 0) {
		fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_usb_reset(&ftdic)) != 0) {
		fprintf(stderr, "unable reset device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_set_interface(&ftdic, INTERFACE_A)) != 0) {
		fprintf(stderr, "unable to set interface: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_write_data_set_chunksize(&ftdic, USBBUFSIZE))  != 0) {
		fprintf(stderr, "unable to set write chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_read_data_set_chunksize(&ftdic, USBBUFSIZE))  != 0) {
		fprintf(stderr, "unable to set read chunksize: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = jtagkey_latency(OTHER_LATENCY)) != 0)
		return ret;

	c = 0x00;
	ftdi_write_data(&ftdic, &c, 1);

	if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, BITMODE_SYNCBB))  != 0) {
		fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_set_baudrate(&ftdic, JTAG_SPEED))  != 0) {
		fprintf(stderr, "unable to set baudrate: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	if ((ret = ftdi_usb_purge_buffers(&ftdic))  != 0) {
		fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
		return ret;
	}

	return ret;
}
Example #10
0
void swdptap_turnaround(uint8_t dir)
{
	static uint8_t olddir = 0;

        //DEBUG("%s", dir ? "\n-> ":"\n<- ");
	platform_buffer_flush();

	if(dir == olddir) return;
	olddir = dir;

	if(dir)	/* SWDIO goes to input */
		assert(ftdi_set_bitmode(ftdic, 0xA3, BITMODE_BITBANG) == 0);

	/* One clock cycle */
	ftdi_write_data(ftdic, "\xAB\xA8", 2);

	if(!dir) /* SWDIO goes to output */
		assert(ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG) == 0);
}
Example #11
0
static void swdptap_init_internal(void)
{
	int err;

	if((err = ftdi_set_bitmode(ftdic, 0, BITMODE_RESET)) ||
	   (err = ftdi_set_bitmode(ftdic, 0, BITMODE_MPSSE))) {
		fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	uint8_t setup[] = {
		DIS_DIV_5,
		TCK_DIVISOR, 0, 1,
		LOOPBACK_END
	};
	platform_buffer_write(setup, sizeof(setup));
	swdptap_set_bits();
	platform_buffer_flush();
}
Example #12
0
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);
}
Example #13
0
int jtagtap_init(void)
{
	int err;

	assert(ftdic != NULL);

	if((err = ftdi_set_bitmode(ftdic, 0, BITMODE_RESET)) ||
	   (err = ftdi_set_bitmode(ftdic, 0, BITMODE_MPSSE))) {
		fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	assert(ftdi_write_data(ftdic, "\x8B\x86\x06\x00\x80\xA8\xAB\x85", 8) == 8);

	/* Go to JTAG mode for SWJ-DP */
	for(int i = 0; i <= 50; i++) jtagtap_next(1, 0); /* Reset SW-DP */
	jtagtap_tms_seq(0xE73C, 16);		/* SWD to JTAG sequence */
	jtagtap_soft_reset();

	return 0;
}
Example #14
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);
}
Example #15
0
void init_hw ()
{
    ftdi_ok = FALSE;

    if (ftdi_init (&ctx) >= 0) {
        int f = ftdi_usb_open (&ctx, 0x0403, 0x6001);

        if (f >= 0 || f == -5) {
            purple_debug_info ("hwnotify", "INIT OK\n");

            ftdi_set_bitmode (&ctx, 0xFF, BITMODE_BITBANG);
            ftdi_ok = TRUE;
        }
    }
}
Example #16
0
int main(int argc, char **argv)
{
    struct ftdi_context *ftdi;
    int f;
    unsigned char buf[1];
    int retval = 0;

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


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


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

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

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

//    usleep(3 * 1000000);

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

//    ftdi_disable_bitbang(ftdi);

    ftdi_usb_close(ftdi);
    ftdi_free(ftdi);
done:
    return retval;
}
Example #17
0
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;
    }
}
//Try to find the ftdi chip and open it.
int FtdiNand::open(int vid, int pid, bool doslow) {
	unsigned char slow=DIS_DIV_5;
	if (doslow) slow=EN_DIV_5;
	m_slowAccess=doslow;
	//If vid/pid is zero, use default FT2232H vid/pid.
	if (vid==0) vid=0x0403;
	if (pid==0) pid=0x6010;
	//Open FTDI communications
	if (ftdi_init(&m_ftdi)<0) return error("init");
	if (ftdi_usb_open(&m_ftdi, vid, pid)<0) return error("open");
	if (ftdi_set_bitmode(&m_ftdi, 0, BITMODE_MCU)<0) error("bitmode");
	if (ftdi_write_data(&m_ftdi, &slow, 1)<0) return error("writing div5 cmd");
	if (ftdi_set_latency_timer(&m_ftdi, 1)<0) return error("setting latency");
	ftdi_usb_purge_buffers(&m_ftdi);
	return 1;
}
Example #19
0
int main(int argc, char **argv)
{
    struct ftdi_context *ftdi;
    int i, t;
    unsigned char data;
    int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */

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

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

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

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

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

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

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

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

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

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

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

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

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

    return 0;
}
Example #22
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;
}
Example #23
0
File: spi.c Project: z80/bt-board
int spi_close(void)
{
    LOG(DEBUG, "spi_nrefs=%d, spi_dev_open=%d", spi_nrefs, spi_dev_open);

    if (spi_dev_open) {
#ifdef ENABLE_LEDS
        spi_led(SPI_LED_OFF);
#endif

        /* Flush and reset the buffer */
        if (ftdi_buf_write_offset) {
            if (spi_ftdi_xfer(ftdi_buf, ftdi_buf_write_offset) < 0)
                return -1;
            ftdi_buf_write_offset = 0;
        }

        if (ftdi_set_bitmode(&ftdic, 0, BITMODE_RESET) < 0) {
            SPI_ERR("FTDI: reset bitmode failed: %s",
                    ftdi_get_error_string(&ftdic));
            return -1;
        }

        if (ftdi_usb_close(&ftdic) < 0) {
            SPI_ERR("FTDI: close failed: %s",
                    ftdi_get_error_string(&ftdic));
            return -1;
        }
#ifdef SPI_STATS
        spi_output_stats();
#endif

        free(ftdi_buf);
        ftdi_buf = NULL;
        ftdi_buf_size = 0;

        spi_dev_open = 0;
    }

    return 0;
}
Example #24
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);
}
Example #25
0
/* prepare the FTDI USB device */
int do_init(struct ftdi2s88_t *fs88) {
    fs88->ftdic = ftdi_new();

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

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

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

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

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

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

    return 0;
}
Example #26
0
int main(int argc, char *argv[])
{
    struct ftdi_context ftdic;
    unsigned char state;

    /* 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");
        fprintf(stderr, "unable to open ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        return EXIT_FAILURE;
    }
    ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG);

    /* Reading existing state */
    state = 0;     
    ftdi_read_data(&ftdic, &state, 1);

    printf("Bitbang state, byte one: 0x%0x\n", state);
    
    return state;
}
Example #27
0
int swdptap_init(void)
{
	int err;

	assert(ftdic != NULL);

	if((err = ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG)) != 0) {
		fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", 
			err, ftdi_get_error_string(ftdic));
		abort();
	}

	assert(ftdi_write_data(ftdic, "\xAB\xA8", 2) == 2);

	/* This must be investigated in more detail.
	 * As described in STM32 Reference Manual... */
	swdptap_seq_out(0xFFFF, 16); 
	swdptap_reset();
	swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */ 
	swdptap_reset();
	swdptap_seq_out(0, 16); 

	return 0;
}
Example #28
0
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;
}
static int send_special_waveform(struct ftdi_context *ftdi)
{
	int ret;
	int i;
	uint64_t *wave;
	uint8_t release_lines[] = {SET_BITS_LOW, 0, 0};

	wave = malloc(SPECIAL_BUFFER_SIZE);

	printf("Waiting for the EC power-on sequence ...");
	fflush(stdout);

retry:
	/* Reset the FTDI into a known state */
	ret = ftdi_set_bitmode(ftdi, 0xFF, BITMODE_RESET);
	if (ret != 0) {
		fprintf(stderr, "failed to reset FTDI\n");
		goto special_failed;
	}

	/*
	 * set the clock divider,
	 * so we output a new bitbang value every 2.5us.
	 */
	ret = ftdi_set_baudrate(ftdi, 160000);
	if (ret != 0) {
		fprintf(stderr, "failed to set bitbang clock\n");
		goto special_failed;
	}

	/* Enable asynchronous bit-bang mode */
	ret = ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
	if (ret != 0) {
		fprintf(stderr, "failed to set bitbang mode\n");
		goto special_failed;
	}

	/* do usb special waveform */

	wave[0] = 0x0;
	ftdi_write_data(ftdi, (uint8_t *)wave, 1);
	usleep(5000);

	/* program each special tick */
	for (i = 0; i < TICK_COUNT; ) {
		wave[i++] = SPECIAL_PATTERN_SDA_L_SCL_L;
		wave[i++] = SPECIAL_PATTERN_SDA_H_SCL_L;
		wave[i++] = SPECIAL_PATTERN_SDA_L_SCL_L;
	}
	wave[19] = SPECIAL_PATTERN_SDA_H_SCL_H;


	/* fill the buffer with the waveform pattern */
	for (i = TICK_COUNT; i < SPECIAL_BUFFER_SIZE / sizeof(uint64_t); i++)
		wave[i] = SPECIAL_PATTERN;

	ret = ftdi_write_data(ftdi, (uint8_t *)wave, SPECIAL_BUFFER_SIZE);
	if (ret < 0)
		fprintf(stderr, "Cannot output special waveform\n");

	/* clean everything to go back to regular I2C communication */
	ftdi_usb_purge_buffers(ftdi);
	ftdi_set_bitmode(ftdi, 0xff, BITMODE_RESET);
	config_i2c(ftdi);
	ftdi_write_data(ftdi, release_lines, sizeof(release_lines));

	/* wait for PLL stable for 5ms (plus remaining USB transfers) */
	usleep(10 * MSEC);

	/* if we cannot communicate, retry the sequence */
	if (check_chipid(ftdi) < 0) {
		sleep(1);
		goto retry;
	}
special_failed:
	printf("Done.\n");
	free(wave);
	return ret;
}
Example #30
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]);
	}