fdufnews
Posts: 355
Joined: Fri Oct 07, 2011 5:37 pm

Re: Plans for USB HID support?

Sun Aug 22, 2021 8:20 pm

I tried the links you just gave.
For the first link, the Pico works as expected and the hid modules is present.
For the second one, the Pico hangs at boot.

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Mon Aug 23, 2021 1:36 pm

coldstone wrote:
Sun Aug 22, 2021 5:05 pm
windows 10 error message instantly after flashing:
"USB device not recognised
The last USB device you connected to this computer malfunctioned and Windows does not recognise it."
Windows can have issues with VID:PID not being known, not finding a suitable device driver '.inf' file, or being the same as previously used when the USB functionality has changed. This can be a nightmare to resolve. That may be the issue here.

I also recall issues with HID on Windows being mentioned - incorrect transfer sizes being reported or used, or something like that. I recall some kludge solutions being offered but cannot recall other details nor know what a proper solution would be.

Using a Pi for development would probably be the best path to walk, but even a Pi can be crashed when a Pico is reset or unplugged.
coldstone wrote:
Sun Aug 22, 2021 5:05 pm
is there maybe a dedicated tutorial for idiots like me how to compile for the pico on a raspberry pi with links to the correct toolchain and commands to get everything installed so i can have a go myself?
The official Getting Started guides explain how to set-up a Raspberry Pi. Install the Pico SDK stuff then install the MicroPython source tree.

https://www.raspberrypi.org/documentati ... ng-started

coldstone
Posts: 5
Joined: Tue Aug 17, 2021 6:33 pm

Re: Plans for USB HID support?

Tue Aug 24, 2021 8:42 am

thank you for that

in.abhi9
Posts: 2
Joined: Fri Sep 03, 2021 9:01 am

Re: Plans for USB HID support?

Sat Sep 04, 2021 2:47 am

coldstone wrote:
Sun Aug 22, 2021 5:05 pm
i just tried it again through a download manager instead of a direct download
https://github.com/robin7331/pixel-pump ... -blank.uf2 564.50kb

flashing flash_nuke.uf2 first then the new firmware

windows 10 error message instantly after flashing:
"USB device not recognised
The last USB device you connected to this computer malfunctioned and Windows does not recognise it."

-powercycling same effect / connections via thonny not possible
-flashing https://github.com/robin7331/pixel-pump ... rmware.uf2 592kb same effect / connections via thonny not possible
-flashing circuitpython 6.3 / 7 alpha works
-flashing micropython 1.16 works

I really dont know what I'm doing wrong,
is there maybe a dedicated tutorial for idiots like me how to compile for the pico on a raspberry pi with links to the correct toolchain and commands to get everything installed so i can have a go myself?

Thank you for all your help and sorry if it's just my stupid ass doing something wrong.
I compiled myself with Micropython 1.17. The issue was with the wrong PID in tusb_port.c; the correct value is 0x0005 for Mu editor or any IDE to recognised the device.
rp2hid.uf2.zip
(217.63 KiB) Downloaded 20 times

sheld0r
Posts: 10
Joined: Fri Apr 30, 2021 8:51 pm

Re: Plans for USB HID support?

Sun Sep 05, 2021 9:45 am

Unfortunately this does not work for me.
I still have issues on Windows 10.

Did you change anything else than this in the tusb_port.c file?

Code: Select all


#define MICROPY_HW_USB_PID      ( 0x0005              + \
                                  PID_MAP(CDC,     0) + \
                                  PID_MAP(HID,     4)   )


hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Sun Sep 05, 2021 1:10 pm

in.abhi9 wrote:
Sat Sep 04, 2021 2:47 am
I compiled myself with Micropython 1.17. The issue was with the wrong PID in tusb_port.c; the correct value is 0x0005 for Mu editor or any IDE to recognised the device.
It's not so much a wrong PID value as IDE's like "Mu" insisting the PID must be 0x0005 or whatever it thinks a Pico PID would be. Not being able to work with other VID or PID values is an unnecessary and unwarranted limitation, even a defect, in my book.

VID and PID values don't affect all IDE's - I have had no issues with Thonny under Raspberry Pi OS.

