Hello all,

I am new to USB.

I have been trying for more than a week to get the mouse example working in Pic18F2450 but it either doesn't get recognized by PC or it says unknown usb device.

I am using PicBasic Pro 2.60 and Pic uses external 24Mhz crystal in low speed usb.

Here is the code:

Code:
    Include    "hid_desc.bas"    ' Include the HID descriptors

Define  OSC     24

buffer    Var    Byte[3]
loopcnt    Var    Byte
state    Var    Byte


    
    Pause 10

    USBInit            ' Get USB going

    buffer[0] = 0
    buffer[1] = 0
    buffer[2] = 0

movecursor:
    For state = 0 To 3    ' Move through each state
        For loopcnt = 1 To 16    ' 16 steps in each direction
            Branch state, [up, right, down, left]

up:
            buffer[1] = 0
            buffer[2] = -2
            Goto endgame
down:
            buffer[1] = 0
            buffer[2] = 2
            Goto endgame
left:
            buffer[1] = -2
            buffer[2] = 0
            Goto endgame
right:
            buffer[1] = 2
            buffer[2] = 0

endgame:
            USBService    ' Must service USB regularly
            USBOut 1, buffer, 3, endgame    ' Send buffer to endpoint 1
        Next loopcnt
    Next state
    Goto movecursor        ' Do it forever

Descriptor code:

Code:
' hid_desc.bas

'USBMEMORYADDRESS Con    $400    ' USB RAM starts here (set in device header file)
USBMEMORYSIZE    Con    256    ' USB RAM size in bytes
USBReservedMemory Var Byte[USBMEMORYSIZE] USBMEMORYADDRESS    ' Reserve memory used by USB assembler code

    Goto    hid_desc_end    ' Skip over all of the USB assembler code

    Asm

#define USB_EP0_BUFF_SIZE    8    ; 8, 16, 32, or 64
#define USB_MAX_NUM_INT        1
#define USB_MAX_EP_NUMBER    1
#define NUM_CONFIGURATIONS    1
#define NUM_INTERFACES        1

;#define UCFG_VAL    USB_PULLUP_ENABLE|USB_INTERNAL_TRANSCEIVER|USB_FULL_SPEED|USB_PING_PONG__NO_PING_PONG
#define UCFG_VAL    USB_PULLUP_ENABLE|USB_INTERNAL_TRANSCEIVER|USB_LOW_SPEED|USB_PING_PONG__NO_PING_PONG

;#define USE_SELF_POWER_SENSE_IO
;#define USE_USB_BUS_SENSE_IO

#define USB_POLLING

; HID
; Endpoints Allocation
#define HID_INTF_ID        0x00
#define HID_EP            1
#define HID_INT_OUT_EP_SIZE    3
#define HID_INT_IN_EP_SIZE    3
#define HID_NUM_OF_DSC        1

    include    "usb_hid.asm"    ; Include rest of USB files, starting with HID class code

; ******************************************************************
; This table is polled by the host immediately after USB Reset has been released.
; This table defines the maximum packet size EP0 can take.
; See section 9.6.1 of the Rev 1.0 USB specification.
; These fields are application DEPENDENT. Modify these to meet
; your specifications.
; ******************************************************************
DeviceDescriptor
    retlw    (EndDeviceDescriptor-DeviceDescriptor)/2    ; Size of this descriptor in bytes
    retlw    USB_DESCRIPTOR_DEVICE    ; DEVICE descriptor type
    retlw    0x10        ; USB Spec Release Number in BCD format - 1.10 (low byte)
    retlw    0x01        ; (high byte)
    retlw    0x00        ; Class Code
    retlw    0x00        ; Subclass code
    retlw    0x00        ; Protocol code
    retlw    USB_EP0_BUFF_SIZE    ; Max packet size for EP0
    retlw    0xD8        ; Vendor ID - 0x04D8 is Microchip Vendor ID (low byte)
    retlw    0x04        ; (high byte)
    retlw    0x00        ; Product ID: Mouse in a circle fw demo (low byte)
    retlw    0x00        ; (high byte)
    retlw    0x03        ; Device release number in BCD format (low byte)
    retlw    0x00        ; (high byte)
    retlw    0x01        ; Manufacturer string index
    retlw    0x02        ; Product string index
    retlw    0x00        ; Device serial number string index
    retlw    NUM_CONFIGURATIONS    ; Number of possible configurations
