USB Library
USBDeviceTasks Function
Syntax
void USBDeviceTasks();
Description

This function is the main state machine/transaction handler of the USB device side stack. When the USB stack is operated in "USB_POLLING" mode (usb_config.h user option) the USBDeviceTasks() function should be called periodically to receive and transmit packets through the stack. This function also takes care of control transfers associated with the USB enumeration process, and detecting various USB events (such as suspend). This function should be called at least once every 1.8ms during the USB enumeration process. After the enumeration process is complete (which can be determined when USBGetDeviceState() returns CONFIGURED_STATE), the USBDeviceTasks() handler may be called the faster of: either once every 9.8ms, or as often as needed to make sure that the hardware USTAT FIFO never gets full. A good rule of thumb is to call USBDeviceTasks() at a minimum rate of either the frequency that USBTransferOnePacket() gets called, or, once/1.8ms, whichever is faster. See the inline code comments near the top of usb_device.c for more details about minimum timing requirements when calling USBDeviceTasks(). 

When the USB stack is operated in "USB_INTERRUPT" mode, it is not necessary to call USBDeviceTasks() from the main loop context. In the USB_INTERRUPT mode, the USBDeviceTasks() handler only needs to execute when a USB interrupt occurs, and therefore only needs to be called from the interrupt context. 

Typical usage:

void main(void)
{
    USBDeviceInit();
    while(1)
    {
        USBDeviceTasks(); //Takes care of enumeration and other USB events
        if((USBGetDeviceState() < CONFIGURED_STATE) ||
           (USBIsDeviceSuspended() == true))
        {
            //Either the device is not configured or we are suspended,
            // so we don't want to execute any USB related application code
            continue;   //go back to the top of the while loop
        }
        else
        {
            //Otherwise we are free to run USB and non-USB related user 
            //application code.
            UserApplication();
        }
    }
}
Preconditions

Make sure the USBDeviceInit() function has been called prior to calling USBDeviceTasks() for the first time.

Remarks

USBDeviceTasks() does not need to be called while in the USB suspend mode, if the user application firmware in the USBCBSuspend() callback function enables the ACTVIF USB interrupt source and put the microcontroller into sleep mode. If the application firmware decides not to sleep the microcontroller core during USB suspend (ex: continues running at full frequency, or clock switches to a lower frequency), then the USBDeviceTasks() function must still be called periodically, at a rate frequent enough to ensure the 10ms resume recovery interval USB specification is met. Assuming a worst case primary oscillator and PLL start up time of <5ms, then USBDeviceTasks() should be called once every 5ms in this scenario. 

When the USB cable is detached, or the USB host is not actively powering the VBUS line to +5V nominal, the application firmware does not always have to call USBDeviceTasks() frequently, as no USB activity will be taking place. However, if USBDeviceTasks() is not called regularly, some alternative means of promptly detecting when VBUS is powered (indicating host attachment), or not powered (host powered down or USB cable unplugged) is still needed. For self or dual self/bus powered USB applications, see the USBDeviceAttach() and USBDeviceDetach() API documentation for additional considerations.

MLA - USB Library Help Version : 2.16
http://www.microchip.com/mla