USB Host HID Keyboard

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:

PIC32MZ - USB Host HID keyboard app terminal output

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.

Here's the code

Tags: code, USB, Host, HID

USB HID Host

I've... I've done it. I'VE DONE IT!

I have gotten this stupid USB peripheral to work in Host mode with an HID keyboard, without any Harmony code at all!

Yes, it's been almost two years since I updated this site but quite frankly it's taken me that long to recharge and sit down for hours and hours trying and retrying to understand this peripheral with the incredibly poor documentation Microchip has provided.

At any rate! I need to work on it for a while more, but this is the start of great things.

If anyone reads this, I hope you have as good a day as I'm having... or will have once I go to bed and get my 4 hours of sleep before work :)

Tags: USB, HID, HOST

USB HID joystick

Right, as promised here are the files for the USB HID joystick. The source code only makes sense for my schematic so adjust as you see fit. As always, if you use this code, please do at least give credit where credit is due. It's been a long battle trying to understand both USB on the PIC32MZ and understanding how USB works in general.

Schematic

PIC32MZ HID joystick by Aidan Mocke - Schematic

Here's a low quality render to give some approximation of what the board looks like in real life

USB HID Joystick Board render

OK, enough of that, Here's the source code

And here are the KiCad files if you need them.

Tags: USB, HID

USB HID joystick

Hello there world. I've finally found the time to complete my USB HID joystick and it seems to be working. In a few days I'll upload both the KiCad files and the source code, along with a very long-winded explanation (yay) of how USB HID and its associated reports work. Hope you're all well!

Tags: USB, HID