They will however affect what drivers get installed and used on Windows when the Pico is plugged in. If you use 2E8A:0005 it will probably load a driver compatible with "MicroPython using just a single CDC serial port" so HID may not work. This is why my own 'tusb_port.c' creates a PID which is unique for what has been configured; single CDC PID is different to CDC plus HID PID, so the correct driver should get loaded.

Technically one shouldn't be using a PID value of 0x0005 for anything but "MicroPython with just a single CDC seril port". You need to change that if using HID -a

https://github.com/raspberrypi/usb-pid

TinyUSB has its own PID value definition but that doesn't allow there to be more than one CDC. Thus I unilaterally created my own PID value assignments (0xC000-0xCFFF) as detailed in 'tusb_port.c'.

As it's not a commercial product it doesn't much matter what PID or VID one uses but it will matter when it comes to Windows drivers. Rather than alter the Pico code so it uses the potentially wrong device driver it is better to create the correct device driver '.inf' file for the VID and PID and the USB capabilities it has.
sheld0r wrote:
Sun Sep 05, 2021 9:45 am
Unfortunately this does not work for me.
I still have issues on Windows 10.

Did you change anything else than this in the tusb_port.c file?

Code: Select all


#define MICROPY_HW_USB_PID      ( 0x0005              + \
                                  PID_MAP(CDC,     0) + \
                                  PID_MAP(HID,     4)   )

If you want to use a PID of 0x0005 simply use the following because the above won't give you a PID of 0x0005 -

Code: Select all

#define MICROPY_HW_USB_PID      ( 0x0005)

sheld0r
Posts: 10
Joined: Fri Apr 30, 2021 8:51 pm

Re: Plans for USB HID support?

Sun Sep 05, 2021 1:50 pm

Thanks for the clarification!
So I assume there is no combination for VID:PID that makes windows load the driver for a "generic" keyboard as all keyboards have their own commercial VID:PID values?

I'd love to have the Pico connected with CDC + HID without having to install a windows driver.
It works just fine under macOS and windows 7 which makes me belive that this should be possible somehow.
Only windows 10 seems to struggle using the device with your PID value and also with 0x0005. :?

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Sun Sep 05, 2021 2:03 pm

sheld0r wrote:
Sun Sep 05, 2021 1:50 pm
So I assume there is no combination for VID:PID that makes windows load the driver for a "generic" keyboard as all keyboards have their own commercial VID:PID values?
I have no idea. You could try a VID and PID from a commercial USB keyboard and see if that works. But, given it works with Linux, MacOS, Windows 7, I would expect it to work with Windows 10.

My suspicion is that it has loaded some driver which it now insists on always loading and Windows 10 needs to be told to forget about that to make things work. That was never a straight forward exercise with XP or 7 and I have no idea how it would be done under Windows 10. Hitting Google would be my best suggestion.

One thing you could try is compiling as 2E8A:0005 and seeing if it works when plugged in to a Windows 10 PC or laptop which it's not been plugged in to. If you have a Raspberry Pi 4B you could temporarily install Windows 10 or 11 on that if you have nothing else you could use.

sheld0r
Posts: 10
Joined: Fri Apr 30, 2021 8:51 pm

Re: Plans for USB HID support?

Sun Sep 05, 2021 2:09 pm

Thats a good idea.
For now I am about to compile it with the CircuitPython PID 0x000B
Will let you know how that goes.

sheld0r
Posts: 10
Joined: Fri Apr 30, 2021 8:51 pm

Re: Plans for USB HID support?

Sun Sep 05, 2021 3:09 pm

The problem seems not to be related to VID:PID but instead something seems to be wrong with the descriptor.
I got a Wireshark capture from another user who has connected it to a Win 10 instance.
I'm investigating but do you have an idea?
CleanShot 2021-09-05 at 17.08.44.png
CleanShot 2021-09-05 at 17.08.44.png (235.9 KiB) Viewed 1286 times

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Sun Sep 05, 2021 4:40 pm

