/** * Handle the USB reset event, should be invoked whenever * HW found USB reset signal on bus, which usually is called * "end of bus reset" status. */ void USBD_ResetHandler() { /* The device enters the Default state */ deviceState = USBD_STATE_DEFAULT; /* Active the USB HW */ USBD_HAL_Activate(); /* Only EP0 enabled */ USBD_HAL_ResetEPs(0xFFFFFFFF, USBD_STATUS_RESET, 0); USBD_ConfigureEndpoint(0); /* Invoke the Reset callback */ USBDCallbacks_Reset(); }
/* Function: USBD_InterruptHandler UDP interrupt handler. Manages device status changes. */ void USBD_InterruptHandler()////////////////////////////////////////////////// { unsigned int status; // Get interrupt status // Some interrupts may get masked depending on the device state status = AT91C_BASE_UDP->UDP_ISR; status &= AT91C_BASE_UDP->UDP_IMR; if (deviceState < USBD_STATE_POWERED) { status &= AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM; AT91C_BASE_UDP->UDP_ICR = ~status; } // Return immediately if there is no interrupt to service if (status == 0) { return; } // Toggle USB LED if the device is active if (deviceState >= USBD_STATE_CONFIGURED) { usb_frame_counter++ ; //TRACE_INFO( " *%d* ", usb_frame_counter); if( (usb_frame_counter & 0x7F) == 0) { LED_TOGGLE_DATA; //LED_Toggle(USBD_LEDUDATA); } } // Service interrupts //// Start Of Frame (SOF) //if (ISSET(dStatus, AT91C_UDP_SOFINT)) { // // TRACE_DEBUG( "SOF"); // // // Invoke the SOF callback // USB_StartOfFrameCallback(pUsb); // // // Acknowledge interrupt // AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_SOFINT; // dStatus &= ~AT91C_UDP_SOFINT; //} // Suspend---------------------------------------------------------------------- // This interrupt is always treated last (hence the '==') if (status == AT91C_UDP_RXSUSP) { TRACE_INFO( "\r\n# Suspend"); // Don't do anything if the device is already suspended if (deviceState != USBD_STATE_SUSPENDED) { // The device enters the Suspended state // Enable wakeup AT91C_BASE_UDP->UDP_IER = AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM; // Acknowledge interrupt AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_RXSUSP; // Switch to the Suspended state previousDeviceState = deviceState; deviceState = USBD_STATE_SUSPENDED; UDP_DisableTransceiver(); UDP_DisablePeripheralClock(); UDP_DisableUsbClock(); // Invoke the Suspended callback USBDCallbacks_Suspended(); } } // Resume-------------------------------------------------------------------- else if ((status & (AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM)) != 0) { TRACE_INFO( "\r\n# Resume"); // Invoke the Resume callback USBDCallbacks_Resumed(); // Don't do anything if the device was not suspended if (deviceState == USBD_STATE_SUSPENDED) { // The device enters its previous state UDP_EnablePeripheralClock(); UDP_EnableUsbClock(); // Enable the transceiver if the device was past the Default // state deviceState = previousDeviceState; if (deviceState >= USBD_STATE_DEFAULT) { UDP_EnableTransceiver(); } } // Clear and disable resume interrupts AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM | AT91C_UDP_RXSUSP; AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM; } // End of bus reset--------------------------------------------------------- else if ((status & AT91C_UDP_ENDBUSRES) != 0) { TRACE_INFO( "\r\n# EoBRST "); // The device enters the Default state deviceState = USBD_STATE_DEFAULT; UDP_EnableTransceiver(); UDP_ResetEndpoints(); UDP_DisableEndpoints(); USBD_ConfigureEndpoint(0); // Flush and enable the Suspend interrupt AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_WAKEUP | AT91C_UDP_RXRSM | AT91C_UDP_RXSUSP; AT91C_BASE_UDP->UDP_IER = AT91C_UDP_RXSUSP; //// Enable the Start Of Frame (SOF) interrupt if needed //if (pUsb->pCallbacks->startOfFrame != 0) { // // AT91C_BASE_UDP->UDP_IER = AT91C_UDP_SOFINT; //} // Invoke the Reset callback USBDCallbacks_Reset(); // Acknowledge end of bus reset interrupt AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES; usb_frame_counter = 0 ;//reset LEDUDATA use counter } // Endpoint interrupts------------------------------------------------------- else { int eptnum = 0; while (status != 0) { // Check if endpoint has a pending interrupt if ((status & (1 << eptnum)) != 0) { UDP_EndpointHandler(eptnum); status &= ~(1 << eptnum); if (status != 0) { //TRACE_INFO( "\n\r - "); } } eptnum++; } } // Toggle LED back to its previous state // TRACE_INFO( "\n\r"); // //if (deviceState >= USBD_STATE_POWERED) { //LED_Clear(USBD_LEDUDATA); //} }