• AVR Freaks

Helpful ReplyMicrochip Custom HID Demo questions ?

Author
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
2011/03/07 06:30:13 (permalink)
0

Microchip Custom HID Demo questions ?

I'm trying out the microchip custom hid demo on my low pin count usb development board.
If I build the example as is, and compile the pc software as is everything works fine.
I did notice a couple of odd things in the example.
usb_config.h

#define HID_INT_OUT_EP_SIZE     3
#define HID_INT_IN_EP_SIZE      3

In usb_descriptors.c the two endpoints are shown as size 64, and also in the source code for pc software form1.h the buffer size assigned to deal with the endpoints was 65 (64 + report id). The hid report descriptor also specifies the usage maximums as 64,  and report count as 64.
But although the usb_config.h settings do not agree with the usb_descriptors or the pc software it still works ok. Which I am surprised by ?

As an experiment, I reduced the sizes in the usb_descriptors.c and form1.h to 16 bytes, and then the pc application does not find the development board. USBlyser still reported the old 64 byte size so I used usbdeview to remove the microchip simple hid device, and reconnected the device. Usblyser now shows the correct number of bytes (16) in the rpt descriptor & endpoints. The pc app then connects to the device ok. The Toggle leds button works ok, but pressing "Get Pushbutton State" hangs the app ? Is this likely to be a pc software problem or the usb firmware ?

#1
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 06:47:26 (permalink)
0
Yeah, that's true: those numbers are "fake" i.e. not used.
Some time ago I had changed the HID descriptors to let it use the #defined values - in the end it's just some convention etc...

If you want to make your packets smaller, you have to change that in some several places - that's how it goes Smile

GENOVA :D :D ! GODO
#2
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 07:17:57 (permalink)
0
Which are the fake numbers ? The ones in the usb_config.h or the ones used in the descriptors/pc software examples ?

I probably didn't explain very well, but I changed the numbers in usb_descriptors.c, endpoints & report descriptors and the buffer sizes in form1.h code from 64/65 bytes to 16/17 bytes. I think I have changed all the places that are relevant, but now the app hangs when I press Get Pushbutton State in the app, but Toggle Leds works ? The readfile command in GetPushbuttonState_btn_Click seem to be the culprit as the app hangs when this is called, once I changed the firmware and pc app to use 16 byte buffers.  Is there something magic about 64 byte ?
#3
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 09:27:52 (permalink)
0
Those you showed above,
so you indeed have another problem if you did change all of the other ones correctly.

Can you try re-setting them 64 just to be sure?

The PC software usually reads the descriptors to know about how large the buffers have to be...

GENOVA :D :D ! GODO
#4
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 12:22:27 (permalink)
0
DarioG
Those you showed above,
so you indeed have another problem if you did change all of the other ones correctly.

Can you try re-setting them 64 just to be sure?

Yes it's fine with 64 bytes.
What I did was change the usb_descriptors.c file ,and edit the two endpoint entries to size 16, the hid report descriptor report count to 16, and in the two event handlers on form1.h, altered the buffer sizes from 65 to 17 bytes for both read and write buffers. But as I posted previously Get Pushbutton State hangs ? I left the lines in usb_config.h alone (still showing ep size 3).



#5
chinzei
Super Member
  • Total Posts : 2250
  • Reward points : 0
  • Joined: 2003/11/07 12:39:02
  • Location: Tokyo, Japan
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 15:12:41 (permalink)
0
What I did was change the usb_descriptors.c file ,and edit the two endpoint entries to size 16, the hid report descriptor report count to 16, and in the two event handlers on form1.h, altered the buffer sizes from 65 to 17 bytes for both read and write buffers. But as I posted previously Get Pushbutton State hangs ?

You'll find many hard-coded "64" in main.c, too

(1), (2), (5) and (6) are just redundant, but harmless
(3) and (4) are lethal - These HIDTxPacket() sends 64 bytes transaction over 16-byte wMaxPacketSize endpoint.

C:\Microchip Solutions v2010-10-19\USB Device - HID - Custom Demos\Generic HID - Firmware\main.c

/** VARIABLES ******************************************************/
...
unsigned char ReceivedDataBuffer[64];                 // <-------------- (1)
unsigned char ToSendDataBuffer[64];                   // <-------------- (2)


void ProcessIO(void)
{  
    ...
    ...
    if(!HIDRxHandleBusy(USBOutHandle))              //Check if data was received from the host.
    {  
        switch(ReceivedDataBuffer[0])               //Look at the data the host sent, to see what kind of application specific command it sent.
        {
            ...
            ...
            case 0x81:  //Get push button state
                ToSendDataBuffer[0] = 0x81;             //Echo back to the host PC the command we are fulfilling in the first byte.  In this case, the Get Pushbutton State command.
                if(sw3 == 1)                            //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
                {
                    ToSendDataBuffer[1] = 0x01;        
                }
                else                                    //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
                {
                    ToSendDataBuffer[1] = 0x00;
                }
                if(!HIDTxHandleBusy(USBInHandle))
                {
                    USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);  // <-------------- (3)
                }
                break;

            case 0x37:  //Read POT command.  Uses ADC to measure an analog voltage on one of the ANxx I/O pins, and returns the result to the host
                {
                    WORD_VAL w;

                    if(!HIDTxHandleBusy(USBInHandle))
                    {
                        mInitPOT();
                        w = ReadPOT();                  //Use ADC to read the I/O pin voltage.  See the relevant HardwareProfile - xxxxx.h file for the I/O pin that it will measure.
                                                        //Some demo boards, like the PIC18F87J50 FS USB Plug-In Module board, do not have a potentiometer (when used stand alone).
                                                        //This function call will still measure the analog voltage on the I/O pin however.  To make the demo more interesting, it
                                                        //is suggested that an external adjustable analog voltage should be applied to this pin.
                        ToSendDataBuffer[0] = 0x37;     //Echo back to the host the command we are fulfilling in the first byte.  In this case, the Read POT (analog voltage) command.
                        ToSendDataBuffer[1] = w.v[0];   //Measured analog voltage LSB
                        ToSendDataBuffer[2] = w.v[1];   //Measured analog voltage MSB

                        USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64); // <-------------- (4)
                    }                  
                }
                break;
        }
        //Re-arm the OUT endpoint for the next packet
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);                 // <-------------- (5)




void USBCBInitEP(void)
{
    //enable the HID endpoint
    USBEnableEndpoint(HID_EP,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
    //Re-arm the OUT endpoint for the next packet
    USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);                     // <-------------- (6)
}


Tsuneo
post edited by chinzei - 2011/03/07 15:14:11
#6
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/07 15:32:27 (permalink)
0
Yeah, that's also true Smile

GENOVA :D :D ! GODO
#7
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/08 08:02:47 (permalink)
0
Of course, that was the problem ! Thanks for the pointer !

I realise also I think I have been in error in assuming the endpoint sizes and report size are one and the same, and I kept making the endpoints and the report count in the hid descriptor the same size. I can have an endpoint size of 16 bytes, but the report could be 20 bytes and would be sent in two transactions ?

One thing I am still confused about. The usb_config.h has settings
#define HID_INT_OUT_EP_SIZE     3
#define HID_INT_IN_EP_SIZE      3
which are used in usb_device.h to define the hid_report_out/hid_report_in buffer sizes, but these buffers seem not be used anywhere ? Is that correct ?
#8
chinzei
Super Member
  • Total Posts : 2250
  • Reward points : 0
  • Joined: 2003/11/07 12:39:02
  • Location: Tokyo, Japan
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/08 12:35:28 (permalink)
0

I can have an endpoint size of 16 bytes, but the report could be 20 bytes and would be sent in two transactions ?

Yes.
wMaxPacketSize field of endpoint descriptor defines division unit.
According to this value, long transfer (greater than wMaxPacketSize) is split into transactions of this size, and last short packet.

In this way, you may exchange large report greater than 64 bytes.

which are used in usb_device.h to define the hid_report_out/hid_report_in buffer sizes, but these buffers seem not be used anywhere ? Is that correct ?

That's right.

Tsuneo
#9
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/09 03:07:40 (permalink)
0
Thanks for the reply.

I had one question which is not about the example directly, but more about the different ways of using hid devices/reports ?
The microchip example uses a general purpose 64 byte in/out buffer, and basically you can pass whatever you like over that interface, as long as both the firmware and the pc app agree what the data content/format is.
An alternative strategy is to define reports actually detailing the content of the data, so it may have 4 off 2 bit fields, a couple of 10 bit values or whatever you need ? Additionally you can have multiple reports for different types of data.
In my case the usb device is always fitted in a closed system where we control the app software and the firmware, and the usb device is never going to be used by a 3rd party so is there any advantage/disadvantage in defining the hid report in either of the two styles ? I imagine keeping the report as a single report containing say 64 bytes is quicker to process/parse/update in the firmware than a complex report type, or multiple reports ?

