google / gousb

gousb provides low-level interface for accessing USB devices
Apache License 2.0
845 stars 124 forks source link

CDC device #120

Closed JetSetIlly closed 1 year ago

JetSetIlly commented 1 year ago

Hello,

I have a STM32 microcontroller (bluepill) that I have successfully set up with a small program to emit data on the USB port. When connected with the cu program (connecting to the /dev/ttyACM0 device) I receive the stream of data. This proves that the bluepill is working as I expect.

I am now trying to use gousb to retrieve the data. I am using the example programs from the package's documentation as a basis for a test program. The logs indicate that I am finding the correct device and successfully opening the In endpoint.

The problem seems to be that the in.Read() function never returns. I see this debug message on the call to Read:

[ 0.192353] [00009985] libusb: debug [libusb_alloc_transfer] transfer 0x2232898 [ 0.192379] [00009985] libusb: debug [libusb_submit_transfer] transfer 0x2232898 [ 0.192392] [00009985] libusb: debug [submit_bulk_transfer] need 1 urbs for new transfer with length 128 [ 0.206358] [00009987] libusb: debug [usbi_wait_for_events] poll() returned 0

and then repeated messages thereafter:

[ 0.206395] [00009987] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling [ 0.206403] [00009987] libusb: debug [usbi_wait_for_events] poll() 3 fds with timeout in 100ms [ 0.306535] [00009987] libusb: debug [usbi_wait_for_events] poll() returned 0

I'm not sure how to proceed from here. Permissions to the usb device are correct and the device itself is functioning as I expect. Is there a configuration step that I have misunderstood?

I include the complete test program as a gist for reference: https://gist.github.com/JetSetIlly/52ae2f639f7d524a1fa58dac91b950ff

JetSetIlly commented 1 year ago

I've also tried the rawread program (from the gousb repository) and ran it with the appropriate interface/endpoint arguments. I receive the same log messages.

zagrodzki commented 1 year ago

sorry, I don't think we can help with such development issues. That being said, if you can see that a C/libusb program works as expected while gousb doesn't, then it's possible the problem lies within gousb and I would take a look.

zagrodzki commented 1 year ago

my personal guess would be that the device needs some kind of setup before it starts transmitting - perhaps the right configuration needs to be selected on the device, or some control command needs to be sent before endpoint data is produced.

I assume that it is obvious to you that reading data from the USB endpoint and reading data from the ttyACM device through the ACM driver are two completely different things and that if your device uses a serial port emulation of some kind, you will have to manually implement some additional protocol on top of raw USB.

Also make sure your device resets correctly (that may be the difference between first use, when Linux kernel attaches the driver for the endpoint, and the second use, when your aplication detaches and claims the endpoint itself).

JetSetIlly commented 1 year ago

Understood. I now think it's probably a USB hardware issue on the micro controller.

zagrodzki commented 1 year ago

using Wireshark to snoop on USB packets in a case where it does and doesn't work may be enlightening.