You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Raspberry Pi Pico: Exposing USB device support #6811
Is it possible or a reasonable to ask about exposing USB device support in MicroPython similar to the support for the PyBoard?
Beyond that, is it reasonable to expose even more support so that custom USB devices could be created? For example, a custom U2F token running from MicroPython or an OpenPGP card implementation. I'm sure someone can come up with examples that aren't security related.
Unsure if this is the right place but anyone that lands here because they want to make a keyboard/macropad/whatever with the pico please note that you can install CircuitPython and the HID functions work fine under CircuitPython (as does the adafruit HID library). KMK is also likely to work but I haven't tested it.
Reacted by LennartPiro, Antony Nasce, Werner, Kot, Samveen and Thomas Christlieb
Also eagerly waiting for USB_HID to be available on micropython.
Is there someone actively working on this?
What would be needed?
Since this works on circuit python, I guess mainly the core usb lib needs to be ported.
I'm not familiar with differences between circuitpython and micropython, but this seems a reasonable task.
How could I help?
@LennartPiro
I had a look at adafruit implementation as well, and it's quite different (shared lib, for all ports)
I guess the way to go here would be to follow the existing pyboard implementation, since it's also tied to the existing, working VCP interface.
I also saw some PR or Issues about using the default tinyusb lib instead of the pico sdk provided one, things lying there as well probably.
Someone with background on that low level code and USB implementation would be needed I'm afraid.
I just wanted to point out that one of my struggles using Circuitpython for usb hid is that it has no _thread support, which makes it difficult to perfrom concurrent functions. While Micropython does have minimal support for _thread, it will be more benificial to have usb hid device as well.
MicroPython really is the neglected younger sibling to C/C++ in the Pico world.
I struggle to see how a maker focused device could even be released without HID support. It is a huge part of the hobbyist microcontroller community. I really can't stomach going back to C, and hate switching between CircuitPython and MicroPython for every other product. Please can we have some traction on this.
Reacted by Ofir Gaash, Ritik, Kevin and Thomas Christlieb
I have been porting my MP code to CP just to findout that I need to create a new board in order to use it. CP is nice and all but I cant bear the fact that if you work on a custom board it need to be re-compiled. USB HID in MP is a must.
I have been porting my MP code to CP just to findout that I need to create a new board in order to use it. CP is nice and all but I cant bear the fact that if you work on a custom board it need to be re-compiled. USB HID in MP is a must.
Custom boards often use different flash chips. A generic build will have a slow but universal setup for the flash. Using a custom build allows for an optimal flash setup for a given board.
Hoping it can't be bricked so I don't need to buy an external programmer or something
Yes, it is almost impossible to brick a device these days, and fortunately the Pico board has an easy "boot" button to get to the uf2 bootloader. The bootloader is in ROM so you can't overwrite it by accident.
(I have never bricked a single microcontroller while working on MicroPython)
You can also use a second Pico as an external (SWD) debugger for a Pico. Not sure if that supports uploading code though.
Once I get my code working, I would of course like to make the usb module.
There's a lot of scope here, and it impacts several ports (mimxrt, samd, rp2, esp32s*, nrf and potentially stm32). Also questions about how this works with host mode, and potentially whether we want to use the tinyusb device class implementations or something more Python-based.
So, great to experiment, but I don't have any specific advice to give here yet. Anything you find out will be useful though to help figure out how to approach this.
Of course, test the (allegdly) HID-enabled firmware i made the other day and get it working
Try and build a usb module which unifies all the pieces of configuration, and provides a standarized API (mode method, keyboard/mouse/consumer classes, ...)
Actually, there is no point on adding a global module, since it would just be using the underlying API. Having port-specific modules would be harmless to other ports and eaiser to maintain&test
I'm by no means a software-engineer, I'm open to ideas but this feels like a decent way of approaching the problem.
Will be sharing updates as soon as i get back working on this project in a couple of days. (perhaps spamming this issue page isn't the best place to do so, should I move the convo elsewhere?)
I can't say I agree with you @elpekenin. This is exactly what I would consider software engineering to be all about.
perhaps spamming this issue page isn't the best place to do so, should I move the convo elsewhere?
I'd say this issue is the right place. The subscribers to the issue are the ones who are getting "spammed" and really do wish to stay up to date on anything that you can share. You might even get a few beta testers this way. The only other location I could suggest is the discussions section, but I would wait for someone to complain first.
Reacted by thoastbrot, LennartPiro, Thomas Christlieb and Abdullah Osama
This is exactly what I would consider software engineering to be all about.
Thanks for the encouraing words :) I meant it in a "maybe there are some design patterns I'm missing" kind-off way
I've spent several hours trying (and failing), my findings so far: ⚠️ Maybe I got my head tangled during the several tests, so take this with a grain of salt)
Adding the modusb_hid.c to CMakeLists.txt, even when not trying to add the module to mpconfigport.h:
Linux has no problem connecting to it.
Apparently breaks the W10 driver, and can't even connect to it with Thonny anymore - maybe just a typo/error on my code.
The way in which modules are added in mpconfigport.h has changed since noobee made their code, and I'm unsure how should i add it, I'll double-check soon, but pretty sure none of this worked:
/* TESTING HID */externconststruct_mp_obj_module_tmp_module_usb_hid;
// #define MICROPY_PY_USB_HID (1) //useless config flag so far, just added so i don't forget// #define MICROPY_PY_USB_HID_INCLUDEFILE "ports/rp2/modusb_hid.c"// #define MICROPY_PY_USB_HID { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&mp_module_usb_hid) }, // #define MICROPY_PORT_BUILTIN_MODULES { MP_ROM_QSTR(MP_QSTR_USB_HID), MP_ROM_PTR(&mp_module_usb_hid) }, //noobee's way// MP_REGISTER_MODULE(MP_QSTR_usb_hid, &mp_module_usb_hid); #defineMICROPY_PORT_BUILTIN_MODULES { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid), (mp_obj_t)&mp_module_usb_hid },
It is """working""" already, you can check my branch here
I'm not sure how/why the drivers were acting weird nor how i fixed it 😅
Changes made:
Moved several defines to enums (im not sure i understood their meaning correctly, have some '??'s over the code)
Added #if CFG_TUD_HID at all my pieces of code, so it can be disabled on compile. Also added checks for stuff related with CFG_TUD_MSC and didn't have it already
USBD_DESC_LEN is now dynamically computed (at compile) using a multiplication thanks to the CFG flags being 1/0 (should i add some extra code to avoid problems with other values? are they already forced to be 0/1?)
TODO list:
Rename module to usb
Rename stuff to match pyb.USB_HID's API
Add some documentation
Would like to add the module from mpconfigport.h with something like
However, i ended up using QMK for HID stuff, so don't expect any further work on it. Also, be aware i made very minimal testing(and it was a year ago), i have no idea how good the code is nor how easy will it merge into current repo.
Activity
safijari commentedon Feb 1, 2021
Unsure if this is the right place but anyone that lands here because they want to make a keyboard/macropad/whatever with the pico please note that you can install CircuitPython and the HID functions work fine under CircuitPython (as does the adafruit HID library). KMK is also likely to work but I haven't tested it.
matemaciek commentedon Feb 11, 2021
I'd love to see USB host support in MicroPython for Pi Pico, I could help in implementation.
AngainorDev commentedon Feb 13, 2021
Also eagerly waiting for USB_HID to be available on micropython.
Is there someone actively working on this?
What would be needed?
Since this works on circuit python, I guess mainly the core usb lib needs to be ported.
I'm not familiar with differences between circuitpython and micropython, but this seems a reasonable task.
How could I help?
LennartPiro commentedon Feb 13, 2021
@AngainorDev there is a USB implementation for the pyboard in MicroPython:
micropython/ports/stm32/usb.c
Lines 366 to 392 in f305c62
Maybe this is a good starting point in porting it to the Pico? I'm not good enough at C programming to make much sense of it.
AngainorDev commentedon Feb 13, 2021
@LennartPiro
I had a look at adafruit implementation as well, and it's quite different (shared lib, for all ports)
I guess the way to go here would be to follow the existing pyboard implementation, since it's also tied to the existing, working VCP interface.
I also saw some PR or Issues about using the default tinyusb lib instead of the pico sdk provided one, things lying there as well probably.
Someone with background on that low level code and USB implementation would be needed I'm afraid.
robin7331 commentedon May 3, 2021
Is there any update on this?
mrkprdo commentedon May 3, 2021
Add me to the list here.
I just wanted to point out that one of my struggles using Circuitpython for usb hid is that it has no _thread support, which makes it difficult to perfrom concurrent functions. While Micropython does have minimal support for _thread, it will be more benificial to have usb hid device as well.
GregWoods commentedon Aug 9, 2021
MicroPython really is the neglected younger sibling to C/C++ in the Pico world.
I struggle to see how a maker focused device could even be released without HID support. It is a huge part of the hobbyist microcontroller community. I really can't stomach going back to C, and hate switching between CircuitPython and MicroPython for every other product. Please can we have some traction on this.
robin7331 commentedon Aug 11, 2021
You might wanna look into my thread on the RPi forums
Its not an official library but it seems to work fine for HID :)
WernerFS commentedon Jan 10, 2022
I have been porting my MP code to CP just to findout that I need to create a new board in order to use it. CP is nice and all but I cant bear the fact that if you work on a custom board it need to be re-compiled. USB HID in MP is a must.
tannewt commentedon Jan 10, 2022
Custom boards often use different flash chips. A generic build will have a slow but universal setup for the flash. Using a custom build allows for an optimal flash setup for a given board.
theboyknowsclass commentedon Jul 10, 2022
any word on this being added? (esp for pico w)
ofirgaash1 commentedon Jul 27, 2022
please, please add HID to MP
WarpRomo commentedon Jul 31, 2022
for tha love of god, add it
4 remaining items
jimmo commentedon Aug 27, 2022
Yes, it is almost impossible to brick a device these days, and fortunately the Pico board has an easy "boot" button to get to the uf2 bootloader. The bootloader is in ROM so you can't overwrite it by accident.
(I have never bricked a single microcontroller while working on MicroPython)
You can also use a second Pico as an external (SWD) debugger for a Pico. Not sure if that supports uploading code though.
There's a lot of scope here, and it impacts several ports (mimxrt, samd, rp2, esp32s*, nrf and potentially stm32). Also questions about how this works with host mode, and potentially whether we want to use the tinyusb device class implementations or something more Python-based.
So, great to experiment, but I don't have any specific advice to give here yet. Anything you find out will be useful though to help figure out how to approach this.
elpekenin commentedon Aug 27, 2022
Thanks for the detailed info, once again
My next steps will be
usbmodule which unifies all the pieces of configuration, and provides a standarized API (modemethod, keyboard/mouse/consumer classes, ...)Actually, there is no point on adding a global module, since it would just be using the underlying API. Having port-specific modules would be harmless to other ports and eaiser to maintain&test
I'm by no means a software-engineer, I'm open to ideas but this feels like a decent way of approaching the problem.
Will be sharing updates as soon as i get back working on this project in a couple of days. (perhaps spamming this issue page isn't the best place to do so, should I move the convo elsewhere?)
mcassaniti commentedon Aug 28, 2022
I can't say I agree with you @elpekenin. This is exactly what I would consider software engineering to be all about.
I'd say this issue is the right place. The subscribers to the issue are the ones who are getting "spammed" and really do wish to stay up to date on anything that you can share. You might even get a few beta testers this way. The only other location I could suggest is the discussions section, but I would wait for someone to complain first.
elpekenin commentedon Aug 28, 2022
Thanks for the encouraing words :) I meant it in a "maybe there are some design patterns I'm missing" kind-off way
I've spent several hours trying (and failing), my findings so far:
⚠️ Maybe I got my head tangled during the several tests, so take this with a grain of salt)
modusb_hid.ctoCMakeLists.txt, even when not trying to add the module tompconfigport.h:mpconfigport.hhas changed since noobee made their code, and I'm unsure how should i add it, I'll double-check soon, but pretty sure none of this worked:theboyknowsclass commentedon Aug 28, 2022
Is there anything to be gained from looking at the circuit python implementation?
elpekenin commentedon Aug 28, 2022
Added the
MP_REGISTER_MODULEline in the ownmodusb_hid.cfile and it worked, im now trying to get drivers working on Windows :))elpekenin commentedon Aug 29, 2022
It is """working""" already, you can check my branch here
I'm not sure how/why the drivers were acting weird nor how i fixed it 😅
Changes made:
#if CFG_TUD_HIDat all my pieces of code, so it can be disabled on compile. Also added checks for stuff related withCFG_TUD_MSCand didn't have it alreadyUSBD_DESC_LENis now dynamically computed (at compile) using a multiplication thanks to the CFG flags being 1/0 (should i add some extra code to avoid problems with other values? are they already forced to be 0/1?)TODO list:
usbpyb.USB_HID's APImpconfigport.hwith something likeBut not sure how to
If you have some time, please take a moment to check if you run into driver issues (as i did yesterday):
cd ports/rp2 && make submodules && make clean && makeAnd you should see a bunch fo
as being printed in your screenCRImier commentedon Oct 8, 2023
@elpekenin did you delete that repo? can't find it, could use some HID in a project of mine
elpekenin commentedon Oct 8, 2023
@CRImier sorry, nuked the repo a couple months back to make GitHub see it as a fork of CircuitPython instead.
I have just pushed that branch back up:https://github.com/elpekenin/circuitpython/tree/peke-devel
However, i ended up using QMK for HID stuff, so don't expect any further work on it. Also, be aware i made very minimal testing(and it was a year ago), i have no idea how good the code is nor how easy will it merge into current repo.
CRImier commentedon Oct 19, 2023
@elpekenin thank you so much, tinkering with it right now, works wonders!
fmohtadi99 commentedon Jan 7, 2024
Even after around 3 years of opening this issue, there's no official update....
Actually no attention from any of 541 contributors :))
pi-mst commentedon Jan 7, 2024
There has been a lot of effort on this topic, see #9497.
This ticket should probably be closed since 9497 is under active development.
LennartPiro commentedon Jun 5, 2024
USB device support was added in MicroPython v1.23.0
Issue can be closed.
robert-hh commentedon Jun 5, 2024
#9497 was merged.