USB Keyboard with PIC18F4550

Thread Starter

Dalaran

Joined Dec 3, 2009
168
I have been working on for some time a USB keyboard with the PIC18F4550. I am using MikroC PRO for PIC and have started working with their example of HID write that they have. Using this example I can open the HID terminal in mikroC and see the device detect and the data being written. I followed the steps here:
http://helmpcb.com/electronics/usb-joystick

If I use the joystick.hid from the HID descriptor tool and follow the other steps it gets detected in the HID terminal of mikroC. However, when I use the keybrd.hid from the HID descriptor and follow the same method it does not detect in the HID terminal. The PC makes the sound to indicate that something new has been detected, but nothing shows up on the HID terminal.

Here is my descriptor code:
Rich (BB code):
//******************************************************************************
//
// File Version 1.01
//
//******************************************************************************

#include "Definit.h"
#include "VARs.h"

//******************************************************************************
// The number of bytes in each report,
// calculated from Report Size and Report Count in the report descriptor
//******************************************************************************
unsigned char const HID_INPUT_REPORT_BYTES      = 1;
unsigned char const HID_OUTPUT_REPORT_BYTES     = 1;

unsigned char const HID_FEATURE_REPORT_BYTES    = 2;
//******************************************************************************
// Byte constants
//******************************************************************************
unsigned char const NUM_ENDPOINTS               = 2;
unsigned char const ConfigDescr_wTotalLength    = USB_CONFIG_DESCRIPTOR_LEN + USB_INTERF_DESCRIPTOR_LEN + USB_HID_DESCRIPTOR_LEN + (NUM_ENDPOINTS * USB_ENDP_DESCRIPTOR_LEN);
unsigned char const HID_ReportDesc_len          = 63;

unsigned char const Low_HID_ReportDesc_len      = HID_ReportDesc_len;
unsigned char const High_HID_ReportDesc_len     = HID_ReportDesc_len >> 8;

unsigned char const Low_HID_PACKET_SIZE         = HID_PACKET_SIZE;
unsigned char const High_HID_PACKET_SIZE        = HID_PACKET_SIZE >> 8;

// Descriptor Tables

