Author Topic: PBP USB HID example sending more than 2 bytes  (Read 24749 times)

Ra2ven

  • Member
  • ***
  • Posts: 6
PBP USB HID example sending more than 2 bytes
« on: January 17, 2013, 11:49:41 am »
Hello, It´s my first post so firstly I say hello to all.

I was using the Jan Axelson´s example whit pic basic pro to send and recieve data through USB (whit usbhidio in PC) and it woks well.

I made a VB.net similar program and only works if I don´t change the buffer output lenght:

outloop:
   USBService                   ' Must service USB regularly
   USBOut 1, buffer, 2, outloop   ' Send the bytes back


If I increase or decrease the bytes output (2) the program in PC doesnt recieve nothing.

I tried to change the descriptors (size of the input-output buffers) but it´s the same.

I hope anybody can help me.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #1 on: January 17, 2013, 03:35:28 pm »
The amount of data the host application sends and receives has to match the report sizes defined in the report descriptor the device returns during enumeration.

Ra2ven

  • Member
  • ***
  • Posts: 6
Re: PBP USB HID example sending more than 2 bytes
« Reply #2 on: February 04, 2013, 10:30:18 am »
Thank you for responding.

Do you mean the endpint buffer size?. In the descriptors I have:

Code: [Select]
#define USB_EP0_BUFF_SIZE 16
#define HID_INT_IN_EP_SIZE 16

And in my program:

Code: [Select]
buffer Var Byte[16]
So I think I should be able to send at least 16 bytes, but if I modify the count of bytes to send (2) I don´t recieve nothing in my PC program:

Code: [Select]
USBOut 1, buffer, 2, programstart
I attach the descriptors code.

Greetings

[attachment deleted by admin]

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #3 on: February 04, 2013, 10:42:56 am »
I wasn't able to download the descriptor file. Please post as text in the forum.

The HID report descriptor defines the length of the reports.

Ra2ven

  • Member
  • ***
  • Posts: 6
Re: PBP USB HID example sending more than 2 bytes
« Reply #4 on: February 06, 2013, 04:21:39 am »
Code: [Select]
' ja_desc.bas
' USB descriptors for Jan Axelson's USB Demo
' 9/16/08 microEngineering Labs, Inc.

'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 16 ; 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 16
#define HID_INT_IN_EP_SIZE 16
#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 0x25 ; Vendor ID - 6017 is LakeView Research ID (low byte)
retlw 0x09 ; (high byte)
retlw 0x34 ; Product ID: 2000 (low byte)
retlw 0x12 ; (high byte)
retlw 0x01 ; 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 0x02 ; Number of endpoints in this intf
retlw HID_INTF ; Class code
retlw 0x00 ; Subclass code
retlw 0x00 ; Protocol code
retlw 0x00 ; Interface string index
HIDDescriptor1
retlw (Endpoint1Out-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)
Endpoint1Out
retlw (Endpoint1In-Endpoint1Out)/2 ; Size of this descriptor in bytes
retlw USB_DESCRIPTOR_ENDPOINT ; ENDPOINT descriptor type
retlw HID_EP|_EP_OUT ; Endpoint Address
retlw _INT ; Attributes
retlw low (HID_INT_OUT_EP_SIZE) ; Size of the endpoint buffer in bytes (low byte)
retlw high (HID_INT_OUT_EP_SIZE) ; (high byte)
retlw 0x0A ; Polling interval (10ms)
Endpoint1In
retlw (EndConfig1-Endpoint1In)/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 0x06 ; usage page (Vendor Defined)
retlw 0xA0
        retlw 0xFF
retlw 0x09 ; usage (Vendor Defined)
        retlw 0x01
retlw 0xA1 ; collection (application)
        retlw 0x01
retlw 0x09 ; usage (Vendor Defined)
        retlw 0x02
retlw 0xA1 ; collection (linked)
        retlw 0x00
retlw 0x06 ; usage page (buttons)
        retlw 0xa1
retlw 0xFF
retlw 0x09 ; usage vendor defined
        retlw 0x03
retlw 0x09 ; usage vendor defined
        retlw 0x04
retlw 0x15 ; logical minimum (-128)
        retlw 0x80
