libusb_bulk_tranfer problem

View: New views
3 Messages — Rating Filter:   Alert me  

libusb_bulk_tranfer problem

by Ford, Pat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All;
 I have a device that requires a program to be loaded through a
cy7C64613 chip. I've downloaded the bootloader for the CY7C now I have
to pass the fpga program through it.
I get error -7 (libusb_timeout).

Thanks
Pat

Bus 005 Device 051: ID XXXX:YYYY
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0xXXXX
  idProduct          0xXXXX
  bcdDevice            0.02
  iManufacturer           1 Somecompany Corp
  iProduct                2 USB Device
  iSerial                 3 0666
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           39
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      0
      bInterfaceProtocol      0
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Device Status:     0x0000
  (Bus Powered)


interesting usbmon section

ffff88012d1969c0 1378138149 C Co:5:051:0 0 0 ffff88012d1969c0 1379073886
S Bo:5:051:2 -115 64 = ff04d4bd f8da7fff df7fdf75 7fffffaf df767bbf
ddd65d6f bd7f7ffd f5d75f7f ffff88012d1969c0 1379075156 C Bo:5:051:2 0 64
> ffff88012d1969c0 1379075225 S Bo:5:051:2 -115 64 = fdffffff f7ffffdf
ffffffff ffffff7f ef7fffff ffffffff ff7ffcff fdf0c36f ffff88012d1969c0
1384082181 C Bo:5:051:2 -2 0 ffff88012d1969c0 1384082296 S Bo:5:051:2
-115 64 = 1f7df7dd e77df7f9 679f7dfc fdf7df7f fffd77ff ffdf7ffd fdff7f7f
fdfdffff ffff88012d1969c0 1389090208 C Bo:5:051:2 -2 0 ffff88012d1969c0
1389090321 S Bo:5:051:2 -115 64 = ffffffff ffffffff ffffffcf ffffcf3f
fffeffdf dfffffff ff7fffff ffffffff ffff88012d1969c0 1394098234 C
Bo:5:051:2 -2 0 ffff88012d1969c0 1394098353 S Bo:5:051:2 -115 64 =
fefbef7f f6ffffff ffffffff ffbfffff ffffffff ffffffff ffffffff ffffffff

relevent code section