unsigned char const DescTables[] = {

// Device Descriptor
    USB_DEVICE_DESCRIPTOR_LEN, 0,           // bLength               - Length of Device descriptor (always 0x12)
    USB_DEVICE_DESCRIPTOR_TYPE, 0,          // bDescriptorType       - 1 = DEVICE descriptor
    0x00, 0,                                // bcdUSB                - USB revision 2.00 (low byte)
    0x02, 0,                                //                                           (high byte)
    0x00, 0,                                // bDeviceClass          - Zero means each interface operates independently (class code in the interface descriptor)
    0x00, 0,                                // bDeviceSubClass
    0x00, 0,                                // bDeviceProtocol
    EP0_PACKET_SIZE, 0,                     // bMaxPacketSize0       - maximum size of a data packet for a control transfer over EP0
    0x34, 0,                                // idVendor              - Vendor  ID (low byte)
    0x12, 0,                                //                                    (high byte)
    0x01, 0,                                // idProduct             - Product ID (low byte)
    0x00, 0,                                //                                    (high byte)
    0x01, 0,                                // bcdDevice             - ( low byte)
    0x00, 0,                                //                         (high byte)
    0x01, 0,                                // iManufacturer         - String1
    0x02, 0,                                // iProduct              - String2
    0x00, 0,                                // iSerialNumber         - ( None )
    0x01, 0,                                // bNumConfigurations    - 1

// Configuration Descriptor
    USB_CONFIG_DESCRIPTOR_LEN, 0,           // bLength               - Length of Configuration descriptor (always 0x09)
    USB_CONFIG_DESCRIPTOR_TYPE, 0,          // bDescriptorType       - 2 = CONFIGURATION descriptor
    ConfigDescr_wTotalLength, 0,            // wTotalLength          - Total length of this config. descriptor plus the interface and endpoint descriptors that are part of the configuration.
    0x00, 0,                                //                         ( high byte)
    0x01, 0,                                // bNumInterfaces        - Number of interfaces
    0x01, 0,                                // bConfigurationValue   - Configuration Value
    0x00, 0,                                // iConfiguration        - String Index for this configuration ( None )
    0xA0, 0,                                // bmAttributes          - attributes - "Bus powered" and "Remote wakeup"
    50, 0,                                  // MaxPower              - bus-powered draws 50*2 mA from the bus.

// Interface Descriptor
    USB_INTERF_DESCRIPTOR_LEN, 0,           // bLength               - Length of Interface descriptor (always 0x09)
    USB_INTERFACE_DESCRIPTOR_TYPE, 0,       // bDescriptorType       - 4 = INTERFACE descriptor
    0x00, 0,                                // bInterfaceNumber      - Number of interface, 0 based array
    0x00, 0,                                // bAlternateSetting     - Alternate setting
    NUM_ENDPOINTS, 0,                       // bNumEndPoints         - Number of endpoints used in this interface
    0x03, 0,                                // bInterfaceClass       - assigned by the USB
    0x00, 0,                                // bInterfaceSubClass    - Not A boot device
    0x00, 0,                                // bInterfaceProtocol    - none
    0x00, 0,                                // iInterface            - Index to string descriptor that describes this interface ( None )

// HID Descriptor
    USB_HID_DESCRIPTOR_LEN, 0,              // bLength               - Length of HID descriptor (always 0x09)
    USB_HID_DESCRIPTOR_TYPE, 0,             // bDescriptorType       - 0x21 = HID descriptor
    0x01, 0,                                // HID class release number (1.01)
    0x01, 0,
    0x00, 0,                                // Localized country code (none)
    0x01, 0,                                // # of HID class descriptor to follow (1)
    0x22, 0,                                // Report descriptor type (HID)
    Low_HID_ReportDesc_len, 0,
    High_HID_ReportDesc_len, 0,

// EP1_RX Descriptor
    USB_ENDP_DESCRIPTOR_LEN, 0,             // bLength               - length of descriptor (always 0x07)
    USB_ENDPOINT_DESCRIPTOR_TYPE, 0,        // bDescriptorType       - 5 = ENDPOINT descriptor
    0x81, 0,                                // bEndpointAddress      - In, EP1
    USB_ENDPOINT_TYPE_INTERRUPT, 0,         // bmAttributes          - Endpoint Type - Interrupt
    Low_HID_PACKET_SIZE, 0,                 // wMaxPacketSize        - max packet size - low order byte
    High_HID_PACKET_SIZE, 0,                //                       - max packet size - high order byte
    1, 0,                                  // bInterval             - polling interval (1 ms)

// EP1_TX Descriptor
    USB_ENDP_DESCRIPTOR_LEN, 0,             // bLength               - length of descriptor (always 0x07)
    USB_ENDPOINT_DESCRIPTOR_TYPE, 0,        // bDescriptorType       - 5 = ENDPOINT descriptor
    0x01, 0,                                // bEndpointAddress      - Out, EP1
    USB_ENDPOINT_TYPE_INTERRUPT, 0,         // bmAttributes          - Endpoint Type - Interrupt
    Low_HID_PACKET_SIZE, 0,                 // wMaxPacketSize        - max packet size - low order byte
    High_HID_PACKET_SIZE, 0,                //                       - max packet size - high order byte
    1, 0,                                  // bInterval             - polling interval (1 ms)
Any ideas what is happening differently between the joystick and keyboard? Any help to get this detect is appreciated. I know this is just the first step, but a major one to get going on my pic keyboard emulator.

(code too long and continuing on next post)

Thanks!
 

Thread Starter

Dalaran

Joined Dec 3, 2009
168
continuation of code

Rich (BB code):
//Keyboard descriptor
    0x05, 0,
    0x01, 0,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0,
    0x06, 0,                    // USAGE (Keyboard)
    0xa1, 0,
    0x01, 0,                    // COLLECTION (Application)
    0x05, 0,
    0x07, 0,                    //   USAGE_PAGE (Keyboard)
    0x19, 0,
    0xe0, 0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
    0x29, 0,
    0xe7, 0,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
    0x15, 0,
    0x00, 0,                    //   LOGICAL_MINIMUM (0)
    0x25, 0,
    0x01, 0,                    //   LOGICAL_MAXIMUM (1)
    0x75, 0,
    0x01, 0,                    //   REPORT_SIZE (1)
    0x95, 0,
    0x08, 0,                    //   REPORT_COUNT (8)
    0x81, 0,
    0x02, 0,                    //   INPUT (Data,Var,Abs)
    0x95, 0,
    0x01, 0,                    //   REPORT_COUNT (1)
    0x75, 0,
    0x08, 0,                    //   REPORT_SIZE (8)
    0x81, 0,
    0x03, 0,                    //   INPUT (Cnst,Var,Abs)
    0x95, 0,
    0x05, 0,                    //   REPORT_COUNT (5)
    0x75, 0,
    0x01, 0,                    //   REPORT_SIZE (1)
    0x05, 0,
    0x08, 0,                    //   USAGE_PAGE (LEDs)
    0x19, 0,
    0x01, 0,                    //   USAGE_MINIMUM (Num Lock)
    0x29, 0,
    0x05, 0,                    //   USAGE_MAXIMUM (Kana)
    0x91, 0,
    0x02, 0,                    //   OUTPUT (Data,Var,Abs)
    0x95, 0,
    0x01, 0,                    //   REPORT_COUNT (1)
    0x75, 0,
    0x03, 0,                    //   REPORT_SIZE (3)
    0x91, 0,
    0x03, 0,                    //   OUTPUT (Cnst,Var,Abs)
    0x95, 0,
    0x06, 0,                    //   REPORT_COUNT (6)
    0x75, 0,
    0x08, 0,                    //   REPORT_SIZE (8)
    0x15, 0,
    0x00, 0,                    //   LOGICAL_MINIMUM (0)
    0x25, 0,
    0x65, 0,                    //   LOGICAL_MAXIMUM (101)
    0x05, 0,
    0x07, 0,                    //   USAGE_PAGE (Keyboard)
    0x19, 0,
    0x00, 0,                    //   USAGE_MINIMUM (Reserved (no event indicated))
    0x29, 0,
    0x65, 0,                    //   USAGE_MAXIMUM (Keyboard Application)
    0x81, 0,
    0x00, 0,                    //   INPUT (Data,Ary,Abs)
    0xc0, 0,                           // END_COLLECTION
};
//******************************************************************************
unsigned char const LangIDDescr[8] = {
    0x04, 0,
    USB_STRING_DESCRIPTOR_TYPE, 0,
    0x09, 0,                                // LangID (0x0409) - Low
    0x04, 0                                 //                 - High
};
//******************************************************************************
unsigned char const ManufacturerDescr[68] = {
    34, 0,
    USB_STRING_DESCRIPTOR_TYPE, 0,
    'B', 0, 0, 0,
    'u', 0, 0, 0,
    'c', 0, 0, 0,
    'h', 0, 0, 0,
    'y', 0, 0, 0,
    's', 0, 0, 0,
    ' ', 0, 0, 0,
    'S', 0, 0, 0,
    'o', 0, 0, 0,
    'l', 0, 0, 0,
    'u', 0, 0, 0,
    't', 0, 0, 0,
    'i', 0, 0, 0,
    'o', 0, 0, 0,
    'n', 0, 0, 0,
    's', 0, 0, 0
};
//******************************************************************************
unsigned char const ProductDescr[100] = {
    50, 0,
    USB_STRING_DESCRIPTOR_TYPE, 0,
    'B', 0, 0, 0,
    'u', 0, 0, 0,
    'c', 0, 0, 0,
    'h', 0, 0, 0,
    'y', 0, 0, 0,
    's', 0, 0, 0,
    ' ', 0, 0, 0,
    'A', 0, 0, 0,
    'r', 0, 0, 0,
    'c', 0, 0, 0,
    'a', 0, 0, 0,
    'd', 0, 0, 0,
    'e', 0, 0, 0,
    ' ', 0, 0, 0,
    'C', 0, 0, 0,
    'o', 0, 0, 0,
    'n', 0, 0, 0,
    't', 0, 0, 0,
    'r', 0, 0, 0,
    'o', 0, 0, 0,
    'l', 0, 0, 0,
    'l', 0, 0, 0,
    'e', 0, 0, 0,
    'r', 0, 0, 0
};

unsigned char const StrUnknownDescr[4] = {
    2, 0,
    USB_STRING_DESCRIPTOR_TYPE, 0
};
 

ErnieM

Joined Apr 24, 2011
8,377
I could not follow what two methods you used, but generally...

The Vendor ID and Product ID (VID and PID) need to match for any device to be recognized by a windows program. That is how windows identifies the actual hardware out there.
 

Thread Starter

Dalaran

Joined Dec 3, 2009
168
I was able to get my keyboard working in windows by modifying the descriptor file as well as my USB write sequence to the PC. I can now push buttons and have my coded key show up in notepad or something similar.

I was originally intending this to be used so that I can convert button strokes on an arcade machine to register in MAME. These key strokes still work in DOS and other Windows applications, but for some reason they do not register in MAME. My regular keyboard does, but my PIC created one does not.

Anyone with ideas why I can not see this in MAME but can in all other apps I've tried?

Thanks.
 

rasyoung

Joined Apr 27, 2011
1
Sir,

Do you have the exact code for this solution, I am struggling with same problem ( I need to able to HID USB keystroke with MicroC)?

I would like to see it please.

Thanks.
 
Top