Question

I'm communicating with a USB device I'm building using PIC18F4550 via Libusb Java. I can write to it asynchronously, so I don't need to be pooling variables with the device, but I cannot do it with my Java application. I have to keep trying to read a message from it, raising interruptions in the microcontroller, what it exactly what I'm trying to avoid. Is there a way of using something like a callback or a blocking command that would try to read from the device only if it actually have something to be read? I'm really sorry if I wasn't able to make my question more clear, any doubts can be asked!

Was it helpful?

Solution

No, this is not possible because of the fundamental way that USB works. A USB device can only talk to the host when the host asks it if it has anything to say. The host in charge of who gets to talk. When you have an IN endpoint the host must poll your device regularly to check if the device has anything to say.

I have programmed the PIC18F4550 before and I know if you configure your IN endpoint to send NAKs, you don't have to handle an interrupt every single time the host asks for data from the endpoint. I suspect you are configuring the endpoint to send zero-length data packets when in fact you should configure it to send NAKs. A NAK response can be handled by the USB SIE without any intervention from your firmware.

There are, however, blocking commands you can use to read data from USB. They are blocking commands from the perspective of your Java program, but in the hardware they are implemented using polling of the USB device. I have not used "Libusb Java" but I know that libusb 1.0 supports blocking commands such as libusb_bulk_transfer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top