Microchip

 PICDEM FS USB under Linux and Windows with libusb

Change Page: 123 > | Showing page 1 of 3, messages 1 to 20 of 41
Author Message
xiaofan

  • Total Posts : 6244
  • Reward points : 0
  • Joined: 4/14/2005
  • Location: Singapore
  • Status: offline
PICDEM FS USB under Linux and Windows with libusb Saturday, August 06, 2005 5:02 AM (permalink)
0
I have successfully ported Rick Luddy's Linux
USB bootloader for the PIC FS USB demo board at
http://internetking.org/fsusb/ to Windows using
libusb-win32 and MingW.

With the source in the website, one should be able to
recompile the application in Windows.

I am still contacting the original author to see if he
can incorporate my changes. I am also trying to find
a place to post the compiled application.

Edit: add the email here
> -----Original Message-----
> From: KILLspampiclist-bouncesspamspamKILLspammit.edu
> [ EraseMEpiclist-bouncesTakeThisOuTspammit.edu] On Behalf Of Chen Xiao Fan
> Sent: 2005 Aug 04, Thu 06:12
> To: 'piclistspam.....mit.edu'
> Cc: '@spam@rluddyspam_OUTspam.....acm.org'
> Subject: Re: [PIC] Porting libusb application to Windows
> using libusb-win32
>
> With the help of the developer of libusb-win32 Stephan Meyer,
> I have my first success in the porting efforts. I have ported
> Rick Luddy's USB Linux bootloader for the PIC FS USB demo
> board to Windows.
>
> Since I do not have a web site, I will list the changes here.
>
> 1) solve the problem of getline with Mingw by downloading
> getline.c and getline.h from the internet and adds the header
> file to rjlhex.c. Add getline.o to the make file as well.
>
> 2) the other changes are all applied to fsusb.c
> a) remove call to geteuid. Windows does not have this.
> b) for (bus=usb_busses;bus!=NULL;bus=bus->next) {
> --> for (bus=usb_get_busses();bus!=NULL;bus=bus->next) {
> c) The main problem is the endpoint address. Linux libusb is
> not so strict as libusb-win32.
>
> c1) change "const static int fsusb_endpoint=1" to "const
> static int fsusb_endpoint_in=0x81" and const static int
> fsusb_endpoint_out=1".
>
> c2) change
> r=usb_bulk_read(d, fsusb_endpoint, dest, len, fsusb_timeout);
> to:
> r=usb_bulk_read(d, fsusb_endpoint_out, dest, len, fsusb_timeout);
>
> c3) change
> r=usb_bulk_write(d, fsusb_endpoint, "\0\0\0\0\0", 5, fsusb_timeout);
> to:
> r=usb_bulk_write(d, fsusb_endpoint_in, "\0\0\0\0\0", 5,
> fsusb_timeout);
>
> c4) change the rest of the usb_bulk_write() functions to use
> fsusb_endpoint_in.
>
> After these changes, the fsusb application works just like
> the Linux version. It seems that the Linux version of libusb
> is not so strict on the endpoint address. Despite the error
> message when flashing the HID demo hex file, the firmware
> seems to work properly. I have the same error message under
> Linux. So this is not the problem in the porting.
>
> If possible I would like to post the changed codes and the
> compiled application to a web site. The ported application is
> command line based and require the installation of the filter
> driver of libusb-win32 and the original Microchip drivers.
>
> My next step is the usb_pickit application.
>
> Regards,
> Xiaofan
>
<message edited by xiaofan on Monday, July 28, 2008 6:27 AM>
 
