libftdi Archives

Subject: How to recover from ftdi_write_data() error?

From: "rick.walker@xxxxxxxxxxx" <rick.walker@xxxxxxxxxxx>
To: "libftdi@xxxxxxxxxxxxxxxxxxxxxxx" <libftdi@xxxxxxxxxxxxxxxxxxxxxxx>
Cc: "rick.walker@xxxxxxxxxxx" <rick.walker@xxxxxxxxxxx>
Date: Wed, 9 Oct 2024 00:20:19 +0000

 

 

I occasionally get my libftdi application locking up with an error on ftdi_write_data().  I would like to recover from this automatically, but I don't know what caused the error.  The code for ftdi_write_data() calls libusb_bulk_transfer().  If it gets an error it returns a generic (-1). 

 

Would it be reasonable to pass through the error from libusb_bulk_transfer() directly so I can diagnose the error?

 

Here’s a snippet of the existing code.  I’ve highlighted the problem area in yellow.

 

 

int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)

{

     int offset = 0;

     int actual_length;

 

     if (ftdi == NULL || ftdi->usb_dev == NULL)

         ftdi_error_return(-666, "USB device unavailable");

 

     while (offset < size)

     {

         int write_size = ftdi->writebuffer_chunksize;

 

         if (offset+write_size > size)

             write_size = size-offset;

 

         if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)

             ftdi_error_return(-1, "usb bulk write failed");

 

 

Could we change to something like this where we pass through the specific negative error code from libusb_bulk_transfer()?  It seems that it might make it easier to figure out how to recover from an ftdi_write_data error.

 

     int err;

     if ((err=libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)) {

          Ftdi_error_return(err, “usb bulk write failed”);

      }

 

Any suggestions for how to recover from ftdi_write_data() errors would be appreciated.  It seems like knowing the lower level error code might be helpful..

 

Currently, I just flush the buffers but it rarely fixes the problem.

 

Kind regards,

--

Rick Walker

 



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


Current Thread