USB Host HID Keyboard post
Using a USB HID keyboard with PIC32MZ in USB host mode
To make this work, as with any USB code, you need to be using an external oscillator. The internal oscillator will NOT work. In my example, I'm using a 24MHz external oscillator. You may need to adjust the fuses to suit your board
So you want to use your PIC32MZ in USB Host mode, but don't want to include an extra 110kB of Harmony code to do so? Well here you go, a working example. Hopefully! I've tested it with the two keyboards I own and it seems to work fine.
I spent two years on and off (mostly off because this stuff is soul draining) looking through Harmony code and the data sheets and using Wireshark to capture traffic. And this is after I got USB device mode to work! It's such a sad mess, this stuff should be much better documented. I will document my findings, just not today. I do, after all, have a real job too :)
The program opens UART4 at 57600 baud, which I have attached to RD5 (U4RX) and RD4 (U4TX). It reads keys from the keyboard and sends them on to UART4. If you open it in a terminal you will see something like this:
When you first power it on, it seems to take a few seconds for the PIC32 to connect to it. This is the same for the Harmony code and I presume it's a hardware thing.
If you want to disable the UART functionality, you need to make the following changes:
- Remove UART_init() - may not be necessary to remove this but why not
- Remove _mon_putc()
If I recall correctly, the printf statements by themselves shouldn't cause any problems but please be aware they exist.
So what does this example do?
- Accepts keys a - z, 0 - 9 as well as left shift, space, Enter, num lock, caps lock and scroll lock
- Ignores all other keys
- Taking into account caps lock and shift status, works out which character to send and sends it to UART4
- Implements turning on and off num lock, caps lock and scroll lock LEDs via sending the appropriate SET_REPORT packets
- Implements the ability to hold down a key and it will repeat the key(s) held down as long as they are held down
- Reads, but ignores, device configurations and HID reports and just uses device configuration 0, which it assumes uses boot protocol
This program does not listen for the USB keyboard disconnecting or do anything else. It's also not perfect but it seems to do its job.
As always I look forward to the genius developers on the fabulous Microchip forums looking at my code with disdain, unable to make it work immediately and tossing it out as they themselves provide nothing of use to anyone before then going on to use it without giving me any credit at all.
Always remember, Harmony is an abomination and the absolute enemy of well made products. It reminds me of very early Visual Basic, where it would generate pages of code for a single UI element. Every time I'm using one of the painfully slow kiosks at McDonald's or anywhere else I think of the Harmony-esque nonsense running behind the scenes.
Categories: pic32