Hello,

when doing MPSSE transfers, e.g. for JTAG, there is a constant need to read
back a fixed amount of data caused be the last write.
ftdi_read_data() doesn't guarantee to read back all  requested data, but can
also return from a timeout, a short read and probably more causes.

In my code I have a wrapper readusb() around ftdi_read_data() to only return
when all data has been read, or some hard error happened:

unsigned int readusb(unsigned char * rbuf, unsigned long len)
{
    int length = (int) len, read = 0;
    int timeout=0, last_errno, last_read;
    last_read = ftdi_read_data(&ftdi_a, rbuf, length );
    if (last_read > 0)
        read += last_read;
    while ((read <length) && ( timeout <1000)) 
    {
        last_errno = 0;
        last_read = ftdi_read_data(&ftdi_a, rbuf+read, length -read);
        if (last_read > 0)
            read += last_read;
        else
            last_errno = errno;
        timeout++;
    }
    if (timeout >= 1000)
    {
        if (last_errno)
        {
            fprintf(stderr,"error %s\n", strerror(last_errno));
            return EXIT_FAILURE;
        }
    }
    if (read <0)
    {
        fprintf(stderr,"Error %d str: %s\n", -read, strerror(-read));
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Shouldn't some code like this be included in libftdi?

Bye
-- 
Uwe Bonnes                [email protected]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [email protected]   

Reply via email to