int get_usb_device(void)
{
    int i=0, err=0, ret=0;
    uint16_t iGpif=0;
    ret = libusb_get_device_list(0, &devs2);
    if (ret < 0)
    return -31;
    while ((dev = devs2[i++]) != NULL)
    {
    ret = libusb_get_device_descriptor(dev, &desc);
    if (ret < 0)
    fprintf(stderr,"\n failed to get descriptor:");
    if (desc.idVendor == Id )
    {
    printf("%04x:%04x (bus %d, device %d)\n",desc.idVendor,
desc.idProduct,
    libusb_get_bus_number(dev),
libusb_get_device_address(dev));
    fprintf(stderr,"found an initialized Device\n");
    ret = libusb_open(dev, &inited);
    if (ret < 0)
    {
    inited = NULL;
    exit (-30);
    }
    err = libusb_claim_interface(inited, 0);
    if(err != LIBUSB_SUCCESS)
    fprintf(stderr, "libusb_claim_interface 0
returned %d\n", err);
    printf("Rad device attached, SN =
%d\n",desc.iSerialNumber);
    // now we need to stop the device and download the fpga
program
    if (!TransferGpifIn(0x118,&iGpif)) // make sure the
device is not running
    fprintf(stderr,"ERROR: usb_device Could not read
status from 0x118 Gpif\n");
    if (!download_fpga(inited,"fpga.hex"))
    {
    fprintf(stderr,"ERROR: usb_device Failed to
download fpga.hex\n");
    return -33;
    }
    break;
    }
    }
    if(!inited)
    {
    fprintf(stderr, "libusb failed to find an initialized rad\n");
    exit (-32);
    }
    printf("\n");
    return 1;
}

int download_fpga (libusb_device_handle *dh, char *fname) {
    int size, transferred, ret=0, i=0,x=0;
    int chunkCount=0, bytes=0, total=0;
    char szData[16];
    unsigned char ptBinFpgaData[MAX_FPGA_DATA_LEN];
    unsigned char *cur;
    infile = fopen (fname, "rb");
    if (infile == NULL)
    {
    printf (" \n FileNotFound %s\n", fname);
    //  //syslog(LOG_ERR,"Logic file not found %s", inputfile);
    exit(EXIT_FAILURE);
    }
    (void)fseek (infile, 0, SEEK_END);
    size = ftell (infile);
    if(size ==0)
    {
    printf ("\nHex file  (%s )for the fpga is empty",fname);
    return (-1);
    }
    printf ("\n size of hex file is %d",size);
    rewind (infile);
    // table is ascii, we need to change it to hex
    for (i=0 ; (!feof(infile)) && (i<(MAX_FPGA_DATA_LEN * 2)) ; i++)
    {
    szData[0] = (char)fgetc(infile);
    szData[1] = (char)fgetc(infile);
    szData[2] = 0;
   
    sscanf(szData,"%x",&ptBinFpgaData[i]);
    }
   
    if (size > MAX_FPGA_DATA_LEN)
    {
    printf ("\nHex file  (%s )for the fpga is too large",fname);
    exit(EXIT_FAILURE);
    }
   
    if ( !TransferGpifOut(0x102, 1) )
    {
    printf("\nERROR: During Start of FPGA download");
    return(-2);
    }
    cur=ptBinFpgaData;
    // we need to split the buffer into 64byte chunks
    chunkCount = (size + CHUNK_SIZE - 1) / CHUNK_SIZE;
    for (x=0; x<=chunkCount; x++)
    {
    if (x==chunkCount-1 && size % CHUNK_SIZE)
    bytes = size % CHUNK_SIZE;
    else
    bytes = CHUNK_SIZE;
    cur = ptBinFpgaData + x*CHUNK_SIZE;
   
    ret = libusb_bulk_transfer (dh, FPGA_PIPE, cur, bytes,
&transferred, 5000);
    if(errno != 0)
    perror("download FPGA Error ");
    if(ret < 0)
    printf("\n libusb_bulk_transfer returned %d bytes =
%d",ret,bytes);
   
    if (transferred != bytes)
    {
    printf("\nERROR: During bulk write FPGA:error %d should
equal %d(size) %d bytes transferred chunk %d\n",
                 transferred, bytes,transferred,x );
    //return(-3);
    }
    else
    {
    printf("\nSucess: During bulk write FPGA %d bytes
transferred chunk %d\n",transferred,x);
    total+=bytes;
    }
    }
   
    if ( !TransferGpifOut(0x102, 0) )
    {
    printf("ERROR: During stop FPGA");
    return(-4);
    }
    printf("\n %d bytes transferred",transferred);
    return 4;
}


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Libusb-devel mailing list
Libusb-devel@...
https://lists.sourceforge.net/lists/listinfo/libusb-devel

Re: libusb_bulk_tranfer problem

by Tim Roberts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ford, Pat wrote:
> I have a device that requires a program to be loaded through a
> cy7C64613 chip. I've downloaded the bootloader for the CY7C now I have
> to pass the fpga program through it.
> I get error -7 (libusb_timeout).
>  

Stock Cypress bootloaders don't know anything about downloading FPGAs.
That all has to be implemented in your own firmware.  From here, it
looks like the device accepted the first 64 bytes then rejected the next
64.  Without knowing the firmware, there's not a lot we can do.

I'm a little surprised at your loading code.  Usually, a file with a
.hex extension is an Intel HEX format file.  That includes addresses and
checksums -- a lot more than just raw bytes to be sent.  It's possible
you're just using the same extension for a raw dump of the FPGA load,
but it's worth asking.

--
Tim Roberts, timr@...
Providenza & Boekelheide, Inc.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Libusb-devel mailing list
Libusb-devel@...
https://lists.sourceforge.net/lists/listinfo/libusb-devel

Parent Message unknown Re: libusb_bulk_tranfer problem

by Tim Roberts :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ford, Pat wrote:
>  A few things I've noticed, this device when the firmware is downloaded
> changes its device number, manufacturer and product id.

Yes, most Cypress devices do this, unless you burn the firmware into EEPROM.

> So after running
> the first stage downloader I need to rescan the bus. It can take 20
> seconds before the device reappears.
>  

Do you close the interface and the device after downloading the
firmware?  Your device file becomes an orphan when your device drops off
of the bus.  Are you sending the 8051 reset to force the device to
re-enumerate with the new descriptors?

> I've tried to manually download each 64 byte chunk. After sending the
> bulk transfer I always end up waiting timeout seconds.
>
> Is there a way to force a faster rescan of the usb bus?
>  

Well, the host controller driver is notified right away when a new
device appears.  You should be able to see messages in the log when your
device disappears and reappears.  Do you have udev rules to manage the
device permissions?  

--
Tim Roberts, timr@...
Providenza & Boekelheide, Inc.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Libusb-devel mailing list
Libusb-devel@...
https://lists.sourceforge.net/lists/listinfo/libusb-devel