Easy HID Command -Response application


Closed Thread
Results 1 to 26 of 26

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    If there's anyone that knows about USB ... She's the one.
    I've never tried "feature reports" before, I'll have to look into that.

    But this might help with the USB interrupt part.
    It's an include file that services the USB via the USB interrupt.
    It works with programs derived from EasyHID or straight PBP with custom HID descriptors.
    I'm hoping it works with HIDmaker programs too.

    It also gives indications of when the device is connected to the PC, when a report has been received and when it's OK to transmit using the BIT variables ...

    Plugged indicates when the device is fully connected to the PC.
    RX_READY indicates when a report has been received.
    TX_READY indicates when it's OK to Send. The device is Plugged and the SIE is ready to accept data.

    It does everything required to handle the interrupts.
    All you have to do is INCLUDE the file.

    Well, you'll also need to comment out any USBSERVICE and USBINIT statements that may already be in your program, because they will conflict with the interrupt servicing.

    If you want to use other High Priority interrupts in your program, that can be done too. See INT_HOOK below.
    Low priority interrupts are still available.

    Code:
    '***************************************************************************
    '*  Name    : USB_ASM_Service.pbp                                          *
    '*  Author  : Darrel Taylor                                                *
    '*  Date    : 9/10/2009                                                    *
    '*  Version : 1.1                                                          *
    '*  Notes  1. DO NOT place ANY USBSERVICE or USBINIT statements ANYWHERE   *
    '*            in your program. They will conflict with interrupt Servicing *
    '*         2. To use other high priority interrupts you can add the define *
    '*            DEFINE  INT_HOOK  handlers                                   *
    '*            handlers will be called on each high priority interrupt      *
    '*            The handlers must be ASM Interrupt compatible                *
    '***************************************************************************
    DEFINE INTHAND _DoUSBSERVICE
    
    GIE                VAR INTCON.7      ; Global Interrupt Enable
    PEIE               VAR INTCON.6      ; Peripheral Interrupt Enable
    USBIE              VAR PIE2.5        ; USB funnel Interrupt Enable
    USBIF              VAR PIR2.5        ; USB Interrupt Flag
    IDLEIF             VAR UIR.4         ; USB Idle Interrupt Flag
    SOFIF              VAR UIR.6         ; USB Start Of Frame Flag
    SOFIE              VAR UIE.6         ; USB Start Of Frame Interrupt Enable
    USB_Flags          VAR BYTE BANK0
      Plugged          VAR USB_Flags.0   ; indicates Fully connected to PC
      RX_READY         VAR USB_Flags.1   ; indicates Report has been received
      TX_READY         VAR USB_Flags.2   ; indicates it's OK to Send
    CONFIGURED_STATE   CON EXT           ; state when PIC is connected to PC
    BD1STATOUT         VAR BYTE EXT      ; OUT report status byte
    BD1STATIN          VAR BYTE EXT      ; IN report status byte
    USBDeviceState     VAR BYTE EXT      ; USB drivers current State
    RXOWNED            VAR BD1STATOUT.7  ; Out report Owned (OUT is PC to PIC)
    TXOWNED            VAR BD1STATIN.7   ;  IN report Owned (IN is PIC to PC)
    
    ;----[Make it compatible with PBP versions previous to 2.60]----------------
    ASM
      ifndef _USBMEMORYADDRESS           ; only PBP 2.60+ has USBMEMORYADDRESS 
    _USBMEMORYADDRESS = 400h             ; fake it for 2.50-
      endif
    BD1STATOUT = _USBMEMORYADDRESS + 8   ; 408h or 208h
    BD1STATIN  = _USBMEMORYADDRESS + 0Ch ; 40Ch or 20Ch
    
      ifndef USBDeviceState              ; 2.60+ uses USBDeviceState
        ifdef usb_device_state           ; 2.50- uses usb_device_state
    USBDeviceState = usb_device_state
        else
          error "USBDeviceState not found" ; somethings not right if we get here
        endif 
      endif
    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
        USBIE = 1                    ; enable USB funnel int
        PEIE = 1                     ; enable peripheral ints
        GIE = 1                      ; enable global ints
        USB_Flags = 0                ; clear the flags
    GOTO OverUSBservice              ; jump over the subroutines
    
    ;----[Interrupt handler -- Service USB]-------------------------------------
    DoUSBSERVICE:
      IF USBIE THEN
        IF USBIF THEN
          BSR = 0                      ; Make sure we're in BANK0
          IF IDLEIF THEN Plugged = 0   ; If the bus is Idle, we can't be Plugged
          IF SOFIF  THEN               ; On Start-Of-Frame
            IF (USBDeviceState = CONFIGURED_STATE) THEN ; Check for CONFIGURED
                Plugged = 1          ; if so, we're Plugged
            ENDIF
          ENDIF
          USBSERVICE                   ; Run the SERVICE routines
          USBIF = 0                    ; clear the USB flag
          IF !Plugged THEN UEIR = 0    ; prevents lock-ups when not terminated
                                         ;  from bit-stuff error
          RX_READY = !RXOWNED          ; indicate if report has been Received
          IF Plugged THEN
            TX_READY = !TXOWNED      ; indicate if ready to Send
          ELSE
            TX_READY = 0             ; don't try to send when not Plugged
          ENDIF
        ENDIF
      ENDIF
      
      ASM
        ifdef INT_HOOK
          L?CALL  INT_HOOK           ; call User Handlers if INT_HOOK is Defined
        endif                        ; User routines must be ASM INT compatible
        retfie 1                     ; Return from interrupt "Fast" with shadows
      ENDASM
    
    OverUSBservice:
    ADDED:
    I've added a define that let's you easliy use more high priority interrupts ...
    Code:
    DEFINE  INT_HOOK  _MyInts
    If defined, the MyInts: routine will be called on each high priority interrupt.
    It should end with a RETURN

    HTH,
    Attached Files Attached Files
    Last edited by Darrel Taylor; - 11th September 2009 at 05:50. Reason: Added some comments to make more sense. Added INT_HOOK define.

  2. #2
    Join Date
    May 2005
    Location
    Ne ohio
    Posts
    21

    Default

    Thanks,
    I'll try your code out. though I'm at Rev. 2.5 I just ordered an upgrade to the Latest PBPro.
    I generated a Feature report using HIDMaker with a 64byte array (similar to what I've been using all along) and I'm getting about 21 msecs on this PC for each pass through the "Timer1_Tic" Handler. This is nearly stock HIDMaker C# and PBasic Code except I've added a Stopwatch from System.Diagnostics to capture the milliseconds to execute the SendReports then ReadReports.

    Elsewhere in my older code from EasyHID, I've moved the USBSERVICE inside my timer interrupt routine and that has improved the time to return input reports. So now I currently USBSERVICE at about 1 msec. intervals.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    The service routine should work with 2.50.

    I've had it all working while embedded into various other programs from 2.47 to 2.60. This time I just put it all in an include file that's specific to your situation. Well, a lot of peoples situations.

    You should be aware that USB is handled differently in PBP 2.60.
    Programs from earlier versions will have to be modified to work with it.

    And since your program is coming from HIDmaker, you'll probably need an upgrade if you've had it awhile. I don't know that for sure because I don't use it, but it only seems logical.

    DT

  4. #4

    Default

    Where should this go?

    I cut'n'pasted from the above code section, into microcodestudio, then added this to the top, before the define

    Code:
    ' **************************** Added from other demo
    buffer	Var	Byte[16]
    cnt	Var	Byte
    LED	Var	PORTB.0
    Define  OSC 48
    '****************************************************
    
    high led


    Then at the bottom after overusbservice I added the usb in/out and a led flash to show its alive

    Code:
    idleloop:
    
        low led
        pause 500
        high led
        pause 500
    
    	cnt = 16	' Specify input buffer size
    	USBIn 3, buffer, cnt, idleloop
    
    ' Message received
    
    	buffer[0] = "H"
    	buffer[1] = "e"
    	buffer[2] = "l"
    	buffer[3] = "l"
    	buffer[4] = "o"
    	buffer[5] = " "
    	buffer[6] = "W"
    	buffer[7] = "o"
    	buffer[8] = "r"
    	buffer[9] = "l"
    	buffer[10] = "d"
    	buffer[11] = 13
    	buffer[12] = 10
    	buffer[13] = 0
    
    outloop:
    	
        USBOut 3, buffer, 14, outloop
    
    	Goto idleloop	' Wait for next buffer
    usbinit and usbservice was removed from the code of the other demo like it says to.

    It looks to me like a worker, but i get compile errors, lots of them

    This one

    Code:
    IDLEIF             VAR UIR.4         ; USB Idle Interrupt Flag
    <
    gives me a bad datatype (first of a few)

    and this one

    Code:
        UIE = $7F                    ; enable USB interrupts
    throws up a syntax error with some more below it


    I tried it as

    INCLUDE "USB_ASM_Service"

    At the top of my little test and MCS opened it as a 2nd page, and threw up the same errors.

    Just for completeness thats here
    Code:
    include "USB_ASM_Service.pbp"
    
    ' **************************** Added from other demo
    buffer	Var	Byte[16]
    cnt	Var	Byte
    LED	Var	PORTB.0
    Define  OSC 48
    ' ****************************************************
    
    high led
    
    idleloop:
    
        low led
        pause 500
        high led
        pause 500
    
    	cnt = 16	' Specify input buffer size
    	USBIn 3, buffer, cnt, idleloop
    
    ' Message received
    
    	buffer[0] = "H"
    	buffer[1] = "e"
    	buffer[2] = "l"
    	buffer[3] = "l"
    	buffer[4] = "o"
    	buffer[5] = " "
    	buffer[6] = "W"
    	buffer[7] = "o"
    	buffer[8] = "r"
    	buffer[9] = "l"
    	buffer[10] = "d"
    	buffer[11] = 13
    	buffer[12] = 10
    	buffer[13] = 0
    
    outloop:
    	
        USBOut 3, buffer, 14, outloop
    
    	Goto idleloop	' Wait for next buffer
    Last edited by f_lez; - 1st November 2009 at 16:05.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Looks like you've been busy while I was sleeping.

    PBP 2.46 was missing a few registers in the PIC18EXT.bas file.
    You can either add these lines to that file, or place them at the top of your program (before the INCLUDE file).
    Code:
    UIR  VAR BYTE EXT
    UIE  VAR BYTE EXT
    UEIE VAR BYTE EXT
    UEIR VAR BYTE EXT
    For the _config depricated warning you can add w = -230 to the 18F2550.inc file ...
    Code:
            LIST p = 18F2550, r = dec, w = -311, w = -230, f = inhx32
    hth,
    DT

  6. #6

    Default

    thanks that was just the little gem of info i was missing!

    And it works!

  7. #7

    Default

    Simple code in a loop...

    Code:
    ' Main Program Loop
    Loop:
     
        USBOut 3, Buffer, Cnt, loopy
    loopy:
        porta=170 ' fancy led pattern0
        pause 500
        porta=85  ' fancy led pattern1
        pause 500
    
        goto loop
    end
    After about 30 minutes, stops running......

    Pic is stable etc had it running 24hrs+ on a simple led flasher. (on same dev board)

    any guesses?
    Last edited by f_lez; - 28th February 2010 at 18:54.

Similar Threads

  1. USB hid maker help please.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd April 2013, 14:49
  2. i cant get 18f2550 work as HID
    By Ahmadabuomar in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 13th October 2009, 16:39
  3. Unusual Interrupts Application Problem
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th May 2009, 11:55
  4. Hid Maker FS Problems
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th April 2009, 21:27
  5. Making your own HID descriptor file
    By NL2TTL in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th April 2005, 12:35

Members who have read this thread : 0

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