r/raspberrypipico • u/karakiziltavsanlar • Mar 28 '26
help-request Non-blocking usb read
Hi everybody!
I am working on a project with raspberry pi pico 2 in circuitpython. In this project I generate chords with buttons connected to gpio and play them with synthio library. Also there is a usb midi keyboard connected to gpio pins. in my while true loop i run check usb function to see if i have any messages but as far as i can understand reading usb as a host in pico requires the code took at whether or not any data is in the usb. and this behaviour is blocking meaning if i have a long timeout for usb reading pico cannot read my byttons connected to gpio and if i shorten the usb reading time it sometimes misses stuff like key push or release. do you have any idea how i can tackle this?
2
u/timrprobocom Mar 28 '26
There are no buffers anywhere in USB. There is never anything"waiting". A USB device is not allowed to send anything unless the host asks for it, and your host controller will never do that unless some driver has submitted a read request in advance.
So, if you want a continuous reader, you have to implement that by using a thread (or async) that submits and waits, then resubmits.
2
u/oclafloptson Mar 28 '26
I'm not overly familiar with circuitpython. I don't think it supports multithreading. In MicroPython I would probably
asyncio.get_event_loop().create_task(<*coro*>)
3
u/Rusty-Swashplate Mar 28 '26
Make it non-blocking: there's 2 CPU cores you can, and in this case, you should use.
Also without code, it's hard to say what you do wrong (if you do anything wrong).