retlw 0x25 ; logical maximum (127)
        retlw 0x7F
retlw 0x35 ; Physical Minimum (0)
        retlw 0x00
retlw 0x45 ; Physical Maximum (255)
        retlw 0xFF
        retlw 0x75 ; report size (8) (bits)
retlw 0x08
        retlw 0x95 ; report count (2) (fields)
retlw 0x02
        retlw 0x81 ; input (Data, Variable, Absolute)
retlw 0x02
        retlw 0x09 ; usage (Vendor Defined)
retlw 0x05
        retlw 0x09 ; useage (Vendor Defined)
retlw 0x06
retlw 0x15 ; logical minimum (-128)
retlw 0x80
        retlw 0x25 ; logical maximum (127)
retlw 0x7F
        retlw 0x35 ; Physical Minimum (0)
retlw 0x00
        retlw 0x45 ; Physical Maximum (255)
retlw 0xFF
        retlw 0x75 ; report size (8) (bits)
retlw 0x08
        retlw 0x95 ; report Count (2) (fields)
retlw 0x02
        retlw 0x91 ; Output (Data, Variable, Absolute)
retlw 0x02
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   'P'
retlw   0x00
retlw   'I'
retlw   0x00
retlw   'C'
retlw   0x00
retlw   '1'
retlw   0x00
retlw   '8'
retlw   0x00
retlw   ' '
retlw   0x00
retlw   'J'
retlw   0x00
retlw   'A'
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

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #5 on: February 06, 2013, 10:36:10 am »
Your report descriptor defines a 2-byte Input report and a 2-byte Output report:

  retlw   0x75      ; report size (8) (bits)
   retlw   0x08
        retlw   0x95      ; report count (2) (fields)
   retlw   0x02

If you want to transfer different quantities of data, you need to edit the report descriptor.

Ra2ven

  • Member
  • ***
  • Posts: 6
Re: PBP USB HID example sending more than 2 bytes
« Reply #6 on: February 19, 2013, 09:32:35 am »
 
I tried to edit the code for send/recieve 16 bytes but it doesn´t work:

retlw   0x75      ; report size (8 bits)
   retlw   0x08
        retlw   0x95      ; report count (2 fields)
   retlw   0x10           ; (16 in hexadecimal)



what am I doing wrong?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #7 on: February 19, 2013, 10:13:20 am »
What do you mean by "it doesn't work"? If it doesn't compile, what is the error message? If it compiles and runs, what are you seeing in the host application?

Ra2ven

  • Member
  • ***
  • Posts: 6
Re: PBP USB HID example sending more than 2 bytes
« Reply #8 on: February 19, 2013, 12:59:51 pm »
It compiles well but when I run my PC program I cant receive nothing.

My PC program is a simple aplication that send/receive 16 bytes to/from the PIC, but if I change the program for allow the PIC to send more than 2 bytes I dont receive nothing (the USB connecton is manteined and the PIC continue running because I have a led turn on/off every second and it works):

USBOut 1, buffer, 2, programstart

I tried to change the descriptors but is the same, if I change the "2" in the code above don´t work.

Greetings

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #9 on: February 20, 2013, 09:49:21 am »
Change the Product ID in the firmware or uninstall the device in Device Manager to keep Windows from remembering previous descriptors.

The Windows HID driver will ignore any received data that doesn't match a defined report length.

Ra2ven

  • Member
  • ***
  • Posts: 6
Re: PBP USB HID example sending more than 2 bytes
« Reply #10 on: August 15, 2013, 06:14:58 am »
Hello, sorry for the delay in answering

I managed to make the program work putting the number of bytes to be sent as a number rather than as a variable:

USBOut 1, bufferout, 16, outloop   (this work)

variable=16
USBOut 1, bufferout, variable, outloop   (this don´t work)


I dont know why cant work with variables, maybe is relationated with this lines in the descriptor?:

retlw   0x91      ; Output (Data, Variable, Absolute)
retlw   0x02


Greetings

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: PBP USB HID example sending more than 2 bytes
« Reply #11 on: August 15, 2013, 11:24:28 am »
Good to hear you have it working.

The PicBasic manual has some cautions about variable use. See the manual for advice on this. Is "variable" a reserved word? Try renaming it.