EndDeviceDescriptor

; ******************************************************************
; This table is retrieved by the host after the address has been set.
; This table defines the configurations available for the device.
; See section 9.6.2 of the Rev 1.0 USB specification (page 184).
; These fields are application DEPENDENT. 
; Modify these to meet your specifications.
; ******************************************************************
; Configuration pointer table
USB_CD_Ptr
Configs
    db    low Config1, high Config1
    db    upper Config1, 0

; Configuration Descriptor
Config1
    retlw    (Interface1-Config1)/2    ; Size of this descriptor in bytes
    retlw    USB_DESCRIPTOR_CONFIGURATION    ; CONFIGURATION descriptor type
Config1Len
    retlw    low ((EndConfig1-Config1)/2)    ; Total length of data for this cfg
    retlw    high ((EndConfig1-Config1)/2)
    retlw    NUM_INTERFACES    ; Number of interfaces in this cfg
    retlw    0x01        ; Index value of this configuration
    retlw    0x00        ; Configuration string index
    retlw    _DEFAULT|_SELF    ; Attributes
    retlw    50        ; Max power consumption (2X mA)
Interface1
    retlw    (HIDDescriptor1-Interface1)/2    ; Size of this descriptor in bytes
    retlw    USB_DESCRIPTOR_INTERFACE    ; INTERFACE descriptor type
    retlw    0x00        ; Interface Number
    retlw    0x00        ; Alternate Setting Number
    retlw    0x01        ; Number of endpoints in this intf
    retlw    HID_INTF    ; Class code
    retlw    BOOT_INTF_SUBCLASS    ; Subclass code
    retlw    HID_PROTOCOL_MOUSE    ; Protocol code
    retlw    0x00        ; Interface string index
HIDDescriptor1
    retlw    (Endpoint1-HIDDescriptor1)/2    ; Size of this descriptor in bytes
    retlw    DSC_HID        ; HID descriptor type
    retlw    0x11        ; HID Spec Release Number in BCD format - 1.11 (low byte)
    retlw    0x01        ; (high byte)
    retlw    0x00        ; Country Code (0x00 for Not supported)
    retlw    HID_NUM_OF_DSC    ; Number of class descriptors
    retlw    DSC_RPT        ; Report descriptor type (HID)
ReportDescriptor1Len
    retlw    low ((EndReportDescriptor1-ReportDescriptor1)/2)
    retlw    high ((EndReportDescriptor1-ReportDescriptor1)/2)
Endpoint1
    retlw    (EndConfig1-Endpoint1)/2    ; Size of this descriptor in bytes
    retlw    USB_DESCRIPTOR_ENDPOINT    ; ENDPOINT descriptor type
    retlw    HID_EP|_EP_IN    ; Endpoint Address
    retlw    _INT        ; Attributes
    retlw    low (HID_INT_IN_EP_SIZE)    ; Size of the endpoint buffer in bytes (low byte)
    retlw    high (HID_INT_IN_EP_SIZE)    ; (high byte)
    retlw    0x0A        ; Polling interval (10ms)
EndConfig1

