Microchip提供USB功能的MCU,

Microchip的libary提供許多USB的範例,

在範例中(USB Device - MCHPUSB - Generic Driver Demo)一開始就出現下列的程式碼:

#if defined(USB_INTERRUPT)
        USBDeviceAttach();
    #endif

    while(1)
    {
        #if defined(USB_POLLING)
        // Check bus status and service USB interrupts.
        USBDeviceTasks(); // Interrupt or polling method.  If using polling, must call
                          // this function periodically.  This function will take care
                          // of processing and responding to SETUP transactions
                          // (such as during the enumeration process when you first
                          // plug in).  USB hosts require that USB devices should accept
                          // and process SETUP packets in a timely fashion.  Therefore,
                          // when using polling, this function should be called
                          // regularly (such as once every 1.8ms or faster** [see
                          // inline code comments in usb_device.c for explanation when
                          // "or faster" applies])  In most cases, the USBDeviceTasks()
                          // function does not take very long to execute (ex: <100
                          // instruction cycles) before it returns.
        #endif

 

在usb_config.h檔中可以選擇是要使用USB_INTERRUPT或USB_POLLING

 

1. 如果是使用USB_INTERRUPT, 就需要使用USBDeviceAttach();

在help中關於USBDeviceAttach()的說明是這樣的:


void USBDeviceAttach();


Description

This function indicates to the USB module that the USB device has been attached to the bus. This function needs to be called in order for the device to start to enumerate on the bus. --> 測試usb裝置是否有插入至USB的BUS之中


Preconditions

Should only be called when USB_INTERRUPT is defined.  --> 說明USBDeviceAttach()跟著USB_INTERRUPT使用

For normal USB devices: Make sure that if the module was previously on, that it has been turned off for a long time (ex: 100ms+) before calling this function to re-enable the module. If the device turns off the D+ (for full speed) or D- (for low speed) ~1.5k ohm pull up resistor, and then turns it back on very quickly, common hosts will sometimes reject this event, since no human could ever unplug and reattach a USB device in a microseconds (or nanoseconds) timescale. The host could simply treat this as some kind of glitch and ignore the event altogether.

 

2. 如果是與用USB_POLLING, 則需要使用USBDeviceTasks(),

在help中關於USBDeviceTasks()的說明是這樣的:

 

void USBDeviceTasks();

Description

This function is the main state machine of the USB device side stack. This function should be called periodically to receive and transmit packets through the stack. This function should be called preferably once every 100us during the enumeration process.After the enumeration process this function still needs to be called periodically to respond to various situations on the bus but is more relaxed in its time requirements. This function should also be called at least as fast as the OUT data expected from the PC.  --> 需要週期性地call USBDeviceTasks()來傳遞或接受Packets,  週期最好是100us, 至少要跟pc端OUT的資料一樣快

Typical usage:

void main(void)
{
    USBDeviceInit()  --> 需要在USBDeviceTasks()之前
    while(1)
    {
        USBDeviceTasks();
        if((USBGetDeviceState() < CONFIGURED_STATE) ||
           (USBIsDeviceSuspended() == TRUE))
        {
            //Either the device is not configured or we are suspended
            // so we don't want to do execute any application code
            continue;   //go back to the top of the while loop
        }
        else
        {
            //Otherwise we are free to run user application code.
            UserApplication();
        }
    }
}

Preconditions

None


Remarks

This function should be called preferably once every 100us during the enumeration process. After the enumeration process this function still needs to be called periodically to respond to various situations on the bus but is more relaxed in its time requirements.


void USBDeviceInit();

 

Description

This function initializes the device stack it in the default state. The USB module will be completely reset including all of the internal variables, registers, and interrupt flags.

 

Preconditions

This function must be called before any of the other USB Device functions can be called, including USBDeviceTasks().


arrow
arrow

    fishark 發表在 痞客邦 留言(0) 人氣()