sheld0r wrote:
Sun Sep 05, 2021 3:09 pm
The problem seems not to be related to VID:PID but instead something seems to be wrong with the descriptor.
I got a Wireshark capture from another user who has connected it to a Win 10 instance.
I'm investigating but do you have an idea?
No idea at all I am afraid. Given that it does work on Linux, MacOS, Windows 7, I cannot believe there is an issue with the descriptors.

I don't know why the WireShark trace shows zero (0x00) for everything in the "DEVICE DESCRIPTOR". That is what it's showing but I don't know how much confidence I should have that it is what the Pico has actually sent back. What is being seen may be a consequence of it not working rather then the cause of it not working.

I will try burning my Pico with the HID supporting code, seeing what happens when I plug that into my Windows 10 PC. I haven't used that for a while and it often takes hours to update so can't say when I will be able to report back.

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Wed Sep 08, 2021 4:06 pm

I eventually managed to get my HID code compiled with MicroPython v1.17-8 and working under Raspberry Pi.

Plugging that into Windows 10 gave me a "USB Composite Device" which Windows claims was malfunctioning but the details for that shows it had retrieved the VID, PID and other data provided in the Device Descriptors. Unfortunately Windows is a second-class citizen when it comes to support for getting Pico to work with Windows over USB so I am not sure how to make it work.

Recent changes to MicroPython means the earlier .zip files don't work with the latest MicroPython firmware and producing MicroPython firmware for a Pico which supports HID is getting more complicated and onerous, especially for me when I have little interest in HID or using it.

Because I built it I have attached a MicroPython v1.17-8 source which includes HID support -
rp2hid-1-17-8.zip
(90.7 KiB) Downloaded 36 times

The installation of that is slightly different to previously -

Code: Select all

cd ~/pico/micropython/ports
unzip -o rp2hid-1-17-8.zip
cd rp2hid-1-17-8
mkdir build
cd build
cmake ..
make
Please note that requires the 'rp2' port to also exist, and it should be the v1.17-8 version.

With MicroPython changes continually breaking things, MicroPython taking a direction which makes Pico specific additions much more difficult, potentially impossible, this may be the last release I make for MicroPython with HID support.

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Sat Sep 18, 2021 5:45 pm

hippy wrote:
Wed Sep 08, 2021 4:06 pm
I eventually managed to get my HID code compiled with MicroPython v1.17-8 and working under Raspberry Pi.

Plugging that into Windows 10 gave me a "USB Composite Device" which Windows claims was malfunctioning but the details for that shows it had retrieved the VID, PID and other data provided in the Device Descriptors.
Yay! It appears I have got a latest build to be recognised on Windows 10. It's showing up as a COM Port and has created two HID input devices, keyboard and mouse, plus gamepad.

The issue appears to be the USB version in the descriptors - 'bcdUSB' - but I will need to investigate further to confirm that. At least with something recognised and working I can tweak things to see what remains okay and what breaks things.

That 'bcdUSB' value specifies what protocol capabilities the device supports, regardless of what the USB hardware supports. What I believe has happened is I moved from "1.1" or "2.0" to "2.01" then "2.1" when trying to get WebUSB to work. Using "2.1" is fine under Raspberry Pi OS; there are 'dmesg' complaints that it cannot read some "BOS descriptors", which would be correct, but it still all worked, so I never reverted that.

Windows 10 on the other hand may have decided "No BOS descriptors; device is malfunctioning".

It works on Windows 7 possibly because that doesn't know what '2.1' is, treated it as '2.0', therefore never asked for something my code does not provide.

It likely works on MacOS because that ignores the lack of "BOS descriptors" exactly as Raspberry Pi OS does, as any Linux probably would.

So the good news is I have CDC+HID using VID:PID of 2E8A:000A and 2E8A:C011 working on Windows 10. It moved the cursor when I called '_hid.move(100,100)' but I haven't tested beyond that.

I will look at releasing an updated 'rp2hid' build but, in the meantime, editing 'tusb_port.c' and changing 'bcdUSB' from '0x0210' to '0x0200' should hopefully be enough to get things working under Windows 10.

And, if that is the case, please accept my apologies for breaking things and not realising I had done that.

