[Japanese][English] 1.INTRODUCTIONFollowing 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.
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:
2-2. Start the demonstration system and devices connection
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.
m:Mouse read test. k:Keyboard read test. j:Joystick read test. =>
Demonstration system is then ready for operation.
address=2 If the mouse is removed and is switched to the port '4' of the Hub, the following messages are displayed on the PC Console:
address=2 mouse attached. address=5 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:
address=2 keyboard attached. address=4 joystick attached. address=3 2-3. Operation of the demonstration system A menu is displayed whenever you press the 'Enter' key at the PC console:
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. 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:
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:
Or else, if a key of the PC keyboard is pressed, test is ended:
2-3-2. 'k' : Keyboard test
When 'k' is pressed at the PC Console, USB keyboard test starts.
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:
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.
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.
Operation of the joystick will display the value of each axis, the value of a hat-switch and a button status.
Y=127 Z=127 R=127 HatSwitch=1 HatSwitch=2 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_hid.c (provided by Microchip)
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:
- usb_host_hid_parser.c (provided by Microchip)
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)
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
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. 4. CONCLUSION 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, http://joco.homeserver.hu) gave me valuable advices. Dear Jozsef, please receive the expression of all my gratitude.
|