Android usb host: asynchronous interrupt transfer

2019-02-02 02:19发布

问题:

I'm trying to connect a USB-Device ( build by myself ) to communicate with my development board ( ODROID-X ) Unfortunately, the examples are very little, as far as the asynchronous communication. I'd some problems with the interrupt driven data exchange - how to build the connection by using the asynchronous interrupt mode? In one direction, the transmission was possible ... but in both it doesn't work. Is there an example like this:

  • send a ByteBuffer with endpoint_OUT
  • get a message from device on endpoint_IN

both in interrupt mode.

Thanks a lot for your support.

Hardy

回答1:

Perhaps I am misunderstanding the question here. The sample missile lanucher app that is part of the API package from level 12 onwards uses the queue() and requestWait() methods to handle interrupt type endpoints. Requests are either In or Out and depend on the direction of the EndPoint.

The code for a pretty noddy request->reply looks something like this. You would want to structure real code differently but this gives you the gist of what needs to happen (I hope)

public void run() {
    int bufferMaxLength=mEndpointOut.getMaxPacketSize();
    ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
    UsbRequest request = new UsbRequest(); // create an URB
    request.initialize(mConnection, mEndpointOut);
    buffer.put(/* your payload here */;

    // queue the outbound request
    boolean retval = request.queue(buffer, 1); 
        if (mConnection.requestWait() == request) { 
             // wait for confirmation (request was sent)
             UsbRequest inRequest = new UsbRequest(); 
             // URB for the incoming data
             inRequest.initialize(mConnection, mEndpointIn); 
             // the direction is dictated by this initialisation to the incoming endpoint.
             if(inRequest.queue(buffer, bufferMaxLength) == true){
                  mConnection.requestWait(); 
                  // wait for this request to be completed
                  // at this point buffer contains the data received
             }
        }
}

If you are actually looking for a way to run this IO in an asynchronous manner without binding a thread to it, then I think you need to consider using the DeviceConnection.getFilehandle() method to return a standard file handle which in theory you can then use as if it were any other file type resource. I would note however that I have not tried this.

If neither of these addresses the issue please revise the question to clarify what you are struggling to find examples of. I hope this helps.