in.abhi9
Posts: 2
Joined: Fri Sep 03, 2021 9:01 am

Re: Plans for USB HID support?

Thu Sep 23, 2021 3:37 am

Fantastic! I'm doing a project which depends on HID and without your work it wouldn't be possible at all! Thank you so much for your work!
I'll make the project open source soon.

noobeepi
Posts: 4
Joined: Wed Oct 06, 2021 3:03 am

Re: Plans for USB HID support?

Wed Oct 06, 2021 3:28 am

hey hippy

thanks for providing an implementation for usb hid.

i have a basic question (being quite new to the pico and micropython)

what version is "v1.17-8"? i see a "1.17" tag at https://github.com/micropython/micropython/tree/v1.17 but don't understand where the "-8" comes from. which commit ID should i be checking out before patching in your files into ./rp2?

thanks!

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Wed Oct 06, 2021 11:48 pm

noobeepi wrote:
Wed Oct 06, 2021 3:28 am
what version is "v1.17-8"? i see a "1.17" tag at https://github.com/micropython/micropython/tree/v1.17 but don't understand where the "-8" comes from.
Me neither. All I know is it's an incrementing value used between stable releases, is what the 'git tag' shows when a 'pull' is done and MicroPython is built, which I assume matches the number on the unstable builds released as '.uf2' files - https://micropython.org/download/rp2-pico
noobeepi wrote:
Wed Oct 06, 2021 3:28 am
which commit ID should i be checking out before patching in your files into ./rp2?
No idea I am afraid. I would say install the latest there is - whatever a 'git clone' or 'git pull' delivers - apply the patches, hope it builds and runs.

It should build because I try to keep up to date and usually have HID permanently included so should quickly spot when it doesn't and I intend to update this thread if that happens. Though as seen earlier, some people can get ahead of what I have, so the first I know of a problem may be when others find them first.

I am on v1.17-74 and that builds.

And also seems to run, at least so far as 'dmesg' reports it as a HID device on Raspberry Pi OS. I don't regularly test functionality, I rely on someone reporting it's not working for them.

Hopefully it stays that way because I can't foresee anything changing again but one can never tell.

noobeepi
Posts: 4
Joined: Wed Oct 06, 2021 3:03 am

Re: Plans for USB HID support?

Thu Oct 07, 2021 1:51 pm

thank you hippy. your note really helps!

so i think the -8 in 1.17-8 means the 8th commit after the 1.17 tag. that means your changes where done against the source code just after this commit.

Code: Select all

Commit: bbbdef4cc1d2cb3a3f6c2d067b6988534773b2a6
rp2/mpconfigport.h: Enable heapq module.
i verified that the change in this commit is reflected in your source code.

so i created a new branch at this point and applied your patches. turns out only a few files are impacted:
Screenshot from 2021-10-07 05-42-03.png
Screenshot from 2021-10-07 05-42-03.png (58.3 KiB) Viewed 396 times
i copied only those files and was able to build a firmware image.

i then merged the rest of the changes from master up to 1.17-74 into the branch. there are a few conflicting changes along the way, in particular, i think this commit caused a conflict in mpconfigport.h that i believe you had to resolve (everything else was auto merged)

Code: Select all

Commit: 38f8e852e04013e4616097320bf9cf75051a1b6b
rp2: Add framework for networking.
importantly, i don't think we should just clone or check out to HEAD and then overwrite ./rp2 with your rp2-1.17-8 as that would revert some files to the older versions. we need to apply the changes at the right point and merge the rest from the master branch.

i completed the merge and was able to build a firmware image as well.

unfortunately i can't test the images yet, but did upload the updated branch to github if anyone wants to try:
https://github.com/noobee/micropython/tree/hippy

just a caveat that i have NO idea if this is the right approach and also have very little experience using git. but the firmware seems to build too :).. would welcome any feedback or suggestions!

hopefully i will have more time this weekend to explore your changes. thank you for all your help!

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Thu Oct 07, 2021 4:15 pm

