USBIN pain


Closed Thread
Results 1 to 11 of 11

Thread: USBIN pain

  1. #1
    Join Date
    Aug 2006
    Posts
    20

    Default USBIN pain

    Hi all,

    I trying to send a two byte word down the pipe but I cannot get the received data to equal what's been sent. ASCII char are fine as each is one byte long. I can send a one byte number like 255 and all good. I'm not quite sure if I load a word variable with two bytes from the buffer, like word.byte0=buff[0], word.byte1=buff[2] is where the problem is? The number being transmitted is not ASCII but is a number 0 to FFFF, scratching my head now. Any ideas?

    Thanks
    John

    Code:
    asm
         __CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
       __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
       __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
       __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    endasm
    DEFINE OSC 48
     
    INCLUDE "cdc_desc.bas" 'Descriptor file    for CDC    serial demo
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"     ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"  ; Include if using PBP interrupts
    Include "modedefs.bas"
    ;------LCD -----//
    ' Set LCD Data port
    DEFINE LCD_DREG PORTD
    '     'Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_DBIT 4
    '    ' Set LCD Register Select port
    DEFINE LCD_RSREG PORTD
    '     'Set LCD Register Select bit
    DEFINE LCD_RSBIT 2
    '      'Set LCD Enable port
    DEFINE LCD_EREG PORTD
    '     'Set LCD Enable bit
    DEFINE LCD_EBIT 3
    '      'Set LCD bus size (4 or 8 bits)
    DEFINE LCD_BITS 4
    '      'Set number of lines on LCD
    DEFINE LCD_LINES 2
    '      'Set command delay time in us
    DEFINE LCD_COMMANDUS 2000
    '     ' Set data delay time in us
    DEFINE LCD_DATAUS 50
     
    buffer Var byte[5]
    buffer2 var BYTE[2]
    
    cnt    Var    Byte
    
    position var word
    
     
    ASM                                        
    INT_LIST  macro     ; IntSource,          Label,  Type, ResetFlag?
            INT_Handler     USB_INT,  _DoUSBSERVICE,   ASM,  yes
            INT_Handler     INT_INT,    _Handle_INT,   PBP,  yes
        endm
        INT_CREATE             ; Creates the Low Priority interrupt processor
        INT_ENABLE  USB_INT   ;we will do this after we initialize USB
        INT_ENABLE  INT_INT
    ENDASM
    
    ;----[Initialise USB and Interrupts]----------------------------------------
        PAUSE 100                    ; Allow VUSB to stabilize
        USBINIT                      ; initialize the USB driver
        USBSERVICE                   ; service it once
        UIE = $7F                    ; enable USB interrupts
        UEIE = $9F                   ; enable USB Error interrupts
    @   INT_ENABLE  USB_INT
    ;----[Main program loop]----------------------------------------------------
    lcdout $FE, 1, "Begin"
    pause 1000
    Main:
        cnt = 2        ' Specify input buffer size
    
        USBIn 3, buffer2, cnt, main
    'lcdout $FE, 1, str buffer2\4 'this displays ASCII ok provided cnt=4
    position.byte0=buffer2[0]
    position.byte1=buffer2[1] 
     
     lcdout $FE, 1, DEC position
     
    outloop:
        USBOut 3, buffer2, 2, outloop
    GOTO Main
    ;----[Interrupt handler -- Service USB]-------------------------------------
    DoUSBSERVICE:
          USBSERVICE                   ; Run the SERVICE routines
    @ INT_RETURN
    ;----[Interrupt handler -- INT]
    Handle_INT:
     ; something here
    @ INT_RETURN

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229

    Default Re: USBIN pain

    Can you give us an example?
    What value are you sending and what value are you receiving?
    Regards,
    TABSoft

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default Re: USBIN pain

    Good suggestions. Am curious to see if low and high bytes are reversed.

    Robert

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229

    Default Re: USBIN pain

    My thoughts exactly Robert.

    Little Endian vs Big Endian.
    Regards,
    TABSoft

  5. #5
    Join Date
    Aug 2006
    Posts
    20

    Default Re: USBIN pain

    If I set the high order byte to 0, word.byte1=0, and send 255 I get 255 in the display, cant send beyond that as 2nd byte is 0. When both bytes set as buff [0], [1] and send 255 or less I get a number begining in 5 or 6 such as 56324, it makes no sense whats going on. I did discover that the terminal prog I was using only sent single bytes only so that if 256 was sent, nothing happened. Took a while to work that out and thought I'd cracked it, noooo of course not. I'm using a VB application to send the number, and on the pic side if I set the high order byte to 0 I do get up to 255 on the lcd, all good so far, when high order byte set as buff [1] I get garbage. I,m not 100% certain though that whats been tx out the VB app is what I,d expect.

    I,ve tried reversing the byte order to see if it was high-low rather than low-high but thats not the issue. Any suggestions on how I would go about further testing appreciated,

    john

  6. #6
    Join Date
    Sep 2009
    Posts
    737

    Default Re: USBIN pain

    Here is my template for USB and VB6
    http://www.picbasic.co.uk/forum/show...812#post106812
    I think that value stored in buffer(1) in vb, is value in buffer(0) in PBP...
    Values are shifted one place when you compare array on pc and pic.
    I'm forget why this is happen, so I'm not sure 100%...

    Here is comment from code
    PORTA=USBRXBuffer[0]'PC BufferOut(1)

    And here is why:
    BufferOut(0) = 0 ' first by is always the report ID
    ' write the data (don't forget, pass the whole array)...

    When you send data from PIC to PC, I think that values are on same place...
    Hope that this will clear what is going on...
    EDIT:
    And as you can see from Darrel's post USB buffer are at least 8 byte's
    DEFINE USB_INSIZE 64 ; IN report is PIC to PC (8,16,32,64)
    Last edited by pedja089; - 10th February 2015 at 23:08.

  7. #7
    Join Date
    Aug 2006
    Posts
    20

    Default Re: USBIN pain

    That might explain why garbage appears on the lcd at switch on, however as its confihured as virtual serial and not pure USB as in hid I would,nt have thought that [0] would be reserved, but it does make me think its worth another look. So you think that the buffer size should be at least 8 bytes?

  8. #8
    Join Date
    Sep 2009
    Posts
    737

    Default Re: USBIN pain

    I'm not sure, but I think it should be at least 8 bytes...
    If you are using CDC, than it maybe different... Best choice is that you leave USB variable, as is. Don't change anything.
    And for CDC I think that numbers of bytes in packet is not constant... It can be one, it can be more... But again, I'm not sure... I don't have much experience with usb.

  9. #9
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default Re: USBIN pain

    Darn, can't remember where I read about buffer length. I would go with 8 bytes too.

    It's not going to affect performance much anyways.

    Robert

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795

    Default Re: USBIN pain

    I think it is in USB Complete, but have to search and the book is far away right now...

    Ioannis

  11. #11
    Join Date
    Mar 2008
    Location
    Gerogetown, Texas
    Posts
    94

    Default Re: USBIN pain

    You can have the buffer any length as long as the discriptor is set correctly.

    example

    Code:
    USBBufferSizeMax   con 16  ' maximum buffer size
    USBBufferSizeTX    con 16  ' input 
    USBBufferSizeRX    con 16  ' output

    Code:
      ; report count (number of fields)
        retlw	0x95
        retlw	0x10   ; 16 byte buffer
    it could be 3 bytes, 5 bytes as many as you want up to 64 bytes

Similar Threads

  1. What a Pain In The USB-utt
    By Cobra_Phil in forum USB
    Replies: 19
    Last Post: - 23rd January 2015, 15:15
  2. PIC18F14K50 and USBin
    By ybosco in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 7th November 2013, 05:52
  3. Replies: 6
    Last Post: - 4th November 2009, 14:36
  4. Usbin and hserin
    By mpardinho in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th October 2007, 16:26
  5. Need help with USBIN
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th June 2007, 19:51

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts