Random USB busreset when using HID library with Android device

05 Jul 2013

Hi all,

We are two Belgian students that are currently working on a project to establish USB communication between an Android device, which has to be the host, and a mbed LPC1768 and we have some trouble using the HID library.

Our setup: We have a USB host device a Samsung Galaxy S2 and the mbed LPC1768 as client device (pin D- and D+).

What should happen: We send some data from the android device to the mbed device where we do nothing with the data (to have as few variables as possible). So the connection should just continue until we reset the mbed.

What happens: After sending some packets of data the android device detects that the device is detached and the mbed resets the bus.

The USB communication first slows down and then crashes completely. (A buffer that overflows maybe?). Communication packages seem to disappear.

This is our code, the rest is the HID library.

main.cpp

#include "AndroidConnection.h"

// read

 
AndroidConnection con;
 
int main(void) {
    printf("Here\r\n");
    con.setupConnection();
    while(1){
        con.readData();
       // uint8_t dummy = 'c';
        //con.write(&dummy,1);    
    }
}

AndroidConnection.cpp

#include "AndroidConnection.h"

BusOut leds(LED1, LED2, LED3, LED4);

void AndroidConnection::setupConnection()
{
    printf("Waiting for connection\r\n");
    connect();
    connected();
}

void AndroidConnection::connected()
{
    printf("Device Connected\r\n");
}

void AndroidConnection::disconnected()
{
    printf("Device Disconnected\r\n");
    setupConnection();
}

void AndroidConnection::write(uint8_t* data, uint32_t len)
{
    send.length = len;
    int index;
    for(index = 0; index < len; index++) {
        send.data[index] = data[index];
    }
    sendNB(&send);
    printf("Wrote bytes\r\n");
}

void AndroidConnection::readData()
{
   // if(configured()) {
        if(read(&recv)) {
            if (recv.length > 0) {
                printf("Read bytes\r\n");
                handlePacket();
            }
        }
  /*  } else {
        disconnected();
    }*/
}

void AndroidConnection::handlePacket()
{
    leds = recv.data[0];
}

void AndroidConnection::USBCallback_busReset(void) {
    printf("Bus Reset\r\n");
}

void AndroidConnection::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
    printf("Incoming Request with len %d\r\n",length);
}

AndroidConnection.h

#include "mbed.h"
#include "USBHID.h"

class AndroidConnection:public USBHID {
    public:
        AndroidConnection():USBHID(32,32,0x1234,0x0006,0x0001,false){
         
        };
        virtual void setupConnection();
        virtual void connected();
        virtual void disconnected();
        virtual void write(uint8_t* buffer, uint32_t len);
        virtual void readData();
        virtual void handlePacket();
        virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
        virtual void USBCallback_busReset(void);
     private:
        HID_REPORT recv;
        HID_REPORT send;
        
};

I hope I gave enough information.

Thank you for your time and effort.

Cheers Thomas & Vincent

06 Jul 2013

I assume your USB connector is attached to p31 (D+), p32 (D-) and also to GND. Did you first test the hardware for proper operation using the mbed HID library and a PC. Try one of the examples in the cookbook such as the mouse or joystick emulation. See http://mbed.org/users/wim/notebook/usb-joystick-device/#. Checkout the USB testtools mentioned on that page.

I did not check your code in detail, but you use printf for debugging. You may want to make sure that there are no interrupts which interfere with printf (eg USBCallback_busReset). Note that prinft is non-reentrant and it will break down when interrupted. May be better to use some LEDs or digital output pins and a logic analyser to check what is going on internally.

08 Jul 2013

We tested it with a PC and that works with no problem. We also removed all printf statements which didn't seem the be the problem.

We believe that the problem is that our android device does not seem to find the Bulk endpoints even when they are clearly defined in the HID library. But we don't seem to find where it goes wrong.