-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Raspberry Pi Pico: Exposing USB device support #6811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
I'd love to see USB host support in MicroPython for Pi Pico, I could help in implementation. |
Also eagerly waiting for USB_HID to be available on micropython. Since this works on circuit python, I guess mainly the core usb lib needs to be ported. |
@AngainorDev there is a USB implementation for the pyboard in MicroPython: 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. |
@LennartPiro Someone with background on that low level code and USB implementation would be needed I'm afraid. |
Is there any update on this? |
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. |
MicroPython really is the neglected younger sibling to C/C++ in the Pico world. |
You might wanna look into my thread on the RPi forums |
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. |
any word on this being added? (esp for pico w) |
please, please add HID to MP |
for tha love of god, add it |
I'm planning to make a custom keyboard using RP2040's. I would like to use MP instead of CP in order to learn some low-level stuff and because CP seems too bloated with stuff I wont use. The main problem for this project is HID, and I'm probably very under-qualified for the task, but would like to give this a try. What would need to be adapted from the STM32 one? Took a quick look at Is TinyUSB worth? For context, I have some experience on both C and Py (tho not using them together) |
@elpekenin The rp2 port uses TinyUSB, whereas the stm32 port uses a modified version of the ST usb driver. So it's probably not particularly useful looking at the stm32 implementation. Look at how the TinyUSB MSC device class is used in the rp2 code as an example of adding an additional device class. However we may want to mirror the Python-level API from the stm32 port (which is to say setting |
@jimmo thanks for the fast reply, i will play around with the code tomorrow and the hardware should arrive soon :)
About the module "design", im thinking maybe |
A couple of boards just arrived, will start testing soon I've adapted noobee's work because the rp2 code has been updated since, commit on my fork. The code compiled, but before going any further:
Once I get my code working, I would of course like to make the
|
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. |
Thanks for the detailed info, once again My next steps will be
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. |
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. |
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:
/* TESTING HID */
extern const struct _mp_obj_module_t mp_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);
#define MICROPY_PORT_BUILTIN_MODULES { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid), (mp_obj_t)&mp_module_usb_hid }, |
Is there anything to be gained from looking at the circuit python implementation? |
Added the |
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:
TODO list:
#if CFG_TUD_HID
# define MICROPY_PY_USB_HID_INCLUDEFILE "ports/rp2/modusb_hid.c"
#endif But 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):
import time
import usb_hid
report = bytearray(8)
report[2] = 0x04 # register 'a' keycode
usb_hid.report(usb_hid.KEYBOARD, report) # send event
time.sleep(2)
report[2] = 0x00 # unregister 'a' keycode
usb_hid.report(usb_hid.KEYBOARD, report) # send event And you should see a bunch fo |
@elpekenin did you delete that repo? can't find it, could use some HID in a project of mine |
@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. |
@elpekenin thank you so much, tinkering with it right now, works wonders! |
Even after around 3 years of opening this issue, there's no official update.... |
There has been a lot of effort on this topic, see #9497. This ticket should probably be closed since 9497 is under active development. |
USB device support was added in MicroPython v1.23.0 |
#9497 was merged. |
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.
Original issue: https://github.com/raspberrypi/micropython/issues/5
The text was updated successfully, but these errors were encountered: