libftdi Archives

Subject: working example for FT2232H in sync fifo mode

From: Andre Haupt <andre@xxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 28 Nov 2013 14:47:48 +0100
Hi all,

I am facing some difficulties when trying to use a FT2232H in syncronuous fifo 
mode.
We have an FPGA hooked up to the FT2232H that can send and receive from/to the 
fifo.
My problem is that writing to the device with ftdi_write_data() always fails 
after a few bytes.

Am i missing something in my setup code (attached)?
Does anybody have a working example for writing in synchronuous fifo mode?
What else does one have to consider when using sync fifo mode?

cheers,

Andre


#include <stdio.h>

#include <ftdi.h>

int main(void)
{
    int ftStatus;
    struct ftdi_context *fifo;

    fifo = ftdi_new();

    //Open FIFO Channel
    ftdi_set_interface(fifo, INTERFACE_A);
    ftStatus = ftdi_usb_open(fifo, 0x0403, 0x6010);
    if (ftStatus != 0) {
        printf("could not open FIFO IF of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Reset FT
    ftStatus = ftdi_usb_reset(fifo);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to RESET FIFO IF of Tricorder\n");
        return EXIT_FAILURE;
    }

    ftStatus = ftdi_usb_purge_buffers(fifo);
    if (ftStatus != 0) {
        printf("could not purge fifo buffers\n");
        return EXIT_FAILURE;
    }

    //Bitmode Reset
    ftStatus = ftdi_set_bitmode(fifo, 0xFF, BITMODE_RESET);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to RESET bitmode of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Set FT 245 Synchronous FIFO mode
    ftStatus = ftdi_set_bitmode(fifo, 0xFF, BITMODE_SYNCFF);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to set synchronous FIFO mode of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Set Latency Timer
    ftStatus = ftdi_set_latency_timer(fifo, 2);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to set LatencyTimer of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Set USB Parameters
    ftStatus = ftdi_read_data_set_chunksize(fifo, 0x10000);
    ftStatus |= ftdi_write_data_set_chunksize(fifo, 0x10000);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to set FlowControl of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Set Flow Control
    ftStatus = ftdi_setflowctrl(fifo, SIO_RTS_CTS_HS);
    if (ftStatus != 0) {
        ftdi_usb_close(fifo);
        printf("unable to set FlowControl of Tricorder\n");
        return EXIT_FAILURE;
    }

    //Set timeouts
    fifo->usb_read_timeout = 100;
    fifo->usb_write_timeout = 1000;

    //Send junk
    char *buffer = "Hello libftdi!";
    int written = 0, total = 0;
    while (1)
    {
        written = ftdi_write_data(fifo, buffer, strlen(buffer));
        if (written < 0) {
            printf("unable to write to the tricorder: %i\n", written);
            ftdi_usb_close(fifo);
            ftdi_free(fifo);
            return EXIT_FAILURE;
        }
        else if (written > 0)
        {
            total += written;
            printf("wrote %s\t written: %i\t total: %i\n",
                   buffer,
                   written,
                   total);
        }
    }

    ftdi_usb_close(fifo);
    ftdi_free(fifo);
    return 0;
}


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread