• AVR Freaks

Hot!Data exchange over USB HID class

Author
vineet
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2011/06/21 06:00:35
  • Location: 0
  • Status: offline
2011/06/21 06:08:08 (permalink)
0

Data exchange over USB HID class

Hello Group,
   I am new to Microchip products and I have a simple question. I am using 18F14K50 as HID class with the sample code:

code source taken from: "USB Device - HID - Custom Demos"

 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 0x80:  //Toggle LEDs command
                 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
                break;
        }
        //Re-arm the OUT endpoint for the next packet
        USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
    }

From the code snippet, I understand that the data comes in the ReceivedDataBuffer from the Host. But how can I get the length of the received buffer. My idea is to exchange variable length data between host and device.

Can anyone give pointers to this one.

Regards,
Vineet.
#1

7 Replies Related Threads

    newfound
    Super Member
    • Total Posts : 1819
    • Reward points : 0
    • Joined: 2003/11/07 12:35:49
    • Status: offline
    Re:Data exchange over USB HID class 2011/06/21 07:24:39 (permalink)
    0
    You cannot exchange variable length data over a HID interface. All transfers are based on the report count given in the HID REPORT descriptor. Of course you don't have to use all the data that is transferred, but the REPORT SIZE is fixed and that is how many bytes are exchanged per report.

    If you change it on the firmware then you also need to change it on the PC side too and vice-versa. For a generic HID device I always set it at the same size as the endpoint size. Usually 0x40 bytes.

    The line that determines the report count is highlighted below. Remember, it must match on the PC APP too.

    ROM struct{BYTE report[HID_RPT01_SIZE];}hid_rpt01={
    {
        0x06, 0x00, 0xFF,       // Usage Page = 0xFF00 (Vendor Defined Page 1)
        0x09, 0x01,             // Usage (Vendor Usage 1)
        0xA1, 0x01,             // Collection (Application)
        0x19, 0x01,             //      Usage Minimum
        0x29, 0x40,             //      Usage Maximum     //64 input usages total (0x01 to 0x40)
        0x15, 0x01,             //      Logical Minimum (data bytes in the report may have minimum value = 0x00)
        0x25, 0x40,                //      Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
        0x75, 0x08,             //      Report Size: 8-bit field size
        0x95, 0x40,             //      Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output", or "Feature" item)
        0x81, 0x00,             //      Input (Data, Array, Abs): Instantiates input packet fields based on the above report size, count, logical min/max, and usage.
        0x19, 0x01,             //      Usage Minimum
        0x29, 0x40,             //      Usage Maximum     //64 output usages total (0x01 to 0x40)
        0x91, 0x00,             //      Output (Data, Array, Abs): Instantiates output packet fields.  Uses same report size and count as "Input" fields, since nothing new/different was specified to the parser since the "Input" item.
        0xC0}                   // End Collection
    };
    #2
    vineet
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2011/06/21 06:00:35
    • Location: 0
    • Status: offline
    Re:Data exchange over USB HID class 2011/06/24 08:21:22 (permalink)
    0
    HI,
      Thank you for your help. I was able to detect my HID device. Now I want to try to do a loop back transfer between the PC and my device. How do I test my code for this. My report size is 64 bytes as you suggested at the moment. is there any app which can help me do that test?

    Regards,
    Vineet.

    #3
    newfound
    Super Member
    • Total Posts : 1819
    • Reward points : 0
    • Joined: 2003/11/07 12:35:49
    • Status: offline
    Re:Data exchange over USB HID class 2011/06/24 13:10:32 (permalink)
    0
    A good starting place for software and firmware is here:

    http://www.lvr.com/hidpage.htm
    #4
    EngineeringTechnician
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2011/07/29 06:53:15
    • Location: 0
    • Status: offline
    Re:Data exchange over USB HID class 2011/07/29 07:06:10 (permalink)
    0
    I'm quite new to C programming and USB. I don't know if I was supposed to start a new thread or not but I figured I would ask the question here since the code is displayed above.

    Why do they start the case switch statement at 0x80 in the firmware? I've search all over the internet for hours but couldn't find an answer.

    Why can't they use 0x00 for the first case and so forth?

    Thank you.
    #5
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:Data exchange over USB HID class 2011/07/29 14:13:47 (permalink)
    0
    I'd say it's just a choice, say for instance 0x00-0x7f commands from PIC to PC and 0x80-0xff viceversa.
    Of course you can do as you like.

    GENOVA :D :D ! GODO
    #6
    EmbeddedEngineer
    New Member
    • Total Posts : 26
    • Reward points : 0
    • Joined: 2016/02/24 06:56:35
    • Location: 0
    • Status: offline
    Re:Data exchange over USB HID class 2019/04/17 00:06:37 (permalink)
    0
    What is the data format in usb hid on controller receiving end? and how to extract desired data from that received buffer? and we have various data types, string, char,int etc how to treat them in usb hid communication? thanks
    #7
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re:Data exchange over USB HID class 2019/04/19 23:56:21 (permalink)
    0
    Please do NOT resurrect old topics. Start your own.
     

    Only 10 to go...
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5