Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC24 Composite USB HID Device setup

Status
Not open for further replies.

BallisticTech

New Member
Hello,
I'm working on a project using a PIC24F as a USB Joystick. The code was originally based on the Microchip MLA HID Joystick example. But the project requires being able to send the Joystick commands to change it's mode. As well as receive data back.

Based on research it seemed like setting it up as a Composite HID device would solve those issues. HID not require special drivers was one main reason. I've gotten the device to enumerate fine on my computers. But others seem to have problems with it enumerating incorrectly on other computers.

My setup is:
Device
-1 Configuration
--2 Interfaces
---Interface 1 - HID Joystick
----1 Endpoint
---Interface 2 - Custom HID
----2 Endpoints

I modified the usb_config.h, usb_device_hid.c, usb_descriptors.c and have an app_device_custom_hid.c file which handles the sending/receiving of custom HID commands. I'm hoping some one might be able to help identify if something is set up incorrectly. I've tried to provide as much info as I can. If I can post anything else please let me know

The usb_device_hid.c file was changed based on an example I was able to find here
https://microchipsupport.force.com/s/article/MLA---USB--Composite-Device-demo-with-HID-interfaces

It basically checks the interface ID in order to switch between reports from the Joystick and Custom HID interfaces. (I believe)
Code:
        switch(SetupPkt.bDescriptorType)
        {
            case DSC_HID: //HID Descriptor
                if(USBActiveConfiguration == 1)
                {
                    if (SetupPkt.bIntfID==JOYSTICK_HID_INTF_ID) //CUST_HID
                    {
                        USBEP0SendROMPtr(
                            (const uint8_t*)&configDescriptor1 + HID_OFFSET_JOYSTICK, //18 is a magic number. It is the offset from start of the configuration descriptor to the start of the HID descriptor.
                            sizeof(USB_HID_DSC)+3,
                            USB_EP0_INCLUDE_ZERO);
                    }
                    if (SetupPkt.bIntfID==CUSTOM_HID_INTF_ID) //HID MULTI-TOUCH
                    {
                        USBEP0SendROMPtr(
                            (const uint8_t*)&configDescriptor1 + HID_OFFSET_CUSTOM, //XX is a magic number. It is the offset from start of the configuration descriptor to the start of the HID descriptor.
                            sizeof(USB_HID_DSC)+3,
                            USB_EP0_INCLUDE_ZERO);
                    }
                }
                break;
            case DSC_RPT: //Report Descriptor
                if(USBActiveConfiguration == 1)
                {
                   // USBEP0SendROMPtr(
                   // (const uint8_t*)&hid_rpt01_custom,
                   // CUST_HID_RPT01_SIZE, //See usbcfg.h
                   // USB_EP0_INCLUDE_ZERO);
                   if (SetupPkt.bIntfID==JOYSTICK_HID_INTF_ID) //CUST_HID
                    {
                        USBEP0SendROMPtr(
                            (const uint8_t*)&joystick_hid_rpt01,
                            JOYSTICK_HID_RPT01_SIZE, //See usbcfg.h
                            USB_EP0_INCLUDE_ZERO);
                    }
                    if (SetupPkt.bIntfID==CUSTOM_HID_INTF_ID) //MULTI-TOUCH
                    {
                        USBEP0SendROMPtr(
                            (const uint8_t*)&custom_hid_rpt01,
                            CUSTOM_HID_RPT01_SIZE, //See usbcfg.h
                            USB_EP0_INCLUDE_ZERO);
                    }
                }
                break;

Here is what is in my USB Descriptors file:

Code:
/*********************************************************************
 * Descriptor specific type definitions are defined in:
 * usb_device.h
 *
 * Configuration options are defined in:
 * usb_config.h
 ********************************************************************/

/** INCLUDES *******************************************************/
#include "usb.h"
#include "usb_device_hid.h"

/** CONSTANTS ******************************************************/
#if defined(__18CXX)
#pragma romdata
#endif

/* Device Descriptor */
const USB_DEVICE_DESCRIPTOR device_dsc = {
    0x12, // Size of this descriptor in bytes
    USB_DESCRIPTOR_DEVICE, // DEVICE descriptor type
    0x0200, // USB Spec Release Number in BCD format
    0x00, // Class Code
    0x00, // Subclass code
    0x00, // Protocol code
    USB_EP0_BUFF_SIZE, // Max packet size for EP0, see usb_config.h
    MY_VID, // Vendor ID, see usb_config.h
    MY_PID, // Specific Product ID 0xEAD3 for "GSI Wheel"
    0x0001, // Device release number in BCD format
    0x01, // Manufacturer string index
    0x02, // Product string index
    0x00, // Device serial number string index
    0x01 // Number of possible configurations
};

/* Configuration 1 Descriptor */
const uint8_t configDescriptor1[] = {
    /* Configuration Descriptor */
    0x09, //sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
    USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
    TOTAL_CONFIG_SIZE,0x00, // Total length of data for this cfg
    2, // Number of interfaces in this cfg
    1, // Index value of this configuration
    0, // Configuration string index
    _DEFAULT | _SELF, // Attributes, see usb_device.h
    50, // Max power consumption (2X mA)

    /* Joystick Interface Descriptor */
    0x09, //sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
    USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type
    JOYSTICK_HID_INTF_ID, // Interface Number
    0, // Alternate Setting Number
    1, // Number of endpoints in this intf
    HID_INTF, // Class code
    0, // Subclass code
    0, // Protocol code
    3, // Interface string index

    /* Joystick HID Class-Specific Descriptor */
    0x09, //sizeof(USB_HID_DSC)+3, // Size of this descriptor in bytes RRoj hack
    DSC_HID, // HID descriptor type
    0x11,0x01, // HID Spec Release Number in BCD format (1.11)
    0x00, // Country Code (0x00 for Not supported)
    JOYSTICK_HID_NUM_OF_DSC, // Number of class descriptors, see usbcfg.h
    DSC_RPT, // Report descriptor type
    JOYSTICK_HID_RPT01_SIZE,0x00, //sizeof(hid_rpt01), // Size of the report descriptor

    /* Joystick Endpoint Descriptor */
    0x07, /*sizeof(USB_EP_DSC)*/
    USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
    JOYSTICK_EP | _EP_IN, //EndpointAddress
    _INTERRUPT, //Attributes
    JOYSTICK_HID_INT_IN_EP_SIZE,0x00, //size
    0x01, //Interval

    /* Custom Interface Descriptor */
    0x09, //sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
    USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type
    CUSTOM_HID_INTF_ID, // Interface Number
    0, // Alternate Setting Number
    2, // Number of endpoints in this intf
    HID_INTF, // Class code
    0, // Subclass code
    0, // Protocol code
    4,

    /* Custom HID Class-Specific Descriptor */
    0x09, //sizeof(USB_HID_DSC)+3, // Size of this descriptor in bytes
    DSC_HID, // HID descriptor type
    0x11,0x01, // HID Spec Release Number in BCD format (1.11)
    0x00, // Country Code (0x00 for Not supported)
    CUSTOM_HID_NUM_OF_DSC, // Number of class descriptors, see usbcfg.h
    DSC_RPT, // Report descriptor type
    CUSTOM_HID_RPT01_SIZE,0x00, //sizeof(hid_rpt01), // Size of the report descriptor

    /* Custom Endpoint IN Descriptor */
    0x07, /*sizeof(USB_EP_DSC)*/
    USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
    CUSTOM_DEVICE_HID_EP | _EP_IN, //EndpointAddress
    _INTERRUPT, //Attributes
    CUSTOM_INT_IN_EP_SIZE,0x00, //size
    0x01, //Interval

    /* Custom Endpoint IN Descriptor */
    0x07, /*sizeof(USB_EP_DSC)*/
    USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
    CUSTOM_DEVICE_HID_EP | _EP_OUT, //EndpointAddress
    _INTERRUPT, //Attributes
    CUSTOM_INT_OUT_EP_SIZE,0x00, //size
    0x01, //Interval
};


//Language code string descriptor

const struct {
    uint8_t bLength;
    uint8_t bDscType;
    uint16_t string[1];
}

sd000 = {
    sizeof (sd000), USB_DESCRIPTOR_STRING, {
        0x0409
    }
};

//Manufacturer string descriptor

const struct {
    uint8_t bLength;
    uint8_t bDscType;
    uint16_t string[25];
}

sd001 = {
    sizeof (sd001), USB_DESCRIPTOR_STRING, {
        'M', 'i', 'c', 'r', 'o', 'c', 'h', 'i', 'p', ' ',
        'T', 'e', 'c', 'h', 'n', 'o', 'l', 'o', 'g', 'y', ' ', 'I', 'n', 'c', '.'
    }
};

//Product string descriptor

const struct {
    uint8_t bLength;
    uint8_t bDscType;
    uint16_t string[18];
}

sd002 = {
    sizeof (sd002), USB_DESCRIPTOR_STRING, {
        'G', 'S', 'I', ' ', 'S', 't', 'e', 'e', 'r', 'i', 'n', 'g', ' ', 'W', 'h', 'e', 'e', 'l'
    }
};

//Interface 1 Joystick string descriptor

const struct {
    uint8_t bLength;
    uint8_t bDscType;
    uint16_t string[12];
}

sd003 = {
    sizeof (sd003), USB_DESCRIPTOR_STRING, {
        'G', 'S', 'I', ' ', 'J', 'o', 'y', 's', 't', 'i', 'c', 'k'
    }
};
//Interface 2 Comms string descriptor

const struct {
    uint8_t bLength;
    uint8_t bDscType;
    uint16_t string[12];
}

sd004 = {
    sizeof (sd004), USB_DESCRIPTOR_STRING, {
        'G', 'S', 'I', ' ', 'C', 'o', 'm', 'm', 'a', 'n', 'd', 's'
    }
};

//Array of configuration descriptors
const uint8_t * const USB_CD_Ptr[] = {
    (const uint8_t * const) &configDescriptor1
};

//Array of string descriptors
const uint8_t * const USB_SD_Ptr[] = {
    (const uint8_t * const) &sd000,
    (const uint8_t * const) &sd001,
    (const uint8_t * const) &sd002,
    (const uint8_t * const) &sd003,
    (const uint8_t * const) &sd004
};

const struct {
    uint8_t report[JOYSTICK_HID_RPT01_SIZE];
} joystick_hid_rpt01 = {
    {
        0x05, 0x01, //USAGE_PAGE (Generic Desktop)
        0x09, 0x05, //USAGE (Game Pad)
        0xA1, 0x01, //COLLECTION (Application)

        0x15, 0x00, // LOGICAL_MINIMUM(0)
        0x25, 0x01, // LOGICAL_MAXIMUM(1)
        0x35, 0x00, // PHYSICAL_MINIMUM(0)
        0x45, 0x01, // PHYSICAL_MAXIMUM(1)
        0x75, 0x01, // REPORT_SIZE(1)
        0x95, 30 /*0x0D*/, // REPORT_COUNT(13) 
        0x05, 0x09, // USAGE_PAGE(Button)
        0x19, 0x01, // USAGE_MINIMUM(Button 1)
        0x29, 30, // USAGE_MAXIMUM(Button 13)
        0x81, 0x02, // INPUT(Data,Var,Abs)
        0x95, 2, // REPORT_COUNT(3) Hier muss am Ende Mod 8 = 0 ergeben
        0x81, 0x01, // INPUT(Cnst,Ary,Abs)

        0x05, 0x01, // USAGE_PAGE(Generic Desktop)
        0x25, 0x07, // LOGICAL_MAXIMUM(7)
        0x46, 0x3B, 0x01, // PHYSICAL_MAXIMUM(315)
        0x75, 0x04, // REPORT_SIZE(4)
        0x95, 0x01, // REPORT_COUNT(1)
        0x65, 0x14, // UNIT(Eng Rot:Angular Pos)
        0x09, 0x39, // USAGE(Hat Switch)
        0x81, 0x42, // INPUT(Data,Var,Abs,Null)
        0x65, 0x00, // UNIT(None)
        0x95, 0x01, // REPORT_COUNT(1)
        0x81, 0x01, // INPUT(Cnst,Ary,Abs)

        0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM(255)
        0x46, 0xFF, 0x00, // PHYSICAL_MAXIMUM(255)
        0x09, 0x30, // USAGE(X)
        0x09, 0x31, // USAGE(Y)
        0x09, 0x32, // USAGE(Z)
        0x09, 0x35, // USAGE(Rz)
        0x75, 0x08, // REPORT_SIZE(8)
        0x95, 4, // REPORT_COUNT(2)
        0x81, 0x02, // INPUT(Data,Var,Abs)
        0xC0 //END_COLLECTION
    }
};

//Class specific descriptor - HID 

const struct {
    uint8_t report[CUSTOM_HID_RPT01_SIZE];
} custom_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, 0x00, // Logical Minimum (data bytes in the report may have minimum value = 0x00)
        0x26, 0xFF, 0x00, // 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
};
/** EOF usb_descriptors.c ***************************************************/

