Datasheet

USB_ApplicationDataEventHandler()
and
USB_ApplicationEventHandler()
for the pre-compiled example. For
the source example the function names are configurable through the usb_config.h ( see page 15) file (see the usb_config.h
( see page 15) section for more information). For more detailed information about these functions or the USB library,
please refer to www.microchip.com/mal. This download includes the USB host library code as well as more detailed
documentation about that library.
The data events are consumed by the Android client driver. So the user application data event handler doesn't need to do
anything. It needs to be present for the library to link successfully but it can just return FALSE.
BOOL USB_ApplicationDataEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size
)
{
return FALSE;
}
The regular event handler has a little more work that needs to be done. This handler notifies the user of device attach and
detach events. For Android devices this is covered in the Detecting a Connection/Disconnection to an Android Device ( see
page 18) section. This function also notifies the user about errors that might occur in the USB subsystem. These can be
useful for debugging development issues or logging issue once released in the field. The last important duty that this function
provides is determining if the power required by the attached device is available. This is done through the
EVENT_VBUS_REQUEST_POWER
event. Remember in this event that the amount of power requested through the USB bus is
the power required/2, not the power required. Below is full implementation of the USB event handler that will work with a
single attached Android device.
BOOL USB_ApplicationEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
{
switch( event )
{
case EVENT_VBUS_REQUEST_POWER:
// The data pointer points to a byte that represents the amount of power
// requested in mA, divided by two. If the device wants too much power,
// we reject it.
if (((USB_VBUS_POWER_EVENT_DATA*)data)->current <= (MAX_ALLOWED_CURRENT / 2))
{
return TRUE;
}
else
{
DEBUG_ERROR( "Device requires too much current\r\n" );
}
break;
case EVENT_VBUS_RELEASE_POWER:
case EVENT_HUB_ATTACH:
case EVENT_UNSUPPORTED_DEVICE:
case EVENT_CANNOT_ENUMERATE:
case EVENT_CLIENT_INIT_ERROR:
case EVENT_OUT_OF_MEMORY:
case EVENT_UNSPECIFIED_ERROR:
case EVENT_DETACH:
//Fall-through
case EVENT_ANDROID_DETACH:
device_attached = FALSE;
return TRUE;
break;
// Android Specific events
case EVENT_ANDROID_ATTACH:
device_attached = TRUE;
device_handle = data;
return TRUE;
default :
break;
}
return FALSE;
}
Microchip's Accessory Framework for Android(tm) 14
14
4