#1
    xiaofan

    • Total Posts : 6244
    • Reward points : 0
    • Joined: 4/14/2005
    • Location: Singapore
    • Status: offline
    RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Thursday, August 11, 2005 7:33 AM (permalink)
    0
    By the way, please feedback to me if you are using the Windows port.
    I still have no answer from the original author.

    Regards,
    Xiaofan
     
    #2
      xiaofan

      • Total Posts : 6244
      • Reward points : 0
      • Joined: 4/14/2005
      • Location: Singapore
      • Status: offline
      RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, August 12, 2005 8:25 AM (permalink)
      0
      There are two ways to use this application.

      1) to use Microchip driver and install libusb-win32 filter driver.
      (download the libusb-win32-filter-bin-0.1.10.1.exe and install it).

      2) "update" Microchip driver to the libusb-win32 device driver
      with or without the libusb filter driver.


      Attached please find the libusb device driver if option 2 is used.
      It is generated using libusb-win32 device driver inf wizard.
      (download libusb-win32-device-bin-0.1.10.1.tar.gz and run
      the inf wizard in the bin sub directory)

      Regards,
      Xiaofan
      <message edited by xiaofan on Sunday, September 23, 2007 1:36 AM>
       
      #3
        xiaofan

        • Total Posts : 6244
        • Reward points : 0
        • Joined: 4/14/2005
        • Location: Singapore
        • Status: offline
        RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, December 02, 2005 7:38 AM (permalink)
        0
        I was told today that the attachement is gone. Therefore I attached
        the modified source code and the pre-comipled application again.

        Regards,
        Xiaofan

        Edit: delete the broken download.
        <message edited by xiaofan on Sunday, September 23, 2007 1:37 AM>
         
        #4
          xiaofan

          • Total Posts : 6244
          • Reward points : 0
          • Joined: 4/14/2005
          • Location: Singapore
          • Status: offline
          RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Saturday, December 03, 2005 6:04 AM (permalink)
          0
          PICDEM FS USB demo appliation under Linux/Windows
          http://www.varxec.net/picdem_fs_usb/

          This is the related demo application. Together with fsusb, we have
          the replacement for Microchip's Pdfsusb.exe under Linux/Windows.
           
          #5
            Guest
            RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Monday, December 05, 2005 5:56 AM (permalink)
            0
            Which compiler did you use for your project? I am not able to compile for Windows

            Thank you.
            < Message edited by Koci -- Dec. 5, 2005 5:53:07 AM >
            Jalal A. H. Sela
             
            #6
              xiaofan

              • Total Posts : 6244
              • Reward points : 0
              • Joined: 4/14/2005
              • Location: Singapore
              • Status: offline
              RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Monday, December 05, 2005 6:04 AM (permalink)
              0
              I use MinGW/MSys and libusb-win32 0.1.10.1 stable version.

              # For Windows, please put usb.h and MingW version of libusb.a into
              # the MingW include and lib directory respectively.

              MinGW/MSys
              http://www.mingw.org/

              libusb-win32
              http://libusb-win32.sourceforge.net/

              Regards,
              Xiaofan
               
              #7
                Guest
                RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Monday, December 05, 2005 6:05 AM (permalink)
                0
                OK, thank you very much, I will try.
                Jalal A. H. Sela
                 
                #8
                  xiaofan

                  • Total Posts : 6244
                  • Reward points : 0
                  • Joined: 4/14/2005
                  • Location: Singapore
                  • Status: offline
                  RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Tuesday, December 06, 2005 7:02 AM (permalink)
                  0
                  By the way, I have returned my PICDEM FS USB board to local Microchip
                  so that I can not further test the application. If you have Windows
                  specific problems, I will try to help here. If you have problems with the
                  bootloader application, try to contact the original author.

                  I remember it is working. However it will issue a warning regarding
                  the miss-match of a few bytes. But actually it is a false alarm.

                  Regards,
                  Xiaofan
                   
                  #9
                    xiaofan

                    • Total Posts : 6244
                    • Reward points : 0
                    • Joined: 4/14/2005
                    • Location: Singapore
                    • Status: offline
                    RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Sunday, January 15, 2006 12:17 AM (permalink)
                    0
                    Here is the attachment again since the old one is lost.

                    Edit: using attachment seems not a good idea. The forum software will not keep if for long.[:@]
                    I will post the codes instead.
                    <message edited by xiaofan on Sunday, September 23, 2007 1:39 AM>
                     
                    #10
                      xiaofan

                      • Total Posts : 6244
                      • Reward points : 0
                      • Joined: 4/14/2005
                      • Location: Singapore
                      • Status: offline
                      RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, September 21, 2007 6:02 AM (permalink)
                      0
                      Take note that there is a mistake on the source code of meming.c.
                      http://forum.sparkfun.com/viewtopic.php?=&p=24707

                      "In the source code for fsusb, in the file meming.c around line 248, there is a line that reads:
                       if(r->addr >= MI_PROGRAM_BASE && r->addr <= MI_PROGRAM_TOP) {
                       

                      ...please change this to:
                       if(r->addr >= 0x800 && r->addr <= MI_PROGRAM_TOP) {"
                       
                       
                      #11
                        xiaofan

                        • Total Posts : 6244
                        • Reward points : 0
                        • Joined: 4/14/2005
                        • Location: Singapore
                        • Status: offline
                        RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, September 21, 2007 6:04 AM (permalink)
                        0
                        For the demo code, if you are using some latest kernel and you may have problems with the demo codes. You need to detach the kernel drivers.

                        The demo code is at http://www.varxec.net/picdem_fs_usb/.

                        The discussion on this topic is here at the linux-usb-devel mailing list.
                        http://www.opensubscriber.com/message/linux-usb-devel@lists.sourceforge.net/7160138.html
                         
                        #12
                          xiaofan

                          • Total Posts : 6244
                          • Reward points : 0
                          • Joined: 4/14/2005
                          • Location: Singapore
                          • Status: offline
                          RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Saturday, September 22, 2007 2:42 AM (permalink)
                          0
                          Here is the patched version of fsusb_demo.c. The forum attachment system is broken so I have to post it here.


                           /**
                           ** This file is part of fsusb_demo
                           **
                           ** fsusb_demo is free software; you can redistribute it and/or
                           ** modify it under the terms of the GNU General Public License as
                           ** published by the Free Software Foundation; either version 2 of the
                           ** License, or (at your option) any later version.
                           **
                           ** fsusb_demo is distributed in the hope that it will be useful, but
                           ** WITHOUT ANY WARRANTY; without even the implied warranty of
                           ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                           ** General Public License for more details.
                           **
                           ** You should have received a copy of the GNU General Public License
                           ** along with fsusb_picdem; if not, write to the Free Software
                           ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
                           ** 02110-1301, USA
                           */
                           
                           /**
                           A USB interface to the Microchip PICDEM FS USB Demo board
                           
                           Manuel Bessler, m.bessler AT gmx.net, 20050619
                           
                           Based on usb_pickit.c :
                           Orion Sky Lawlor, [email=olawlor@acm.org]olawlor AT acm DOT org[/email], 2003/8/3
                           Mark Rages, [email=markrages@gmail.com]markrages AT gmail DOT com[/email], 2005/4/1
                           
                           Simple modification by Xiaofan Chen, 20050811
                           Email: xiaofanc AT gmail DOT com
                           Note: I am in the process of trying to port it to Windows 
                           using libusb_win32 and MinGW.
                           By the way, I do not know where I got the original source.
                           
                           Notes by Xiaofan Chen, 20050812
                           Okay I got the reply from the author. The code comes from here:
                           [link=http://www.varxec.net/picdem_fs_usb/]http://www.varxec.net/picdem_fs_usb/[/link] 
                           Now the code works with Win32. Tested on Windows XP SP2.
                           
                           Notes by Xiaofan Chen, 20070922
                           Minor modifications: to detach the kernel driver if it is
                           calimed by some newer kernel ldusb module.
                           Also I remove the check of uid: better to use udev rules so that we do not need
                           to run as root.
                           */
                           #include <usb.h> /* libusb header */
                           #include <unistd.h> /* for geteuid */
                           #include <stdio.h>
                           #include <string.h>
                           
                           #define READ_VERSION     0x00
                           #define READ_VERSION_LEN 0x02
                           
                           #define ID_BOARD         0x31
                           #define UPDATE_LED       0x32
                           #define SET_TEMP_REAL    0x33
                           #define RD_TEMP          0x34
                           #define SET_TEMP_LOGGING 0x35
                           #define RD_TEMP_LOGGING  0x36
                           #define RD_POT           0x37
                           #define RESET            0xFF
                           
                           //#define USB_DEBUG
                           
                           /* PICkit USB values */ /* From Microchip firmware */
                           const static int vendorID=0x04d8; // Microchip, Inc
                           const static int productID=0x000c; // PICDEM FS USB demo app
                           const static int configuration=1; /*  Configuration 1*/
                           const static int interface=0;	/* Interface 0 */
                           
                           /* change by Xiaofan */
                           /* libusb-win32 requires the correct endpoint address
                              including the direction bits. This is the most important
                              differece between libusb-win32 and libusb.
                           */  
                           //const static int endpoint=1; /* first endpoint for everything */
                           const static int endpoint_in=0x81; /* endpoint 0x81 address for IN */
                           const static int endpoint_out=1; /* endpoint 1 address for OUT */
                           
                           const static int timeout=2000; /* timeout in ms */
                           
                           /* PICDEM FS USB max packet size is 64-bytes */
                           const static int reqLen=64;
                           typedef unsigned char byte;
                           
                           void bad(const char *why) {
                           	fprintf(stderr,"Fatal error> %s\n",why);
                           	exit(17);
                           }
                           
                           /****************** Internal I/O Commands *****************/
                           
                           /** Send this binary string command. */
                           static void send_usb(struct usb_dev_handle * d, int len, const char * src)
                           {
                              int r = usb_bulk_write(d, endpoint_out, (char *)src, len, timeout);
                           //   if( r != reqLen )
                              if( r < 0 )
                              {
                           	  perror("usb PICDEM FS USB write"); bad("USB write failed"); 
                              }
                           }
                           
                           /** Read this many bytes from this device */
                           static void recv_usb(struct usb_dev_handle * d, int len, byte * dest)
                           {
                           //   int i;
                              int r = usb_bulk_read(d, endpoint_in, dest, len, timeout);
                              if( r != len )
                              {
                           	  perror("usb PICDEM FS USB read"); bad("USB read failed"); 
                              }
                           }
                           
                           void picdem_fs_usb_read_version(struct usb_dev_handle * d)
                           {
                              byte answer[reqLen];
                              byte question[reqLen];
                              question[0] = READ_VERSION;
                              question[1] = READ_VERSION_LEN;
                              send_usb(d, 2, question);
                              recv_usb(d, 2+READ_VERSION_LEN, answer);
                              if( (int)answer[0] == READ_VERSION &&
                           	   (int)answer[1] == READ_VERSION_LEN )
                           	  printf("answer was correct!\n");
                              printf("Onboard firmware version is %d.%d\n",
                           		  (int)answer[3],(int)answer[2]);
                           }
                           
                           void picdem_fs_usb_led(struct usb_dev_handle * d, int lednum, int onoff)
                           {
                              if( lednum < 3 || lednum > 4 )
                           	  return;
                               byte answer[reqLen];
                              byte question[reqLen];
                              question[0] = UPDATE_LED;
                              question[1] = lednum;
                              question[2] = (onoff==0) ? 0 : 1;
                              send_usb(d, 3, question);
                              recv_usb(d, 1, answer);
                              printf("LED #%i is now %s\n", lednum, (onoff==0) ? "off" : "on");
                           }
                           
                           void picdem_fs_usb_readtemp(struct usb_dev_handle * d)
                           {
                              byte answer[reqLen];
                              byte question[reqLen];
                              int rawval,rawtemp;
                              float degCtemp;
                              question[0] = RD_TEMP;
                              send_usb(d, 1, question);
                              recv_usb(d, 3, answer);
                              rawval = answer[1] + (answer[2]<<8);
                              
                              /* improved temperature conversion by Bill Freeman */
                              if( rawval & (1 << 15) )
                                    rawval -= 1 << 16;
                           			 
                           /*   if( (rawval>>15) & 0x01 )
                           	  rawval = (~rawval) + 1; */
                              rawtemp = (rawval)>>3;
                              degCtemp = rawtemp * 0.0625;
                              printf("Temperature now is %f degC (raw: %i, rawval: %i)\n", degCtemp, rawtemp, rawval);
                           }
                           
                           void picdem_fs_usb_readpot(struct usb_dev_handle * d)
                           {
                              byte answer[reqLen];
                              byte question[reqLen];
                              int rawval;
                              question[0] = RD_POT;
                              send_usb(d, 1, question);
                              recv_usb(d, 3, answer);
                              rawval = answer[1] + (answer[2]<<8);
                              printf("Potentiometer now reads %i\n", rawval);
                           }
                           
                           void picdem_fs_usb_reset(struct usb_dev_handle * d)
                           {
                           //   byte answer[reqLen];
                              byte question[reqLen];
                              question[0] = RESET;
                              send_usb(d, 1, question);
                           //   recv_usb(d, 3, answer);
                              printf("Board resetted\n");
                           }
                           
                           /* debugging: enable debugging error messages in libusb */
                           extern int usb_debug;
                           
                           /* Find the first USB device with this vendor and product.
                              Exits on errors, like if the device couldn't be found.
                           */
                           struct usb_dev_handle *usb_picdem_fs_usb_open(void)
                           {
                             struct usb_device * device;
                             struct usb_bus * bus;
                           
                           /* change by Xiaofan: it is better not to run as root. It is
                           better to use udev rules to set up the correct perimissions */
                           /*
                           #ifndef WIN32  
                             if( geteuid()!=0 )
                           	 bad("This program must be run as root, or made setuid root");
                           #endif  
                           */
                           #ifdef USB_DEBUG
                             usb_debug=255; 
                           #endif
                           
                             printf("Locating Microchip(tm) PICDEM(tm) FS USB Demo Board (vendor 0x%04x/product 0x%04x)\n", vendorID, productID);
                             /* (libusb setup code stolen from John Fremlin's cool "usb-robot") */
                           
                             // added the two debug lines  
                             usb_set_debug(255);
                             printf("setting USB debug on by adding usb_set_debug(255) \n");
                             //End of added codes
                           
                             usb_init();
                             usb_find_busses();
                             usb_find_devices();
                           
                           /* change by Xiaofan */  
                           /* libusb-win32: not using global variable like usb_busses*/
                           /*  for (bus=usb_busses;bus!=NULL;bus=bus->next) */  
                             for (bus=usb_get_busses();bus!=NULL;bus=bus->next) 
                             {
                           	 struct usb_device * usb_devices = bus->devices;
                           	 for( device=usb_devices; device!=NULL; device=device->next )
                           	 {
                           		if( device->descriptor.idVendor == vendorID &&
                           			device->descriptor.idProduct == productID )
                           		{
                           		   struct usb_dev_handle *d;
                           		   printf("Found USB PICDEM FS USB Demo Board as device '%s' on USB bus %s\n",
                           				   device->filename,
                           				   device->bus->dirname);
                           		   d = usb_open(device);
                           		   if( d )
                           		   { /* This is our device-- claim it */
                           //			  byte retData[reqLen];
                           
                           /* change by Xiaofan */ 
                           /* to detach the kernel driver if it is calimed by some newer kernel ldusb module.*/
                           #ifdef LINUX
                           
                           			{
                           
                           				int retval;
                           
                           				char dname[32] = {0};
                           
                           				retval = usb_get_driver_np(d, 0, dname, 31);
                           
                           				if (!retval)
                           
                           					usb_detach_kernel_driver_np(d, 0);
                           
                           				}
                           
                           #endif
                           
                           			  if( usb_set_configuration(d, configuration) ) 
                           			  {
                           				 bad("Error setting USB configuration.\n");
                           			  }
                           			  if( usb_claim_interface(d, interface) ) 
                           			  {
                           				 bad("Claim failed-- the USB PICDEM FS USB is in use by another driver.\n");
                           			  }
                           			  printf("Communication established.\n");
                           			  picdem_fs_usb_read_version(d);
                           			  return d;
                           		   }
                           		   else 
                           			  bad("Open failed for USB device");
                           		}
                           		/* else some other vendor's device-- keep looking... */
                           	 }
                             }
                             bad("Could not find USB PICDEM FS USB Demo Board--\n"
                                 "you might try lsusb to see if it's actually there.");
                             return NULL;
                           }
                           
                           /* Change by Xiaofan */
                           /* Please also refer to README */
                           void show_usage(void)
                           {
                             printf("fsusb_demo: Software for PICDEM Full Speed USB demo board\n");
                             printf("fsusb_demo --ledon n, n=3 or 4: ON LED D3 or D4\n");
                             printf("fsusb_demo --ledoff n, n=3 or 4: Off LED D3 or D4\n");
                             printf("fsusb_demo --readtemp: read out temperature\n");
                             printf("fsusb_demo --readpot: read out poti value\n");
                             printf("fsusb_demo --reset: to reset the PICDEM FS USB demo board\n");
                           }
                           
                           int main(int argc, char ** argv) 
                           {
                              struct usb_dev_handle * picdem_fs_usb = usb_picdem_fs_usb_open();
                              if(argc < 2 || argc > 3) {
                               show_usage();
                               exit(1);
                             } 
                             if(argc == 3){
                           	  if( 0 == strcmp(argv[1],"--ledon") )
                           	  {
                           		 picdem_fs_usb_led(picdem_fs_usb, atoi(argv[2]), 1);
                           	  }
                           	  else if( 0 == strcmp(argv[1],"--ledoff") )
                           	  {
                           		 picdem_fs_usb_led(picdem_fs_usb, atoi(argv[2]), 0);	 
                           	  }
                           	 }
                              
                              if( argc == 2 ){
                           	  if(  0 == strcmp(argv[1],"--readtemp") )
                           	  {
                           		 picdem_fs_usb_readtemp(picdem_fs_usb);
                           	  }
                           	  else if(  0 == strcmp(argv[1],"--readpot") )
                           	  {
                           		 picdem_fs_usb_readpot(picdem_fs_usb);
                           	  }
                           	  else if(  0 == strcmp(argv[1],"--reset") )
                           	  {
                           		 picdem_fs_usb_reset(picdem_fs_usb);
                           	  }
                           	}
                              usb_close(picdem_fs_usb);
                              return 0;
                           }
                           


                          Edit: Oops, the code format is gone. I will format the code later.
                          Edit: post again with the correct format
                          <message edited by xiaofan on Wednesday, March 10, 2010 6:03 AM>
                           
                          #13
                            xiaofan

                            • Total Posts : 6244
                            • Reward points : 0
                            • Joined: 4/14/2005
                            • Location: Singapore
                            • Status: offline
                            RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Sunday, September 23, 2007 1:13 AM (permalink)
                            0
                            An updated version of fsusb.c:

                             
                             /*
                             ** This file is part of fsusb_picdem
                             **
                             ** fsusb_picdem is free software; you can redistribute it and/or
                             ** modify it under the terms of the GNU General Public License as
                             ** published by the Free Software Foundation; either version 2 of the
                             ** License, or (at your option) any later version.
                             **
                             ** fsusb_picdem is distributed in the hope that it will be useful, but
                             ** WITHOUT ANY WARRANTY; without even the implied warranty of
                             ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                             ** General Public License for more details.
                             **
                             ** You should have received a copy of the GNU General Public License
                             ** along with fsusb_picdem; if not, write to the Free Software
                             ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
                             ** 02110-1301, USA
                             */
                             
                             /*
                             ** portions from usb_pickit by Orion Sky Lawlor, olawlor@acm.org
                             */
                             
                             #include <usb.h> /* libusb header */
                             #include <unistd.h> /* for geteuid */
                             #include <stdio.h>
                             #include <string.h>
                             #include "bootload.h"
                             #include "fsusb.h"
                             
                             
                             const static int fsusb_vendorID=0x04d8; // Microchip, Inc
                             const static int fsusb_productID=0x000b; // PICDEM-FS USB
                             const static int fsusb_configuration=1; /* 1: bootloader
                                                                      * ### may change in future firmware versions
                                                                      */
                             const static int fsusb_interface=0;
                             
                             /* change by Xiaofan */
                             //const static int fsusb_endpoint=1; 
                             				/* first endpoint for everything
                                                                 * ### may change in future firmware versions
                                                                 */
                             
                             
                             /* libusb-win32 requires the correct endpoint address
                                including the direction bits. This is the most important
                                differece between libusb-win32 and libusb.
                             */  
                             const static int fsusb_endpoint_in=0x81; /* endpoint 0x81 address for IN */
                             const static int fsusb_endpoint_out=1; /* endpoint 1 address for OUT */
                             
                             const static int fsusb_timeout=1000; /* timeout in ms */
                             
                             
                             
                             void bad(const char *why)
                             {
                               fprintf(stderr,"Fatal error> %s\n",why);
                               exit(17);
                             }
                             
                             
                             
                             void recv_usb(picdem_handle *d, int len, byte *dest) {
                               int r;
                             
                               r=usb_bulk_read(d, fsusb_endpoint_in, dest, len, fsusb_timeout);
                             
                               if (r!=len) {
                                 perror("usb PICDEM read");
                                 bad("USB read failed");
                               }
                               //  printf("read %i bytes\n", r);
                             }
                             
                             
                             
                             void rjl_request_version(picdem_handle *d, unsigned char *ret)
                             {
                               int r;
                               char buf[4];
                             
                               // ### "\0\0\0\0\0" may not be correct in future firmware versions
                               r=usb_bulk_write(d, fsusb_endpoint_out, "\0\0\0\0\0", 5, fsusb_timeout);
                               if(r != 5) {
                                 perror("usb_bulk_write");
                                 bad("rjl_request_version(): USB write failed");
                               }
                             
                               // command, len, minor, major
                               recv_usb(d,4,buf);
                               ret[0]=buf[3];
                               ret[1]=buf[2];
                             }
                             
                             
                             
                             void rjl_request_flash(picdem_handle *d, int offset, int len, bl_packet *pack)
                             {
                               int r;
                               bl_packet p;
                             
                             
                               p.command=READ_FLASH;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=len;
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5, fsusb_timeout);
                               if(r != 5) {
                                 perror("usb_bulk_write");
                                 bad("rjl_request_flash(): USB write failed");
                               }
                             
                             
                               recv_usb(d,len+5,(byte*)pack);
                             }
                             
                             
                             
                             /* write in 16-byte boundary-aligned blocks only in this version of
                              * the bootloader
                              */
                             void rjl_write_flash(picdem_handle *d, int offset, int len, byte *data, bl_packet *pack)
                             {
                               int r;
                               bl_packet p;
                               int i;
                               byte retbuf[5];
                             
                             
                             
                               if(offset & 0x0f) {
                                 printf("*** WARNING: not boundary-aligned\n");
                                 return;
                               }
                               if(len != 16) {
                                 printf("*** WARNING: not 16 bytes\n");
                                 return;
                               }
                             
                             
                             
                               p.command=WRITE_FLASH;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=len;
                               for(i=0;i<len;i++) {
                                 p.data[i]=data[i];
                               }
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5+len, fsusb_timeout);
                               if(r != 5+len) {
                                 perror("usb_bulk_write");
                                 bad("rjl_write_flash(): USB write failed");
                               }
                             
                               recv_usb(d,1,retbuf);
                               //  printf("write reply is %x\n", retbuf[0]);
                             }
                             
                             
                             
                             /* write on 64-byte boundaries only in blocks of 64 bytes.
                              *  It's a feature.
                              */
                             void rjl_write_block(picdem_handle *d, int offset, byte *data)
                             {
                               int r;
                               bl_packet p;
                               byte retbuf[5];
                               int subblock=0;
                             
                             
                               if(offset & 0x3f) {
                                 printf("*** WARNING: not boundary-aligned\n");
                                 return;
                               }
                             
                             
                               p.command=ERASE_FLASH;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=1;
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5, fsusb_timeout);
                               if(r != 5) {
                                 perror("usb_bulk_write");
                                 bad("rjl_write_block(): USB write failed");
                               }
                             
                             
                               recv_usb(d,1,retbuf);
                               //  printf("erase reply is %x\n", retbuf[0]);
                             
                             
                               for(subblock=0;subblock<4;subblock++) {
                                 p.command=WRITE_FLASH;
                                 p.address.low=((offset+16*subblock) & 0xff)>>0;
                                 p.address.high=((offset+16*subblock) & 0xff00)>>8;
                                 p.address.upper=((offset+16*subblock) & 0xf0000)>>16;
                                 p.len=16;
                                 memcpy(p.data, data+(subblock*16), 16);
                             
                             
                                 r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5+16, fsusb_timeout);
                                 if(r != 5+16) {
                                   perror("usb_bulk_write");
                                   bad("rjl_write_block(): USB write failed");
                                 }
                             
                             
                                 recv_usb(d,1,retbuf);
                                 //  printf("write reply is %x\n", retbuf[0]);
                               }
                             }
                             
                             
                             
                             // 59ish bytes max
                             void rjl_write_config_block(picdem_handle *d, int offset, int len, byte *data)
                             {
                               int r;
                               bl_packet p;
                               //  int i;
                               byte retbuf[5];
                             
                             
                               if(len>=BL_DATA_LEN) {
                                 printf("*** ERROR: config block too big\n");
                                 return;
                               }
                             
                             
                             
                               /* The firmware clips the erase to a 64-byte block, which
                                *  we don't worry about because in any real device
                                *  the config starts on a 64-byte boundary.
                                */
                               p.command=ERASE_FLASH;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=1;
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5, fsusb_timeout);
                               if(r != 5) {
                                 perror("usb_bulk_write");
                                 bad("rjl_write_config_block(): USB write failed");
                               }
                             
                             
                               recv_usb(d,1,retbuf);
                               //  printf("erase reply is %x\n", retbuf[0]);
                             
                             
                               // config writes have no alignment restriction
                               p.command=WRITE_CONFIG;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=len;
                               memcpy(p.data, data, len);
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5+len, fsusb_timeout);
                               if(r != 5+len) {
                                 perror("usb_bulk_write");
                                 bad("rjl_write_config_block(): USB write failed");
                               }
                             
                             
                               recv_usb(d,1,retbuf);
                               //  printf("write reply is %x\n", retbuf[0]);
                             }
                             
                             
                             
                             // write on 64-byte boundaries only in blocks of 64 bytes
                             void rjl_erase_block(picdem_handle *d, int offset)
                             {
                               int r;
                               bl_packet p;
                               byte retbuf[5];
                             
                             
                               if(offset & 0x3f) {
                                 printf("*** WARNING: not boundary-aligned\n");
                                 return;
                               }
                             
                             
                             
                               p.command=ERASE_FLASH;
                               p.address.low=(offset & 0xff)>>0;
                               p.address.high=(offset & 0xff00)>>8;
                               p.address.upper=(offset & 0xf0000)>>16;
                               p.len=1;
                             
                             
                               r=usb_bulk_write(d, fsusb_endpoint_out, (char*)&p, 5, fsusb_timeout);
                               if(r != 5) {
                                 perror("usb_bulk_write");
                                 bad("rjl_erase_block(): USB write failed");
                               }
                             
                             
                               recv_usb(d,1,retbuf);
                               //  printf("erase reply is %x\n", retbuf[0]);
                             }
                             
                             
                             
                             /* Find the first USB device with this vendor and product.
                              *  Exits on errors, like if the device couldn't be found. -osl
                              *
                              * This function is heavily based upon Orion Sky Lawlor's
                              *  usb_pickit program, which was a very useful reference
                              *  for all the USB stuff.  Thanks!
                              */
                             picdem_handle *rjl_fsusb_open(void)
                             {
                               struct usb_device *device;
                               struct usb_bus* bus;
                               unsigned char buf[2];
                             
                             /*
                               if (geteuid()!=0) {
                                 bad("This program must be run as root, or made setuid root");
                               }
                             */
                             
                             #ifdef USB_DEBUG
                               usb_debug=255; 
                             #endif
                             
                               printf("Locating USB Microchip(tm) PICDEM-FS USB(tm) (vendor 0x%04x/product 0x%04x)\n",
                               	fsusb_vendorID,fsusb_productID);
                               /* (libusb setup code stolen from John Fremlin's cool "usb-robot") -osl */
                               usb_init();
                               usb_find_busses();
                               usb_find_devices();
                             
                               for (bus=usb_busses;bus!=NULL;bus=bus->next) {
                                 struct usb_device* usb_devices = bus->devices;
                                 for(device=usb_devices;device!=NULL;device=device->next) {
                             
                             
                                   if (device->descriptor.idVendor == fsusb_vendorID
                                       && device->descriptor.idProduct == fsusb_productID) {
                             
                                     usb_dev_handle *d;
                                     printf( "Found USB PICDEM-FS USB as device '%s' on USB bus %s\n",
                                             device->filename,
                                             device->bus->dirname);
                                     d=usb_open(device);
                             
                             
                                     if (d) { /* This is our device-- claim it */
                                       if (usb_set_configuration(d,fsusb_configuration)) {
                                         bad("Error setting USB configuration.\n");
                                       }
                             
                                       if (usb_claim_interface(d,fsusb_interface)) {
                                         bad("Claim failed-- the USB PICDEM is in use by another driver.\n"
                                             "Do a `dmesg` to see which kernel driver has claimed it--\n"
                                             "You may need to `rmmod hid` or patch your kernel's hid driver.\n");
                                       }
                             
                                       rjl_request_version(d, buf);
                             
                                       printf("Communication established.  Onboard firmware version is %d.%d\n",
                                              (int)buf[0],(int)buf[1]);
                             
                                       if (buf[0]!=0x01u) {
                                         bad("This PICDEM's version is too new (only support version 1.x !)\n");
                                       }
                             
                                       return d;
                                     } else 
                                       bad("Open failed for USB device");
                                   }
                             
                             
                                   /* else some other vendor's device-- keep looking... -osl*/
                                 }
                               }
                             
                               bad("Could not find USB PICDEM device--\n"
                                   "you might try lsusb to see if it's actually there.");
                             
                               return NULL;
                             }
                             


                            Edit: to post again with proper format
                            <message edited by xiaofan on Monday, July 28, 2008 6:33 AM>
                             
                            #14
                              xiaofan

                              • Total Posts : 6244
                              • Reward points : 0
                              • Joined: 4/14/2005
                              • Location: Singapore
                              • Status: offline
                              RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Sunday, September 23, 2007 1:17 AM (permalink)
                              0
                              Sample udev rules for Ubuntu 6.06 and 7.04. For udev rules under other Linux distros, please refer to the Piklab site:
                              http://piklab.sourceforge.net/wiki/index.php/USB_port_problems
                              http://piklab.sf.net/files/libusb_udev_hotplug.txt (excellent document by Linus Walleij, the libnjb developer).

                              mcuee@mcuee-desktop:/etc/udev/rules.d$ cat 026_microchip.rules
                              #PICKit 1
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="0032", MODE="0660", GROUP="microchip"
                              #PICKit 2
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="0033", MODE="0660", GROUP="microchip"
                              #ICD2
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="8000", MODE="0660", GROUP="microchip"
                              #ICD21
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="8001", MODE="0660", GROUP="microchip"
                              #PICDEM FS USB Bootload
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000b", MODE="0660", GROUP="microchip"
                              #PICDEM FS USB Demo
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000c", MODE="0660", GROUP="microchip"
                              #PICDEM FS USB CDC-ACM
                              SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000a", MODE="0660", GROUP="microchip"
                               
                              #15
                                xiaofan

                                • Total Posts : 6244
                                • Reward points : 0
                                • Joined: 4/14/2005
                                • Location: Singapore
                                • Status: offline
                                RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Sunday, September 23, 2007 1:58 AM (permalink)
                                0
                                If you encounter problems with the USB firmware framework based device under later kernels, you might want to use the kernel.org vanilla kernel and build the kernel which disable auto-suspend.

                                Problem:
                                http://forum.microchip.com/tm.aspx?m=283378
                                http://sourceforge.net/mailarchive/forum.php?thread_name=1189786321l.13173l.12l%40mofo&forum_name=libusb-devel

                                Solution:
                                http://forum.microchip.com/tm.aspx?m=275422&mpage=2
                                http://sourceforge.net/mailarchive/forum.php?thread_name=1189786321l.13173l.12l%40mofo&forum_name=libusb-devel
                                 
                                #16
                                  tapenot

                                  • Total Posts : 7
                                  • Reward points : 0
                                  • Joined: 10/18/2007
                                  • Location: 0
                                  • Status: offline
                                  RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Thursday, October 18, 2007 5:15 AM (permalink)
                                  0
                                  My operating system is ubuntu 7.04. I have compiled the patch version of fsusb_demo.c and executed the program. It establishes communication with my PICDEM board but can't send commands. That's the error i get:

                                  Locating Microchip(tm) PICDEM(tm) FS USB Demo Board (vendor 0x04d8/product 0x000c)
                                  usb_set_debug: Setting debugging level to 255 (on)
                                  setting USB debug on by adding usb_set_debug(255)
                                  usb_os_init: Found USB VFS at /dev/bus/usb
                                  usb_os_find_busses: Skipping non bus directory devices
                                  usb_os_find_busses: Found 005
                                  usb_os_find_busses: Found 001
                                  usb_os_find_busses: Found 003
                                  usb_os_find_busses: Found 004
                                  usb_os_find_busses: Found 002
                                  usb_os_find_devices: Found 001 on 005
                                  usb_os_find_devices: Found 001 on 001
                                  usb_os_find_devices: Found 001 on 003
                                  usb_os_find_devices: Found 001 on 004
                                  usb_os_find_devices: Found 004 on 002
                                  usb_os_find_devices: Found 001 on 002
                                  error obtaining child information: Inappropriate ioctl for device
                                  Found USB PICDEM FS USB Demo Board as device '004' on USB bus 002
                                  Communication established.
                                  USB error: error submitting URB: Invalid argument
                                  usb PICDEM FS USB write: Invalid argument
                                  Fatal error> USB write failed


                                  I already have installed by default libusb v0.1.12. About the CONFIG_USB_SUSPEND, I have solved the problem without recompiling the kernel. I keep the connection opened doing:
                                  # sleep 3600 < /proc/bus/usb/002/004

                                  Does anyone have an idea about what can be the problem. I would be grateful for any kind of suggestion.
                                  <message edited by tapenot on Thursday, October 18, 2007 5:19 AM>
                                   
                                  #17
                                    xiaofan

                                    • Total Posts : 6244
                                    • Reward points : 0
                                    • Joined: 4/14/2005
                                    • Location: Singapore
                                    • Status: offline
                                    RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Thursday, October 18, 2007 5:34 AM (permalink)
                                    0
                                    If you have not set up udev rules correctly, you need to run the program using sudo. Try again.
                                    It works fine under my Ubuntu 6.06/7.04 installation.

                                    I have the demo mode working under FreeBSD 6.2 Stable as well. Please refer to my blog.
                                    http://mcuee.blogspot.com

                                     
                                    #18
                                      tapenot

                                      • Total Posts : 7
                                      • Reward points : 0
                                      • Joined: 10/18/2007
                                      • Location: 0
                                      • Status: offline
                                      RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, October 19, 2007 12:20 AM (permalink)
                                      0
                                      My udev rules are set up as you explained before:

                                      # /etc/udev/rules.d$ ls -l
                                      -rw-r--r-- 1 root root 674 2007-10-16 14:16 026_microchip.rules

                                      # /etc/udev/rules.d$ cat 026_microchip.rules
                                      #PICDEM FS USB Demo
                                      SYSFS{idVendor}=="04d8", SYSFS{idProduct}=="000c", MODE="0660", GROUP="microchip"

                                      and also i have created a user group called microchip and i have added my username (not root) to this group.
                                      Appart from this, i execute the program with "sudo". Any idea? Thanks.
                                       
                                      #19
                                        xiaofan

                                        • Total Posts : 6244
                                        • Reward points : 0
                                        • Joined: 4/14/2005
                                        • Location: Singapore
                                        • Status: offline
                                        RE: PICDEM FS USB bootloader command line application ported to Windows using libusb-win3 Friday, October 19, 2007 12:49 AM (permalink)
                                        0
                                        Sudo should be fine.

                                        Try to use my version of fsusb_demo.c and see how it goes. You may want to change usb_bulk to usb_interrupt as shown below.

                                        One potential problem is that your may have a kernel which calims the PICDEM FS USB board with the other driver. You may need to detach the kernel driver.
                                         
                                          /** Send this binary string command. */
                                         static void send_usb(struct usb_dev_handle * d, int len, const char * src)
                                         {
                                            int r = usb_interrupt_write(d, endpoint_out, (char *)src, len, timeout);
                                         //   if( r != reqLen )
                                            if( r < 0 )
                                            {
                                         	  perror("usb PICDEM FS USB write"); bad("USB write failed"); 
                                            }
                                         }
                                         
                                         /** Read this many bytes from this device */
                                         static void recv_usb(struct usb_dev_handle * d, int len, byte * dest)
                                         {
                                         //   int i;
                                            int r = usb_interrupt_read(d, endpoint_in, dest, len, timeout);
                                            if( r != len )
                                            {
                                         	  perror("usb PICDEM FS USB read"); bad("USB read failed"); 
                                            }
                                         }
                                         
                                         

                                        <message edited by xiaofan on Friday, October 19, 2007 12:51 AM>
                                         
                                        #20
                                          Online Bookmarks Sharing: Share/Bookmark
                                          Change Page: 123 > | Showing page 1 of 3, messages 1 to 20 of 41

                                          Jump to:

                                          Current active users

                                          There are 0 members and 1 guests.

                                          Icon Legend and Permission

                                          • New Messages
                                          • No New Messages
                                          • Hot Topic w/ New Messages
                                          • Hot Topic w/o New Messages
                                          • Locked w/ New Messages
                                          • Locked w/o New Messages
                                          • Read Message
                                          • Post New Thread
                                          • Reply to message
                                          • Post New Poll
                                          • Submit Vote
                                          • Post reward post
                                          • Delete my own posts
                                          • Delete my own threads
                                          • Rate post

                                          2000-2013 ASPPlayground.NET Forum Version 3.9