And finally the USB Config file
Code:
/*********************************************************************
 * Descriptor specific type definitions are defined in: usbd.h
 ********************************************************************/

#ifndef USBCFG_H
#define USBCFG_H

#include <usb_ch9.h>

/** DEFINITIONS ****************************************************/
#define USB_EP0_BUFF_SIZE 8 // Valid Options: 8, 16, 32, or 64 bytes.
        // Using larger options take more SRAM, but
        // does not provide much advantage in most types
        // of applications. Exceptions to this, are applications
        // that use EP0 IN or OUT for sending large amounts of
        // application related data.
         
#define USB_MAX_NUM_INT 2 //Set this number to match the maximum interface number used in the descriptors for this firmware project
#define USB_MAX_EP_NUMBER 2 //Set this number to match the maximum endpoint number used in the descriptors for this firmware project

//Device descriptor - if these two definitions are not defined then
// a const USB_DEVICE_DESCRIPTOR variable by the exact name of device_dsc
// must exist.
#define USB_USER_DEVICE_DESCRIPTOR &device_dsc
#define USB_USER_DEVICE_DESCRIPTOR_INCLUDE extern const USB_DEVICE_DESCRIPTOR device_dsc

//Configuration descriptors - if these two definitions do not exist then
// a const BYTE *const variable named exactly USB_CD_Ptr[] must exist.
#define USB_USER_CONFIG_DESCRIPTOR USB_CD_Ptr
#define USB_USER_CONFIG_DESCRIPTOR_INCLUDE extern const uint8_t *const USB_CD_Ptr[]

