USB HID OUT endpoint implementation

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
philandstuff
Posts: 2
Joined: Mon Aug 29, 2016 10:00 am

USB HID OUT endpoint implementation

Post by philandstuff » Mon Aug 29, 2016 10:13 am

I'm using micropython on the TiLDA-Mk3 -- https://badge.emfcamp.org/wiki/TiLDA_MK3

Currently, the USB HID interface only has an IN endpoint, so your HID device can only send data to the host, and can't receive data from the host. I'm interested in adding support for a USB HID OUT endpoint as well as IN.

There was a bit of discussion about this in the PR that allowed HID customization: https://github.com/micropython/micropython/pull/955
There is also an issue for this, but it quickly descended into a discussion about screen/picocom instead: https://github.com/micropython/micropython/issues/1217

Is there any kind of plan for this at the moment? I don't want to implement this, issue a PR, and discover it's not the right shape for the project.

My idea was basically to copy the basic receive interface of the pyb.USB_VCP() class, and implement it in much the same was as stmhal/usbd_cdc_interface.c for receiving over the VCP. It may also require changing the interface of the pyb.usb_mode() method because it currently assumes there's only one endpoint (what if you have a different maximum packet size or polling interval for IN vs OUT?)

I'm working on a branch here: https://github.com/philandstuff/micropy ... t-endpoint

My use case is that I'm interested in creating a FIDO U2F token from my TiLDA-Mk3. U2F uses the HID interface but requires bidirectional communication for the cryptographic challenge and response.

User avatar
stick
Posts: 13
Joined: Fri Jan 08, 2016 9:17 pm

Re: USB HID OUT endpoint implementation

Post by stick » Thu Sep 22, 2016 7:46 pm

I was just implementing the same stuff today and went almost exactly the same path. Luckily I found your changes before I re-implemented them all. I took your changes, added a small fix on top and created a pull request: https://github.com/micropython/micropython/pull/2448

User avatar
stick
Posts: 13
Joined: Fri Jan 08, 2016 9:17 pm

Re: USB HID OUT endpoint implementation

Post by stick » Tue Oct 04, 2016 8:14 am

Merged to master in https://github.com/micropython/micropyt ... 21f017db29 and previous 3 commits. Thanks Philip!

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: USB HID OUT endpoint implementation

Post by marfis » Wed Oct 05, 2016 4:21 am

Did you manage to get a repl over HID?

just curious because it would enable complete driver-less OS support (no more inf files etc)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: USB HID OUT endpoint implementation

Post by dhylands » Wed Oct 05, 2016 2:38 pm

I've gotten a REPL while HID mode is enabled, similar to having REPL when USB mass storage is enabled.

But that runs the REPL using usb-serial and not "over HID". What exactly do you mean by "REPL over HID"?

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: USB HID OUT endpoint implementation

Post by marfis » Wed Oct 05, 2016 6:50 pm

I meant routing the HID IN/OUT endpoints as in-/output for the REPL (instead of using the VCP). It would replace the VCP functionality (there wouldn't be a COM port anymore).

Pro:
- no INF file necessary (only a problem on windows systems), so no driver mess/maintenace for these platforms

Con:
- No visible COM port (in device manager), tool support
- HID can be a bit tricky (OS dependant, python support)

Using pyserial, it's quite easy to write a custom url handler (see http://pyserial.readthedocs.io/en/lates ... dlers.html).

You would then open the port with "serial_for_url" instead of "serial" (which is good practise anyway).

As an example, the url can have a form like "hid://<vid:pid>". In the implementation of this custom url handler, you'd need to handle/buffer the 64 Bytes packets rx/tx from the HID device. Examples of this can be found in the pyserial github repo.

I did this before with another microcontroller and it's just fun not to worry about distributing drivers. Plug & play - on every OS. Maybe that justifies the effort it needs...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: USB HID OUT endpoint implementation

Post by dhylands » Wed Oct 05, 2016 8:18 pm

Thanks for the explanation. That does sound cool.

Post Reply