Creating USB Descripter, parsing offsets for bytes?

Status
Not open for further replies.

buznee

New member
Hello everyone,

I apologize I am a bit of a newbee to microcontrollers and coding stuff. I am in the process of creating a joystick interface with the teensy board. I would like to create my own customized USB descripter that uses 2 16 bit axis controllers and 32 buttons. I have followed hamaluik's tutorial for doing just this (located here http://hamaluik.com/posts/making-a-custom-teensy3-hid-joystick/) but I am hung up on the portion regarding byte count that is supposed to be inserted in the usb_desc.h file.

#define ARCADE_DESC_OFFSET (9)
#define CONFIG_DESC_SIZE (9 + 9+9+7)


I don't understand how you come up with 9 and 9 + 9+9+7

He describes it as "The remaining parameters all relate to sizes of the data fields that describe the USB device. Nothing to do here but count bytes in the usb_desc.c file."

How do I get those values from this...

static uint8_t arcade_report_desc[] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x04, // USAGE (Joystick)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x04, // USAGE (Joystick)
0xa1, 0x00, // COLLECTION (Physical)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x33, // USAGE (Rx)
0x09, 0x34, // USAGE (Ry)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x04, // REPORT_COUNT (4)
0x45, 0x7f, // PHYSICAL_MAXIMUM (127)
0x35, 0x81, // PHYSICAL_MINIMUM (-127)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x10, // USAGE_MAXIMUM (Button 16)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x10, // REPORT_COUNT (16)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
};



This is his example. I was thinking his was a little simpler so if someone can explain how to do it with his I can then figure mine out.

Oh yeah and one more question. Does the arduino software always recognize the arduino/teensy board when you plug it in regardless of the USB descripter that has been uploaded to it? or does a part of it define that? I'm just worried that if I get rid of the wrong thing I might not be able to upload anything else to it because the software will not recognize it.
 
Hello everyone,

I apologize I am a bit of a newbee to microcontrollers and coding stuff. I am in the process of creating a joystick interface with the teensy board. I would like to create my own customized USB descripter that uses 2 16 bit axis controllers and 32 buttons. I have followed hamaluik's tutorial for doing just this (located here http://hamaluik.com/posts/making-a-custom-teensy3-hid-joystick/) but I am hung up on the portion regarding byte count that is supposed to be inserted in the usb_desc.h file.

#define ARCADE_DESC_OFFSET (9)
#define CONFIG_DESC_SIZE (9 + 9+9+7)


I don't understand how you come up with 9 and 9 + 9+9+7

This is my understanding from playing with this code for a few days. First, don't look at "arcade_report_desc". What you want are offsets into the "config_descriptor" array in usb_desc.c. This array is divided in "blocks" given by the "bLength" item. Depending on what items you have defined, the array includes or excludes various blocks. Your offset points to the "block" with bDescriptorType of 0x21.

For an example, consider if you have USB_HID defined. That includes KEYBOARD_INTERFACE, MOUSE_INTERFACE, etc.

Code:
  #define KEYBOARD_DESC_OFFSET  (9 + 9)
  #define MOUSE_DESC_OFFSET     (9 + 9+9+7 + 9)

Looking back at usb_dev.c

Code:
static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
        // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
        9,                                      // bLength;
        2,                                      // bDescriptorType;
        LSB(CONFIG_DESC_SIZE),                 // wTotalLength

We have the first "9" bytes in this first block. Subsequent blocks are not included because they are removed by "#ifdef" until we get to "#ifdef KEYBOARD_INTERFACE"

Code:
#ifdef KEYBOARD_INTERFACE
        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
        9,                                      // bLength
        4,                                      // bDescriptorType
        KEYBOARD_INTERFACE,                     // bInterfaceNumber
        0,                                      // bAlternateSetting
        1,                                      // bNumEndpoints
        0x03,                                   // bInterfaceClass (0x03 = HID)
        0x01,                                   // bInterfaceSubClass (0x01 = Boot)
        0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
        0,                                      // iInterface
        // HID interface descriptor, HID 1.11 spec, section 6.2.1
        9,                                      // bLength
        0x21,                                   // bDescriptorType
        0x11, 0x01,                             // bcdHID
        0,                                      // bCountryCode

The next block also has 9 bytes, but we don't want it. We want to point to the next one, which has bDescriptorType of 0x21. So, thus the offset is 9+9.

For MOUSE_DESC_OFFSET, we skip another 9 bytes for the next block in the keyboard section, then 7 for the last block in the keyboard section. Then we get to "#ifdef MOUSE_INTERFACE". Skip the first block, another 9 bytes, and we point to the block we want. So for its offset we skip 9+9+9+7+9.
 
Thanks so much! That definitely helped get me going in the right direction. One step closer to getting my project to be a reality.

Any thoughts regarding the last equation I had?

"Does the arduino software always recognize the arduino/teensy board when you plug it in regardless of the USB descripter that has been uploaded to it? or does a part of it define that? I'm just worried that if I get rid of the wrong thing I might not be able to upload anything else to it because the software will not recognize it. "
 
Status
Not open for further replies.
Back
Top