P2 Hosted USB Keyboard/Mouse
garryj
Posts: 337
Edit: 2020-09-17: added the KbMObjDemos v0.1.4 .zip package.
Edit: 2019-05-20: added the P2asm version .zip package.
Edit: 2019-05-18: update v0.02 (minor changes) and added the port A/B and the verbose enumeration .zip packages.
P2-Eval board + Serial Host accessory board, using pins 16..24 (16..25 if using A/B ports). Pin definitions are at the top of the 1CogKbm.spin2 file.
Sysclock 160MHz, 230400 baud serial.
Compile/run using fastspin and loadp2 and its built-in -t option terminal.
Spin2USBKbm-v0.02.zip:
The Spin2 top object file is KbMObjTest.spin2. The 1CogKbM.spin2 file is a one cog USB host + keyboard/mouse driver. The P2 pin usage is documented at the top of the 1CogKbm.spin2 file. The top object also loads the SmartSerial.spin and std_text_routines.spinh files that are packaged with Spin2gui.
Spin2USBKbMx2-v0.02.zip:
The same as above, but loads a second 1CogKbM object to utilize both ports (A-lower/B-upper) of the Serial Host accessory board.
USBEnumerate.zip
This is older code (three cogs) that outputs the contents of the various USB descriptors read at device enumeration. If you have a keyboard/mouse that is not recognized (or any other USB device) , the descriptor information may help when troubleshooting. Sometimes at cold startup, it may not detect a device when connected. If this happens, a reload of the code should get it going.
P2asmUSBKbm-v0.02.zip
P2asm version of the one cog USBKbM demo.
Edit: 2019-05-20: added the P2asm version .zip package.
Edit: 2019-05-18: update v0.02 (minor changes) and added the port A/B and the verbose enumeration .zip packages.
P2-Eval board + Serial Host accessory board, using pins 16..24 (16..25 if using A/B ports). Pin definitions are at the top of the 1CogKbm.spin2 file.
Sysclock 160MHz, 230400 baud serial.
Compile/run using fastspin and loadp2 and its built-in -t option terminal.
Spin2USBKbm-v0.02.zip:
The Spin2 top object file is KbMObjTest.spin2. The 1CogKbM.spin2 file is a one cog USB host + keyboard/mouse driver. The P2 pin usage is documented at the top of the 1CogKbm.spin2 file. The top object also loads the SmartSerial.spin and std_text_routines.spinh files that are packaged with Spin2gui.
Spin2USBKbMx2-v0.02.zip:
The same as above, but loads a second 1CogKbM object to utilize both ports (A-lower/B-upper) of the Serial Host accessory board.
USBEnumerate.zip
This is older code (three cogs) that outputs the contents of the various USB descriptors read at device enumeration. If you have a keyboard/mouse that is not recognized (or any other USB device) , the descriptor information may help when troubleshooting. Sometimes at cold startup, it may not detect a device when connected. If this happens, a reload of the code should get it going.
P2asmUSBKbm-v0.02.zip
P2asm version of the one cog USBKbM demo.
zip
62K
Comments
Great progress, and in Spin2 too. Thanks for your ongoing efforts
I was hoping this was possible.
I think 250 MHz is going to be the sweet spot though.
Looking forward to replacing my current USB keyboard code in Self hosted Micropython to this.
I've tried several mice now and they've worked fine. Unfortunately I'm having trouble with keyboards. I've tried two keyboards. The first wasn't detected properly (it triggered the DEV_UNKNOWN "Boot protocol keyboard/mouse not found") so perhaps it just doesn't support boot protocol? The second was a wireless combo keyboard/mouse; the mouse part works fine, but only a few function keys are recognized and they're completely wrong (e.g. F1 is coming out as 'd' and F2 as backspace). Have you seen that before?
Thanks,
Eric
Yes, during device enumeration "boot protocol" interfaces must be defined. I have an early MS wireless keyboard/mouse combo where the keyboard supported boot protocol and the mouse did not. Are the keyboards wired, or wireless? If the device is wireless and BlueTooth it's likely it won't work, as it appears that BlueTooth devices that support boot protocol are pretty rare.
For keyboards, do the alpha, numeric and punctuation keys emit the proper characters? The demo doesn't process keys outside the (US)ASCII $20..$7f "printable" range, so function keys and cursor navigation keys will emit their raw scancode, instead.
Third time lucky though, I managed to find another USB keyboard and it's actually working correctly. Now to hook your code up to mine. It looks very well commented and should be easy to integrate. Thanks for writing this!
Eric
Could you post the brand & model# of the failing keyboard keyboards? I've got six keyboards of various types in my test pool and they're all working for me. I'd like to investigate further as to why your's fail.
Could other COG(s) of the P2 somehow become a USB analyzer I wonder? Would it be possible to monitor/timestamp/log USB transaction activity in real time? If so, that could become a handy tool as well, particularly during driver/device bringup.
I had a wireless combo but having trouble finding it...
KB is K330, mouse is m215
Microsoft Media Pro (wired) keyboard, M/N 1031: works
Logitech K270 (wireless) keyboard and mouse, M/N Y-R0042: both work
Both come back after being disconnected and reconnected
If I can find its receiver, I have an old Logitech diNovo Edge keyboard (has a builtin touchpad) I can try
EDIT:
Yeah, no luck with the diNovo... Boot protocol keyboard/mouse not found as ersmith ran into above. The receiver is, IIRC, combination BT and non-specific 2.4GHz RF (maybe mostly for when the PC first starts and there is no Bluetooth stack yet?).
Cheers,
Jesse
Yeah, a sniffer cog could monitor the usb pretty easily. The challenging part, as you mentioned, would be the filters that you need to build a comprehensive picture of the parts of the data stream that you're interested in. It's not even close, as an analyzer, but I've added some older code to the top post that outputs descriptor contents at device enumeration that may shed some light on why a keyboard/mouse device is not being recognized.
J
For games, you want to know all the keys that are currently being pressed.
Fortunately, I think this is going to be fairly easy.
I think this section has access to the keyboard report about all keys currently pressed:
BTW: This code is impressively well written and documented. Much nicer than anything I've ever done!
https://hackaday.com/2018/11/04/launchpad-midi-controller-put-to-work-with-python/
I'm also considering trying to get my Akai MPK249 working over USB, although since that has MIDI ports I'll probably just go that route.
https://usb.org/sites/default/files/documents/hid1_11.pdf
I should probably add an event posting by the host when a keyboard data packet is received, but polling at 10ms or less intervals should catch the changes.
I haven't looked at the code yet, but before I do - has anyone managed to get them working on RevB?
Ross.
One further question to @garryj on this USB driver...
From memory these drivers support a keyboard, a mouse or a combined keyboard+mouse USB device on a single port, or devices on separate USB ports. Will it be feasible in the future to support an independent keyboard and mouse device on a single port, via a USB hub ? Any idea how many COGs that might take?
That could be the problem. Unfortunately, I also can never seem to find any useful documentation on the Smart Pins. Perhaps Garry will have more luck
Does anyone know of any other USB code that works on the Propeller 2 RevB?
There's some new updates here too:
https://forums.parallax.com/discussion/comment/1479811/#Comment_1479811
Guess you have that Ross, but might help someone.
Try the last page or so of this topic:
https://forums.parallax.com/discussion/comment/1479560/#Comment_1479560
Yes, I've looked at that, and it seems to accord with Garry's code (you have to manually enable the V2 alternate code). But it still doesn't work.
I have no equipment suitable for debugging this, so I will have to wait till someone with more experience with USB takes a look at it.
Here is also a link to Chip's post regarding an additional step he had to implement to get it working:
https://forums.parallax.com/discussion/comment/1475565/#Comment_1475565
Wish I could be of more help
Correct, regarding single ports. A USB hub driver has been on my todo list for quite a while, but has been side-tracked the last few months by other things. Since there shouldn't be any additional low-level buss signalling changes, I think the core code should still fit in one cog + lut exec, with the hub buss scheduler driver developed using C and/or spin. I'm hoping to get to that post-revb p2 silicon.
Sounds promising, and I am hoping that might end up being the case. A single USB COG doing both devices separately on a single bus will be handy/essential once COGs become tight, which they inevitably will at some point when people make full use of the P2. Thanks garryj!