ReportDescriptor1
    retlw   0x05
    retlw   0x01    ; usage page (generic desktop)
    retlw   0x09
    retlw   0x02    ; usage (mouse)
    retlw   0xA1
    retlw   0x01    ; collection (application)
    retlw   0x09
    retlw   0x01    ;   usage (pointer)
    retlw   0xA1
    retlw   0x00    ;   collection (linked)
    retlw   0x05
    retlw   0x09    ;     usage page (buttons)
    retlw   0x19
    retlw   0x01    ;     usage minimum (1)
    retlw   0x29
    retlw   0x03    ;     usage maximum (3)
    retlw   0x15
    retlw   0x00    ;     logical minimum (0)
    retlw   0x25
    retlw   0x01    ;     logical maximum (1)
    retlw   0x95
    retlw   0x03    ;     report count (3)
    retlw   0x75
    retlw   0x01    ;     report size (1)
    retlw   0x81
    retlw   0x02    ;     input (3 button bits)
    retlw   0x95
    retlw   0x01    ;     report count (1)
    retlw   0x75
    retlw   0x05    ;     report size (5)
    retlw   0x81
    retlw   0x01    ;     input (constant 5 bit padding)
    retlw   0x05
    retlw   0x01    ;     usage page (generic desktop)
    retlw   0x09
    retlw   0x30    ;     usage (X)
    retlw   0x09
    retlw   0x31    ;     usage (Y)
    retlw   0x15
    retlw   0x81    ;     logical minimum (-127)
    retlw   0x25
    retlw   0x7F    ;     logical maximum (127)
    retlw   0x75
    retlw   0x08    ;     report size (8)
    retlw   0x95
    retlw   0x02    ;     report count (2)
    retlw   0x81
    retlw   0x06    ;     input (2 position bytes X & Y)
    retlw   0xC0    ;   end collection
    retlw   0xC0    ; end collection
EndReportDescriptor1

; String pointer table
USB_SD_Ptr
Strings
    db    low String0, high String0
    db    upper String0, 0
    db    low String1, high String1
    db    upper String1, 0
    db    low String2, high String2
    db    upper String2, 0

; Language code string descriptor
String0
    retlw   (String1-String0)/2    ; Size of this descriptor in bytes
    retlw   USB_DESCRIPTOR_STRING    ; STRING descriptor type
    retlw   0x09        ; Language ID as defined by MS - 0x0409 (low byte)
    retlw   0x04        ; (high byte)

; Manufacturer string descriptor
String1
    retlw   (String2-String1)/2    ; Size of this descriptor in bytes
    retlw   USB_DESCRIPTOR_STRING    ; STRING descriptor type
    retlw   'M'
    retlw   0x00
    retlw   'i'
    retlw   0x00
    retlw   'c'
    retlw   0x00
    retlw   'r'
    retlw   0x00
    retlw   'o'
    retlw   0x00
    retlw   'c'
    retlw   0x00
    retlw   'h'
    retlw   0x00
    retlw   'i'
    retlw   0x00
    retlw   'p'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'T'
    retlw   0x00
    retlw   'e'
    retlw   0x00
    retlw   'c'
    retlw   0x00
    retlw   'h'
    retlw   0x00
    retlw   'n'
    retlw   0x00
    retlw   'o'
    retlw   0x00
    retlw   'l'
    retlw   0x00
    retlw   'o'
    retlw   0x00
    retlw   'g'
    retlw   0x00
    retlw   'y'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'I'
    retlw   0x00
    retlw   'n'
    retlw   0x00
    retlw   'c'
    retlw   0x00
    retlw   '.'
    retlw   0x00

; Product string descriptor
String2
    retlw   (EndStringDescriptors-String2)/2    ; Size of this descriptor in bytes
    retlw   USB_DESCRIPTOR_STRING    ; STRING descriptor type
    retlw   'M'
    retlw   0x00
    retlw   'o'
    retlw   0x00
    retlw   'u'
    retlw   0x00
    retlw   's'
    retlw   0x00
    retlw   'e'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'I'
    retlw   0x00
    retlw   'n'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'a'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'C'
    retlw   0x00
    retlw   'i'
    retlw   0x00
    retlw   'r'
    retlw   0x00
    retlw   'c'
    retlw   0x00
    retlw   'l'
    retlw   0x00
    retlw   'e'
    retlw   0x00
    retlw   ' '
    retlw   0x00
    retlw   'D'
    retlw   0x00
    retlw   'e'
    retlw   0x00
    retlw   'm'
    retlw   0x00
    retlw   'o'
    retlw   0x00
EndStringDescriptors

    Endasm

hid_desc_end            ' End of skipped over USB assembler code

Any idea?