//#define USB_PING_PONG_MODE USB_PING_PONG__NO_PING_PONG //Not recommended
#define USB_PING_PONG_MODE USB_PING_PONG__FULL_PING_PONG //A good all around setting
//#define USB_PING_PONG_MODE USB_PING_PONG__EP0_OUT_ONLY //Another good setting
//#define USB_PING_PONG_MODE USB_PING_PONG__ALL_BUT_EP0 //Not recommended

#define USB_POLLING
//#define USB_INTERRUPT

#define USB_PULLUP_OPTION USB_PULLUP_ENABLE
//#define USB_PULLUP_OPTION USB_PULLUP_DISABLED

#define USB_TRANSCEIVER_OPTION USB_INTERNAL_TRANSCEIVER
//External Transceiver support is not available on all product families. Please
// refer to the product family datasheet for more information if this feature
// is available on the target processor.
//#define USB_TRANSCEIVER_OPTION USB_EXTERNAL_TRANSCEIVER

#define USB_SPEED_OPTION USB_FULL_SPEED
//#define USB_SPEED_OPTION USB_LOW_SPEED //(this mode is only supported on some microcontrollers)

#define USB_ENABLE_STATUS_STAGE_TIMEOUTS //Comment this out to disable this feature.

#define USB_STATUS_STAGE_TIMEOUT (uint8_t)45 //Approximate timeout in milliseconds, except when
                                                //USB_POLLING mode is used, and USBDeviceTasks() is called at < 1kHz
                                                //In this special case, the timeout becomes approximately:
