Reading from device - works on OS X but not on Arm-Linux

278 views
Skip to first unread message

Gustavo Campos

unread,
Apr 14, 2014, 5:07:03 PM4/14/14
to usb4...@googlegroups.com
First of all I would like to congratulate everybody involved in this project, you've done a really nice job!
I have a device that notifies me of some events via USB. It's a personal project so there is no need to worry about protocols.
I have built a software using Java to present the results to me and it is working nicelly on my Mac, but when I try to run it in a Arm Linux device (tried two, Raspberry Pi and Cubieboard) I cannot read from the device anymore. The commands I use to control the device (toggle a led, for example) work fine but the reading doesn't!
If anyone could give me a light on that it would be of great help! Thank you all in advance!
Here is my code (deleted some of the unimportant parts so that it gets easier to understand):

public class TransmitterModuleHIDInterface implements Runnable
{
public static MessageOutputBuffer m_OutputBuffer;
public static MessageOutputBuffer m_InputBuffer;
static int m_Interface = 0;
private int m_Endpoint = (byte)0x01;
private byte m_OutEndpoint = (byte) 0x80;
private byte m_InEndpoint = (byte) 0x00;
private int m_USBTimeoutMs = 50;
public void run()
{
try
{
DeviceHandle v_DeviceHandle = initUSB();
long m_LastPooling = System.nanoTime();
while(true)
{
try
{
if(m_OutputBuffer.HasPendingMessage())
sendData(v_DeviceHandle, v_Message.m_Parameters.get(0).getBytes())
else if((System.nanoTime() - m_LastPooling) > 10*1000*1000)
{
byte[] v_InFromTransmitter = getData(v_DeviceHandle);
if(v_InFromTransmitter != null)
{
System.out.println("Received message. Size: " + v_InFromTransmitter.lenght) 
m_LastPooling = System.nanoTime();
}
} 
catch (LibUsbException e)
{
System.out.println("Disconnected");
while(true)
{
try
{
v_DeviceHandle = initUSB();
break;
}
catch(LibUsbException e1)
{
try 
{
Thread.sleep(300);
} 
catch (InterruptedException e2) 
{
}
}
}

catch(LibUsbException e)
{
System.out.println("Transmitter module interface failure");
System.out.println(e.getMessage());
e.printStackTrace();
return;
}
 
private void sendData(DeviceHandle p_Device, byte[] p_OutToTransmitter)
{
IntBuffer v_Transfered = IntBuffer.allocate(1).put(v_SentMsg.length);
ByteBuffer v_Buffer = ByteBuffer.allocateDirect(v_SentMsg.length);
v_Buffer.put(v_SentMsg);
int v_Result = LibUsb.interruptTransfer(p_Device, (byte)(m_Endpoint | m_InEndpoint), v_Buffer, v_Transfered, m_USBTimeoutMs);
if (v_Result != LibUsb.SUCCESS)
throw new LibUsbException("Error writing to transmitter: ", v_Result);
}

private byte[] readData(DeviceHandle p_Device) throws LibUsbException
{
IntBuffer v_Transfered = IntBuffer.allocate(1);
ByteBuffer v_Buffer = ByteBuffer.allocateDirect(64);
int v_Result = LibUsb.interruptTransfer(p_Device, (byte)(m_Endpoint | m_OutEndpoint), v_Buffer, v_Transfered, m_USBTimeoutMs);
if (v_Result == LibUsb.SUCCESS)
{
int v_ReadSize = v_Transfered.get(0);
byte[] v_InFromTransmitter = new byte[v_ReadSize];
v_Buffer.get(v_InFromTransmitter, 0, v_ReadSize);
return v_InFromTransmitter;
}
else if(v_Result == LibUsb.ERROR_NO_DEVICE || v_Result == LibUsb.ERROR_IO)
throw new LibUsbException("Device disconnected", v_Result);
else if(v_Result != LibUsb.ERROR_TIMEOUT)
throw new LibUsbException("Error: ", v_Result);
else
return null;
}
}

Klaus Reimer

unread,
Apr 15, 2014, 1:02:06 AM4/15/14
to usb4...@googlegroups.com
On 04/14/2014 11:07 PM, Gustavo Campos wrote:
> anymore. The commands I use to control the device (toggle a led, for
> example) work fine but the reading doesn't!

And what exactly happens? Do you get an exception? A segfault? Or just a
read timeout? Have you tried increasing the log level with the
setDebug() method? Maybe libusb is printing some useful information
about what's going on.

--
Klaus Reimer <http://www.ailis.de/~k/>
[2FC4 CCA0 C03B 1E5F 1ACC 94D6 6461 426C E734 75A1]

Gustavo Campos

unread,
Apr 15, 2014, 9:00:10 AM4/15/14
to usb4...@googlegroups.com
Dear Mr Reimer,

Thank you very much for the quick response!
Im getting an error number -7, a timeout. The way I built my device, I'm only going to respond with a valid message if one is available, so I'm expecting to get timeouts every 10ms. When a message is available, though, I'm supposed to read it but I'm also getting a timeout.
For debug purposes, every 50 times my device identifies a read attempt from the host it toggles the state of a LED. When it's connected to the Mac it is toggled every 500ms approximatelly, but when it is connected to the Arm-Linux computers it is not toggled at all.
I'm going to try using the setDebug() method and will write the results here after! If anyone could help me out with this extra information it woult be very much appreciated!

Thank you very much!

Colin Godsey

unread,
Apr 21, 2014, 9:08:21 AM4/21/14
to usb4...@googlegroups.com
Did you try building your own native drivers on the device? Had to do this with my Matrix board. 

Gustavo Campos

unread,
Apr 24, 2014, 2:45:46 PM4/24/14
to usb4...@googlegroups.com
Dear Mr Klaus,

I'm sorry for the delay in testing the way you asked, but finally did.

Here is what the debug shows in a normal timeout in my Mac:

[ 2.132930] [00005103] libusb: debug [ep_to_pipeRef] converting ep address 0x81 to pipeRef and interface
[ 2.132944] [00005103] libusb: debug [ep_to_pipeRef] pipe 1 on interface 0 matches
[ 2.132992] [00005103] libusb: debug [libusb_get_next_timeout] next timeout in 0.049936s
[ 2.132996] [00005103] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 2.133000] [00005103] libusb: debug [handle_events] poll() 3 fds with timeout in 50ms
[ 2.184144] [00005103] libusb: debug [handle_events] poll() returned 0
[ 2.184268] [00005103] libusb: debug [libusb_cancel_transfer] 
[ 2.184291] [00005103] libusb: debug [ep_to_pipeRef] converting ep address 0x81 to pipeRef and interface
[ 2.184309] [00005103] libusb: debug [ep_to_pipeRef] pipe 1 on interface 0 matches
[ 2.184325] [00005103] libusb: warning [darwin_abort_transfers] aborting all transactions on interface 0 pipe 1
[ 2.184491] [00005103] libusb: debug [darwin_abort_transfers] calling clear pipe stall to clear the data toggle bit
[ 2.184531] [00006107] libusb: debug [darwin_async_io_callback] an async io operation has completed
[ 2.185103] [00005103] libusb: debug [libusb_get_next_timeout] no URB with timeout or all handled by OS; no timeout!
[ 2.185127] [00005103] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 2.185138] [00005103] libusb: debug [handle_events] poll() 3 fds with timeout in 60000ms
[ 2.185166] [00005103] libusb: debug [handle_events] poll() returned 1
[ 2.185175] [00005103] libusb: debug [op_handle_events] checking fd 23 with revents = 0
[ 2.185181] [00005103] libusb: debug [op_handle_events] checking fd 25 with revents = 0
[ 2.185188] [00005103] libusb: debug [op_handle_events] checking fd 27 with revents = 1
[ 2.185198] [00005103] libusb: debug [darwin_handle_callback] handling bulk completion with kernel status -536870165
[ 2.185206] [00005103] libusb: warning [darwin_transfer_status] transfer error: timed out
[ 2.185213] [00005103] libusb: debug [usbi_handle_transfer_completion] transfer 0x7f9d70609b50 has callback 0x1177257b0
[ 2.185220] [00005103] libusb: debug [sync_transfer_cb] actual_length=0

Here is what the debug shows when data is received in my Mac

[ 7.394170] [00005103] libusb: debug [ep_to_pipeRef] converting ep address 0x81 to pipeRef and interface
[ 7.394184] [00005103] libusb: debug [ep_to_pipeRef] pipe 1 on interface 0 matches
[ 7.394232] [00005103] libusb: debug [libusb_get_next_timeout] next timeout in 0.049937s
[ 7.394236] [00005103] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 7.394240] [00005103] libusb: debug [handle_events] poll() 3 fds with timeout in 50ms
[ 7.395218] [00006107] libusb: debug [darwin_async_io_callback] an async io operation has completed
[ 7.395279] [00005103] libusb: debug [handle_events] poll() returned 1
[ 7.395289] [00005103] libusb: debug [op_handle_events] checking fd 23 with revents = 0
[ 7.395294] [00005103] libusb: debug [op_handle_events] checking fd 25 with revents = 0
[ 7.395300] [00005103] libusb: debug [op_handle_events] checking fd 27 with revents = 1
[ 7.395307] [00005103] libusb: debug [darwin_handle_callback] handling bulk completion with kernel status 0
[ 7.395313] [00005103] libusb: debug [usbi_handle_transfer_completion] transfer 0x7f9d70609b50 has callback 0x1177257b0
[ 7.395319] [00005103] libusb: debug [sync_transfer_cb] actual_length=10

Here is what the debug shows in the Cubieboard:

[ 8.440985] [0000048b] libusb: debug [add_to_flying_list] arm timerfd for timeout in 50ms (first in line)
[ 8.441322] [0000048b] libusb: debug [submit_bulk_transfer] need 1 urbs for new transfer with length 64
[ 8.441637] [0000048b] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 8.441889] [0000048b] libusb: debug [handle_events] poll() 4 fds with timeout in 60000ms
[ 8.491002] [0000048b] libusb: debug [handle_events] poll() returned 1
[ 8.491236] [0000048b] libusb: debug [handle_events] timerfd triggered
[ 8.491414] [0000048b] libusb: debug [libusb_cancel_transfer] 
[ 8.492215] [0000048b] libusb: debug [disarm_timerfd] 
[ 8.492399] [0000048b] libusb: debug [handle_events] poll() 4 fds with timeout in 0ms
[ 8.492573] [0000048b] libusb: debug [handle_events] poll() returned 1
[ 8.492750] [0000048b] libusb: debug [reap_for_handle] urb type=3 status=-2 transferred=0
[ 8.492924] [0000048b] libusb: debug [handle_bulk_completion] handling completion status -2 of bulk urb 1/1
[ 8.493091] [0000048b] libusb: debug [handle_bulk_completion] abnormal reap: urb status -2
[ 8.493261] [0000048b] libusb: debug [handle_bulk_completion] abnormal reap: last URB handled, reporting
[ 8.493430] [0000048b] libusb: debug [usbi_handle_transfer_cancellation] detected timeout cancellation
[ 8.493598] [0000048b] libusb: debug [disarm_timerfd] 
[ 8.493763] [0000048b] libusb: debug [usbi_handle_transfer_completion] transfer 0x95bee654 has callback 0x95601334
[ 8.493932] [0000048b] libusb: debug [sync_transfer_cb] actual_length=0

It doesn't change when it has messages!
Do you have any idea of what is wrong?

Thank you again!

Gustavo Campos

unread,
Apr 24, 2014, 3:01:55 PM4/24/14
to usb4...@googlegroups.com
Dear mr Godsey

I have built libUsb on the device and it is still not working! Was it what you built on your Matrix board?

Klaus Reimer

unread,
Apr 25, 2014, 4:31:40 AM4/25/14
to usb4...@googlegroups.com
On 14.04.2014 23:07, Gustavo Campos wrote:
> The commands I use to control the device (toggle a led, for
> example) work fine but the reading doesn't!
> ...
> private int m_USBTimeoutMs = 50;

Just a quick idea: Have you tried increasing this timeout? Maybe the
Raspberry is simply not fast enough to receive an answer in 50ms while
the Mac has no problem with it.
signature.asc

Colin Godsey

unread,
Apr 25, 2014, 9:01:40 AM4/25/14
to usb4...@googlegroups.com
Yeah, I just built the native module like normal on the Matrix. I did build and install the newest version of libusb from source to get it to work.  

On Thursday, April 24, 2014 2:01:55 PM UTC-5, Gustavo Campos wrote:
Dear mr Godsey

I have built libUsb on the device and it is still not working! Was it what you built on your Matrix board?


I would definitely check out the timing. Especially with debug on, it can probably really slow things down. 
Reply all
Reply to author
Forward
0 new messages