Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

USB in 18f4550

Help
sansenan
2008-04-16
2013-03-12
1 2 > >> (Page 1 of 2)
  • sansenan
    sansenan
    2008-04-16

    Hello ,

          I was looking for USB based PC to USB communication in SDCC,,but did not come across one..THe one I found in the forums were all broken link (http://www.mail-archive.com/sdcc-user@lists.sourceforge.net/msg01101.html):

    http://www.nutsvolts.com/%7Edownloads/USB.zip
    (Does anyone has the above file :Can u post this file or mail me :sansenan at gmail dot com)

    I saw the discussion where Raphael & Vaclav was trying to get a USB code(http://www.mail-archive.com/sdcc-user@lists.sourceforge.net/msg01092.html) compiled with
    Have you guys sorted that out...

    Can you share the code for getting a basic code of USB compiled in SDCC

    regards
    Sans

     
    • kein0r
      kein0r
      2008-04-17

      SourceForge.net schrieb:
      > Read and respond to this message at: https://sourceforge.net/forum/message.php?msg_id=4908885
      > By: sansenan
      >
      > Hello ,
      >
      >
      >       I was looking for USB based PC to USB communication in SDCC,,but did not
      > come across one..THe one I found in the forums were all broken link
      > (http://www.mail-archive.com/sdcc-user@lists.sourceforge.net/msg01101.html):

      The original nuts&volts article can be found here http://www.nutsvolts.com/preview.php?issue=10 There you also find a link the the USB.zip which includes the sdcc usb code.
      >
      > I saw the discussion where Raphael & Vaclav was trying to get a USB
      > code(http://www.mail-archive.com/sdcc-user@lists.sourceforge.net/msg01092.html)
      > compiled with
      > Have you guys sorted that out...
      >
      > Can you share the code for getting a basic code of USB compiled in SDCC

      The code from the nuts&volts article implements a usb HID device (?). I rewrote the code the implement a USB CDC device (USB to serial converter). THe code is written for a pic18f2550 but should be easily compiled for the 18f4550.
      I put my code under: http://www.pic-o-rama.org/index.php?view=article&catid=1%3Aevaluation-boards&id=2%3Asimple-usb-board&option=com_content&Itemid=2
      just scroll down.

      If you need some help regarding the code just ask.

      Jan

       
      • sansenan
        sansenan
        2008-04-17

        thanks a lot jan,

        i would get back to u if i have any problem in compiling them...

        The link for the CDC version seems broken..please verify{http://www.pic-o-rama.org/images/stories/SimpleUSB/SimpleUSB_firmware.tar.gz}

        i tried it 2-3 times but got a 404 Error...
        thanks again

        san

         
        • kein0r
          kein0r
          2008-04-17

           
          • sansenan
            sansenan
            2008-06-19

            hi,
               I tried compiling the CDC code in windows,this is the eroor i got..any ideas to fix it?? usb_CDC.c and usb_config seems to compile properly.But picUSB.c has some errors.

            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 usb_cdc.c

            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 usb_config.c
            usb_config.c:34: warning 147: excess elements in struct initializer after 'confi
            guration_descriptor'

            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 picUSB.c
            picUSB.c:139: error 47: indirections to different types assignment
            from type 'const-unsigned-char []  code-code* '
            to type 'unsigned-char generic* '

            picUSB.c:333: error 47: indirections to different types assignment
            from type 'volatile-unsigned-char [64]  near* '
            to type 'unsigned-char generic* '

            picUSB.c:676: error 26: '_ACTIVIE' not a structure/union member
            picUSB.c:677: error 26: '_ACTIVIF' not a structure/union member
            picUSB.c:769: error 26: '_ACTIVIF' not a structure/union member
            picUSB.c:769: error 26: '_ACTIVIE' not a structure/union member

            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 main.c

            C:\USB_PIC>gplink -o test.hex -s 18f4550.lkr crt0i.o usb_cdc.o usb_config.o picU
            SB.o main.o pic18f4550.lib
            picUSB.o: No such file or directory

            the batch file used to compile had
            "
            sdcc -c -mpic16 -p18f4550 --ivt-loc=0x800 crt0i.c
            sdcc -c -mpic16 -p18f4550 usb_cdc.c  
            sdcc -c -mpic16 -p18f4550 usb_config.c
            sdcc -c -mpic16 -p18f4550 picUSB.c
            sdcc -c -mpic16 -p18f4550 main.c
            gplink -o test.hex -s 18f4550.lkr crt0i.o usb_cdc.o usb_config.o picUSB.o main.o pic18f4550.
            "
            also these are the lines in the file :
            Line 139:      outPtr = &string_descriptor[descriptorIndex];
            Line 333:       outPtr = &controlTransferBuffer;
            Line 676:       UIEbits.ACTIVIE = 0;
            Line 677:       UIRbits.ACTIVIF = 0;
            Line 769:       if(UIRbits.ACTIVIF && UIEbits.ACTIVIE)

            Please help..

            regards
            Sans

             
            • kein0r
              kein0r
              2008-06-22

              yes your right. Originally there was a typo in the registers in the .h file coming with sdcc. This is corrected now, just rename all
              ACTIVIE with ACTVIE
              and
              ACTIVIF with ACTVIF

              or redownload the code from the website, i corrected it.

               
      • sansenan
        sansenan
        2008-04-20

        Jan

        can i use your code to implement the HID device ...
        I am pretty new to USB can you please explain what changes i need to make in your file
        if i have to use it with USB connection itself rather than rs232.

        have you compiled the nuts and volts version in sdcc,does it work in the latest versions.

        can u give me a brief of doing the write and read operations in USB, or a link explaining about USB device side,

        thanks againg

        san

         
        • kein0r
          kein0r
          2008-04-22

          Most parts of the N&V HID implementation is still in my code. But if you really need to build a HID device i would suggest you use the N&V Code, maybe together with my Makefile.
          Afaik the N&V Code doens't compile with sdcc 2.7.X but should compile easily, means with minor changes, with sdcc 2.6.X

           
    • sansenan
      sansenan
      2008-05-02

      hello ,

            How do i communicate i.e do a read and write at the host side,in a windows based system using your CDC version...Any host side program available for windows to do this.....

      thanks again
      san

       
    • sansenan
      sansenan
      2008-05-02

      And in the below link
      http://www.pic-o-rama.org/index.php?view=article&catid=1%3Aevaluation-boards&id=2%3Asimple-usb-board&option=com_content&Itemid=2
      its mentioned: "You just need two low (22 or 27 Ω) resistors in series with the D+ and D- lines and a 100kΩ resistor to signal the usb host that a device is present."

      Is this necessary,because as i have seen in most of the 18F4550 circuits...a pull up is not used.D+ and D- are directly connected to the USB B type connector.Is it internal in the PIC itself or we need a pull up..

      I have bootloaded the device successfully without the resistor ..so am a bit confused...

      Will The CDC firmware be recognized if i don't use the pull up resistor??or are there alternates to be done in the code..which can do this..

       
      • kein0r
        kein0r
        2008-05-02

        All three resistors arent really necessary. They are, howeve, recommended.

         
    • sansenan
      sansenan
      2008-06-19

      hi,
      I tried compiling the CDC code in windows,this is the eroor i got..any ideas to fix it?? usb_CDC.c and usb_config seems to compile properly.But picUSB.c has some errors.

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 usb_cdc.c

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 usb_config.c
      usb_config.c:34: warning 147: excess elements in struct initializer after 'confi
      guration_descriptor'

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 picUSB.c
      picUSB.c:139: error 47: indirections to different types assignment
      from type 'const-unsigned-char [] code-code* '
      to type 'unsigned-char generic* '

      picUSB.c:333: error 47: indirections to different types assignment
      from type 'volatile-unsigned-char [64] near* '
      to type 'unsigned-char generic* '

      picUSB.c:676: error 26: '_ACTIVIE' not a structure/union member
      picUSB.c:677: error 26: '_ACTIVIF' not a structure/union member
      picUSB.c:769: error 26: '_ACTIVIF' not a structure/union member
      picUSB.c:769: error 26: '_ACTIVIE' not a structure/union member

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 main.c

      C:\USB_PIC>gplink -o test.hex -s 18f4550.lkr crt0i.o usb_cdc.o usb_config.o picU
      SB.o main.o pic18f4550.lib
      picUSB.o: No such file or directory

      the batch file used to compile had
      "
      sdcc -c -mpic16 -p18f4550 --ivt-loc=0x800 crt0i.c
      sdcc -c -mpic16 -p18f4550 usb_cdc.c 
      sdcc -c -mpic16 -p18f4550 usb_config.c
      sdcc -c -mpic16 -p18f4550 picUSB.c
      sdcc -c -mpic16 -p18f4550 main.c
      gplink -o test.hex -s 18f4550.lkr crt0i.o usb_cdc.o usb_config.o picUSB.o main.o pic18f4550.
      "
      also these are the lines in the file :
      Line 139: outPtr = &string_descriptor[descriptorIndex];
      Line 333: outPtr = &controlTransferBuffer;
      Line 676: UIEbits.ACTIVIE = 0;
      Line 677: UIRbits.ACTIVIF = 0;
      Line 769: if(UIRbits.ACTIVIF && UIEbits.ACTIVIE)

      Please help..

      regards
      Sans

       
    • sansenan
      sansenan
      2008-06-26

      Thanks jan,

      That solves one of the problem..
      but you still did not tell about the indirection problem..

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 picUSB.c 
      picUSB.c:139: error 47: indirections to different types assignment 
      from type 'const-unsigned-char [] code-code* ' 
      to type 'unsigned-char generic* ' 

      picUSB.c:333: error 47: indirections to different types assignment 
      from type 'volatile-unsigned-char [64] near* ' 
      to type 'unsigned-char generic*

      what to do with this??

      regards
      sans

       
      • kein0r
        kein0r
        2008-06-27

        The errors you mention are jsut warning here:
        /usr/local//bin/sdcc  -mpic16 -V -p18f2550 -DUSB_USE_UART -DDEBUG_UART -DDEBUG -DDEBUG_PRINT   -I. -c picUSB.c
        + "/usr/local//bin/sdcpp" -nostdinc -Wall -std=c99 -DUSB_USE_UART -DDEBUG_UART -DDEBUG -DDEBUG_PRINT -I"." -Dpic18f2550 -D__18f2550 -DSTACK_MODEL_SMALL -obj-ext=.o -DSDCC_MODEL_SMALL -DSDCC=274 -DSDCC_pic16 -D__pic16 -I"/usr/local//bin/../share/sdcc/include/pic16" -I"/usr/local/share/sdcc/include/pic16" -I"."  "picUSB.c"
        picUSB.c:372: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        picUSB.c:464: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        picUSB.c:476: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        picUSB.c:480: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        picUSB.c:494: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        picUSB.c:509: warning 154: converting integral to pointer without a cast
        from type 'unsigned-int'
        to type 'volatile-unsigned-int near* '
        + "/usr/local//bin/gpasm" -DSDCC_MODEL_SMALL -Dpic18f2550 -D__18F2550 -DSTACK_MODEL_SMALL -c "picUSB.asm" -o "picUSB.o"

        Did you use my makefile?

         
        • sansenan
          sansenan
          2008-06-27

          No jan, i am trying this using a batch file in windows...

          the batch file used to compile had 

          sdcc -c -mpic16 -p18f4550 --ivt-loc=0x800 crt0i.c 
          sdcc -c -mpic16 -p18f4550 usb_cdc.c 
          sdcc -c -mpic16 -p18f4550 usb_config.c 
          sdcc -c -mpic16 -p18f4550 picUSB.c 
          sdcc -c -mpic16 -p18f4550 main.c 
          gplink -o test.hex -s 18f4550.lkr crt0i.o usb_cdc.o usb_config.o picUSB.o main.o pic18f4550. 

          Do i need to include any extra flag settings or options?? with the above

           
          • kein0r
            kein0r
            2008-06-27

            yes please add
            -DUSB_USE_UART
            to the compile options. Also i used crt0.o instead of crt0i.o

            Please also make sure that you have the memory location configured correct in your 18f4550.lkr.

            for the 18f2550 those are:
            SECTION    NAME=CONFIG     ROM=config
            SECTION    NAME=bank1      RAM=gpr1
            SECTION    NAME=usbram4    RAM=usb4
            SECTION    NAME=usbram5    RAM=usb5
            SECTION    NAME=eeprom     ROM=eedata

            Why do you recompile crt0i.c? This should already be compiled during the installation step and included with -L/usr/local/share/sdcc/lib/pic16/ i.e. under Linux.

             
    • sansenan
      sansenan
      2008-06-27

      Thanks jan,

      But that doesn't have the errors affected...I did the updates in the linker file too..

      the error ""

      C:\USB_PIC>sdcc -c -mpic16 -p18f4550 -DUSB_USE_UART picUSB.c

      picUSB.c:139: error 47: indirections to different types assignment
      from type 'const-unsigned-char []  code-code* '
      to type 'unsigned-char generic* '

      picUSB.c:333: error 47: indirections to different types assignment
      from type 'volatile-unsigned-char [64]  near* '
      to type 'unsigned-char generic* '

      ""
      And for the crt0i.c,i had sdcc report some errors when i tried to compile in windows and thus included that manually thats all.

       
      • Raphael Neider
        Raphael Neider
        2008-06-27

        Nuts and Volts code:
        Cast all failing assignments to outPtr to (byte *), like
        outPtr = (byte *)&configDescriptor.HIDDescriptor;

        pic-o-rama simple USB code:
        Replace the PTR16 macro in picUSB.h with
        #define PTR16(x) ((__data unsigned int *)(x))

        HTH,
        Raphael

         
        • kein0r
          kein0r
          2008-06-28

          I even tried it with the latest sdcc version (2.8.0) and dont ecounter this problem. Can you please post the code region where sdcc reports the error.
          outPtr is just a simple pointer to the different data regions used during the USB configuration with the host.

           
          • sansenan
            sansenan
            2008-06-28

            Jan,

            Raphael suggestion solved the indirection problem.probabaly you can update that in your file as well.

            Thanks Raphael for the help,though
            that solves the pointer problem..But here pops up many others.

            1.)Can all the below warning be ignored or is their a way to supress them..A flag setting or something??

            2.)For the errors below what all files have i to compile??
            (Pls note:I had to manually include the crt0.c file to get the programs running in windows due to path identification problem,so i may well have to do that with others too,like compiling the usart.c and similar files manually is it??)

            These are the Warnings and errors displayed by SDCC
            ""
            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 -DUSB_USE_UART picUSB.c
            picUSB.c:344: warning 110: conditional flow changed by optimizer: so said EVELYN
            the modified DOG
            picUSB.c:387: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.c:483: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.c:496: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.c:500: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.c:515: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.c:530: warning 154: converting integral to pointer without a cast
            from type 'unsigned-int'
            to type 'volatile-unsigned-int near* '
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:377:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:378:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:379:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.
            picUSB.asm:380:Warning [220] Address exceeds maximum range for this processor.

            C:\USB_PIC>sdcc -c -mpic16 -p18f4550 -DUSB_USE_UART main.c

            C:\USB_PIC>gplink -o test.hex -s 18f4550.lkr crt0.o usb_cdc.o usb_config.o picUS
            B.o main.o pic18f4550.lib
            warning: processor mismatch in "pic18f4550.o"
            error: missing definition for symbol "_stdout", required by "main.o"
            error: missing definition for symbol "_printf", required by "main.o"
            error: missing definition for symbol "__gptrput1", required by "picUSB.o"
            error: missing definition for symbol "__gptrget1", required by "picUSB.o"
            error: missing definition for symbol "_usart_open", required by "main.o"

            ""

             
            • kein0r
              kein0r
              2008-06-28

              The warnings can be ignored or add a cast to disable the warning.
              For the linker errors pls use the linker options provided in the Makefile. You need at least to link against libio for your processor and libc18f.lib. Also make sure that you have copied the memory regions from the linker script provided in the tar.gz archive.

              You should first try to setup a working sdcc, make etc environment or use some IDE like http://eclipse-sdcc.sourceforge.net/ before you start compile bigger projects.

               
              • sansenan
                sansenan
                2008-06-30

                thanks all..I got the code compiled and got it detected as a COM port.
                Now in order to send and receive data,

                1.What are the APIs i need to call??to send and receive data from PC.

                2.Where do i call the APIs,whether it needs to be in the
                ProcessUSBTransactions() function or after ProcessUSBTransactions() in the while()loop inside main().

                 
                • kein0r
                  kein0r
                  2008-06-30

                  Under linux i just do:

                  #define DEVICE  "/dev/ttyUSB"
                  int fd;

                  main(int argc,char** argv){
                    if (argc!=2) {
                      printf("synax: serialt devicenum\n");
                      return -1;
                    }
                    sprintf(devname,"%s%s",DEVICE,argv[1]);
                    fd = open(devname,O_RDWR);
                    if (fd <0) {
                      printf("Could not open device %s !\n",devname);
                      return -1;
                    }
                    printf("Device %s opened ... trying to configure it ...", devname);
                    sprintf(sttystring,"stty 115200 -parenb -cstopb cs8 raw -onlcr -echo < %s ", devname);
                    system(sttystring);
                    printf("done configuring\n");
                  }

                  You can the use simple read/write to access the port. I usually use structs to acces the device, you just need to take care the you use the same bit-length on both sides (int in sdcc is short in gcc etc).
                  read(fd, &kennung, sizeof(char)); /* read one char */
                  write(fd, (const void *) &config, sizeof(config)); /* write a config struct to the device */

                  In my pic main programms i use something like:

                  ...
                  union
                  {
                    struct {
                    unsigned char cmd;
                    unsigned int pwm_value;
                    }; /* add more here */
                    unsigned char buffer[31];
                  } cmd;
                  deviceState = DETACHED;
                  remoteWakeup = 0x00;
                  currentConfiguration = 0x00;
                  ...
                  // First only init USB
                  while(deviceState != CONFIGURED)
                    {
                      EnableUSBModule();
                      ProcessUSBTransactions();
                    }
                  cmd[0] = 0;
                  while(1) {
                      ProcessUSBTransactions();
                      if ( UARTgets((char *)&cmd, sizeof(cmd)) ){
                        switch( cmd[0] ){
                           case 'I': /* do some stuff */
                        }

                  hope this helps.

                   
    • sansenan
      sansenan
      2008-07-07

      hello,

             I tried connecting to usb device using hyper terminal in windows,But it showed a "Cannot Open COM PORT 3 "error.(I was able to do that with RS232 based other devices) When i connect the device,windows correctly recognizes and assigns a new com port.

      How do i check/test the working of CDC after its detected,does the CDC code send some informatuion to the host?How do i read it?

      And Jan,
      Where is this function defined,

      UARTgets((char *)&cmd, sizeof(cmd)) ?? I did not find this definition??

      IS usart library linked to this ???

      The usart_open( ) on the other hand returns no error??

         Coming to the second part .I even sucessufully compiled the Nuts and Volts code of USB HID,  The device gets detected  and registers itself as a HID device and after a second,it gives and exclamation mark(in Windows) and stops.

      What can be the problem.Its not the PID VID error.I checked that.

      I tried the Microchip USBHID mouse demo..and it works well.

      Pls help

       
1 2 > >> (Page 1 of 2)