//Timeout(in milliseconds) = ((1000 * (USB_STATUS_STAGE_TIMEOUT - 1)) / (USBDeviceTasks() polling frequency in Hz))
//------------------------------------------------------------------------------------------------------------------

#define USB_SUPPORT_DEVICE

#define USB_NUM_STRING_DESCRIPTORS 5 //Set this number to match the total number of string descriptors that are implemented in the usb_descriptors.c file

//#define USB_DISABLE_SUSPEND_HANDLER
//#define USB_DISABLE_WAKEUP_FROM_SUSPEND_HANDLER
//#define USB_DISABLE_SOF_HANDLER
//#define USB_DISABLE_TRANSFER_TERMINATED_HANDLER
//#define USB_DISABLE_ERROR_HANDLER 
//#define USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER 
//#define USB_DISABLE_SET_DESCRIPTOR_HANDLER 
//#define USB_DISABLE_SET_CONFIGURATION_HANDLER
//#define USB_DISABLE_TRANSFER_COMPLETE_HANDLER 

/** DEVICE CLASS USAGE *********************************************/
#define USB_USE_HID

/** ENDPOINTS ALLOCATION *******************************************/
#define MY_VID 0x04D8
#define MY_PID 0xEAD3

/* HID */
#define JOYSTICK_HID_INTF_ID 0x00
#define JOYSTICK_EP 1
#define JOYSTICK_HID_INT_OUT_EP_SIZE 64
#define JOYSTICK_HID_INT_IN_EP_SIZE 64
#define JOYSTICK_HID_NUM_OF_DSC 1
#define JOYSTICK_HID_RPT01_SIZE 74

/* HID */
#define CUSTOM_HID_INTF_ID 0x01
#define CUSTOM_DEVICE_HID_EP 2
#define CUSTOM_INT_OUT_EP_SIZE 64
#define CUSTOM_INT_IN_EP_SIZE 64
#define CUSTOM_HID_NUM_OF_DSC 1
#define CUSTOM_HID_RPT01_SIZE 29

#define JOYSTICK_CONFIG_SIZE ( (2*9) + (7*1) )
#define CUSTOM_CONFIG_SIZE ( (2*9) + (7*2) )

#define TOTAL_CONFIG_SIZE (9 + CUSTOM_CONFIG_SIZE + JOYSTICK_CONFIG_SIZE ) // sizeof(configDescriptor1[])

#define HID_OFFSET_JOYSTICK ( 9 + 9 )
#define HID_OFFSET_CUSTOM ( 9 + JOYSTICK_CONFIG_SIZE + 9 )

/** DEFINITIONS ****************************************************/
//Report ID Definitions.
#define JOYSTICK_REPORT_ID (uint8_t)0x01
#define CUSTOM_REPORT_ID (uint8_t)0x02

#endif //USBCFG_H
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top