Demonstration of a PIC24F/PIC32MX Driver supporting USB/HID devices through an USB Hub 2013.11.20 2014.1.13



    Following the demonstration of a PIC24F/PIC32MX Driver for a USB Host module supporting an USB Hub, I prepared a new one supporting two or more HID devices (e.g. mouse, keyboard, joystick...) connected to the micro-controller via an USB Hub.

    SBDBT32 Micom board

    In this project, I also used the 'SBDBT32' Micom board sold by Running Electronics Company.

    The USB Hub has 4 ports and is connected to the Micom board.

    Furthermore, this Micom board is connected to the PC by an RS-232 Serial port. On the PC, a terminal emulation software (e.g. Hyper Terminal, Tera Term etc.) is started at 115.2Kbps. This is what is called hereafter the 'PC Console'.

    2. HOW TO USE

    2-1. Setting up the demonstration system

    Start by downloading the project file shown below. Then compile it on MPLAB IDE. Load the compiled binary into the 'SBDBT32' Micom board using the Microchip PicKit3.

    The main source files of this demonstration system are the following:

      Source FileComment
      main.cDemonstration program
      usb_config.cUSB configuration
      usb_host.cUSB Host driver which includes support for the USB Hub
      usb_host_hid.cUSB HID driver
      (support multiple devices)
      usb_host_hid_device.cHID device (mouse,keyboard,joystick) driver

    2-2. Start the demonstration system and devices connection

    2 mices and 1 keyboard are connected 2 mices and 2 joysticks are connected

    First, 'SBDBT32' (within which demonstration system was loaded) is connected to a PC. When +5V power is connected, demonstration system starts and the following message is displayed on the PC Console.

      *** Demonstration program Menu *********************
      m:Mouse read test.
      k:Keyboard read test.
      j:Joystick read test.

    Demonstration system is then ready for operation.
    Next, a USB Hub is connected to the USB port of 'SBDBT32'. Nothing is displayed when only connecting the Hub.
    If a mouse is connected to the port '1' of the Hub, the following messages are displayed on the PC Console:

      mouse attached.

    If the mouse is removed and is switched to the port '4' of the Hub, the following messages are displayed on the PC Console:

      mouse detached.

      mouse attached.

    If a another mouse is connected to the port '1' of the USB Hub, a keyboard is connected to the port '3' and a joystick is connected to the port '2', the following messages are displayed:

      mouse attached.

      keyboard attached.

      joystick attached.

    2-3. Operation of the demonstration system

    A menu is displayed whenever you press the 'Enter' key at the PC console:

      *** Demonstration program Menu *********************
      m:Mouse read test.
      k:Keyboard read test.
      j:Joystick read test.

    If 'm' is pressed at the PC Console, the mouse can be tested.
    if 'k' is pressed, the USB keyboard can be tested.

    2-3-1. 'm' : Mouse test

    The state of mouse is displayed on the PC Console screen during the mouse test.

    If the mouse moves or a button of the mouse is pressed or released, the following messages are displayed on the PC Console:
    (Press any key at the PC Console to return to the operation menu)

      Start mouse test.
      Push any keys of PC keyboards when you will stop the mouse test.

      left button on.
      right button on.
      wheel button on.
      move to up.
      move to down.
      move to right.
      move to left.
      move to right up.
      move to right down.
      move to left up.
      move to left down.
      wheel up.
      wheel down.

    When two or more mice are connected, the states changes of both mouses are displayed.

    If all the mice are disconnected during a test, the test is ended with the following message:

      Mouse detached, to stop the mouse test!

    Or else, if a key of the PC keyboard is pressed, test is ended:

      You pressed any keys, to stop the mouse test!

    2-3-2. 'k' : Keyboard test

    When 'k' is pressed at the PC Console, USB keyboard test starts.

      *** Start USB keyboard test.
      Please press a key of USB keyboard.
      If you press Ctrl+C, to stop the keyboard test.

    If the keyboard is not connected, the following error message is displayed:

      Keyboard not connected. Can't test!

    Characters input from the USB keyboard is displayed during the test. The test ends when Ctrl+'C' is entered on the PC Console.

    2-3-3. 'j' : Joystick test

    When 'j' is pressed at the PC Console, joystick test starts.

      *** Start joystick test.
      Push any keys of PC keyboards when you will stop the joystick test.

    If the joystick is not connected, the following error message is displayed.

      Joystick not connected. Can't test!

    Operation of the joystick will display the value of each axis, the value of a hat-switch and a button status.

      btn: 1 2 3 4 5 6 7 8 9 10 11 12

    3. About USB/HUB driver and the USB/HID Device driver

    I modified the program provided by Microchip to work with multiple HID devices.

    - usb_host.c (provided by Microchip; I added HUB driver)
    - usb_host.c (multiple hub version)

      I fixed a bug in the previously developed code. I also modified 2 functions: USBHOSTInit() and USBHOSTShutdown().

    - usb_host_hid.c (provided by Microchip)

      I modified 2 global variables, 'pInterfaceDetails' and 'pCurrentInterfaceDetails', to change them into an array.

      The macro USB_HOST_APP_EVENT_HANDLER is changed to USB_HIDDeviceEventHandler from USB_ApplicationEventHandler.

      I added also the 'deviceInfoHID[].rptDescriptor' to the third argument of the event handler in order to parse report descriptor. When the handler is called, the device type is determined using usb_host_hid_device.c,

      original code:
      if (USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RPT_DESC_PARSED, NULL, 0 ))

      modified code:
      if (USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RPT_DESC_PARSED, deviceInfoHID[i].rptDescriptor, 0 ))

    - usb_host_hid_parser.c (provided by Microchip)

      As far as I've tested this file, there is no need to change anything. I thought that a global variable itemListPtrs or deviceRptInfo might need an array variable, but there was no need for that.
      After the interpretation of report descriptor, these variables are not needed any more to operate the device.

    - usb_host_hid_device.c (provided by this project)

      I have created this source file, based on the mouse_demo.c and keyboard_demo.c provided by Microchip.

      By using an array variable, I can work at the same time with more than one joystick or mouse or any mix of devices of different types and quantities.

      Four functions can be called from the application:

      int mouse_read(int num, USB_MOUSE_DATA *data);
      int keyboard_read(int num, USB_KEYBOARD_DATA *data);
      int joystick_read(int num, USB_JOYSTICK_DATA *data);
      int getHIDnumOfDevice(BYTE devicetype);

      (USB_XXX_DATA macros are defined in usb_host_hid_device.h)

      Functions named 'xxx_read(int num, USB_XXX_DATA *data)' get the value from device 'xxx'. The first argument 'num' indicates the rank of the device of type 'xxx' to be accessed.

      For example, when two mice are connected to the USB Hub port, to access the second mouse, set 'num' to 2 when calling mouse_read().

      'getHIDnumOfDevice()' returns the number of connected devices.

    - main.c

      This file is the sample program to test the Driver. It is testing all the mouses at the same time.
      During the test, you can connect or disconnect the mouses one by one: as long as one remains, the test continues.
      For other questions, please look at the source file.


    When you try this demonstration program in PIC24, you must be careful about the revision number of the CPU chip. On some revisions, this demonstration program does not work well. Please see the Silicon Errata which was provided by Microchip.

    In the development of this USB HID+HUB device driver, Mr. Jozsef Laszlo (a.k.a joco, gave me valuable advices. Dear Jozsef, please receive the expression of all my gratitude.

    Note:Mr.Dupriez reviewed this english page. I am thankful also to him. (2014.1.5 Suwa)