noobeepi wrote:
Thu Oct 07, 2021 1:51 pm
importantly, i don't think we should just clone or check out to HEAD and then overwrite ./rp2 with your rp2-1.17-8 as that would revert some files to the older versions. we need to apply the changes at the right point and merge the rest from the master branch.
Yes that will cause problems. The process I recommended doesn't overwrite anything which exists ...

Code: Select all

cd ~/pico/micropython/ports
unzip -o rp2hid-1-17-8.zip
cd rp2hid-1-17-8
mkdir build
cd build
cmake ..
make
Install some version of MicroPython, the latest usually, then unzipping my file while in the 'ports' directory will create an 'rp2hid' port parallel with 'rp2'.

Previously I was recommending copying 'rp2' to 'rp2hid' then overwriting files in 'rp2hid' from my earliest .zips. That minimised the number of changes and files overwritten. The process could be repeated whenever 'rp2' changed so 'rp2hid' was kept up to date in most respects.

Due to MicroPython refactoring things that had to change, and the best way to do that was to provide a snap-shot of the full 'rp2hid' port at that time, which now means always getting an out of date port, something frozen at "1.17-8" regardless of what MicroPython or 'rp2' has reached.

This is why I can't guarantee it will always build. If MicroPython refactor again my "rphid" frozen at "1.17-8" may stop building. It hasn't so far but I am expecting that at some point it will.

Merging my 'rp2hid' changes with 'rp2', or keeping my 'rp2hid' up to date from 'rp2', is something I leave for others to figure out.

Doing that manually, as you are finding, can present problems which need resolving because of MicroPython refactoring since "1.17-8". The best thing to do is to encourage MicroPython to support HID themselves, then none of this would be necessary.

noobeepi
Posts: 4
Joined: Wed Oct 06, 2021 3:03 am

Re: Plans for USB HID support?

Thu Oct 07, 2021 6:31 pm

cool, thanks.

i now understand you meant for your changes to be in a separate rp2hid-1.17-8/ directory and build.

agreed that pushing it upstream is best, i have no idea how that is done.

in the mean time, it looks like the "merge to HEAD" option is still ok with small overhead.

nautilus27
Posts: 92
Joined: Fri May 22, 2015 9:38 pm

Re: Plans for USB HID support?

Fri Oct 15, 2021 8:39 pm

Fantastic work. Is it possible to get the working version as an UF2 file to flash directly to Pico? I'm on Windows and I have no means of compiling so I'd appreciate if you guys can give me a UF2 file with USB HID feature implemented.

hippy
Posts: 10741
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Plans for USB HID support?

Sat Oct 16, 2021 6:04 pm

Here's the '.uf2' for my PicoPython port of MicroPython which includes HID and a few other things - Use "picotool info -a picopython.uf2" for a list.

Due to the fact it auto-uploads library files to the Pico file system, and MicroPython caching can get in the way, I recommend waiting a few seconds after flashing, unplugging, then re-plugging without BOOTSEL held.

If using Thonny; connect to the Pico, then in the command shell ...

Code: Select all

PicoPython v1.17-84 on 2021-10-16; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import hid
>>> hid.Send("Hello World!")
>>> Hello World!
The 'hid.Send()' sends out via HID so Thonny sees the text as if typed in from the keyboard -- Note the uppercase "S" in "Send".

picopython-0050.uf2.zip
(255.8 KiB) Downloaded 7 times

noobeepi
Posts: 4
Joined: Wed Oct 06, 2021 3:03 am

Re: Plans for USB HID support?

Thu Oct 21, 2021 4:18 am

hey Hippy

did you find that, if you send USB reports too close together, the device might drop some or subsequent reports?

for the code below, without the time.sleep(0.01), the keypress release (0x0, 0x0) is not sent and the host will auto repeat on the 'a' (0x0, 0x4) after the program terminates.

i ran it through a USB sniffer (usbpcap) and found that the device is indeed not sending some reports (if issued too close together).

circuitpython's implementation does not have this problem, so i don't know if this is an issue with MP's tiny USB library.

thanks!

Code: Select all

import time
import _hid

for i in range(10):
    _hid.keypress(0x00, 0x04)
    time.sleep(0.01)
    _hid.keypress(0x00, 0x00)
    time.sleep(0.01)

Return to “MicroPython”