#10
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/09 04:48:30 (permalink) ☄ Helpful
0
I do like this, to answer to the above:

    0x06, 0x00, 0xff,              // USAGE_PAGE (Vendor Defined Page 1)
    0x09, 0x01,                    // USAGE (Vendor Usage 1)
    0xa1, 0x01,                    // COLLECTION (Application)
                                   // -------- common global items ---------
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //   REPORT_SIZE (8)
                                   // -------- input report ---------
    0x85, 1, /*      Report ID (1)                                                */
    0x95, HID_INT_IN_EP_SIZE-1,                    //   REPORT_COUNT (64)
    0x19, 0x01,                    //   USAGE_MINIMUM (Vendor Usage 1)
    0x29, HID_INT_IN_EP_SIZE,                    //   USAGE_MAXIMUM (Vendor Usage 64)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
                                   // -------- output report ---------
    0x85, 2, /*      Report ID (2)                                                */
    0x95, HID_INT_OUT_EP_SIZE-1,                    //   REPORT_COUNT (64)
    0x19, 0x01,                    //   USAGE_MINIMUM (Vendor Usage 1)
    0x29, HID_INT_OUT_EP_SIZE,                    //   USAGE_MAXIMUM (Vendor Usage 64)
    0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
                                   // -------- feature report ---------
    0x85, 3, /*      Report ID (3)                                                */
    0x95, HID_FEATURE_REPORT_BYTES-1,                    //   REPORT_COUNT ()
    0x09, 0x01,                    //   USAGE (Vendor Usage 1)
    0xb1, 0x02,                    //   FEATURE (Data,Var,Abs)
 
    0xc0                           // END_COLLECTION




GENOVA :D :D ! GODO
#11
tonyb61
Starting Member
  • Total Posts : 51
  • Reward points : 0
  • Joined: 2010/08/26 04:16:23
  • Location: 0
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/11 10:10:47 (permalink)
0
Thank you Dario.
I guess you are suggesting the simple approach is good. I had a question about your example.
I coded it into the hid firmware & pc app examples, adding support for the report id for in and out reports (I hope) and it works fine if the IN and OUT endponts are both the same size. I changed the buffer sizes in usb_config.h/usb_descriptors.c/main.c, and added the report id into the buffer data sent by the pc app.
But I'm trying a case where the OUT report in 3 bytes long (EP is 4 bytes), and the IN report is 15 bytes long (EP is 16 bytes). The OUT report works fine, so I can toggle the LEDS, but the Get Pushbutton State button causes a hang in the pc app ?
#12
chinzei
Super Member
  • Total Posts : 2250
  • Reward points : 0
  • Joined: 2003/11/07 12:39:02
  • Location: Tokyo, Japan
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/11 18:22:35 (permalink)
0

For vendor specific HID devices, in which we implement the report parser on the host side, we don't need any detailed format on the report descriptor. Just the definition of byte size of each report is enough. In these cases, we apply fixed data structure directly to each raw report, to parse the report. No report variation. The report structures are shared with the device and the host application, by a common header file, documents or even your memory. We don't need to describe the detailed data structure using the report descriptor.

For existing HID devices, like mouse and keyboard, OS implements elaborated parser on the host side. The parser allows variation of the report data fields. For example, single keyboard parser allows both report descriptors of full keyboard and ten keyboard. Unless you are going to implement such variations and elaborated parser, detailed report format is just redundant.


but the Get Pushbutton State button causes a hang in the pc app ?

Do you see the input report on a software sniffer ?
If so, how about the report byte size, and its format (report ID + body)?
If you see expected reports on a sniffer, the problem lies in the PC application side. Otherwise, the device side.

Software sniffers

SnoopyPro (Open source) http://sourceforge.net/projects/usbsnoop/
SniffUsb (free) http://www.pcausa.com/Uti..es/UsbSnoop/default.htm

USBlyzer (commercial, 33 days trial) http://www.usblyzer.com/
USBTrace (commercial, 15 days trial) http://www.sysnucleus.com/

Tsuneo
post edited by chinzei - 2011/03/11 18:29:16
#13
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Microchip Custom HID Demo questions ? 2011/03/12 03:28:57 (permalink)
0
Yeah, I agree with Tsuneo.

And also about the "hanging", you first have to find out whether it's a PIC or Host issue, and then check for the usual - ID #, length...

GENOVA :D :D ! GODO
#14
Jump to:
© 2019 APG vNext Commercial Version 4.5