Datasheet
4.2.3 Keeping the Stack Running
The Microchip USB host stack receives and logs events via an interrupt handler, but processes them as the USBTasks() (or
USBHostTasks()) function is called. This limits the amount of time spent in an interrupt context and helps limit context related
issues. This means that in order to keep the USB host stack running, the USBTasks() function needs to be called
periodically in order to keep processing these events.
int main(void)
{
//Initialize the USB stack
USBInitialize(0);
//Pass my accessory information to the Android client driver
AndroidAppStart(&myDeviceInfo);
while(1)
{
//Keep the USB stack running
USBTasks();
//Do my application specific stuff here
//...
}
}
The rate at which USBTasks() is called will contribute to determining the throughput that the stack is able to get, the
timeliness of the data reception, and the accuracy and latency of the events thrown from the stack.
4.2.4 Detecting a Connection/Disconnection to an Android
Device
The USB Host stack notifies users of attachment and detachment events through an event handler call back function. The
name of this function is configurable in source code projects. In pre-compiled projects, this function is named
USB_ApplicationEventHandler()
.
The Android client driver uses this same event handler function to notify the user of the attachment or detachment of Android
devices. The Android client driver adds the
EVENT_ANDROID_ATTACH ( see page 33)
and
EVENT_ANDROID_DETACH ( see page 33)
events. These two events are key to interfacing to the attached Android
device. The
data
field of the attach event provides the handle to the Android device. This handle must be passed to all of
the read/write functions to it is important to save this information when it is received. Similarly the detach event specifies the
handle of the device that detached so that the application knows which device detached (if multiple devices are attached).
void* device_handle = NULL;
static BOOL device_attached = FALSE;
int main(void)
{
//Initialize the USB stack
USBInitialize(0);
//Send the accessory information to the Android client driver
AndroidAppStart(&myDeviceInfo);
while(1)
{
//Keep the USB stack running
USBTasks();
//If the device isn't attached yet,
Microchip's Accessory Framework for Android(tm) 18
18
4